diff --git a/commons/test/getTokenType.js b/commons/test/getTokenType.js new file mode 100644 index 00000000..03e8e228 --- /dev/null +++ b/commons/test/getTokenType.js @@ -0,0 +1,64 @@ +const { expect } = require('chai') +const { getTokenType, ERC_TYPES } = require('..') + +describe('getTokenType', () => { + it('should return ERC677 if bridgeContract is equal to bridgeAddress', async () => { + // Given + const bridgeAddress = '0xCecBE80Ed3548dE11D7d2D922a36576eA40C4c26' + const contract = { + methods: { + bridgeContract: () => { + return { + call: () => Promise.resolve(bridgeAddress) + } + } + } + } + + // When + const type = await getTokenType(contract, bridgeAddress) + + // Then + expect(type).to.equal(ERC_TYPES.ERC677) + }) + + it('should return ERC20 if bridgeContract is not equal to bridgeAddress', async () => { + // Given + const bridgeAddress = '0xCecBE80Ed3548dE11D7d2D922a36576eA40C4c26' + const contract = { + methods: { + bridgeContract: () => { + return { + call: () => Promise.resolve('0xBFCb120F7B1de491262CA4D9D8Eba70438b6896E') + } + } + } + } + + // When + const type = await getTokenType(contract, bridgeAddress) + + // Then + expect(type).to.equal(ERC_TYPES.ERC20) + }) + + it('should return ERC20 if bridgeContract is not present', async () => { + // Given + const bridgeAddress = '0xCecBE80Ed3548dE11D7d2D922a36576eA40C4c26' + const contract = { + methods: { + bridgeContract: () => { + return { + call: () => Promise.reject() + } + } + } + } + + // When + const type = await getTokenType(contract, bridgeAddress) + + // Then + expect(type).to.equal(ERC_TYPES.ERC20) + }) +}) diff --git a/commons/utils.js b/commons/utils.js index b8e8661d..e1a29fde 100644 --- a/commons/utils.js +++ b/commons/utils.js @@ -1,4 +1,4 @@ -const { BRIDGE_MODES, FEE_MANAGER_MODE } = require('./constants') +const { BRIDGE_MODES, FEE_MANAGER_MODE, ERC_TYPES } = require('./constants') function decodeBridgeMode(bridgeModeHash) { switch (bridgeModeHash) { @@ -33,6 +33,19 @@ async function getBridgeMode(contract) { } } +const getTokenType = async (bridgeTokenContract, bridgeAddress) => { + try { + const resultBridgeAddress = await bridgeTokenContract.methods.bridgeContract().call() + if (resultBridgeAddress === bridgeAddress) { + return ERC_TYPES.ERC677 + } else { + return ERC_TYPES.ERC20 + } + } catch (e) { + return ERC_TYPES.ERC20 + } +} + const getUnit = bridgeMode => { let unitHome = null let unitForeign = null @@ -56,5 +69,6 @@ module.exports = { decodeBridgeMode, decodeFeeManagerMode, getBridgeMode, + getTokenType, getUnit } diff --git a/monitor/utils/ercUtils.js b/monitor/utils/ercUtils.js deleted file mode 100644 index 83664d38..00000000 --- a/monitor/utils/ercUtils.js +++ /dev/null @@ -1,18 +0,0 @@ -const { ERC_TYPES } = require('../../commons') - -const getTokenType = async (contract, bridgeAddress) => { - try { - const bridgeContract = await contract.methods.bridgeContract().call() - if (bridgeContract === bridgeAddress) { - return ERC_TYPES.ERC677 - } else { - return ERC_TYPES.ERC20 - } - } catch (e) { - return ERC_TYPES.ERC20 - } -} - -module.exports = { - getTokenType -} diff --git a/monitor/utils/events.js b/monitor/utils/events.js index 2a4d981a..b562fc89 100644 --- a/monitor/utils/events.js +++ b/monitor/utils/events.js @@ -8,9 +8,10 @@ const { getBridgeABIs, getBridgeMode, HOME_ERC_TO_ERC_ABI, - ERC20_ABI + ERC20_ABI, + ERC677_BRIDGE_TOKEN_ABI, + getTokenType } = require('../../commons') -const { getTokenType } = require('./ercUtils') const { HOME_RPC_URL, FOREIGN_RPC_URL, HOME_BRIDGE_ADDRESS, FOREIGN_BRIDGE_ADDRESS } = process.env const HOME_DEPLOYMENT_BLOCK = toBN(Number(process.env.HOME_DEPLOYMENT_BLOCK) || 0) @@ -30,12 +31,15 @@ async function main(mode) { const { HOME_ABI, FOREIGN_ABI } = getBridgeABIs(bridgeMode) const homeBridge = new web3Home.eth.Contract(HOME_ABI, HOME_BRIDGE_ADDRESS) const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ABI, FOREIGN_BRIDGE_ADDRESS) - const tokenType = await getTokenType(foreignBridge, FOREIGN_BRIDGE_ADDRESS) - const isExternalErc20 = tokenType === ERC_TYPES.ERC20 const v1Bridge = bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC_V1 const erc20MethodName = bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC || v1Bridge ? 'erc677token' : 'erc20token' const erc20Address = await foreignBridge.methods[erc20MethodName]().call() + const tokenType = await getTokenType( + new web3Foreign.eth.Contract(ERC677_BRIDGE_TOKEN_ABI, erc20Address), + FOREIGN_BRIDGE_ADDRESS + ) + const isExternalErc20 = tokenType === ERC_TYPES.ERC20 const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address) logger.debug('getting last block numbers') diff --git a/oracle/scripts/initialChecks.js b/oracle/scripts/initialChecks.js index 06099443..553653f8 100644 --- a/oracle/scripts/initialChecks.js +++ b/oracle/scripts/initialChecks.js @@ -1,6 +1,6 @@ require('../env') const Web3 = require('web3') -const { ERC677_BRIDGE_TOKEN_ABI, ERC_TYPES } = require('../../commons') +const { ERC677_BRIDGE_TOKEN_ABI, getTokenType } = require('../../commons') async function initialChecks() { const { ERC20_TOKEN_ADDRESS, BRIDGE_MODE, FOREIGN_RPC_URL, FOREIGN_BRIDGE_ADDRESS } = process.env @@ -8,17 +8,12 @@ async function initialChecks() { if (BRIDGE_MODE === 'ERC_TO_ERC') { const foreignWeb3 = new Web3(new Web3.providers.HttpProvider(FOREIGN_RPC_URL)) - const tokenContract = new foreignWeb3.eth.Contract(ERC677_BRIDGE_TOKEN_ABI, ERC20_TOKEN_ADDRESS) - try { - const bridgeContract = await tokenContract.methods.bridgeContract().call() - if (bridgeContract === FOREIGN_BRIDGE_ADDRESS) { - result.foreignERC = ERC_TYPES.ERC677 - } else { - result.foreignERC = ERC_TYPES.ERC20 - } - } catch (e) { - result.foreignERC = ERC_TYPES.ERC20 - } + const bridgeTokenContract = new foreignWeb3.eth.Contract( + ERC677_BRIDGE_TOKEN_ABI, + ERC20_TOKEN_ADDRESS + ) + + result.foreignERC = await getTokenType(bridgeTokenContract, FOREIGN_BRIDGE_ADDRESS) } console.log(JSON.stringify(result)) return result diff --git a/ui/src/stores/ForeignStore.js b/ui/src/stores/ForeignStore.js index 0cd5d17e..ac6564c9 100644 --- a/ui/src/stores/ForeignStore.js +++ b/ui/src/stores/ForeignStore.js @@ -8,6 +8,7 @@ import { getUnit, decodeFeeManagerMode, getBridgeABIs, + getTokenType, ERC20_BYTES32_ABI } from '../../../commons' import { @@ -28,7 +29,6 @@ import { ZERO_ADDRESS, getDeployedAtBlock, getValidatorList, - getTokenType, getValidatorContract, getRequiredSignatures, getValidatorCount diff --git a/ui/src/stores/utils/__tests__/contract.test.js b/ui/src/stores/utils/__tests__/contract.test.js index 5e7c954e..3662beaf 100644 --- a/ui/src/stores/utils/__tests__/contract.test.js +++ b/ui/src/stores/utils/__tests__/contract.test.js @@ -1,66 +1,6 @@ import BN from 'bignumber.js' -import { getTokenType, mintedTotallyByBridge } from '../contract' -import { ERC_TYPES } from '../../../../../commons' +import { mintedTotallyByBridge } from '../contract' -describe('getTokenType', () => { - it('should return ERC677 if bridgeContract is equal to bridgeAddress', async () => { - // Given - const bridgeAddress = '0xCecBE80Ed3548dE11D7d2D922a36576eA40C4c26' - const contract = { - methods: { - bridgeContract: () => { - return { - call: () => Promise.resolve(bridgeAddress) - } - } - } - } - - // When - const type = await getTokenType(contract, bridgeAddress) - - // Then - expect(type).toEqual(ERC_TYPES.ERC677) - }) - it('should return ERC20 if bridgeContract is not equal to bridgeAddress', async () => { - // Given - const bridgeAddress = '0xCecBE80Ed3548dE11D7d2D922a36576eA40C4c26' - const contract = { - methods: { - bridgeContract: () => { - return { - call: () => Promise.resolve('0xBFCb120F7B1de491262CA4D9D8Eba70438b6896E') - } - } - } - } - - // When - const type = await getTokenType(contract, bridgeAddress) - - // Then - expect(type).toEqual(ERC_TYPES.ERC20) - }) - it('should return ERC20 if bridgeContract is not present', async () => { - // Given - const bridgeAddress = '0xCecBE80Ed3548dE11D7d2D922a36576eA40C4c26' - const contract = { - methods: { - bridgeContract: () => { - return { - call: () => Promise.reject() - } - } - } - } - - // When - const type = await getTokenType(contract, bridgeAddress) - - // Then - expect(type).toEqual(ERC_TYPES.ERC20) - }) -}) describe('mintedTotallyByBridge', () => { it('should call mintedTotallyByBridge from contract', async () => { // Given diff --git a/ui/src/stores/utils/contract.js b/ui/src/stores/utils/contract.js index f6cf142d..94374053 100644 --- a/ui/src/stores/utils/contract.js +++ b/ui/src/stores/utils/contract.js @@ -157,19 +157,6 @@ export const getDeployedAtBlock = async contract => { } } -export const getTokenType = async (contract, bridgeAddress) => { - try { - const bridgeContract = await contract.methods.bridgeContract().call() - if (bridgeContract === bridgeAddress) { - return ERC_TYPES.ERC677 - } else { - return ERC_TYPES.ERC20 - } - } catch (e) { - return ERC_TYPES.ERC20 - } -} - export const getBlockRewardContract = contract => contract.methods.blockRewardContract().call() export const getValidatorContract = contract => contract.methods.validatorContract().call()