Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #352 from maticnetwork/migration-fix
Browse files Browse the repository at this point in the history
Migration fix
  • Loading branch information
jdkanani authored Mar 17, 2021
2 parents b5cc84b + 783349f commit 47c585f
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 16 deletions.
53 changes: 47 additions & 6 deletions migrations/2_deploy_root_contracts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Deploy minimal number of contracts to link the libraries with the contracts
const utils = require('./utils')

const bluebird = require('bluebird')

Expand All @@ -18,6 +19,7 @@ const RLPEncode = artifacts.require('RLPEncode')

const Registry = artifacts.require('Registry')
const Governance = artifacts.require('Governance')
const GovernanceProxy = artifacts.require('GovernanceProxy')
const RootChain = artifacts.require('RootChain')
const RootChainProxy = artifacts.require('RootChainProxy')
const DepositManager = artifacts.require('DepositManager')
Expand All @@ -31,6 +33,7 @@ const SlashingManager = artifacts.require('SlashingManager')
const StakingInfo = artifacts.require('StakingInfo')
const StakingNFT = artifacts.require('StakingNFT')
const ValidatorShareFactory = artifacts.require('ValidatorShareFactory')
const ValidatorShare = artifacts.require('ValidatorShare')
const ERC20Predicate = artifacts.require('ERC20Predicate')
const ERC721Predicate = artifacts.require('ERC721Predicate')
const MintableERC721Predicate = artifacts.require('MintableERC721Predicate')
Expand All @@ -46,6 +49,7 @@ const StakeManagerTest = artifacts.require('StakeManagerTest')
const ExitNFT = artifacts.require('ExitNFT')
const MaticWeth = artifacts.require('MaticWETH')
const TestToken = artifacts.require('TestToken')
const RootERC721 = artifacts.require('RootERC721')

const StakeManagerExtension = artifacts.require('StakeManagerExtension')
const EventsHub = artifacts.require('EventsHub')
Expand Down Expand Up @@ -168,9 +172,13 @@ module.exports = async function(deployer, network, accounts) {
})

await deployer.deploy(Governance)
await deployer.deploy(Registry, Governance.address)
await deployer.deploy(GovernanceProxy, Governance.address)
await deployer.deploy(Registry, GovernanceProxy.address)
await deployer.deploy(ValidatorShareFactory)
await deployer.deploy(TestToken, 'Matic Test', 'MATICTEST')
await deployer.deploy(ValidatorShare)
const maticToken = await deployer.deploy(TestToken, 'MATIC', 'MATIC')
await deployer.deploy(TestToken, 'Test ERC20', 'TEST20')
await deployer.deploy(RootERC721, 'Test ERC721', 'TST721')
await deployer.deploy(StakingInfo, Registry.address)
await deployer.deploy(StakingNFT, 'Matic Validator', 'MV')

Expand All @@ -186,7 +194,7 @@ module.exports = async function(deployer, network, accounts) {
DepositManager.address,
Registry.address,
RootChainProxy.address,
Governance.address
GovernanceProxy.address
)

await deployer.deploy(ExitNFT, Registry.address)
Expand Down Expand Up @@ -214,12 +222,12 @@ module.exports = async function(deployer, network, accounts) {
StakeManager.address,
stakeManager.contract.methods.initialize(
Registry.address,
RootChain.address,
TestToken.address,
RootChainProxy.address,
maticToken.address,
StakingNFT.address,
StakingInfo.address,
ValidatorShareFactory.address,
Governance.address,
GovernanceProxy.address,
accounts[0],
auctionImpl.address
).encodeABI()
Expand Down Expand Up @@ -263,5 +271,38 @@ module.exports = async function(deployer, network, accounts) {
Registry.address
)
])

const contractAddresses = {
root: {
Registry: Registry.address,
RootChain: RootChain.address,
GovernanceProxy: GovernanceProxy.address,
RootChainProxy: RootChainProxy.address,
DepositManager: DepositManager.address,
DepositManagerProxy: DepositManagerProxy.address,
WithdrawManager: WithdrawManager.address,
WithdrawManagerProxy: WithdrawManagerProxy.address,
StakeManager: StakeManager.address,
StakeManagerProxy: StakeManagerProxy.address,
SlashingManager: SlashingManager.address,
StakingInfo: StakingInfo.address,
ExitNFT: ExitNFT.address,
StateSender: StateSender.address,
predicates: {
ERC20Predicate: ERC20Predicate.address,
ERC721Predicate: ERC721Predicate.address,
MarketplacePredicate: MarketplacePredicate.address,
TransferWithSigPredicate: TransferWithSigPredicate.address
},
tokens: {
MaticToken: maticToken.address,
MaticWeth: MaticWeth.address,
TestToken: TestToken.address,
RootERC721: RootERC721.address
}
}
}

