Skip to content

Instantly share code, notes, and snippets.

@imewish
Created November 25, 2019 08:01
Show Gist options
  • Save imewish/e0bf96225fcfe641c2de9d100cdc107e to your computer and use it in GitHub Desktop.
Save imewish/e0bf96225fcfe641c2de9d100cdc107e to your computer and use it in GitHub Desktop.
import Web3 from 'web3'
import { generateFirstWallets, mnemonics } from './helpers/wallets'
import RootTokenArtifacts from '../contracts/RootToken.json'
import ChildTokenArtifacts from '../contracts/ChildToken.json'
import RootNftArtifacts from '../contracts/RootNft.json'
import ChildNftArtifacts from '../contracts/ChildNft.json'
import RootManagerArtifacts from '../contracts/RootManager.json'
import ChildManagerArtifacts from '../contracts/ChildManager.json'
import chain from '../static/chain.json'
// send tokens
const web3 = new Web3('http://localhost:8545')
const web3Child = new Web3('http://localhost:8540')
const wallets = generateFirstWallets(mnemonics, 5)
const userWallet = wallets[0]
for (let i = 0; i < wallets.length; i++) {
web3.eth.accounts.wallet.add(wallets[i].getPrivateKeyString())
web3Child.eth.accounts.wallet.add(wallets[i].getPrivateKeyString())
}
const RootToken = new web3.eth.Contract(
RootTokenArtifacts.abi,
chain.rootToken
)
const ChildToken = new web3Child.eth.Contract(
ChildTokenArtifacts.abi,
chain.childToken
)
const RootNft = new web3.eth.Contract(
RootNftArtifacts.abi,
chain.rootNft
)
const ChildNft = new web3Child.eth.Contract(
ChildNftArtifacts.abi,
chain.childNft
)
const RootManager = new web3.eth.Contract(
RootManagerArtifacts.abi,
chain.rootManager
)
const ChildManager = new web3Child.eth.Contract(
ChildManagerArtifacts.abi,
chain.childManager
)
function PromiseTimeout(delayms) {
return new Promise(function(resolve, reject) {
setTimeout(resolve, delayms)
})
}
async function depositTokens() {
// mint 1 token
console.log('--- ERC20 Deposit ---')
await RootToken.methods
.mintTokens(1)
.send({
from: userWallet.getAddressString(),
gas: 500000
})
.then((res) => {
console.log('minted 1 token on root chain. tx hash - ', res.transactionHash)
})
// approve root manager
await RootToken.methods
.approve(chain.rootManager, 1)
.send({
from: userWallet.getAddressString(),
gas: 500000
})
.then((res) => {
console.log('\napproved root manager for 1 token.')
})
// get balance of acc on root, child. and balance of root manager
console.log('\nbalances before deposit:')
await RootToken.methods
.balanceOf(userWallet.getAddressString())
.call()
.then((res) => {
console.log('balance of user on root chain:', res)
})
await RootToken.methods
.balanceOf(chain.rootManager)
.call()
.then((res) => {
console.log('balance of root chain manager contract:', res)
})
await ChildToken.methods
.balanceOf(userWallet.getAddressString())
.call()
.then((res) => {
console.log('balance of user on child chain: ', res)
})
// perform deposit
await RootManager.methods
.deposit(chain.rootToken, 1, false)
.send({
from: userWallet.getAddressString(),
gas: 500000
})
.then((res) => {
console.log('\ninitiated deposit on root manager contract. tx - ', res.transactionHash)
})
// get all balances after 10 sec timeout
await PromiseTimeout(10000)
console.log('\nbalances after deposit:')
await RootToken.methods
.balanceOf(userWallet.getAddressString())
.call()
.then((res) => {
console.log('balance of user on root chain:', res)
})
await RootToken.methods
.balanceOf(chain.rootManager)
.call()
.then((res) => {
console.log('balance of root chain manager contract:', res)
})
await ChildToken.methods
.balanceOf(userWallet.getAddressString())
.call()
.then((res) => {
console.log('balance of user on child chain: ', res)
})
}
// depositTokens()
ChildManager.methods
.withdraw(chain.childToken,
userWallet.getAddressString(),
1,
false)
.send({
from: userWallet.getAddressString(),
gas: 500000
})
.then(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment