diff --git a/ui/src/stores/ForeignStore.js b/ui/src/stores/ForeignStore.js index 1fd43d29..10115acb 100644 --- a/ui/src/stores/ForeignStore.js +++ b/ui/src/stores/ForeignStore.js @@ -19,7 +19,10 @@ import { ZERO_ADDRESS, getDeployedAtBlock, getValidatorList, - getTokenType + getTokenType, + getValidatorContract, + getRequiredSignatures, + getValidatorCount } from './utils/contract' import { balanceLoaded, removePendingTransaction } from './utils/testUtils' import sleep from './utils/sleep' @@ -390,16 +393,14 @@ class ForeignStore { @action async getValidators() { try { - const foreignValidatorsAddress = await this.foreignBridge.methods.validatorContract().call() + const foreignValidatorsAddress = await getValidatorContract(this.foreignBridge) this.foreignBridgeValidators = new this.foreignWeb3.eth.Contract( BRIDGE_VALIDATORS_ABI, foreignValidatorsAddress ) - this.requiredSignatures = await this.foreignBridgeValidators.methods - .requiredSignatures() - .call() - this.validatorsCount = await this.foreignBridgeValidators.methods.validatorCount().call() + this.requiredSignatures = await getRequiredSignatures(this.foreignBridgeValidators) + this.validatorsCount = await getValidatorCount(this.foreignBridgeValidators) this.validators = await getValidatorList(foreignValidatorsAddress, this.foreignWeb3.eth) } catch (e) { diff --git a/ui/src/stores/HomeStore.js b/ui/src/stores/HomeStore.js index 772b837e..9ff5ef6c 100644 --- a/ui/src/stores/HomeStore.js +++ b/ui/src/stores/HomeStore.js @@ -15,7 +15,7 @@ import { getDecimals, getTotalSupply, getBalanceOf, - mintedTotally, + mintedTotallyByBridge, totalBurntCoins, getName, getFeeManager, @@ -24,7 +24,11 @@ import { getFeeManagerMode, ZERO_ADDRESS, getValidatorList, - getDeployedAtBlock + getDeployedAtBlock, + getBlockRewardContract, + getValidatorContract, + getRequiredSignatures, + getValidatorCount } from './utils/contract' import { balanceLoaded, removePendingTransaction } from './utils/testUtils' import sleep from './utils/sleep' @@ -253,7 +257,10 @@ class HomeStore { balanceLoaded() }) } else if (this.rootStore.bridgeMode === BRIDGE_MODES.ERC_TO_NATIVE) { - const mintedCoins = await mintedTotally(this.blockRewardContract) + const mintedCoins = await mintedTotallyByBridge( + this.blockRewardContract, + this.HOME_BRIDGE_ADDRESS + ) const burntCoins = await totalBurntCoins(this.homeBridge) this.balance = fromDecimals(mintedCoins.minus(burntCoins).toString(10), this.tokenDecimals) } else { @@ -418,14 +425,14 @@ class HomeStore { @action async getValidators() { try { - const homeValidatorsAddress = await this.homeBridge.methods.validatorContract().call() + const homeValidatorsAddress = await getValidatorContract(this.homeBridge) this.homeBridgeValidators = new this.homeWeb3.eth.Contract( BRIDGE_VALIDATORS_ABI, homeValidatorsAddress ) - this.requiredSignatures = await this.homeBridgeValidators.methods.requiredSignatures().call() - this.validatorsCount = await this.homeBridgeValidators.methods.validatorCount().call() + this.requiredSignatures = await getRequiredSignatures(this.homeBridgeValidators) + this.validatorsCount = await getValidatorCount(this.homeBridgeValidators) this.validators = await getValidatorList(homeValidatorsAddress, this.homeWeb3.eth) } catch (e) { @@ -524,7 +531,7 @@ class HomeStore { } async getBlockRewardContract() { - const blockRewardAddress = await this.homeBridge.methods.blockRewardContract().call() + const blockRewardAddress = await getBlockRewardContract(this.homeBridge) this.blockRewardContract = new this.homeWeb3.eth.Contract(BLOCK_REWARD_ABI, blockRewardAddress) } diff --git a/ui/src/stores/__tests__/HomeStore.test.js b/ui/src/stores/__tests__/HomeStore.test.js new file mode 100644 index 00000000..3b934c4f --- /dev/null +++ b/ui/src/stores/__tests__/HomeStore.test.js @@ -0,0 +1,55 @@ +import Web3 from 'web3' +import BN from 'bignumber.js' +import HomeStore from '../HomeStore' +import { BRIDGE_MODES } from '../utils/bridgeMode' +import * as contract from '../utils/contract' +import * as web3StoreUtils from '../utils/web3' + +describe('HomeStore', () => { + const rootStore = { + web3Store: { + injectedWeb3: new Web3(), + homeWeb3: new Web3() + }, + foreignStore: { + feeEventsFinished: true, + feeManager: {} + }, + bridgeModeInitialized: true, + bridgeMode: BRIDGE_MODES.ERC_TO_NATIVE + } + it('should call mintedTotallyByBridge', async () => { + // Method to spy + contract.mintedTotallyByBridge = jest.fn(() => Promise.resolve(new BN(100))) + + // Other mocks + contract.totalBurntCoins = jest.fn(() => Promise.resolve(new BN(0))) + contract.getBlockRewardContract = jest.fn(() => + Promise.resolve('0xCecBE80Ed3548dE11D7d2D922a36576eA40C4c26') + ) + contract.getPastEvents = jest.fn(() => Promise.resolve([])) + web3StoreUtils.getBlockNumber = jest.fn(() => Promise.resolve(10)) + contract.getMinPerTxLimit = jest.fn(() => Promise.resolve(100000000)) + contract.getMaxPerTxLimit = jest.fn(() => Promise.resolve(10000000000)) + contract.getCurrentLimit = jest.fn(() => Promise.resolve({})) + contract.getValidatorContract = jest.fn(() => + Promise.resolve('0xcDF12C376F43A70a07d7Ad2fD3449634717C9235') + ) + contract.getRequiredSignatures = jest.fn(() => Promise.resolve(1)) + contract.getValidatorCount = jest.fn(() => Promise.resolve(1)) + contract.getValidatorList = jest.fn(() => + Promise.resolve(['0x52576e0cCaA0C9157142Fbf1d1c6DbfAc5e4E33e']) + ) + + // When + new HomeStore(rootStore) + + // Need to wait until HomeStore is initialized + await new Promise(resolve => { + setTimeout(() => { + expect(contract.mintedTotallyByBridge).toHaveBeenCalled() + resolve() + }, 1000) + }) + }) +}) diff --git a/ui/src/stores/utils/__tests__/contract.test.js b/ui/src/stores/utils/__tests__/contract.test.js index ff99643a..49123363 100644 --- a/ui/src/stores/utils/__tests__/contract.test.js +++ b/ui/src/stores/utils/__tests__/contract.test.js @@ -1,4 +1,5 @@ -import { getTokenType } from '../contract' +import BN from 'bignumber.js' +import { getTokenType, mintedTotallyByBridge } from '../contract' import { ERC_TYPES } from '../bridgeMode' describe('getTokenType', () => { @@ -60,3 +61,26 @@ describe('getTokenType', () => { expect(type).toEqual(ERC_TYPES.ERC20) }) }) +describe('mintedTotallyByBridge', () => { + it('should call mintedTotallyByBridge from contract', async () => { + // Given + const bridgeAddress = '0xCecBE80Ed3548dE11D7d2D922a36576eA40C4c26' + const value = '120000' + const contract = { + methods: { + mintedTotallyByBridge: address => { + return { + call: () => Promise.resolve(address === bridgeAddress ? value : '0') + } + } + } + } + + // When + const result = await mintedTotallyByBridge(contract, bridgeAddress) + + // Then + expect(BN.isBigNumber(result)).toBeTruthy() + expect(result.toString()).toEqual(value) + }) +}) diff --git a/ui/src/stores/utils/contract.js b/ui/src/stores/utils/contract.js index 89b964d8..5c64c107 100644 --- a/ui/src/stores/utils/contract.js +++ b/ui/src/stores/utils/contract.js @@ -53,8 +53,8 @@ export const getBalanceOf = async (contract, address) => { return fromDecimals(balance, decimals) } -export const mintedTotally = async contract => { - const mintedCoins = await contract.methods.mintedTotally().call() +export const mintedTotallyByBridge = async (contract, bridgeAddress) => { + const mintedCoins = await contract.methods.mintedTotallyByBridge(bridgeAddress).call() return new BN(mintedCoins) } @@ -170,3 +170,11 @@ export const getTokenType = async (contract, bridgeAddress) => { return ERC_TYPES.ERC20 } } + +export const getBlockRewardContract = contract => contract.methods.blockRewardContract().call() + +export const getValidatorContract = contract => contract.methods.validatorContract().call() + +export const getRequiredSignatures = contract => contract.methods.requiredSignatures().call() + +export const getValidatorCount = contract => contract.methods.validatorCount().call()