tokenbridge/oracle-e2e/test/ercToNative.js

104 lines
3.4 KiB
JavaScript
Raw Normal View History

const path = require('path')
const Web3 = require('web3')
const assert = require('assert')
const promiseRetry = require('promise-retry')
2019-06-04 15:02:45 +03:00
const { user, ercToNativeBridge, homeRPC, foreignRPC } = require('../../e2e-commons/constants.json')
2019-06-04 15:37:21 +03:00
const { abiPath } = require('../config.json')
2019-06-04 11:28:11 +03:00
const { generateNewBlock } = require('../../e2e-commons/utils')
2019-06-04 15:37:21 +03:00
const abisDir = path.join(__dirname, '..', abiPath)
2019-06-04 15:02:45 +03:00
const homeWeb3 = new Web3(new Web3.providers.HttpProvider(homeRPC.URL))
const foreignWeb3 = new Web3(new Web3.providers.HttpProvider(foreignRPC.URL))
2019-06-04 14:43:53 +03:00
const HOME_BRIDGE_ADDRESS = ercToNativeBridge.home
const FOREIGN_BRIDGE_ADDRESS = ercToNativeBridge.foreign
const { toBN } = foreignWeb3.utils
homeWeb3.eth.accounts.wallet.add(user.privateKey)
foreignWeb3.eth.accounts.wallet.add(user.privateKey)
const tokenAbi = require(path.join(abisDir, 'ERC677BridgeToken.json')).abi
const erc20Token = new foreignWeb3.eth.Contract(
tokenAbi,
2019-06-04 14:43:53 +03:00
ercToNativeBridge.foreignToken
)
describe('erc to native', () => {
it('should convert tokens in foreign to coins in home', async () => {
const balance = await erc20Token.methods.balanceOf(user.address).call()
const originalBalanceOnHome = await homeWeb3.eth.getBalance(user.address)
assert(!toBN(balance).isZero(), 'Account should have tokens')
// send tokens to foreign bridge
await erc20Token.methods
.transfer(FOREIGN_BRIDGE_ADDRESS, homeWeb3.utils.toWei('0.01'))
.send({
from: user.address,
gas: '1000000'
})
.catch(e => {
console.error(e)
})
// Send a trivial transaction to generate a new block since the watcher
// is configured to wait 1 confirmation block
await generateNewBlock(foreignWeb3, user.address)
// check that balance increases
await promiseRetry(async retry => {
const balance = await homeWeb3.eth.getBalance(user.address)
if (toBN(balance).lte(toBN(originalBalanceOnHome))) {
retry()
}
})
})
it('should convert coins in home to tokens in foreign', async () => {
const originalBalance = await erc20Token.methods.balanceOf(user.address).call()
// check that account has tokens in home chain
const balance = await homeWeb3.eth.getBalance(user.address)
assert(!toBN(balance).isZero(), 'Account should have tokens')
// send transaction to home bridge
const depositTx = await homeWeb3.eth.sendTransaction({
from: user.address,
to: HOME_BRIDGE_ADDRESS,
gasPrice: '1',
gas: '1000000',
value: homeWeb3.utils.toWei('0.01')
})
// Send a trivial transaction to generate a new block since the watcher
// is configured to wait 1 confirmation block
await generateNewBlock(homeWeb3, user.address)
// The bridge should create a new transaction with a CollectedSignatures
// event so we generate another trivial transaction
await promiseRetry(
async retry => {
const lastBlockNumber = await homeWeb3.eth.getBlockNumber()
if (lastBlockNumber >= depositTx.blockNumber + 2) {
await generateNewBlock(homeWeb3, user.address)
} else {
retry()
}
},
{
forever: true,
factor: 1,
minTimeout: 500
}
)
// check that balance increases
await promiseRetry(async retry => {
const balance = await erc20Token.methods.balanceOf(user.address).call()
if (toBN(balance).lte(toBN(originalBalance))) {
retry()
}
})
})
})