tokenbridge/monitor-e2e/utils.js
Przemyslaw Rzad dd9add50a0
Monitor E2E rewrite (#187)
* Started monitor-e2e rewrite

* axios

* Implemented tests

* Monitor start

* First deploy

* Wait for monitor

* Removed redundant files

* Tests.

* TODO

* Links to minitor in constants

* Typo.

* [PR-into-PR] Monitor E2E rewrite - balance (#191)

* Test run for more monitor-e2e tests

* macos/docker

* timeout

* Little refactor

* Trying to test balances in other types of bridges.

* Utils.

* test

* check all

* erc to erc try

* Final tests

* typo

* All jobs

* Lint

* Roll back docker in docker

* WaitUntil

* Axios version

* New validator checks (#192)
2019-08-27 16:26:49 +02:00

58 lines
1.9 KiB
JavaScript

const Web3 = require('web3')
const { ERC677_BRIDGE_TOKEN_ABI, BRIDGE_VALIDATORS_ABI, FOREIGN_NATIVE_TO_ERC_ABI } = require('../commons')
const waitUntil = async (predicate, step = 100, timeout = 10000) => {
const stopTime = Date.now() + timeout
while (Date.now() <= stopTime) {
const result = await predicate()
if (result) {
return result
}
await new Promise(resolve => setTimeout(resolve, step)) // sleep
}
throw new Error(`waitUntil timed out after ${timeout} ms`)
}
const sendEther = async (rpcUrl, account, to) => {
const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl))
web3.eth.accounts.wallet.add(account.privateKey)
await web3.eth.sendTransaction({
from: account.address,
to,
gasPrice: '1',
gas: '50000',
value: '1000000000000000000'
})
}
const sendTokens = async (rpcUrl, account, tokenAddress, recipientAddress) => {
const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl))
web3.eth.accounts.wallet.add(account.privateKey)
const erc20Token = new web3.eth.Contract(ERC677_BRIDGE_TOKEN_ABI, tokenAddress)
await erc20Token.methods.transfer(recipientAddress, web3.utils.toWei('0.01')).send({
from: account.address,
gas: '1000000'
})
}
const addValidator = async (rpcUrl, account, bridgeAddress) => {
const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl))
web3.eth.accounts.wallet.add(account.privateKey)
const bridgeContract = new web3.eth.Contract(FOREIGN_NATIVE_TO_ERC_ABI, bridgeAddress)
const foreignValidatorsAddress = await bridgeContract.methods.validatorContract().call()
const foreignBridgeValidators = new web3.eth.Contract(BRIDGE_VALIDATORS_ABI, foreignValidatorsAddress)
await foreignBridgeValidators.methods.addValidator('0xE71FBa5db00172bb0C93d649362B006300000935').send({
from: account.address,
gas: '5000000'
})
}
module.exports = {
waitUntil,
sendEther,
sendTokens,
addValidator
}