-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add fork test tooling #419
Changes from 11 commits
df1e6f4
aaae7d6
d3f4806
28e29a7
cc01b28
516937b
383f2df
e5582e1
8526d1d
9728d78
5a7e530
efd24a5
14d1073
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ out/ | |
.vscode | ||
brownie-deploy/ | ||
.idea | ||
deployments-fork.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,7 @@ dependencies: | |
- OpenZeppelin/[email protected] | ||
- OpenZeppelin/[email protected] | ||
- paulrberg/[email protected] | ||
compiler: | ||
solc: | ||
remappings: | ||
- forge-std/=./lib/forge-std/src/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"1": { | ||
"executions": {}, | ||
"contracts": {} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,10 +90,6 @@ contract FixedRateRewardsSource is Governable, Initializable { | |
function previewRewards() public view returns (uint256 rewardAmount) { | ||
RewardConfig memory _config = rewardConfig; | ||
|
||
if (_config.lastCollect == 0) { | ||
return 0; | ||
} | ||
|
||
rewardAmount = (block.timestamp - _config.lastCollect) * _config.rewardsPerSecond; | ||
uint256 balance = IERC20(rewardToken).balanceOf(address(this)); | ||
if (rewardAmount > balance) { | ||
|
@@ -139,6 +135,11 @@ contract FixedRateRewardsSource is Governable, Initializable { | |
// Update storage | ||
RewardConfig storage _config = rewardConfig; | ||
emit RewardsPerSecondChanged(_rewardsPerSecond, _config.rewardsPerSecond); | ||
if (_config.rewardsPerSecond == 0) { | ||
// When changing rate from zero to non-zero, | ||
// Update lastCollect timestamp as well | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I only have 1 more nitpick:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is done |
||
_config.lastCollect = uint64(block.timestamp); | ||
} | ||
_config.rewardsPerSecond = _rewardsPerSecond; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// SPDX-License-Identifier: Unlicense | ||
pragma solidity 0.8.10; | ||
|
||
interface IMintableERC20 { | ||
function mint(address to, uint256 amount) external; | ||
function balanceOf(address owner) external view returns (uint256); | ||
function totalSupply() external view returns (uint256); | ||
function transfer(address to, uint256 amount) external returns (bool); | ||
function approve(address spender, uint256 allowance) external; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// SPDX-License-Identifier: Unlicense | ||
pragma solidity 0.8.10; | ||
|
||
interface IOGNGovernance { | ||
function state(uint256 proposalId) external view returns (uint256); | ||
function proposalCount() external view returns (uint256); | ||
function queue(uint256 proposalId) external; | ||
function execute(uint256 proposalId) external; | ||
function propose( | ||
address[] memory targets, | ||
string[] memory signatures, | ||
bytes[] memory calldatas, | ||
string memory description | ||
) external returns (uint256); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity 0.8.10; | ||
|
||
import {Vm} from "forge-std/Vm.sol"; | ||
import {Addresses} from "contracts/utils/Addresses.sol"; | ||
import "forge-std/console.sol"; | ||
|
||
import "OpenZeppelin/[email protected]/contracts/utils/Strings.sol"; | ||
|
||
library GovFive { | ||
struct GovFiveAction { | ||
address receiver; | ||
string fullsig; | ||
bytes data; | ||
} | ||
|
||
struct GovFiveProposal { | ||
string name; | ||
string description; | ||
GovFiveAction[] actions; | ||
} | ||
|
||
function setName(GovFiveProposal storage prop, string memory name) internal { | ||
prop.name = name; | ||
} | ||
|
||
function setDescription(GovFiveProposal storage prop, string memory description) internal { | ||
prop.description = description; | ||
} | ||
|
||
function action(GovFiveProposal storage prop, address receiver, string memory fullsig, bytes memory data) | ||
internal | ||
{ | ||
prop.actions.push(GovFiveAction({receiver: receiver, fullsig: fullsig, data: data})); | ||
} | ||
|
||
function printTxData(GovFiveProposal storage prop) internal { | ||
console.log("-----------------------------------"); | ||
console.log("Create following tx on Gnosis safe:"); | ||
console.log("-----------------------------------"); | ||
for (uint256 i = 0; i < prop.actions.length; i++) { | ||
GovFiveAction memory propAction = prop.actions[i]; | ||
bytes memory sig = abi.encodePacked(bytes4(keccak256(bytes(propAction.fullsig)))); | ||
|
||
console.log("### Tx", i + 1); | ||
console.log("Address:", propAction.receiver); | ||
console.log("Data:"); | ||
console.logBytes(abi.encodePacked(sig, propAction.data)); | ||
} | ||
} | ||
|
||
function execute(GovFiveProposal storage prop) internal { | ||
address VM_ADDRESS = address(uint160(uint256(keccak256("hevm cheat code")))); | ||
Vm vm = Vm(VM_ADDRESS); | ||
for (uint256 i = 0; i < prop.actions.length; i++) { | ||
GovFiveAction memory propAction = prop.actions[i]; | ||
bytes memory sig = abi.encodePacked(bytes4(keccak256(bytes(propAction.fullsig)))); | ||
vm.prank(Addresses.TIMELOCK); | ||
(bool success, bytes memory data) = propAction.receiver.call(abi.encodePacked(sig, propAction.data)); | ||
if (!success) { | ||
console.log(propAction.fullsig); | ||
revert("Multisig action failed"); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,12 @@ src = 'contracts' | |
test = 'tests' | ||
remappings = [ | ||
"contracts/=./contracts", | ||
"script/=./script", | ||
"tests/=./tests", | ||
"OpenZeppelin/openzeppelin-contracts@02fcc75bb7f35376c22def91b0fb9bc7a50b9458/=./lib/openzeppelin-contracts", | ||
"OpenZeppelin/openzeppelin-contracts-upgradeable@a16f26a063cd018c4c986832c3df332a131f53b9/=./lib/openzeppelin-contracts-upgradeable", | ||
"OpenZeppelin/[email protected]/=./lib/openzeppelin-contracts", | ||
"OpenZeppelin/[email protected]/=./lib/openzeppelin-contracts-upgradeable", | ||
"paulrberg/[email protected]/=./lib/prb-math" | ||
] | ||
fs_permissions = [{ access = "read-write", path = "./build"}] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// SPDX-License-Identifier: Unlicense | ||
pragma solidity 0.8.10; | ||
|
||
import "forge-std/Script.sol"; | ||
import "OpenZeppelin/[email protected]/contracts/utils/Strings.sol"; | ||
|
||
import {BaseMainnetScript} from "./mainnet/BaseMainnetScript.sol"; | ||
|
||
import {XOGNSetupScript} from "./mainnet/010_xOGNSetupScript.sol"; | ||
import {OgnOgvMigrationScript} from "./mainnet/011_OgnOgvMigrationScript.sol"; | ||
import {XOGNGovernanceScript} from "./mainnet/012_xOGNGovernanceScript.sol"; | ||
|
||
contract DeployManager is Script { | ||
mapping(string => address) public deployedContracts; | ||
mapping(string => bool) public scriptsExecuted; | ||
|
||
function isForked() public view returns (bool) { | ||
return vm.isContext(VmSafe.ForgeContext.ScriptDryRun) || vm.isContext(VmSafe.ForgeContext.Test) | ||
|| vm.isContext(VmSafe.ForgeContext.TestGroup); | ||
} | ||
|
||
function getDeploymentFilePath() public view returns (string memory) { | ||
return isForked() ? getForkDeploymentFilePath() : getMainnetDeploymentFilePath(); | ||
} | ||
|
||
function getMainnetDeploymentFilePath() public view returns (string memory) { | ||
return string(abi.encodePacked(vm.projectRoot(), "/build/deployments.json")); | ||
} | ||
|
||
function getForkDeploymentFilePath() public view returns (string memory) { | ||
return string(abi.encodePacked(vm.projectRoot(), "/build/deployments-fork.json")); | ||
} | ||
|
||
function setUp() external { | ||
string memory chainIdStr = Strings.toString(block.chainid); | ||
string memory chainIdKey = string(abi.encodePacked(".", chainIdStr)); | ||
|
||
string memory mainnetFilePath = getMainnetDeploymentFilePath(); | ||
if (!vm.isFile(mainnetFilePath)) { | ||
// Create mainnet deployment file if it doesn't exist | ||
vm.writeFile( | ||
mainnetFilePath, | ||
string(abi.encodePacked('{ "', chainIdStr, '": { "executions": {}, "contracts": {} } }')) | ||
); | ||
} else if (!vm.keyExistsJson(vm.readFile(mainnetFilePath), chainIdKey)) { | ||
// Create network entry if it doesn't exist | ||
vm.writeJson( | ||
vm.serializeJson(chainIdStr, '{ "executions": {}, "contracts": {} }'), mainnetFilePath, chainIdKey | ||
); | ||
} | ||
|
||
if (isForked()) { | ||
// Duplicate Mainnet File | ||
vm.writeFile(getForkDeploymentFilePath(), vm.readFile(mainnetFilePath)); | ||
} | ||
} | ||
|
||
function run() external { | ||
// TODO: Use vm.readDir to recursively build this? | ||
_runDeployFile(new XOGNSetupScript()); | ||
_runDeployFile(new OgnOgvMigrationScript()); | ||
_runDeployFile(new XOGNGovernanceScript()); | ||
} | ||
|
||
function _runDeployFile(BaseMainnetScript deployScript) internal { | ||
string memory chainIdStr = Strings.toString(block.chainid); | ||
string memory chainIdKey = string(abi.encodePacked(".", chainIdStr)); | ||
|
||
string memory contractsKey = string(abi.encodePacked(chainIdKey, ".contracts")); | ||
string memory executionsKey = string(abi.encodePacked(chainIdKey, ".executions")); | ||
|
||
string memory deploymentsFilePath = getDeploymentFilePath(); | ||
string memory fileContents = vm.readFile(deploymentsFilePath); | ||
|
||
/** | ||
* Execution History | ||
*/ | ||
string memory currentExecutions = ""; | ||
string[] memory executionKeys = vm.parseJsonKeys(fileContents, executionsKey); | ||
|
||
for (uint256 i = 0; i < executionKeys.length; ++i) { | ||
uint256 deployedTimestamp = | ||
vm.parseJsonUint(fileContents, string(abi.encodePacked(executionsKey, ".", executionKeys[i]))); | ||
|
||
currentExecutions = vm.serializeUint(executionsKey, executionKeys[i], deployedTimestamp); | ||
scriptsExecuted[executionKeys[i]] = true; | ||
} | ||
|
||
if (scriptsExecuted[deployScript.DEPLOY_NAME()]) { | ||
// TODO: Handle any active governance proposal | ||
console.log("Skipping already deployed script"); | ||
return; | ||
} | ||
|
||
/** | ||
* Pre-deployment | ||
*/ | ||
string memory networkDeployments = ""; | ||
string[] memory existingContracts = vm.parseJsonKeys(fileContents, contractsKey); | ||
for (uint256 i = 0; i < existingContracts.length; ++i) { | ||
address deployedAddr = | ||
vm.parseJsonAddress(fileContents, string(abi.encodePacked(contractsKey, ".", existingContracts[i]))); | ||
|
||
networkDeployments = vm.serializeAddress(contractsKey, existingContracts[i], deployedAddr); | ||
|
||
deployedContracts[existingContracts[i]] = deployedAddr; | ||
|
||
deployScript.preloadDeployedContract(existingContracts[i], deployedAddr); | ||
} | ||
|
||
// Deployment | ||
deployScript.setUp(); | ||
deployScript.run(); | ||
|
||
/** | ||
* Post-deployment | ||
*/ | ||
BaseMainnetScript.DeployRecord[] memory records = deployScript.getAllDeployRecords(); | ||
|
||
for (uint256 i = 0; i < records.length; ++i) { | ||
string memory name = records[i].name; | ||
address addr = records[i].addr; | ||
|
||
console.log(string(abi.encodePacked("> Recorded Deploy of ", name, " at")), addr); | ||
networkDeployments = vm.serializeAddress(contractsKey, name, addr); | ||
deployedContracts[name] = addr; | ||
} | ||
|
||
vm.writeJson(networkDeployments, deploymentsFilePath, contractsKey); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm still getting a lot of random failure on these writeJson's (about 2/3 of the 50% of tests fail, about 1/6 of the time, all tests succeed) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. It's mostly about writing twice to the same file back to back. Thats why I added a small delay. Will work on a clean fix to do a single write instead |
||
console.log("> Deployment addresses stored."); | ||
|
||
/** | ||
* Write Execution History | ||
*/ | ||
currentExecutions = vm.serializeUint(executionsKey, deployScript.DEPLOY_NAME(), block.timestamp); | ||
|
||
// Sleep 0.5s so that the previous write is complete | ||
vm.sleep(500); | ||
vm.writeJson(currentExecutions, deploymentsFilePath, executionsKey); | ||
console.log("> Deploy script execution complete."); | ||
} | ||
|
||
function getDeployment(string calldata contractName) external view returns (address) { | ||
return deployedContracts[contractName]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently deploy script 10 deploys the contract and deploy script 12 sets the rewards per second. I would leave this check in since it is a sanity check and not very gas expensive one at that.
With the current code of the contract, the contract needs to be deployed and funded in the correct order, or it will not function properly. Since one can:
I think the correct functioning of the contract should be assured even if the deploy is not configured perfectly as expected. For that reason I would:
_rewardsPerSecond
from the initialize function. Any deployment of this contract will suffer the same issues on initialisation of rewards source and start of emission.setRewardsPerSecond
isn't called with a non 0 value.setRewardsPerSecond
with a 0 value. This would in effect pause the contract and remove any rewards accrued since the last collect. Is that intentional? If not change the_setRewardsPerSecond
to:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As of now, The deploy script initializes the proxy and implementation in the same call. So, once the proxy is initialized, it'll always have a lastCollect set and will never be zero. That's the reason I removed that check as it seemed redundant.
If the
rewardsPerSecond
isn't changed, won't previewRewards just return zero (and 0 reward collected)? Why would it revert?Agree with this, will make this change
Yep. xOGN Governance (
rewardsTarget
) is only contract that can callcollectRewards
. Also, xOGN Governance doesn't account for any rewards sent to it directly (It does a balance check before and after collectRewards to figure out the diff and uses that as reward collected). So, collecting rewards internally won't be possible when changing the rateI'm not sure if we want the ability to pause rewards in future for any reason. Let me check with @DanielVF
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for correcting me on first 2 points; you're right they are not an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm good with a change to rewards rate potentially changing how much users receive for the past, as well as the future. We'll probably usually couple it with a xOGN collect in most changes though.
Having a "pause" feature that stops rewards could be a useful thing.