add example for erc20
MODIFY
examples/config.js
examples/config.js
// Testnet V3 config
module.exports = {
- PLASMA: {
+ plasma: {
+ parent: {
+ erc20: '0x3f152B63Ec5CA5831061B2DccFb29a874C317502',
+ },
+ child: {
+ erc721: '0x0f3Fb1354337965BAd0775a75c692D167fbb39F9',
+ erc20: '0x2d7882beDcbfDDce29Ba99965dd3cdF7fcB10A1e',
+ },
REGISTRY_ADDRESS: '0x56B082d0a590A7ce5d170402D6f7f88B58F71988',
ROOTCHAIN_ADDRESS: '0x82a72315E16cE224f28E1F1fB97856d3bF83f010', // The address for the main Plasma contract in Ropsten testnet
WITHDRAWMANAGER_ADDRESS: '0x3cf9aD3395028a42EAfc949e2EC4588396b8A7D4', // An address for the WithdrawManager contract on Ropsten testnet
@@ -11,8 +18,6 @@ module.exports = {
},
child: {
rpc: 'https://rpc-mumbai.matic.today', // This is the MATIC testnet RPC
- erc721: '0x0f3Fb1354337965BAd0775a75c692D167fbb39F9',
- erc20: '0x2d7882beDcbfDDce29Ba99965dd3cdF7fcB10A1e',
},
SYNCER_URL: 'https://testnetv3-syncer.api.matic.network/api/v1', // Backend service which syncs the Matic sidechain state to a MySQL database which we use for faster querying. This comes in handy especially for constructing withdrawal proofs while exiting assets from Plasma.
WATCHER_URL: 'https://testnetv3-watcher.api.matic.network/api/v1', // Backend service which syncs the Matic Plasma contract events on Ethereum mainchain to a MySQL database which we use for faster querying. This comes in handy especially for listening to asset deposits on the Plasma contract.
@@ -29,4 +34,7 @@ module.exports = {
privateKey: '<paste your private key here>', // A sample private key prefix with `0x`
address: '<paste address belonging to private key here>', // Your address
},
+ user2: {
+ address: '<paste address here>', // Your address
+ },
}

ADD
examples/plasma/deposit_status_from_txhash.js
+const { getMaticPlasmaClient } = require('../utils')
+
+const txHash = '0x8cff1ef2b198273fe7f9fb41918cd7e3e8d04c58650614af39b3086e1de1a7e6'
+
+const execute = async () => {
+ const { matic } = await getMaticPlasmaClient()
+ await matic.depositStatusFromTxHash(txHash).then(res => {
+ console.log(res) // eslint-disable-line
+ })
+}
+
+execute().then(_ => {
+ process.exit(0)
+})

ADD
examples/plasma/erc20_approve.js
+const { getMaticPlasmaClient, getAccount, plasma, from } = require('../utils')
+
+const amount = '1000000000000000' // amount in wei
+const erc20TokenOnParent = plasma.parent.erc20
+async function execute() {
+ const { matic } = await getMaticPlasmaClient()
+ const result = await matic.approveERC20TokensForDeposit(erc20TokenOnParent, amount, { from })
+ console.log(result)
+}
+
+async function executeRaw() {
+ // eslint-disable-line
+ const { matic } = await getMaticPlasmaClient()
+ const { from } = getAccount()
+ const txParams = await matic.approveERC20TokensForDeposit(erc20TokenOnParent, amount, { from, encodeAbi: true })
+ let serializedTx = utils.buildRawTransaction(txParams, user1.privateKey)
+ return matic.web3Client.parentWeb3.eth.sendSignedTransaction(serializedTx)
+}
+
+execute().then(_ => {
+ process.exit(0)
+})

MODIFY
examples/plasma/erc20_balance.js
examples/plasma/erc20_balance.js
-const { child } = require('../config')
-const { getMaticPlasmaClient, getAccount } = require('../utils')
+const { getMaticPlasmaClient, from, plasma } = require('../utils')
+
const execute = async () => {
const { matic } = await getMaticPlasmaClient()
- const { from } = getAccount()
- const balance = await matic.balanceOfERC20(from, child.erc20, {})
+ const balance = await matic.balanceOfERC20(from, plasma.child.erc20, {})
console.log('balance', balance)
}

ADD
examples/plasma/erc20_deposit.js
+const { getMaticPlasmaClient, plasma, from } = require('../utils')
+
+const amount = '1000000000000000' // amount in wei
+const token = plasma.parent.erc20
+async function execute() {
+ const { matic } = await getMaticPlasmaClient()
+ // get approved first
+ await matic.approveERC20TokensForDeposit(token, amount, { from, gasPrice: '10000000000' })
+ // then deposit
+ return matic.depositERC20ForUser(token, from, amount, { from, gasPrice: '10000000000' })
+}
+
+async function executeRaw() {
+ // eslint-disable-line
+ const { matic } = await getMaticPlasmaClient()
+ const { from } = getAccount()
+
+ // get approved first
+ let txParams = await matic.approveERC20TokensForDeposit(token, amount, { from, encodeAbi: true })
+ let serializedTx = utils.buildRawTransaction(txParams, user1.privateKey)
+ await matic.web3Client.parentWeb3.eth.sendSignedTransaction(serializedTx)
+
+ txParams = await matic.depositERC20ForUser(token, from, amount, { from, encodeAbi: true })
+ serializedTx = utils.buildRawTransaction(txParams, user1.privateKey)
+ return matic.web3Client.parentWeb3.eth.sendSignedTransaction(serializedTx)
+}
+
+execute()
+ .then(console.log)
+ .then(_ => {
+ process.exit(0)
+ })

ADD
examples/plasma/erc20_start_withdraw.js
+const { getMaticPlasmaClient, from, plasma, to } = require('../utils')
+
+const amount = '1000000000000000' // amount in wei
+const token = plasma.child.erc20
+async function execute() {
+ const { matic } = await getMaticPlasmaClient()
+
+ await matic
+ .startWithdraw(token, amount, {
+ from,
+ })
+ .then(res => {
+ console.log(res) // eslint-disable-line
+ })
+}
+
+execute().then(_ => {
+ process.exit(0)
+})

ADD
examples/plasma/erc20_transfer.js
+const { getMaticPlasmaClient, from, plasma, to } = require('../utils')
+
+const amount = '1000000000000000' // amount in wei
+const token = plasma.child.erc20
+const recipient = to
+async function execute() {
+ const { matic } = await getMaticPlasmaClient()
+
+ await matic
+ .transferERC20Tokens(token, recipient, amount, {
+ from,
+ // parent: true, // For token transfer on Main network (false for Matic Network)
+ })
+ .then(result => {
+ console.log('hash', result) // eslint-disable-line
+ })
+}
+
+execute().then(_ => {
+ process.exit(0)
+})

ADD
examples/plasma/erc20_withdraw.js
+const { getMaticPlasmaClient, from } = require('../utils')
+
+var transactionHash = '0xbbaf7712bc426b665650992215f9c15ac9bda72bbb4e7d453ba9ed3875e0ebf5' // Insert txHash generated from initiate-withdraw.js
+
+async function execute() {
+ const { matic } = await getMaticPlasmaClient()
+
+ await matic
+ .withdraw(transactionHash, {
+ from,
+ })
+ .then(result => {
+ console.log(result)
+ })
+}
+
+execute().then(_ => {
+ process.exit(0)
+})

ADD
examples/plasma/process_exit.js
+const { plasma } = require('../config')
+const { getMaticPlasmaClient, from } = require('../utils')
+
+const rootTokenAddress = plasma.parent.erc20
+
+const execute = async () => {
+ const { matic } = await getMaticPlasmaClient()
+ await matic
+ .processExits(rootTokenAddress, {
+ from,
+ })
+ .then(res => {
+ console.log(res) // eslint-disable-line
+ })
+}
+
+execute().then(_ => {
+ process.exit(0)
+})

MODIFY
examples/utils.js
examples/utils.js
@@ -6,11 +6,10 @@ const MaticPlasmaClient = require('@maticnetwork/maticjs').default
const SCALING_FACTOR = new bn(10).pow(new bn(18))
const privateKey = config.user1.privateKey
-const fromAddress = config.user1.address
async function getMaticPlasmaClient(network = 'testnet', version = 'mumbai') {
const networkInstance = new Network(network, version)
- const { from } = getAccount()
+ const from = config.user1.address
const matic = new MaticPlasmaClient({
network: network,
version: version,
@@ -23,16 +22,12 @@ async function getMaticPlasmaClient(network = 'testnet', version = 'mumbai') {
return { matic, network: networkInstance }
}
-function getAccount() {
- if (!privateKey || !fromAddress) {
- throw new Error('Please set the PRIVATE_KEY/FROM in config.js')
- }
- return { privateKey: privateKey, from: fromAddress }
-}
-
module.exports = {
SCALING_FACTOR,
getMaticPlasmaClient: getMaticPlasmaClient,
- getAccount,
child: config.child,
+ plasma: config.plasma,
+ from: config.user1.address,
+ privateKey: config.user1.privateKey,
+ to: config.user2.address,
}

add example for erc20