Use mintedTotallyByBridge for bridge-ui statistics (#135)

* Use mintedTotallyByBridge for bridge-ui statistics
This commit is contained in:
Gerardo Nardelli 2019-07-08 18:03:02 -03:00 committed by Alexander Kolotov
parent 4e5e21541e
commit 319b493568
5 changed files with 111 additions and 16 deletions

@ -19,7 +19,10 @@ import {
ZERO_ADDRESS, ZERO_ADDRESS,
getDeployedAtBlock, getDeployedAtBlock,
getValidatorList, getValidatorList,
getTokenType getTokenType,
getValidatorContract,
getRequiredSignatures,
getValidatorCount
} from './utils/contract' } from './utils/contract'
import { balanceLoaded, removePendingTransaction } from './utils/testUtils' import { balanceLoaded, removePendingTransaction } from './utils/testUtils'
import sleep from './utils/sleep' import sleep from './utils/sleep'
@ -390,16 +393,14 @@ class ForeignStore {
@action @action
async getValidators() { async getValidators() {
try { try {
const foreignValidatorsAddress = await this.foreignBridge.methods.validatorContract().call() const foreignValidatorsAddress = await getValidatorContract(this.foreignBridge)
this.foreignBridgeValidators = new this.foreignWeb3.eth.Contract( this.foreignBridgeValidators = new this.foreignWeb3.eth.Contract(
BRIDGE_VALIDATORS_ABI, BRIDGE_VALIDATORS_ABI,
foreignValidatorsAddress foreignValidatorsAddress
) )
this.requiredSignatures = await this.foreignBridgeValidators.methods this.requiredSignatures = await getRequiredSignatures(this.foreignBridgeValidators)
.requiredSignatures() this.validatorsCount = await getValidatorCount(this.foreignBridgeValidators)
.call()
this.validatorsCount = await this.foreignBridgeValidators.methods.validatorCount().call()
this.validators = await getValidatorList(foreignValidatorsAddress, this.foreignWeb3.eth) this.validators = await getValidatorList(foreignValidatorsAddress, this.foreignWeb3.eth)
} catch (e) { } catch (e) {

@ -15,7 +15,7 @@ import {
getDecimals, getDecimals,
getTotalSupply, getTotalSupply,
getBalanceOf, getBalanceOf,
mintedTotally, mintedTotallyByBridge,
totalBurntCoins, totalBurntCoins,
getName, getName,
getFeeManager, getFeeManager,
@ -24,7 +24,11 @@ import {
getFeeManagerMode, getFeeManagerMode,
ZERO_ADDRESS, ZERO_ADDRESS,
getValidatorList, getValidatorList,
getDeployedAtBlock getDeployedAtBlock,
getBlockRewardContract,
getValidatorContract,
getRequiredSignatures,
getValidatorCount
} from './utils/contract' } from './utils/contract'
import { balanceLoaded, removePendingTransaction } from './utils/testUtils' import { balanceLoaded, removePendingTransaction } from './utils/testUtils'
import sleep from './utils/sleep' import sleep from './utils/sleep'
@ -253,7 +257,10 @@ class HomeStore {
balanceLoaded() balanceLoaded()
}) })
} else if (this.rootStore.bridgeMode === BRIDGE_MODES.ERC_TO_NATIVE) { } 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) const burntCoins = await totalBurntCoins(this.homeBridge)
this.balance = fromDecimals(mintedCoins.minus(burntCoins).toString(10), this.tokenDecimals) this.balance = fromDecimals(mintedCoins.minus(burntCoins).toString(10), this.tokenDecimals)
} else { } else {
@ -418,14 +425,14 @@ class HomeStore {
@action @action
async getValidators() { async getValidators() {
try { try {
const homeValidatorsAddress = await this.homeBridge.methods.validatorContract().call() const homeValidatorsAddress = await getValidatorContract(this.homeBridge)
this.homeBridgeValidators = new this.homeWeb3.eth.Contract( this.homeBridgeValidators = new this.homeWeb3.eth.Contract(
BRIDGE_VALIDATORS_ABI, BRIDGE_VALIDATORS_ABI,
homeValidatorsAddress homeValidatorsAddress
) )
this.requiredSignatures = await this.homeBridgeValidators.methods.requiredSignatures().call() this.requiredSignatures = await getRequiredSignatures(this.homeBridgeValidators)
this.validatorsCount = await this.homeBridgeValidators.methods.validatorCount().call() this.validatorsCount = await getValidatorCount(this.homeBridgeValidators)
this.validators = await getValidatorList(homeValidatorsAddress, this.homeWeb3.eth) this.validators = await getValidatorList(homeValidatorsAddress, this.homeWeb3.eth)
} catch (e) { } catch (e) {
@ -524,7 +531,7 @@ class HomeStore {
} }
async getBlockRewardContract() { 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) this.blockRewardContract = new this.homeWeb3.eth.Contract(BLOCK_REWARD_ABI, blockRewardAddress)
} }

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

@ -1,4 +1,5 @@
import { getTokenType } from '../contract' import BN from 'bignumber.js'
import { getTokenType, mintedTotallyByBridge } from '../contract'
import { ERC_TYPES } from '../bridgeMode' import { ERC_TYPES } from '../bridgeMode'
describe('getTokenType', () => { describe('getTokenType', () => {
@ -60,3 +61,26 @@ describe('getTokenType', () => {
expect(type).toEqual(ERC_TYPES.ERC20) 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)
})
})

@ -53,8 +53,8 @@ export const getBalanceOf = async (contract, address) => {
return fromDecimals(balance, decimals) return fromDecimals(balance, decimals)
} }
export const mintedTotally = async contract => { export const mintedTotallyByBridge = async (contract, bridgeAddress) => {
const mintedCoins = await contract.methods.mintedTotally().call() const mintedCoins = await contract.methods.mintedTotallyByBridge(bridgeAddress).call()
return new BN(mintedCoins) return new BN(mintedCoins)
} }
@ -170,3 +170,11 @@ export const getTokenType = async (contract, bridgeAddress) => {
return ERC_TYPES.ERC20 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()