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

Commit

Permalink
fix: bridge ERC20 tokens from L1 to L2 using migrations scripts (#486)
Browse files Browse the repository at this point in the history
* fix: initialise child ERC20 tokens

* test

* fix: update child chain address of newly created tokens

* chore: simplify

* chore: rename variables

* test: update erc20/erc721 deployment

* chore: typo

* fix: small issue when initializing erc721

* chore: use `new` instead of `deploy`

* chore: nit

* test

* chore: nit

* chore: update the other migration file

* chore: nit
  • Loading branch information
leovct authored Nov 28, 2023
1 parent fa783ea commit a29ed28
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 58 deletions.
80 changes: 51 additions & 29 deletions deploy-migrations/4_deploy_child_contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ const SafeMath = artifacts.require(
'openzeppelin-solidity/contracts/math/SafeMath.sol'
)
const ChildChain = artifacts.require('ChildChain')

const ChildERC20Proxified = artifacts.require('ChildERC20Proxified')
const ChildERC721Proxified = artifacts.require('ChildERC721Proxified')
const ChildTokenProxy = artifacts.require('ChildTokenProxy')

const MRC20 = artifacts.require('MRC20')

module.exports = async function(deployer, network, accounts) {
module.exports = async function(deployer, _, _) {
deployer.then(async() => {
await deployer.deploy(SafeMath)
await deployer.link(SafeMath, [ChildChain])
Expand All @@ -15,33 +20,50 @@ module.exports = async function(deployer, network, accounts) {
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
)
// Deploy MaticWeth (ERC20) child contract and its proxy.
// Initialize the contract, update the child chain and map the token with its root contract.
const childMaticWethProxified = await ChildERC20Proxified.new()
console.log('Child MaticWethProxified contract deployed')
const childMaticWethProxy = await ChildTokenProxy.new(childMaticWethProxified.address)
console.log('Child MaticWeth proxy contract deployed')
const childMaticWeth = await ChildERC20Proxified.at(childMaticWethProxy.address)

await childMaticWeth.initialize(contractAddresses.root.tokens.MaticWeth, 'Eth on Matic', 'ETH', 18)
console.log('Child MaticWeth contract initialized')
await childMaticWeth.changeChildChain(childChain.address)
console.log('Child MaticWeth child chain updated')
await childChain.mapToken(contractAddresses.root.tokens.MaticWeth, childMaticWeth.address, false)
console.log('Root and child MaticWeth contracts mapped')

// Same thing for TestToken (ERC20).
const childTestTokenProxified = await ChildERC20Proxified.new()
console.log('\nChild TestTokenProxified contract deployed')
const childTestTokenProxy = await ChildTokenProxy.new(childTestTokenProxified.address)
console.log('Child TestToken proxy contract deployed')
const childTestToken = await ChildERC20Proxified.at(childTestTokenProxy.address)

await childTestToken.initialize(contractAddresses.root.tokens.TestToken, 'Test Token', 'TST', 18)
console.log('Child TestToken contract initialized')
await childTestToken.changeChildChain(childChain.address)
console.log('Child TestToken child chain updated')
await childChain.mapToken(contractAddresses.root.tokens.TestToken, childTestToken.address, false)
console.log('Root and child TestToken contracts mapped')

let TestToken = await childChain.addToken(
accounts[0],
contractAddresses.root.tokens.TestToken,
'Test Token',
'TST',
18,
false // _isERC721
)
// Same thing for TestERC721.
const childTestERC721Proxified = await ChildERC721Proxified.new()
console.log('\nChild TestERC721Proxified contract deployed')
const childTestERC721Proxy = await ChildTokenProxy.new(childTestERC721Proxified.address)
console.log('Child TestERC721 proxy contract deployed')
const childTestERC721 = await ChildERC721Proxified.at(childTestERC721Proxy.address)

let RootERC721 = await childChain.addToken(
accounts[0],
contractAddresses.root.tokens.RootERC721,
'Test ERC721',
'TST721',
0,
true // _isERC721
)
await childTestERC721.initialize(contractAddresses.root.tokens.RootERC721, 'Test ERC721', 'TST721')
console.log('Child TestERC721 contract initialized')
await childTestERC721.changeChildChain(childChain.address)
console.log('Child TestERC721 child chain updated')
await childChain.mapToken(contractAddresses.root.tokens.RootERC721, childTestERC721.address, true) // ERC721
console.log('Root and child testERC721 contracts mapped')

// Initialize and map MaticToken.
const maticToken = await MRC20.at('0x0000000000000000000000000000000000001010')
const maticOwner = await maticToken.owner()
if (maticOwner === '0x0000000000000000000000000000000000000000') {
Expand All @@ -51,12 +73,12 @@ module.exports = async function(deployer, network, accounts) {
await childChain.mapToken(contractAddresses.root.tokens.MaticToken, '0x0000000000000000000000000000000000001010', false)

contractAddresses.child = {
ChildChain: ChildChain.address,
ChildChain: childChain.address,
tokens: {
MaticWeth: MaticWeth.logs.find(log => log.event === 'NewToken').args.token,
MaticWeth: childMaticWeth.address,
MaticToken: '0x0000000000000000000000000000000000001010',
TestToken: TestToken.logs.find(log => log.event === 'NewToken').args.token,
RootERC721: RootERC721.logs.find(log => log.event === 'NewToken').args.token
TestToken: childTestToken.address,
RootERC721: childTestERC721.address
}
}
utils.writeContractAddresses(contractAddresses)
Expand Down
80 changes: 51 additions & 29 deletions migrations/5_deploy_child_contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ const SafeMath = artifacts.require(
'openzeppelin-solidity/contracts/math/SafeMath.sol'
)
const ChildChain = artifacts.require('ChildChain')

const ChildERC20Proxified = artifacts.require('ChildERC20Proxified')
const ChildERC721Proxified = artifacts.require('ChildERC721Proxified')
const ChildTokenProxy = artifacts.require('ChildTokenProxy')

const MRC20 = artifacts.require('MRC20')

module.exports = async function(deployer, network, accounts) {
module.exports = async function(deployer, _, _) {
if (deployer.network !== 'bor') {
return
}
Expand All @@ -19,33 +24,50 @@ module.exports = async function(deployer, network, accounts) {
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
)
// Deploy MaticWeth (ERC20) child contract and its proxy.
// Initialize the contract, update the child chain and map the token with its root contract.
const childMaticWethProxified = await ChildERC20Proxified.new()
console.log('Child MaticWethProxified contract deployed')
const childMaticWethProxy = await ChildTokenProxy.new(childMaticWethProxified.address)
console.log('Child MaticWeth proxy contract deployed')
const childMaticWeth = await ChildERC20Proxified.at(childMaticWethProxy.address)

await childMaticWeth.initialize(contractAddresses.root.tokens.MaticWeth, 'Eth on Matic', 'ETH', 18)
console.log('Child MaticWeth contract initialized')
await childMaticWeth.changeChildChain(childChain.address)
console.log('Child MaticWeth child chain updated')
await childChain.mapToken(contractAddresses.root.tokens.MaticWeth, childMaticWeth.address, false)
console.log('Root and child MaticWeth contracts mapped')

// Same thing for TestToken (ERC20).
const childTestTokenProxified = await ChildERC20Proxified.new()
console.log('\nChild TestTokenProxified contract deployed')
const childTestTokenProxy = await ChildTokenProxy.new(childTestTokenProxified.address)
console.log('Child TestToken proxy contract deployed')
const childTestToken = await ChildERC20Proxified.at(childTestTokenProxy.address)

await childTestToken.initialize(contractAddresses.root.tokens.TestToken, 'Test Token', 'TST', 18)
console.log('Child TestToken contract initialized')
await childTestToken.changeChildChain(childChain.address)
console.log('Child TestToken child chain updated')
await childChain.mapToken(contractAddresses.root.tokens.TestToken, childTestToken.address, false)
console.log('Root and child TestToken contracts mapped')

let TestToken = await childChain.addToken(
accounts[0],
contractAddresses.root.tokens.TestToken,
'Test Token',
'TST',
18,
false // _isERC721
)
// Same thing for TestERC721.
const childTestERC721Proxified = await ChildERC721Proxified.new()
console.log('\nChild TestERC721Proxified contract deployed')
const childTestERC721Proxy = await ChildTokenProxy.new(childTestERC721Proxified.address)
console.log('Child TestERC721 proxy contract deployed')
const childTestERC721 = await ChildERC721Proxified.at(childTestERC721Proxy.address)

let RootERC721 = await childChain.addToken(
accounts[0],
contractAddresses.root.tokens.RootERC721,
'Test ERC721',
'TST721',
0,
true // _isERC721
)
await childTestERC721.initialize(contractAddresses.root.tokens.RootERC721, 'Test ERC721', 'TST721')
console.log('Child TestERC721 contract initialized')
await childTestERC721.changeChildChain(childChain.address)
console.log('Child TestERC721 child chain updated')
await childChain.mapToken(contractAddresses.root.tokens.RootERC721, childTestERC721.address, true) // ERC721
console.log('Root and child testERC721 contracts mapped')

// Initialize and map MaticToken.
const maticToken = await MRC20.at('0x0000000000000000000000000000000000001010')
const maticOwner = await maticToken.owner()
if (maticOwner === '0x0000000000000000000000000000000000000000') {
Expand All @@ -55,12 +77,12 @@ module.exports = async function(deployer, network, accounts) {
await childChain.mapToken(contractAddresses.root.tokens.MaticToken, '0x0000000000000000000000000000000000001010', false)

contractAddresses.child = {
ChildChain: ChildChain.address,
ChildChain: childChain.address,
tokens: {
MaticWeth: MaticWeth.logs.find(log => log.event === 'NewToken').args.token,
MaticWeth: childMaticWeth.address,
MaticToken: '0x0000000000000000000000000000000000001010',
TestToken: TestToken.logs.find(log => log.event === 'NewToken').args.token,
RootERC721: RootERC721.logs.find(log => log.event === 'NewToken').args.token
TestToken: childTestToken.address,
RootERC721: childTestERC721.address
}
}
utils.writeContractAddresses(contractAddresses)
Expand Down

0 comments on commit a29ed28

Please sign in to comment.