From 85104d67c05d4cb1138900817a24e4017229c93c Mon Sep 17 00:00:00 2001 From: Kirill Fedoseev Date: Tue, 18 Feb 2020 19:17:01 +0300 Subject: [PATCH] Support of Chai token by monitor in erc-to-native (#287) --- monitor-e2e/test/ercToNative.js | 95 +++++++++++++++++++++++++++++++-- monitor-e2e/utils.js | 58 +++++++++++++++++++- monitor/getBalances.js | 26 +++++++++ parity/chain-foreign.json | 3 +- 4 files changed, 176 insertions(+), 6 deletions(-) diff --git a/monitor-e2e/test/ercToNative.js b/monitor-e2e/test/ercToNative.js index b7c2b568..b90ecf18 100644 --- a/monitor-e2e/test/ercToNative.js +++ b/monitor-e2e/test/ercToNative.js @@ -1,7 +1,15 @@ const assert = require('assert') const axios = require('axios') const { ercToNativeBridge, user, foreignRPC, validator } = require('../../e2e-commons/constants.json') -const { waitUntil, sendTokens, addValidator } = require('../utils') +const { + waitUntil, + sendTokens, + addValidator, + initializeChaiToken, + convertDaiToChai, + setMinDaiTokenBalance, + migrateToMCD +} = require('../utils') const baseUrl = ercToNativeBridge.monitor @@ -24,12 +32,45 @@ describe('ERC TO NATIVE with changing state of contracts', () => { assert((await axios.get(`${baseUrl}/validators`)).data.validatorsMatch === true) }) - it('should change balanceDiff', async () => { + it('should change balanceDiff', async function() { + this.timeout(60000) await sendTokens(foreignRPC.URL, user, ercToNativeBridge.halfDuplexToken, ercToNativeBridge.foreign) await waitUntil(async () => { ;({ data } = await axios.get(`${baseUrl}`)) - return data.balanceDiff !== 0 + const { erc20Balance, halfDuplexErc20Balance, investedErc20Balance } = data.foreign + return ( + data.balanceDiff === 0.01 && + erc20Balance === '0.01' && + halfDuplexErc20Balance === undefined && + investedErc20Balance === undefined + ) + }) + + await migrateToMCD(foreignRPC.URL, ercToNativeBridge.foreign) + + await waitUntil(async () => { + ;({ data } = await axios.get(`${baseUrl}`)) + const { erc20Balance, halfDuplexErc20Balance, investedErc20Balance } = data.foreign + return ( + data.balanceDiff === 0.01 && + erc20Balance === '0.01' && + halfDuplexErc20Balance === '0' && + investedErc20Balance === undefined + ) + }) + + await sendTokens(foreignRPC.URL, user, ercToNativeBridge.halfDuplexToken, ercToNativeBridge.foreign) + + await waitUntil(async () => { + ;({ data } = await axios.get(`${baseUrl}`)) + const { erc20Balance, halfDuplexErc20Balance, investedErc20Balance } = data.foreign + return ( + data.balanceDiff === 0.02 && + erc20Balance === '0.01' && + halfDuplexErc20Balance === '0.01' && + investedErc20Balance === undefined + ) }) }) @@ -40,4 +81,52 @@ describe('ERC TO NATIVE with changing state of contracts', () => { return data.validatorsMatch === false }) }) + + it('should consider chai token balance', async function() { + this.timeout(60000) + await initializeChaiToken(foreignRPC.URL, ercToNativeBridge.foreign) + await sendTokens(foreignRPC.URL, user, ercToNativeBridge.foreignToken, ercToNativeBridge.foreign) + + await waitUntil(async () => { + ;({ data } = await axios.get(`${baseUrl}`)) + const { erc20Balance, halfDuplexErc20Balance, investedErc20Balance, accumulatedInterest } = data.foreign + return ( + data.balanceDiff === 0.03 && + erc20Balance === '0.02' && + halfDuplexErc20Balance === '0.01' && + investedErc20Balance === '0' && + accumulatedInterest === '0.001' // value of dsrBalance() is initially defined in genesis block as 0.001 + ) + }) + + await setMinDaiTokenBalance(foreignRPC.URL, ercToNativeBridge.foreign, '0.01') + await convertDaiToChai(foreignRPC.URL, ercToNativeBridge.foreign) + + await waitUntil(async () => { + ;({ data } = await axios.get(`${baseUrl}`)) + const { erc20Balance, halfDuplexErc20Balance, investedErc20Balance, accumulatedInterest } = data.foreign + return ( + data.balanceDiff === 0.03 && + erc20Balance === '0.01' && + halfDuplexErc20Balance === '0.01' && + investedErc20Balance === '0.01' && + accumulatedInterest === '0.001' + ) + }) + + await setMinDaiTokenBalance(foreignRPC.URL, ercToNativeBridge.foreign, '0.005') + await convertDaiToChai(foreignRPC.URL, ercToNativeBridge.foreign) + + await waitUntil(async () => { + ;({ data } = await axios.get(`${baseUrl}`)) + const { erc20Balance, halfDuplexErc20Balance, investedErc20Balance, accumulatedInterest } = data.foreign + return ( + data.balanceDiff === 0.03 && + erc20Balance === '0.005' && + halfDuplexErc20Balance === '0.01' && + investedErc20Balance === '0.015' && + accumulatedInterest === '0.001' + ) + }) + }) }) diff --git a/monitor-e2e/utils.js b/monitor-e2e/utils.js index 14361c6d..f452ffb5 100644 --- a/monitor-e2e/utils.js +++ b/monitor-e2e/utils.js @@ -1,5 +1,12 @@ const Web3 = require('web3') -const { ERC677_BRIDGE_TOKEN_ABI, BRIDGE_VALIDATORS_ABI, FOREIGN_NATIVE_TO_ERC_ABI, BOX_ABI } = require('../commons') +const { + ERC677_BRIDGE_TOKEN_ABI, + BRIDGE_VALIDATORS_ABI, + FOREIGN_NATIVE_TO_ERC_ABI, + FOREIGN_ERC_TO_NATIVE_ABI, + BOX_ABI +} = require('../commons') +const { validator } = require('../e2e-commons/constants') const waitUntil = async (predicate, step = 100, timeout = 20000) => { const stopTime = Date.now() + timeout @@ -60,10 +67,57 @@ const addValidator = async (rpcUrl, account, bridgeAddress) => { }) } +const migrateToMCD = async (rpcUrl, bridgeAddress) => { + const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl)) + web3.eth.accounts.wallet.add(validator.privateKey) + const bridgeContract = new web3.eth.Contract(FOREIGN_ERC_TO_NATIVE_ABI, bridgeAddress) + await bridgeContract.methods.migrateToMCD().send({ + from: validator.address, + gas: '4000000' + }) +} + +const initializeChaiToken = async (rpcUrl, bridgeAddress) => { + const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl)) + web3.eth.accounts.wallet.add(validator.privateKey) + const bridgeContract = new web3.eth.Contract(FOREIGN_ERC_TO_NATIVE_ABI, bridgeAddress) + + await bridgeContract.methods.initializeChaiToken().send({ + from: validator.address, + gas: '1000000' + }) +} + +const setMinDaiTokenBalance = async (rpcUrl, bridgeAddress, limit) => { + const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl)) + web3.eth.accounts.wallet.add(validator.privateKey) + const bridgeContract = new web3.eth.Contract(FOREIGN_ERC_TO_NATIVE_ABI, bridgeAddress) + + await bridgeContract.methods.setMinDaiTokenBalance(web3.utils.toWei(limit)).send({ + from: validator.address, + gas: '1000000' + }) +} + +const convertDaiToChai = async (rpcUrl, bridgeAddress) => { + const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl)) + web3.eth.accounts.wallet.add(validator.privateKey) + const bridgeContract = new web3.eth.Contract(FOREIGN_ERC_TO_NATIVE_ABI, bridgeAddress) + + await bridgeContract.methods.convertDaiToChai().send({ + from: validator.address, + gas: '1000000' + }) +} + module.exports = { waitUntil, sendEther, sendTokens, addValidator, - sendAMBMessage + sendAMBMessage, + migrateToMCD, + initializeChaiToken, + setMinDaiTokenBalance, + convertDaiToChai } diff --git a/monitor/getBalances.js b/monitor/getBalances.js index 289787fe..795ba251 100644 --- a/monitor/getBalances.js +++ b/monitor/getBalances.js @@ -83,7 +83,10 @@ async function main(bridgeMode) { const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ERC_TO_NATIVE_ABI, COMMON_FOREIGN_BRIDGE_ADDRESS) const erc20Address = await foreignBridge.methods.erc20token().call() const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address) + let investedAmountInDai = 0 + let bridgeDsrBalance = 0 let foreignHalfDuplexErc20Balance = 0 + let displayChaiToken = false let displayHalfDuplexToken = false let tokenSwapAllowed = false try { @@ -103,6 +106,21 @@ async function main(bridgeMode) { logger.debug('Methods for half duplex token are not present') } + try { + logger.debug('calling foreignBridge.methods.isChaiTokenEnabled') + if (await foreignBridge.methods.isChaiTokenEnabled().call()) { + displayChaiToken = true + logger.debug('calling foreignBridge.methods.investedAmountInDai') + investedAmountInDai = await foreignBridge.methods.investedAmountInDai().call() + logger.debug('calling foreignBridge.methods.dsrBalance') + bridgeDsrBalance = await foreignBridge.methods.dsrBalance().call() + } else { + logger.debug('Chai token is currently disabled') + } + } catch (e) { + logger.debug('Methods for chai token are not present') + } + logger.debug('calling erc20Contract.methods.balanceOf') const foreignErc20Balance = await erc20Contract.methods.balanceOf(COMMON_FOREIGN_BRIDGE_ADDRESS).call() @@ -119,11 +137,14 @@ async function main(bridgeMode) { const burntCoinsBN = new BN(burntCoins) const totalSupplyBN = mintedCoinsBN.minus(burntCoinsBN) const foreignErc20BalanceBN = new BN(foreignErc20Balance) + const investedAmountInDaiBN = new BN(investedAmountInDai) + const bridgeDsrBalanceBN = new BN(bridgeDsrBalance) const halfDuplexErc20BalanceBN = displayHalfDuplexToken && tokenSwapAllowed ? new BN(foreignHalfDuplexErc20Balance) : new BN(0) const diff = foreignErc20BalanceBN .plus(halfDuplexErc20BalanceBN) + .plus(investedAmountInDaiBN) .minus(totalSupplyBN) .toFixed() @@ -143,6 +164,11 @@ async function main(bridgeMode) { } } + if (displayChaiToken) { + foreign.investedErc20Balance = Web3Utils.fromWei(investedAmountInDai) + foreign.accumulatedInterest = Web3Utils.fromWei(bridgeDsrBalanceBN.minus(investedAmountInDaiBN).toString(10)) + } + logger.debug('Done') return { home: { diff --git a/parity/chain-foreign.json b/parity/chain-foreign.json index d6c80e40..10473494 100644 --- a/parity/chain-foreign.json +++ b/parity/chain-foreign.json @@ -141,7 +141,8 @@ "balance": "0", "code": "0x6080604052600436106100565763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416633b4da69f811461005b5780636c25b3461461008e578063be22f546146100ce575b600080fd5b34801561006757600080fd5b5061008c73ffffffffffffffffffffffffffffffffffffffff6004351660243561010c565b005b34801561009a57600080fd5b506100bc73ffffffffffffffffffffffffffffffffffffffff600435166101c5565b60408051918252519081900360200190f35b3480156100da57600080fd5b506100e36101cc565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b60008054604080517f23b872dd00000000000000000000000000000000000000000000000000000000815233600482015230602482015260448101859052905173ffffffffffffffffffffffffffffffffffffffff909216926323b872dd926064808401936020939083900390910190829087803b15801561018d57600080fd5b505af11580156101a1573d6000803e3d6000fd5b505050506040513d60208110156101b757600080fd5b505060018054909101905550565b5060015490565b60005473ffffffffffffffffffffffffffffffffffffffff16815600a165627a7a72305820d01d11b7ea4dad7896e3fbb5d06966bf715276ebd69a35990fa39c3158ca489d0029", "storage": { - "0x0": "0x7cC4B1851c35959D34e635A470F6b5C43bA3C9c9" + "0x0": "0x7cC4B1851c35959D34e635A470F6b5C43bA3C9c9", + "0x1": "0x38d7ea4c68000" } } }