From f62c3c48e8a12b3899b2fc81e1fd998abc95d4bb Mon Sep 17 00:00:00 2001 From: ethyla <36268899+ethyla@users.noreply.github.com> Date: Mon, 19 Aug 2024 15:30:27 +0200 Subject: [PATCH 1/9] fix: still sending amount on migrate matic --- .../deployers/pol-upgrade/UpgradeDepositManager_Sepolia.s.sol | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/scripts/deployers/pol-upgrade/UpgradeDepositManager_Sepolia.s.sol b/scripts/deployers/pol-upgrade/UpgradeDepositManager_Sepolia.s.sol index dc1710fb..2e0ac404 100644 --- a/scripts/deployers/pol-upgrade/UpgradeDepositManager_Sepolia.s.sol +++ b/scripts/deployers/pol-upgrade/UpgradeDepositManager_Sepolia.s.sol @@ -88,10 +88,8 @@ contract UpgradeDepositManager_Sepolia is Script { // STEP 4 // call migrateMatic on the new DepositManager, migrating all MATIC - ERC20 maticToken = ERC20(matic); - uint256 amount = maticToken.balanceOf(address(depositManagerProxy)); bytes memory payloadMigrateMatic = abi.encodeWithSelector( - governance.update.selector, address(depositManagerProxy), abi.encodeWithSelector(depositManager.migrateMatic.selector, amount) + governance.update.selector, address(depositManagerProxy), abi.encodeWithSelector(depositManager.migrateMatic.selector) ); console.log("\n Send payloadMigrateMatic to: ", governanceAddress); From 08e71ffd8c19327d0e9e0adec6386797bfd9503d Mon Sep 17 00:00:00 2001 From: ethyla <36268899+ethyla@users.noreply.github.com> Date: Mon, 19 Aug 2024 15:41:16 +0200 Subject: [PATCH 2/9] fix: make disable init clearer --- contracts/staking/stakeManager/StakeManager.sol | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/contracts/staking/stakeManager/StakeManager.sol b/contracts/staking/stakeManager/StakeManager.sol index 877ea40f..f06bc38d 100644 --- a/contracts/staking/stakeManager/StakeManager.sol +++ b/contracts/staking/stakeManager/StakeManager.sol @@ -68,7 +68,9 @@ contract StakeManager is require(validators[validatorId].contractAddress == msg.sender, "Invalid contract address"); } - constructor() public GovernanceLockable(address(0x0)) initializer {} + constructor() public GovernanceLockable(address(0x0)) { + _disableInitializer(); + } function initialize( address _registry, From b025f59b5dd5ddaea5831d072a292c3a90920861 Mon Sep 17 00:00:00 2001 From: ethyla <36268899+ethyla@users.noreply.github.com> Date: Mon, 19 Aug 2024 15:50:50 +0200 Subject: [PATCH 3/9] fix: print correct depositManagerImpl address --- .../pol-upgrade/UpgradeStake_DepositManager_Mainnet.s.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/deployers/pol-upgrade/UpgradeStake_DepositManager_Mainnet.s.sol b/scripts/deployers/pol-upgrade/UpgradeStake_DepositManager_Mainnet.s.sol index c30e03e5..cf28cccd 100644 --- a/scripts/deployers/pol-upgrade/UpgradeStake_DepositManager_Mainnet.s.sol +++ b/scripts/deployers/pol-upgrade/UpgradeStake_DepositManager_Mainnet.s.sol @@ -102,7 +102,7 @@ contract UpgradeStake_DepositManager_Mainnet is Script { // deploy new DepositManager version depositManagerImpl = DepositManager(payable(deployCode("out/DepositManager.sol/DepositManager.json"))); - console.log("deployed DepositManager implementation at: ", address(validatorShareImpl)); + console.log("deployed DepositManager implementation at: ", address(depositManagerImpl)); vm.stopBroadcast(); } From 993fe4861b235a7b520f4f0f6701edb076b29423 Mon Sep 17 00:00:00 2001 From: ethyla <36268899+ethyla@users.noreply.github.com> Date: Mon, 19 Aug 2024 15:57:43 +0200 Subject: [PATCH 4/9] fix: make init function ordering clearer --- .../staking/stakeManager/StakeManager.sol | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/contracts/staking/stakeManager/StakeManager.sol b/contracts/staking/stakeManager/StakeManager.sol index f06bc38d..0f3f181e 100644 --- a/contracts/staking/stakeManager/StakeManager.sol +++ b/contracts/staking/stakeManager/StakeManager.sol @@ -110,6 +110,30 @@ contract StakeManager is delegationEnabled = true; } + function reinitialize( + address _NFTContract, + address _stakingLogger, + address _validatorShareFactory, + address _extensionCode + ) external onlyGovernance initializer { + require(isContract(_extensionCode)); + eventsHub = address(0x0); + extensionCode = _extensionCode; + NFTContract = StakingNFT(_NFTContract); + logger = StakingInfo(_stakingLogger); + validatorShareFactory = ValidatorShareFactory(_validatorShareFactory); + } + + function initializePOL( + address _tokenNew, + address _migration + ) external onlyGovernance initializer { + tokenMatic = IERC20(token); + token = IERC20(_tokenNew); + migration = IPolygonMigration(_migration); + _convertMaticToPOL(tokenMatic.balanceOf(address(this))); + } + function isOwner() public view returns (bool) { address _owner; bytes32 position = keccak256("matic.network.proxy.owner"); @@ -323,30 +347,6 @@ contract StakeManager is _transferToken(destination, amount, true); } - function reinitialize( - address _NFTContract, - address _stakingLogger, - address _validatorShareFactory, - address _extensionCode - ) external onlyGovernance { - require(isContract(_extensionCode)); - eventsHub = address(0x0); - extensionCode = _extensionCode; - NFTContract = StakingNFT(_NFTContract); - logger = StakingInfo(_stakingLogger); - validatorShareFactory = ValidatorShareFactory(_validatorShareFactory); - } - - function initializePOL( - address _tokenNew, - address _migration - ) external onlyGovernance { - tokenMatic = IERC20(token); - token = IERC20(_tokenNew); - migration = IPolygonMigration(_migration); - _convertMaticToPOL(tokenMatic.balanceOf(address(this))); - } - /** Public Methods */ From e33a4b2133e2170198754164682594463a230cfd Mon Sep 17 00:00:00 2001 From: ethyla <36268899+ethyla@users.noreply.github.com> Date: Mon, 19 Aug 2024 16:15:57 +0200 Subject: [PATCH 5/9] fix: disable slashing on validatorshare --- contracts/staking/stakeManager/StakeManager.sol | 2 -- .../staking/validatorShare/ValidatorShare.sol | 17 +---------------- 2 files changed, 1 insertion(+), 18 deletions(-) diff --git a/contracts/staking/stakeManager/StakeManager.sol b/contracts/staking/stakeManager/StakeManager.sol index 0f3f181e..b04559de 100644 --- a/contracts/staking/stakeManager/StakeManager.sol +++ b/contracts/staking/stakeManager/StakeManager.sol @@ -1114,8 +1114,6 @@ contract StakeManager is } function _unstake(uint256 validatorId, uint256 exitEpoch, bool pol) internal { - // TODO: if validators unstake and slashed to 0, he will be forced to unstake again - // must think how to handle it correctly _updateRewards(validatorId); uint256 amount = validators[validatorId].amount; diff --git a/contracts/staking/validatorShare/ValidatorShare.sol b/contracts/staking/validatorShare/ValidatorShare.sol index 7665f67b..52116445 100644 --- a/contracts/staking/validatorShare/ValidatorShare.sol +++ b/contracts/staking/validatorShare/ValidatorShare.sol @@ -267,22 +267,7 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I uint256 delegatedAmount, uint256 totalAmountToSlash ) external onlyOwner returns (uint256) { - uint256 _withdrawPool = withdrawPool; - uint256 delegationAmount = delegatedAmount.add(_withdrawPool); - if (delegationAmount == 0) { - return 0; - } - // total amount to be slashed from delegation pool (active + inactive) - uint256 _amountToSlash = delegationAmount.mul(totalAmountToSlash).div(validatorStake.add(delegationAmount)); - uint256 _amountToSlashWithdrawalPool = _withdrawPool.mul(_amountToSlash).div(delegationAmount); - - // slash inactive pool - uint256 stakeSlashed = _amountToSlash.sub(_amountToSlashWithdrawalPool); - stakeManager.decreaseValidatorDelegatedAmount(validatorId, stakeSlashed); - activeAmount = activeAmount.sub(stakeSlashed); - - withdrawPool = withdrawPool.sub(_amountToSlashWithdrawalPool); - return _amountToSlash; + revert("Slashing disabled"); } function updateDelegation(bool _delegation) external onlyOwner { From 76632fb9c1debd3c8bd739a8d60aefb9934b270f Mon Sep 17 00:00:00 2001 From: ethyla <36268899+ethyla@users.noreply.github.com> Date: Mon, 19 Aug 2024 16:49:55 +0200 Subject: [PATCH 6/9] fix: restore init behaiviour --- contracts/staking/stakeManager/StakeManager.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/staking/stakeManager/StakeManager.sol b/contracts/staking/stakeManager/StakeManager.sol index b04559de..68fef49f 100644 --- a/contracts/staking/stakeManager/StakeManager.sol +++ b/contracts/staking/stakeManager/StakeManager.sol @@ -115,7 +115,7 @@ contract StakeManager is address _stakingLogger, address _validatorShareFactory, address _extensionCode - ) external onlyGovernance initializer { + ) external onlyGovernance { require(isContract(_extensionCode)); eventsHub = address(0x0); extensionCode = _extensionCode; @@ -127,7 +127,7 @@ contract StakeManager is function initializePOL( address _tokenNew, address _migration - ) external onlyGovernance initializer { + ) external onlyGovernance { tokenMatic = IERC20(token); token = IERC20(_tokenNew); migration = IPolygonMigration(_migration); From be3df7f4f873e89754336d990a6e2238d008ac87 Mon Sep 17 00:00:00 2001 From: ethyla <36268899+ethyla@users.noreply.github.com> Date: Mon, 19 Aug 2024 18:10:53 +0200 Subject: [PATCH 7/9] fix: consistency in encodeCall --- .../UpgradeStake_DepositManager_Mainnet.s.sol | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/deployers/pol-upgrade/UpgradeStake_DepositManager_Mainnet.s.sol b/scripts/deployers/pol-upgrade/UpgradeStake_DepositManager_Mainnet.s.sol index cf28cccd..9d249a20 100644 --- a/scripts/deployers/pol-upgrade/UpgradeStake_DepositManager_Mainnet.s.sol +++ b/scripts/deployers/pol-upgrade/UpgradeStake_DepositManager_Mainnet.s.sol @@ -118,7 +118,7 @@ contract UpgradeStake_DepositManager_Mainnet is Script { // STEP 1 // Update ValidatorShare registry entry bytes memory payloadRegistry1 = abi.encodeCall( - governance.update, (address(registry), abi.encodeCall(registry.updateContractMap, (keccak256("validatorShare"), address(validatorShareImpl)))) + Governance.update, (address(registry), abi.encodeCall(Registry.updateContractMap, (keccak256("validatorShare"), address(validatorShareImpl)))) ); console.log("Created payloadRegistry1 for: ", address(governance)); @@ -134,7 +134,7 @@ contract UpgradeStake_DepositManager_Mainnet is Script { // STEP 3 // Call initializePOL bytes memory payloadInitializePol3 = abi.encodeCall( - governance.update, (address(stakeManagerProxy), abi.encodeCall(stakeManagerProxy.initializePOL, (address(polToken), migrationAddress))) + Governance.update, (address(stakeManagerProxy), abi.encodeCall(StakeManagerProxy.initializePOL, (address(polToken), migrationAddress))) ); console.log("Created payloadInitializePol3 for: ", address(governance)); @@ -143,7 +143,7 @@ contract UpgradeStake_DepositManager_Mainnet is Script { // STEP 4 // Call updateContractMap on registry to add "pol" bytes memory payloadContractMapPol4 = - abi.encodeCall(governance.update, (address(registry), abi.encodeCall(registry.updateContractMap, (keccak256("pol"), address(polToken))))); + abi.encodeCall(Governance.update, (address(registry), abi.encodeCall(Registry.updateContractMap, (keccak256("pol"), address(polToken))))); console.log("Send payloadContractMapPol4 to: ", address(governance)); console.logBytes(payloadContractMapPol4); @@ -151,7 +151,7 @@ contract UpgradeStake_DepositManager_Mainnet is Script { // STEP 5 // Call updateContractMap on registry to add "matic" bytes memory payloadContractMapMatic5 = - abi.encodeCall(governance.update, (address(registry), abi.encodeCall(registry.updateContractMap, (keccak256("matic"), address(maticToken))))); + abi.encodeCall(Governance.update, (address(registry), abi.encodeCall(Registry.updateContractMap, (keccak256("matic"), address(maticToken))))); console.log("Send payloadContractMapMatic5 to: ", address(governance)); console.logBytes(payloadContractMapMatic5); @@ -159,7 +159,7 @@ contract UpgradeStake_DepositManager_Mainnet is Script { // STEP 6 // Call updateContractMap on registry to add "polygonMigration" bytes memory payloadContractMapMigration6 = abi.encodeCall( - governance.update, (address(registry), abi.encodeCall(registry.updateContractMap, (keccak256("polygonMigration"), migrationAddress))) + Governance.update, (address(registry), abi.encodeCall(Registry.updateContractMap, (keccak256("polygonMigration"), migrationAddress))) ); console.log("Send payloadContractMapMigration6 to: ", address(governance)); @@ -168,7 +168,7 @@ contract UpgradeStake_DepositManager_Mainnet is Script { // STEP 7 // call mapToken on the Registry to map POL to the PoS native gas token address (1010) bytes memory payloadMapToken7 = - abi.encodeCall(governance.update, (address(registry), abi.encodeCall(registry.mapToken, (address(polToken), nativeGasTokenAddress, false)))); + abi.encodeCall(Governance.update, (address(registry), abi.encodeCall(Registry.mapToken, (address(polToken), nativeGasTokenAddress, false)))); console.log("Send payloadMapToken7 to: ", address(governance)); console.logBytes(payloadMapToken7); @@ -183,7 +183,7 @@ contract UpgradeStake_DepositManager_Mainnet is Script { // STEP 9 // call migrateMatic on the new DepositManager, migrating all MATIC bytes memory payloadMigrateMatic9 = - abi.encodeCall(governance.update, (address(depositManagerProxy), abi.encodeCall(depositManagerProxy.migrateMatic, ()))); + abi.encodeCall(Governance.update, (address(depositManagerProxy), abi.encodeCall(DepositManagerProxy.migrateMatic, ()))); console.log("Send payloadMigrateMatic9 to: ", address(governance)); console.logBytes(payloadMigrateMatic9); From f4a5924fe35af6e2faf40f5bbe76928a2a468c84 Mon Sep 17 00:00:00 2001 From: ethyla <36268899+ethyla@users.noreply.github.com> Date: Tue, 20 Aug 2024 13:36:00 +0200 Subject: [PATCH 8/9] feat: improve readme --- README.md | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index b2047add..182ed875 100644 --- a/README.md +++ b/README.md @@ -4,19 +4,20 @@ Ethereum smart contracts that power the [Matic Network](https://polygon.technology/polygon-pos). +## Development ### Install dependencies with ``` npm install ``` -### Setup +### Setup git hooks ``` pre-commit install ``` -### Compile +### Prepare templates ``` npm run template:process -- --bor-chain-id 15001 @@ -27,7 +28,30 @@ bor-chain-id should be: Mainnet = 137 TestnetV4 (Mumbai) = 80001 -### Main chain and side chain +### Generate interfaces + +``` +npm run generate:interfaces +``` + +### Build + +``` +forge build +``` + +## Testing + +### Run forge upgrade forktest + +``` +forge test +``` + +### Run unit tests + + +#### Main chain and side chain - Main chain @@ -51,7 +75,7 @@ npm run bor:stop npm run bor:clean ``` -### Run tests +#### Run tests Run Hardhat test @@ -59,12 +83,6 @@ Run Hardhat test npm test:hardhat ``` -Run Foundry test - -``` -npm test:foundry -``` - ### Coverage Run coverage with From f0bdac8473124efb8024417fe61f1753f9a0ccb2 Mon Sep 17 00:00:00 2001 From: ethyla <36268899+ethyla@users.noreply.github.com> Date: Tue, 20 Aug 2024 13:36:18 +0200 Subject: [PATCH 9/9] fix: wrong interface --- .../UpgradeStake_DepositManager_Mainnet.s.sol | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/scripts/deployers/pol-upgrade/UpgradeStake_DepositManager_Mainnet.s.sol b/scripts/deployers/pol-upgrade/UpgradeStake_DepositManager_Mainnet.s.sol index 9d249a20..b4e481db 100644 --- a/scripts/deployers/pol-upgrade/UpgradeStake_DepositManager_Mainnet.s.sol +++ b/scripts/deployers/pol-upgrade/UpgradeStake_DepositManager_Mainnet.s.sol @@ -134,7 +134,7 @@ contract UpgradeStake_DepositManager_Mainnet is Script { // STEP 3 // Call initializePOL bytes memory payloadInitializePol3 = abi.encodeCall( - Governance.update, (address(stakeManagerProxy), abi.encodeCall(StakeManagerProxy.initializePOL, (address(polToken), migrationAddress))) + Governance.update, (address(stakeManagerProxy), abi.encodeCall(StakeManager.initializePOL, (address(polToken), migrationAddress))) ); console.log("Created payloadInitializePol3 for: ", address(governance)); @@ -145,7 +145,7 @@ contract UpgradeStake_DepositManager_Mainnet is Script { bytes memory payloadContractMapPol4 = abi.encodeCall(Governance.update, (address(registry), abi.encodeCall(Registry.updateContractMap, (keccak256("pol"), address(polToken))))); - console.log("Send payloadContractMapPol4 to: ", address(governance)); + console.log("Created payloadContractMapPol4 for: ", address(governance)); console.logBytes(payloadContractMapPol4); // STEP 5 @@ -153,7 +153,7 @@ contract UpgradeStake_DepositManager_Mainnet is Script { bytes memory payloadContractMapMatic5 = abi.encodeCall(Governance.update, (address(registry), abi.encodeCall(Registry.updateContractMap, (keccak256("matic"), address(maticToken))))); - console.log("Send payloadContractMapMatic5 to: ", address(governance)); + console.log("Created payloadContractMapMatic5 for: ", address(governance)); console.logBytes(payloadContractMapMatic5); // STEP 6 @@ -162,7 +162,7 @@ contract UpgradeStake_DepositManager_Mainnet is Script { Governance.update, (address(registry), abi.encodeCall(Registry.updateContractMap, (keccak256("polygonMigration"), migrationAddress))) ); - console.log("Send payloadContractMapMigration6 to: ", address(governance)); + console.log("Created payloadContractMapMigration6 for: ", address(governance)); console.logBytes(payloadContractMapMigration6); // STEP 7 @@ -170,22 +170,22 @@ contract UpgradeStake_DepositManager_Mainnet is Script { bytes memory payloadMapToken7 = abi.encodeCall(Governance.update, (address(registry), abi.encodeCall(Registry.mapToken, (address(polToken), nativeGasTokenAddress, false)))); - console.log("Send payloadMapToken7 to: ", address(governance)); + console.log("Created payloadMapToken7 for: ", address(governance)); console.logBytes(payloadMapToken7); // STEP 8 // update impl of proxy to DepositManager bytes memory payloadUpgradeDepositManager8 = abi.encodeCall(DepositManagerProxy.updateImplementation, (address(depositManagerImpl))); - console.log("Send payloadUpgradeDepositManager8 to: ", address(depositManagerProxy)); + console.log("Created payloadUpgradeDepositManager8 for: ", address(depositManagerProxy)); console.logBytes(payloadUpgradeDepositManager8); // STEP 9 // call migrateMatic on the new DepositManager, migrating all MATIC bytes memory payloadMigrateMatic9 = - abi.encodeCall(Governance.update, (address(depositManagerProxy), abi.encodeCall(DepositManagerProxy.migrateMatic, ()))); + abi.encodeCall(Governance.update, (address(depositManagerProxy), abi.encodeCall(DepositManager.migrateMatic, ()))); - console.log("Send payloadMigrateMatic9 to: ", address(governance)); + console.log("Created payloadMigrateMatic9 for: ", address(governance)); console.logBytes(payloadMigrateMatic9); console.log("----------------------");