tokenbridge/oracle-e2e/test/nativeToErc.js

229 lines
7.3 KiB
JavaScript
Raw Normal View History

const Web3 = require('web3')
const assert = require('assert')
const promiseRetry = require('promise-retry')
const {
user,
validator,
nativeToErcBridge,
secondUser,
homeRPC,
foreignRPC
} = require('../../e2e-commons/constants.json')
2019-07-12 11:53:16 +03:00
const { ERC677_BRIDGE_TOKEN_ABI } = require('../../commons')
2019-06-04 11:28:11 +03:00
const { generateNewBlock } = require('../../e2e-commons/utils')
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))
const { toBN } = foreignWeb3.utils
const HOME_BRIDGE_ADDRESS = nativeToErcBridge.home
const FOREIGN_BRIDGE_ADDRESS = nativeToErcBridge.foreign
homeWeb3.eth.accounts.wallet.add(user.privateKey)
homeWeb3.eth.accounts.wallet.add(validator.privateKey)
homeWeb3.eth.accounts.wallet.add(secondUser.privateKey)
foreignWeb3.eth.accounts.wallet.add(user.privateKey)
foreignWeb3.eth.accounts.wallet.add(validator.privateKey)
foreignWeb3.eth.accounts.wallet.add(secondUser.privateKey)
2019-07-12 11:53:16 +03:00
const token = new foreignWeb3.eth.Contract(ERC677_BRIDGE_TOKEN_ABI, nativeToErcBridge.foreignToken)
const sleep = timeout => new Promise(res => setTimeout(res, timeout))
describe('native to erc', () => {
it('should convert eth in home to tokens in foreign', async () => {
// check that account has zero tokens in the foreign chain
const balance = await token.methods.balanceOf(user.address).call()
assert(toBN(balance).isZero(), 'Account should not have tokens yet')
// send transaction to home chain
const depositTx = await homeWeb3.eth.sendTransaction({
from: user.address,
to: HOME_BRIDGE_ADDRESS,
gasPrice: '1',
gas: '50000',
value: '1000000000000000000'
})
// 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 account has tokens in the foreign chain
await promiseRetry(async retry => {
const balance = await token.methods.balanceOf(user.address).call()
if (toBN(balance).isZero()) {
retry()
}
})
})
it('should convert tokens in foreign to eth in home', async () => {
const originalBalance = await homeWeb3.eth.getBalance(user.address)
// send tokens to foreign bridge
await token.methods
.transferAndCall(FOREIGN_BRIDGE_ADDRESS, homeWeb3.utils.toWei('0.01'), '0x')
.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(originalBalance))) {
retry()
}
})
})
it('should wait for funds if these are insufficient (Home)', async () => {
// check that account has zero tokens in the foreign chain
const originalBalance = toBN(await token.methods.balanceOf(user.address).call())
// empty validator funds
await sendAllBalance(homeWeb3, validator.address, secondUser.address)
// send transaction to home chain
const depositTx = await homeWeb3.eth.sendTransaction({
from: user.address,
to: HOME_BRIDGE_ADDRESS,
gasPrice: '1',
gas: '50000',
value: '1000000000000000000'
})
// Send a Tx to generate a new block
await generateNewBlock(homeWeb3, user.address)
// wait two seconds, no new blocks should have been generated
await sleep(2000)
const lastBlockNumber = await homeWeb3.eth.getBlockNumber()
const balance = toBN(await token.methods.balanceOf(user.address).call())
assert(lastBlockNumber === depositTx.blockNumber + 1, "Shouldn't have emitted a new block")
assert(originalBalance.eq(balance), "Token balance shouldn't have changed")
// send funds back to validator
const sendBalanceBackTx = await sendAllBalance(homeWeb3, secondUser.address, validator.address)
// expect Deposit event to be processed
await promiseRetry(
async retry => {
const lastBlockNumber = await homeWeb3.eth.getBlockNumber()
// check that a new block was created since the last transaction
if (lastBlockNumber === sendBalanceBackTx.blockNumber + 1) {
await generateNewBlock(homeWeb3, user.address)
} else {
retry()
}
},
{
forever: true,
factor: 1,
minTimeout: 500
}
)
// check that token balance was incremented in foreign chain
await promiseRetry(async retry => {
const balance = toBN(await token.methods.balanceOf(user.address).call())
if (!balance.gt(originalBalance)) {
retry()
}
})
})
it('should wait for funds if these are insufficient (Foreign)', async () => {
// get original tokens balance
const originalBalance = toBN(await token.methods.balanceOf(user.address).call())
// empty foreign validator funds
await sendAllBalance(foreignWeb3, validator.address, secondUser.address)
const foreignBlockNumber = await foreignWeb3.eth.getBlockNumber()
// send transaction to home chain
await homeWeb3.eth.sendTransaction({
from: user.address,
to: HOME_BRIDGE_ADDRESS,
gasPrice: '1',
gas: '50000',
value: '1000000000000000000'
})
// Send a Tx to generate a new block
const lastHomeTx = await generateNewBlock(homeWeb3, user.address)
// wait for the deposit to be processed
await promiseRetry(
async retry => {
const lastBlockNumber = await homeWeb3.eth.getBlockNumber()
if (lastBlockNumber === lastHomeTx.blockNumber + 1) {
await generateNewBlock(homeWeb3, user.address)
} else {
retry()
}
},
{
forever: true,
factor: 1,
minTimeout: 500
}
)
// tokens shouldn't be generated in the foreign chain because the validator doesn't have funds
await sleep(2000)
const lastForeignBlockNumber = await foreignWeb3.eth.getBlockNumber()
assert(lastForeignBlockNumber === foreignBlockNumber, "Shouldn't have emitted a new block")
// send funds back to validator
await sendAllBalance(foreignWeb3, secondUser.address, validator.address)
// check that account has tokens in the foreign chain
await promiseRetry(async retry => {
const balance = toBN(await token.methods.balanceOf(user.address).call())
if (balance.eq(originalBalance)) {
retry()
}
})
})
})
async function sendAllBalance(web3, from, to) {
const balance = await web3.eth.getBalance(from)
const value = toBN(balance).sub(toBN('21000'))
return web3.eth.sendTransaction({
from,
to,
value,
gas: '21000',
gasPrice: '1'
})
}