utils.writeContractAddresses(contractAddresses)
})
}
16 changes: 13 additions & 3 deletions migrations/4_initialize_state.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const ethUtils = require('ethereumjs-util')
const bluebird = require('bluebird')

const utils = require('./utils')
const Registry = artifacts.require('Registry')
const ValidatorShare = artifacts.require('ValidatorShare')
const DepositManagerProxy = artifacts.require('DepositManagerProxy')
const StateSender = artifacts.require('StateSender')
const WithdrawManagerProxy = artifacts.require('WithdrawManagerProxy')
Expand All @@ -24,10 +25,13 @@ async function updateContractMap(governance, registry, nameHash, value) {

module.exports = async function(deployer) {
deployer.then(async() => {
const contractAddresses = utils.getContractAddresses()
const governance = await Governance.at(contractAddresses.root.GovernanceProxy)

await bluebird
.all([
Governance.deployed(),
Registry.deployed(),
ValidatorShare.deployed(),
DepositManagerProxy.deployed(),
StateSender.deployed(),
WithdrawManagerProxy.deployed(),
Expand All @@ -40,8 +44,8 @@ module.exports = async function(deployer) {
EventsHubProxy.deployed()
])
.spread(async function(
governance,
registry,
validatorShare,
depositManagerProxy,
stateSender,
withdrawManagerProxy,
Expand All @@ -53,6 +57,12 @@ module.exports = async function(deployer) {
TransferWithSigPredicate,
EventsHubProxy
) {
await updateContractMap(
governance,
registry,
ethUtils.keccak256('validatorShare'),
validatorShare.address
)
await updateContractMap(
governance,
registry,
Expand Down
68 changes: 68 additions & 0 deletions migrations/5_deploy_child_contracts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const utils = require('./utils')

const SafeMath = artifacts.require(
'openzeppelin-solidity/contracts/math/SafeMath.sol'
)
const ChildChain = artifacts.require('ChildChain')
const MRC20 = artifacts.require('MRC20')

module.exports = async function(deployer, network, accounts) {
if (deployer.network !== 'bor') {
return
}

deployer.then(async() => {
await deployer.deploy(SafeMath)
await deployer.link(SafeMath, [ChildChain])
await deployer.deploy(ChildChain)

const childChain = await ChildChain.deployed()
const contractAddresses = utils.getContractAddresses()

let MaticWeth = await childChain.addToken(
accounts[0],
contractAddresses.root.tokens.MaticWeth,
'ETH on Matic',
'ETH',
18,
false // _isERC721
)

let TestToken = await childChain.addToken(
accounts[0],
contractAddresses.root.tokens.TestToken,
'Test Token',
'TST',
18,
false // _isERC721
)

let RootERC721 = await childChain.addToken(
accounts[0],
contractAddresses.root.tokens.RootERC721,
'Test ERC721',
'TST721',
0,
true // _isERC721
)

const maticToken = await MRC20.at('0x0000000000000000000000000000000000001010')
const maticOwner = await maticToken.owner()
if (maticOwner === '0x0000000000000000000000000000000000000000') {
// matic contract at 0x1010 can only be initialized once, after the bor image starts to run
await maticToken.initialize(ChildChain.address, contractAddresses.root.tokens.MaticToken)
}
await childChain.mapToken(contractAddresses.root.tokens.MaticToken, '0x0000000000000000000000000000000000001010', false)

contractAddresses.child = {
ChildChain: ChildChain.address,
tokens: {
MaticWeth: MaticWeth.logs.find(log => log.event === 'NewToken').args.token,
MaticToken: '0x0000000000000000000000000000000000001010',
TestToken: TestToken.logs.find(log => log.event === 'NewToken').args.token,
RootERC721: RootERC721.logs.find(log => log.event === 'NewToken').args.token
}
}
utils.writeContractAddresses(contractAddresses)
})
}
69 changes: 69 additions & 0 deletions migrations/6_sync_child_state_to_root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const ethUtils = require('ethereumjs-util')
const utils = require('./utils')

const Registry = artifacts.require('Registry')
const Governance = artifacts.require('Governance')
const StateSender = artifacts.require('StateSender')
const DepositManager = artifacts.require('DepositManager')

module.exports = async function(deployer, network, accounts) {
deployer.then(async() => {
const contractAddresses = utils.getContractAddresses()

if (!contractAddresses.child) {
return
}

const registry = await Registry.at(contractAddresses.root.Registry)
const governance = await Governance.at(contractAddresses.root.GovernanceProxy)

await governance.update(
registry.address,
registry.contract.methods.mapToken(
contractAddresses.root.tokens.MaticWeth,
contractAddresses.child.tokens.MaticWeth,
false /* isERC721 */
).encodeABI()
)

await governance.update(
registry.address,
registry.contract.methods.mapToken(
contractAddresses.root.tokens.MaticToken,
contractAddresses.child.tokens.MaticToken,
false /* isERC721 */
).encodeABI()
)

await governance.update(
registry.address,
registry.contract.methods.mapToken(
contractAddresses.root.tokens.TestToken,
contractAddresses.child.tokens.TestToken,
false /* isERC721 */
).encodeABI()
)

await governance.update(
registry.address,
registry.contract.methods.mapToken(
contractAddresses.root.tokens.RootERC721,
contractAddresses.child.tokens.RootERC721,
true /* isERC721 */
).encodeABI()
)

await governance.update(
registry.address,
registry.contract.methods.updateContractMap(
ethUtils.keccak256('childChain'),
contractAddresses.child.ChildChain
).encodeABI()
)

const stateSenderContract = await StateSender.at(contractAddresses.root.StateSender)
await stateSenderContract.register(contractAddresses.root.DepositManagerProxy, contractAddresses.child.ChildChain)
let depositManager = await DepositManager.at(contractAddresses.root.DepositManagerProxy)
await depositManager.updateChildChainAndStateSender()
})
}
12 changes: 12 additions & 0 deletions migrations/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const fs = require('fs')

export function getContractAddresses() {
return JSON.parse(fs.readFileSync(`${process.cwd()}/contractAddresses.json`).toString())
}

export function writeContractAddresses(contractAddresses) {
fs.writeFileSync(
`${process.cwd()}/contractAddresses.json`,
JSON.stringify(contractAddresses, null, 2) // Indent 2 spaces
)
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"truffle:test": "truffle test",
"truffle:coverage": "truffle run coverage",
"truffle:compile": "truffle compile",
"truffle:migrate": "truffle migrate --network development",
"truffle:migrate": "scripts/run-migration.sh",
"truffle:migrate:dev": "truffle migrate --network development",
"truffle:migrate:dev:bor": "truffle migrate --network bor",
"truffle:verify": "truffle run verify $npm_package_verify_contracts --network ropsten",
"test": "truffle test",
"coverage": "scripts/run-coverage.sh",
Expand Down
19 changes: 19 additions & 0 deletions scripts/run-blockchain.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

start_testrpc() {
npm run testrpc
}

start_blockchain() {
cd $PWD/test-blockchain
bash run-docker.sh
cd ..
}

echo "Starting our own geth instance"
start_blockchain

echo "Starting our own testrpc instance"
start_testrpc


4 changes: 4 additions & 0 deletions scripts/run-migration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
npm run truffle:compile
npm run truffle:migrate:dev -- --reset --to 4
npm run truffle:migrate:dev:bor -- --reset -f 5 --to 5
npm run truffle:migrate:dev -- -f 6 --to 6
2 changes: 0 additions & 2 deletions scripts/run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ start_testrpc
echo "Starting our own geth instance"
start_blockchain

npm run truffle:migrate "$@"

if [ "$SOLIDITY_COVERAGE" = true ]; then
npm run truffle:coverage "$@"
else
Expand Down
6 changes: 2 additions & 4 deletions scripts/stake.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ async function stake() {
const maticToken = await RootToken.at(contracts.root.tokens.MaticToken)
console.log({ stakeManager: stakeManager.address, maticToken: maticToken.address, stakeToken: await stakeManager.token() })
console.log('Sender accounts has a balanceOf', (await maticToken.balanceOf(accounts[0])).toString())
maticToken.approve(stakeManager.address, web3.utils.toWei('1000000'))
await delay(5)
await maticToken.approve(stakeManager.address, web3.utils.toWei('1000000'))
console.log('sent approve tx, staking now...')
// Remember to change the 4th parameter to false if delegation is not required
stakeManager.stakeFor(validatorAccount, stakeAmount, heimdallFee, true, pubkey)
return delay(5)
await stakeManager.stakeFor(validatorAccount, stakeAmount, heimdallFee, true, pubkey)
}

async function topUpForFee() {
Expand Down

0 comments on commit 47c585f

Please sign in to comment.