2019-08-27 17:26:49 +03:00
|
|
|
const Web3 = require('web3')
|
2021-05-08 18:50:46 +03:00
|
|
|
const { ERC20_ABI, BRIDGE_VALIDATORS_ABI, FOREIGN_ERC_TO_NATIVE_ABI, BOX_ABI } = require('../commons')
|
2019-08-27 17:26:49 +03:00
|
|
|
|
2020-09-02 17:43:48 +03:00
|
|
|
const waitUntil = async (predicate, step = 100, timeout = 60000) => {
|
2019-08-27 17:26:49 +03:00
|
|
|
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)
|
2021-05-08 18:50:46 +03:00
|
|
|
const erc20Token = new web3.eth.Contract(ERC20_ABI, tokenAddress)
|
2019-08-27 17:26:49 +03:00
|
|
|
|
|
|
|
await erc20Token.methods.transfer(recipientAddress, web3.utils.toWei('0.01')).send({
|
|
|
|
from: account.address,
|
|
|
|
gas: '1000000'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-04 22:16:43 +03:00
|
|
|
const sendAMBMessage = async (rpcUrl, account, boxAddress, bridgeAddress, boxOtherSideAddress, manualLane = false) => {
|
2019-11-13 09:51:11 +03:00
|
|
|
const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl))
|
|
|
|
web3.eth.accounts.wallet.add(account.privateKey)
|
|
|
|
const homeBox = new web3.eth.Contract(BOX_ABI, boxAddress)
|
|
|
|
|
2020-11-04 22:16:43 +03:00
|
|
|
await homeBox.methods[manualLane ? 'setValueOnOtherNetworkUsingManualLane' : 'setValueOnOtherNetwork'](
|
|
|
|
3,
|
|
|
|
bridgeAddress,
|
|
|
|
boxOtherSideAddress
|
|
|
|
).send({
|
2019-11-13 09:51:11 +03:00
|
|
|
from: account.address,
|
|
|
|
gas: '400000'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-27 17:26:49 +03:00
|
|
|
const addValidator = async (rpcUrl, account, bridgeAddress) => {
|
|
|
|
const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl))
|
|
|
|
web3.eth.accounts.wallet.add(account.privateKey)
|
2021-05-08 18:50:46 +03:00
|
|
|
const bridgeContract = new web3.eth.Contract(FOREIGN_ERC_TO_NATIVE_ABI, bridgeAddress)
|
2019-08-27 17:26:49 +03:00
|
|
|
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,
|
2019-11-13 09:51:11 +03:00
|
|
|
addValidator,
|
2021-05-08 18:50:46 +03:00
|
|
|
sendAMBMessage
|
2019-08-27 17:26:49 +03:00
|
|
|
}
|