Skip to content

Commit

Permalink
Merge pull request #50 from razor-network/update-result-manager
Browse files Browse the repository at this point in the history
Remove storing of block and activeCollectionIDs from ResultManager
  • Loading branch information
yohanelly95 authored Dec 4, 2023
2 parents 8511477 + a0f462a commit f9882b6
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 42 deletions.
43 changes: 18 additions & 25 deletions contracts/ResultManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,31 @@ contract ResultManager is AccessControlEnumerable {

address public signerAddress;
uint256 public lastUpdatedTimestamp;
uint16[] public activeCollectionIds;
uint32 public latestEpoch;

// epoch => Block
mapping(uint32 => Block) public blocks;

/// @notice mapping for name of collection in bytes32 -> collectionid
mapping(bytes32 => uint16) public collectionIds;

/// @notice mapping for CollectionID -> Value Info
mapping(uint16 => Value) private _collectionResults;

event BlockReceived(uint32 epoch, uint256 timestamp, Value[] values);
event BlockReceived(Block messageBlock);

event SignerUpdated(
address sender,
address indexed prevSigner,
address indexed newSigner
);

constructor(address _signerAddress) {
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
signerAddress = _signerAddress;
}

function updateSignerAddress(address _signerAddress)
external
onlyRole(RESULT_MANAGER_ADMIN_ROLE)
{
function updateSignerAddress(
address _signerAddress
) external onlyRole(RESULT_MANAGER_ADMIN_ROLE) {
emit SignerUpdated(msg.sender, signerAddress, _signerAddress);
signerAddress = _signerAddress;
}

Expand All @@ -71,43 +73,34 @@ contract ResultManager is AccessControlEnumerable {
"invalid signature"
);

uint16[] memory ids = new uint16[](values.length);
blocks[epoch] = messageBlock;
for (uint256 i; i < values.length; i++) {
_collectionResults[values[i].collectionId] = values[i];
collectionIds[values[i].name] = values[i].collectionId;
ids[i] = values[i].collectionId;
}
activeCollectionIds = ids;
lastUpdatedTimestamp = timestamp;
latestEpoch = epoch;

emit BlockReceived(epoch, timestamp, values);
emit BlockReceived(messageBlock);
}

/**
* @dev using the hash of collection name, clients can query the result of that collection
* @param _name bytes32 hash of the collection name
* @return result of the collection and its power
*/
function getResult(bytes32 _name)
external
view
onlyRole(FORWARDER_ROLE)
returns (uint256, int8)
{
function getResult(
bytes32 _name
) external view onlyRole(FORWARDER_ROLE) returns (uint256, int8) {
uint16 id = collectionIds[_name];
return _getResultFromID(id);
}

/**
* @dev Returns collection result and power with collectionId as parameter
*/
function _getResultFromID(uint16 _id)
internal
view
returns (uint256, int8)
{
function _getResultFromID(
uint16 _id
) internal view returns (uint256, int8) {
return (_collectionResults[_id].value, _collectionResults[_id].power);
}
}
23 changes: 9 additions & 14 deletions contracts/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ contract Staking is AccessControlEnumerable {
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
}

function setPermission(address sender)
external
onlyRole(STAKING_ADMIN_ROLE)
{
function setPermission(
address sender
) external onlyRole(STAKING_ADMIN_ROLE) {
grantRole(WHITELISTED_ROLE, sender);
}

function removePermission(address sender)
external
onlyRole(STAKING_ADMIN_ROLE)
{
function removePermission(
address sender
) external onlyRole(STAKING_ADMIN_ROLE) {
revokeRole(WHITELISTED_ROLE, sender);
}

Expand All @@ -39,12 +37,9 @@ contract Staking is AccessControlEnumerable {
isWhitelistEnabled = false;
}

function isWhitelisted(address caller)
external
payable
onlyRole(TRANSPARENT_FORWARDER_ROLE)
returns (bool)
{
function isWhitelisted(
address caller
) external payable onlyRole(TRANSPARENT_FORWARDER_ROLE) returns (bool) {
return isWhitelistEnabled ? hasRole(WHITELISTED_ROLE, caller) : true;
}

Expand Down
File renamed without changes.
14 changes: 14 additions & 0 deletions contracts/mocks/Token.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract Token is ERC20 {
constructor(
string memory _name,
string memory _symbol,
uint256 mintAmount
) ERC20(_name, _symbol) {
_mint(msg.sender, mintAmount);
}
}
4 changes: 2 additions & 2 deletions gasCost.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require("fs");

const ethGasApi = "https://ethgasstation.info/api/ethgasAPI";
const ethPriceApi = "https://api.gemini.com/v1/pubticker/ethusd";
const maticGasApi = "https://gasstation-mainnet.matic.network/";
const maticGasApi = "https://gasstation.polygon.technology/v2";
const maticPriceApi = "https://api.gemini.com/v1/pubticker/maticusd";

const getFileData = () => {
Expand Down Expand Up @@ -53,7 +53,7 @@ const main = async () => {
// * Polygon
console.log("\n----------------- POLYGON -------------------------");
let maticGasPrice = await axios.get(maticGasApi);
maticGasPrice = Number(maticGasPrice.data.standard);
maticGasPrice = Number(maticGasPrice.data.standard.maxFee);
let maticPrice = await axios.get(maticPriceApi);
maticPrice = Number(maticPrice.data.last);

Expand Down
66 changes: 66 additions & 0 deletions scripts/deployResultManagerv2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const hre = require("hardhat");
require('dotenv').config();

const SIGNER_ADDRESS = process.env.SIGNER_ADDRESS || "0xC68AcC784227DbEaE98Bb6F5aC3C57cCe1aE9B4B";
const FORWARDER_ADDRESS = "0xAfAf9554D8f425030AB51188fC84Fe0Bd5f3E908";
const TRANSPARENT_FORWARDER_ADDRESS = "0x76a6AB56E27823B2175F11b0041c489bFdb13c88";

async function main() {
console.log("Deploying ResultManager contract...");

const ResultManager = await hre.ethers.getContractFactory("ResultManager");
const resultManagerV2 = await ResultManager.deploy(SIGNER_ADDRESS);
console.log("ResultManager contract deployed at:", resultManagerV2.address);

const Forwarder = await hre.ethers.getContractFactory("Forwarder");
const forwarder = Forwarder.attach(FORWARDER_ADDRESS); // Forwarder address from previous deployment
console.log("Forwarder contract deployed at", forwarder.address);

const tx1 = await forwarder.setResultManager(resultManagerV2.address);
await tx1.wait();

console.log("Deploying Staking contract...");
const Staking = await hre.ethers.getContractFactory("Staking");
const staking = await Staking.deploy();
console.log("Staking contract deployed at:", staking.address);

console.log(
"[ResultManager] Granting FORWARDER_ROLE to forwarder contract address"
);
const FORWARDER_ROLE = await resultManagerV2.FORWARDER_ROLE();
const tx2 = await resultManagerV2.grantRole(FORWARDER_ROLE, forwarder.address);
await tx2.wait();


const TransparentForwarder = await hre.ethers.getContractFactory(
"TransparentForwarder"
);
const transparentForwarder = TransparentForwarder.attach(TRANSPARENT_FORWARDER_ADDRESS); // Transparent Forwarder address from previous deployment

console.log(
`[Tranparent Forwarder] setting staking address to ${staking.address}`
);
const tx3 = await transparentForwarder.setStaking(staking.address);
await tx3.wait();
console.log("Transaction hash: ", tx3.hash);
console.log("--------------------------------------------------------------");

console.log(
`[Staking] Granting TRANSPARENT_FORWARDER_ROLE to Transparent Forwarder contract address`
);
const TRANSPARENT_FORWARDER_ROLE = await forwarder.TRANSPARENT_FORWARDER_ROLE();
const tx4 = await staking.grantRole(
TRANSPARENT_FORWARDER_ROLE,
transparentForwarder.address
);
await tx4.wait();
console.log("Transaction hash: ", tx4.hash);
console.log("--------------------------------------------------------------");
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
2 changes: 1 addition & 1 deletion test/Forwarder.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,4 @@ describe("Forwarder tests", () => {
await expect(client.getResult(namesHash[0])).to.be.not.reverted;
});
});
});
});
55 changes: 55 additions & 0 deletions test/Staking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { ethers } = require("hardhat");
const { expect } = require("chai");

const clientAddresses = [
"0x2546BcD3c84621e976D8185a91A922aE77ECEc30",
"0xbDA5747bFD65F08deb54cb465eB87D40e51B197E",
"0xdD2FD4581271e230360230F9337D5c0430Bf44C0",
"0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199",
];

describe("Staking tests", () => {
const tokenName = "Token";
const tokenSymbol = "TKN";
const mintAmount = ethers.utils.parseEther("10000000");
const defaultMinStake = ethers.utils.parseEther("1000000");
let signers;

let staking;
let token;

before(async () => {
signers = await hre.ethers.getSigners();

const Staking = await ethers.getContractFactory("Staking");
staking = await Staking.deploy();
await staking.deployed();
});

it("Deployment tests", async () => {

// * deployer should have default admin role
const DEFAULT_ADMIN_ROLE = await staking.DEFAULT_ADMIN_ROLE();
expect(await staking.hasRole(DEFAULT_ADMIN_ROLE, signers[0].address)).to.be
.true;
});

it("Only STAKING_ADMIN_ROLE can enable/disable whitelisting", async () => {
const STAKING_ADMIN_ROLE = await staking.STAKING_ADMIN_ROLE();

// * cannot enable/disable whitelisting
await expect(staking.connect(signers[1]).enableWhitelist()).to.be.reverted;
await expect(staking.connect(signers[1]).disableWhitelist()).to.be.reverted;

await staking.grantRole(STAKING_ADMIN_ROLE, signers[1].address);

await expect(staking.connect(signers[1]).enableWhitelist()).to.be.not
.reverted;
expect(await staking.isWhitelistEnabled()).to.be.true;
await expect(staking.connect(signers[1]).disableWhitelist()).to.be.not
.reverted;
expect(await staking.isWhitelistEnabled()).to.be.false;
await staking.revokeRole(STAKING_ADMIN_ROLE, signers[1].address);
});

});

0 comments on commit f9882b6

Please sign in to comment.