Last active
July 29, 2020 16:11
-
-
Save probablyangg/4c5cd0ba42e150fa4dc3c5cecb6a6e1b to your computer and use it in GitHub Desktop.
mapping erc20/721 tokens via PoS/Plasma - deploys new token on child, updates mapping on root and child chain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Web3 = require('web3') | |
const rootRPC = '' | |
const childRPC = '' | |
const web3 = new Web3 () | |
// add pvt for owner account | |
const pvtKey = '' | |
// registry contract | |
const registryAbi = require('./artifacts/Registry.json').abi | |
const registryAddress = require('./mumbai.json').Main.Contracts.Registry | |
const registry = new web3.eth.Contract (registryAbi, registryAddress) | |
registry.setProvider(rootRPC) | |
// governance contract | |
const governanceAbi = require('./artifacts/Governance.json').abi | |
const governanceAddress = require('./mumbai.json').Main.Contracts.GovernanceProxy | |
const governance = new web3.eth.Contract(governanceAbi, governanceAddress) | |
governance.setProvider(rootRPC) | |
// child chain contract | |
const childchainAbi = require('./artifacts/ChildChain.json').abi | |
const childchainAddress = require('./mumbai.json').Matic.Contracts.ChildChain | |
const childchain = new web3.eth.Contract(childchainAbi, childchainAddress) | |
childchain.setProvider(childRPC) | |
// child token -- to be deployed | |
const childTokenAbi = require('./artifacts/ChildERC20Proxified.json').abi | |
const childTokenBytecode = require('./artifacts/ChildERC20Proxified.json').bytecode | |
const childToken = new web3.eth.Contract(childTokenAbi) | |
childToken.setProvider(childRPC) | |
let childTokenInstance | |
let flag = false | |
const token = { | |
root: '', | |
name: '', | |
symbol: '', | |
decimals: '', | |
isNFT: '' | |
} | |
let childAddress | |
async function deployERC20AndMapOnChild () { | |
let t = await childchain.methods.tokens(token.root).call() | |
console.log ('currently mapped token for', token.root, ':', t) | |
await childToken.deploy({ | |
data: childTokenBytecode | |
}).send({ | |
from: web3.eth.accounts.wallet[0].address, | |
gas: 7000000 | |
}, (err, transactionHash) => { | |
console.log ('tx hash:', transactionHash) | |
if (err) console.log('error:', err) | |
}).then((contractInstance) => { | |
childTokenInstance = contractInstance | |
childAddress = contractInstance.options.address | |
console.log ('\n===\nchild token address:', childAddress,'\n===\n') | |
}) | |
// initialise contract | |
console.log ('initialising child token...') | |
let r = await childTokenInstance.methods.initialise(token.root, token.name, token.symbol, token.decimals).send ({ | |
from: web3.eth.accounts.wallet[0].address, | |
gas: 5000000 | |
}) | |
console.log ('initialised child:', r.transactionHash) | |
console.log("Setting child chain") | |
await childTokenInstance.methods.changeChildChain(childchain.options.address).send({ | |
from: web3.eth.accounts.wallet[0].address, | |
gas: 5000000 | |
}) | |
console.log("Token child chain address:", await childToken.methods.childChain().call()) | |
console.log("Token parent address:", await childToken.methods.parent().call()) | |
console.log("Token owner address:", await childToken.methods.owner().call()) | |
console.log('\nmapping on child chain\n...') | |
await childchain.methods.mapToken(token, childToken.address, false).send ({ | |
from: web3.eth.accounts.wallet[0].address, | |
gas: 5000000 | |
}) | |
console.log("Updated mapped token for root token (should be child token):", await childchain.methods.tokens(token).call()) | |
} | |
async function check () { | |
let _child = await registry.methods.rootToChildToken(root).call() | |
if (_child === child) { | |
console.log ('✅') | |
} else { | |
console.log ('root:', root, 'mapped to child:', _child) | |
flag = true | |
} | |
} | |
async function mapOnRoot () { | |
console.log ('sending from:',web3.eth.accounts.wallet[0].address) | |
if (flag) { | |
let r = await registry.methods.mapToken(root, child, isErc721).encodeABI() | |
let p = await governance.methods.update( | |
registry.options.address, | |
r | |
).send({ | |
from: web3.eth.accounts.wallet[0].address, | |
gas: 5000000 | |
}) | |
console.log (p) | |
} | |
} | |
async function main () { | |
web3.eth.accounts.wallet.add(pvtKey) | |
await deployERC20AndMapOnChild() | |
await check () | |
await mapOnRoot() | |
await check () | |
} | |
main () | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Web3 = require('web3') | |
const rootRPC = 'https://goerli.infura.io/v3/<API-KEY>' | |
const childRPC = 'https://rpc-mumbai.matic.today' | |
const token = { | |
root: '', | |
name: '', | |
symbol: '', | |
decimals: '', | |
owner: '', | |
isNFT: false | |
} | |
// @todo: query from contracts | |
const erc20TokenType = '0x8ae85d849167ff996c04040c44924fd364217285e4cad818292c7ac37c0a345b' | |
const web3 = new Web3 () | |
const childChainManagerProxy = '0xb5505a6d998549090530911180f38aC5130101c6' | |
const childChainAbi = require('../build/contracts/ChildChainManager.json').abi | |
const childchain = new web3.eth.Contract(childChainAbi, childChainManagerProxy) | |
const rootChainManagerProxy = '0xBbD7cBFA79faee899Eaf900F13C9065bF03B1A74' | |
const rootChainAbi = require('../build/contracts/RootChainManager.json').abi | |
const rootchain = new web3.eth.Contract(rootChainAbi, rootChainManagerProxy) | |
// add private key for the account with mapping permissions | |
const pvtKey = '' | |
web3.eth.accounts.wallet.add(pvtKey) | |
let childAddress | |
let child | |
// deploy on child | |
async function deployOnChild() { | |
web3.setProvider(childRPC) | |
let artifacts = require('../build/contracts/ChildERC20.json') | |
// get child token ABI | |
let abi = artifacts.abi | |
// get child token bytecode | |
let bytecode = artifacts.bytecode | |
let contract = new web3.eth.Contract(abi) | |
await contract.deploy({ | |
data: bytecode, | |
arguments: [token.name, token.symbol, token.decimals] | |
}).send({ | |
from: web3.eth.accounts.wallet[0].address, | |
gas: 7000000 | |
}, (err, transactionHash) => { | |
console.log ('tx hash:', transactionHash) | |
if (err) console.log('error:', err) | |
}).then((contractInstance) => { | |
child = contractInstance | |
childAddress = contractInstance.options.address | |
console.log ('\n===\nchild token address:', childAddress,'\n===\n') | |
}) | |
} | |
// check name, symbol, decimals of the mapped token | |
async function checkToken () { | |
console.log ('deployed token:') | |
let name = await child.methods.name().call() | |
let decimals = await child.methods.decimals().call() | |
let symbol = await child.methods.symbol().call() | |
const DEPOSITOR_ROLE = await child.methods.DEPOSITOR_ROLE().call() | |
console.log ('\tname:',name,'\n\tdecimals:',decimals,'\n\tsymbol:',symbol,'\n\tdepositor role:', DEPOSITOR_ROLE) | |
console.log ('---') | |
console.log ('granting depositor role to ChildChain.sol ...') | |
let r = await child.methods.grantRole(DEPOSITOR_ROLE, childChainManagerProxy).send({ | |
from: web3.eth.accounts.wallet[0].address, | |
gas: 500000 | |
}) | |
console.log ('granted depositor role. txHash:', r.transactionHash) | |
console.log ('===') | |
} | |
// map on root | |
async function mapOnRoot() { | |
web3.setProvider(rootRPC) | |
console.log ('mapping on root') | |
let map = await rootchain.methods.mapToken(token.root, childAddress, erc20TokenType).send({ | |
from: web3.eth.accounts.wallet[0].address , | |
gas: 500000 | |
}) | |
console.log ('token mapped on root:', map.transactionHash) | |
} | |
async function displayInfo () { | |
console.log ('===') | |
web3.setProvider(rootRPC) | |
console.log ('querying mapping on root chain') | |
let child = await rootchain.methods.rootToChildToken(token.root).call() | |
console.log ('root token:',token.root,';child token:', child) | |
web3.setProvider(childRPC) | |
// the following lines would give correct address only after sync has happened | |
// console.log ('querying mapping on child chain') | |
// let root = await childchain.methods.childToRootToken(childAddress).call() | |
// console.log ('root token:', root, ';child token:', childAddress) | |
console.log ('===') | |
} | |
async function map () { | |
await deployOnChild () | |
await checkToken () | |
await mapOnRoot () | |
await displayInfo () | |
} | |
map() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow ! clean code 👍