Common getTokenType used on UI, monitor and Oracle (#165)

* Stopped silently swallowing errors

* getTokenType in commons

* Oracle using getTokenType from commons

* Moved the getTokenType tests from ui to commons

* Using getTokenType in ui and oracle

* Revert "Stopped silently swallowing errors"

fc92c0af9dc7cb4e59a7f1779afc8da8d9c33ecf

* using common getTokenType in monitor.

* Lint.
This commit is contained in:
Przemyslaw Rzad 2019-07-26 14:26:14 +02:00 committed by GitHub
parent c94b68ab5b
commit c66923827b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 96 additions and 110 deletions

@ -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)
})
})

@ -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) { function decodeBridgeMode(bridgeModeHash) {
switch (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 => { const getUnit = bridgeMode => {
let unitHome = null let unitHome = null
let unitForeign = null let unitForeign = null
@ -56,5 +69,6 @@ module.exports = {
decodeBridgeMode, decodeBridgeMode,
decodeFeeManagerMode, decodeFeeManagerMode,
getBridgeMode, getBridgeMode,
getTokenType,
getUnit getUnit
} }

@ -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
}

@ -8,9 +8,10 @@ const {
getBridgeABIs, getBridgeABIs,
getBridgeMode, getBridgeMode,
HOME_ERC_TO_ERC_ABI, HOME_ERC_TO_ERC_ABI,
ERC20_ABI ERC20_ABI,
ERC677_BRIDGE_TOKEN_ABI,
getTokenType
} = require('../../commons') } = require('../../commons')
const { getTokenType } = require('./ercUtils')
const { HOME_RPC_URL, FOREIGN_RPC_URL, HOME_BRIDGE_ADDRESS, FOREIGN_BRIDGE_ADDRESS } = process.env 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) 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 { HOME_ABI, FOREIGN_ABI } = getBridgeABIs(bridgeMode)
const homeBridge = new web3Home.eth.Contract(HOME_ABI, HOME_BRIDGE_ADDRESS) const homeBridge = new web3Home.eth.Contract(HOME_ABI, HOME_BRIDGE_ADDRESS)
const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ABI, FOREIGN_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 v1Bridge = bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC_V1
const erc20MethodName = const erc20MethodName =
bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC || v1Bridge ? 'erc677token' : 'erc20token' bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC || v1Bridge ? 'erc677token' : 'erc20token'
const erc20Address = await foreignBridge.methods[erc20MethodName]().call() 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) const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address)
logger.debug('getting last block numbers') logger.debug('getting last block numbers')

@ -1,6 +1,6 @@
require('../env') require('../env')
const Web3 = require('web3') const Web3 = require('web3')
const { ERC677_BRIDGE_TOKEN_ABI, ERC_TYPES } = require('../../commons') const { ERC677_BRIDGE_TOKEN_ABI, getTokenType } = require('../../commons')
async function initialChecks() { async function initialChecks() {
const { ERC20_TOKEN_ADDRESS, BRIDGE_MODE, FOREIGN_RPC_URL, FOREIGN_BRIDGE_ADDRESS } = process.env 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') { if (BRIDGE_MODE === 'ERC_TO_ERC') {
const foreignWeb3 = new Web3(new Web3.providers.HttpProvider(FOREIGN_RPC_URL)) const foreignWeb3 = new Web3(new Web3.providers.HttpProvider(FOREIGN_RPC_URL))
const tokenContract = new foreignWeb3.eth.Contract(ERC677_BRIDGE_TOKEN_ABI, ERC20_TOKEN_ADDRESS) const bridgeTokenContract = new foreignWeb3.eth.Contract(
try { ERC677_BRIDGE_TOKEN_ABI,
const bridgeContract = await tokenContract.methods.bridgeContract().call() ERC20_TOKEN_ADDRESS
if (bridgeContract === FOREIGN_BRIDGE_ADDRESS) { )
result.foreignERC = ERC_TYPES.ERC677
} else { result.foreignERC = await getTokenType(bridgeTokenContract, FOREIGN_BRIDGE_ADDRESS)
result.foreignERC = ERC_TYPES.ERC20
}
} catch (e) {
result.foreignERC = ERC_TYPES.ERC20
}
} }
console.log(JSON.stringify(result)) console.log(JSON.stringify(result))
return result return result

@ -8,6 +8,7 @@ import {
getUnit, getUnit,
decodeFeeManagerMode, decodeFeeManagerMode,
getBridgeABIs, getBridgeABIs,
getTokenType,
ERC20_BYTES32_ABI ERC20_BYTES32_ABI
} from '../../../commons' } from '../../../commons'
import { import {
@ -28,7 +29,6 @@ import {
ZERO_ADDRESS, ZERO_ADDRESS,
getDeployedAtBlock, getDeployedAtBlock,
getValidatorList, getValidatorList,
getTokenType,
getValidatorContract, getValidatorContract,
getRequiredSignatures, getRequiredSignatures,
getValidatorCount getValidatorCount

@ -1,66 +1,6 @@
import BN from 'bignumber.js' import BN from 'bignumber.js'
import { getTokenType, mintedTotallyByBridge } from '../contract' import { mintedTotallyByBridge } from '../contract'
import { ERC_TYPES } from '../../../../../commons'
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', () => { describe('mintedTotallyByBridge', () => {
it('should call mintedTotallyByBridge from contract', async () => { it('should call mintedTotallyByBridge from contract', async () => {
// Given // Given

@ -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 getBlockRewardContract = contract => contract.methods.blockRewardContract().call()
export const getValidatorContract = contract => contract.methods.validatorContract().call() export const getValidatorContract = contract => contract.methods.validatorContract().call()