diff --git a/data/networks.js b/data/networks.js index 7111bb9da0..b78979088f 100644 --- a/data/networks.js +++ b/data/networks.js @@ -219,7 +219,15 @@ module.exports = [ action_proposal: false, default: false, stakingDenom: 'KSM', - enabled: false, + // https://wiki.polkadot.network/docs/en/learn-DOT + coinLookup: [ + { + chainDenom: 'Planck', + viewDenom: 'KSM', + chainToViewConversionFactor: 1e-12 + } + ], + enabled: true, icon: 'https://app.lunie.io/img/networks/polkadot-testnet.png', slug: 'kusama' } diff --git a/lib/reducers/polkadotV0-reducers.js b/lib/reducers/polkadotV0-reducers.js index 7aca733add..c57c0ef3ba 100644 --- a/lib/reducers/polkadotV0-reducers.js +++ b/lib/reducers/polkadotV0-reducers.js @@ -1,7 +1,5 @@ const BigNumber = require('bignumber.js') -const POLKADOT_CONVERSION = 1000000000000 - function blockReducer( networkId, blockHeight, @@ -52,31 +50,32 @@ function validatorReducer(network, validator) { delegatorShares: undefined, selfStake: ( - BigNumber(validator.exposure.own).toNumber() / POLKADOT_CONVERSION + BigNumber(validator.exposure.own).toNumber() * + network.coinLookup[0].chainToViewConversionFactor ).toFixed(9) || 0, nominations: validator.nominations } } -function balanceReducer(balance) { +function balanceReducer(network, balance) { // hack. We convert the balance into an Array to make it an Iterable return [ { amount: BigNumber(balance) - .div(POLKADOT_CONVERSION) + .times(network.coinLookup[0].chainToViewConversionFactor) .toFixed(4), - denom: `KSM` // hardcoded for now. We could use coinLookup.viewDenom from PR 434 + denom: network.coinLookup[0].viewDenom } ] } -function delegationReducer(delegation, validator) { +function delegationReducer(network, delegation, validator) { return { validatorAddress: validator.operatorAddress, delegatorAddress: delegation.who, validator, amount: BigNumber(delegation.value) - .dividedBy(10 ** 12) // FIXME: Replace by constant + .times(network.coinLookup[0].chainToViewConversionFactor) .toFixed(9) } } diff --git a/lib/schema.js b/lib/schema.js index 1b32eb8de6..3f097f003a 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -109,6 +109,12 @@ const typeDefs = gql` type: String } + type coinLookup { + chainDenom: String! + viewDenom: String! + chainToViewConversionFactor: Int! + } + type Network { id: String title: String @@ -138,6 +144,7 @@ const typeDefs = gql` action_proposal: Boolean default: Boolean stakingDenom: String + coinLookup: [coinLookup] enabled: Boolean icon: String slug: String diff --git a/lib/source/polkadotV0-source.js b/lib/source/polkadotV0-source.js index f43ad5af9a..73a15f0666 100644 --- a/lib/source/polkadotV0-source.js +++ b/lib/source/polkadotV0-source.js @@ -148,7 +148,9 @@ class polkadotAPI { if (validator.exposure) { const validatorStake = new BigNumber(validator.exposure.total) validator.votingPower = validatorStake.div(networkTotalStake).toNumber() - validator.tokens = validatorStake / 1e12 // IMPROVE IT! Add denom decimal places to network.js + validator.tokens = + validatorStake * + this.network.coinLookup[0].chainToViewConversionFactor validator.nominations = JSON.parse( JSON.stringify(validator.exposure.others) ) // Added for faster delegations search @@ -175,7 +177,7 @@ class polkadotAPI { const api = await this.getAPI() const account = await api.query.system.account(address) const freeBalance = account.data.free - return this.reducers.balanceReducer(JSON.stringify(freeBalance)) + return this.reducers.balanceReducer(this.network, freeBalance.toString()) } async getRewards(delegatorAddress) { @@ -204,7 +206,7 @@ class polkadotAPI { validator.nominations.forEach(nomination => { if (delegatorAddress === nomination.who) { delegations.push( - this.reducers.delegationReducer(nomination, validator) + this.reducers.delegationReducer(this.network, nomination, validator) ) } }) diff --git a/tests/network-configs.test.js b/tests/network-configs.test.js index 5a6827a554..7e82a733be 100644 --- a/tests/network-configs.test.js +++ b/tests/network-configs.test.js @@ -1,6 +1,12 @@ const Joi = require('@hapi/joi') const { allNetworks } = require('../lib/networks') +const coinLookup = Joi.object().keys({ + chainDenom: Joi.string(), + viewDenom: Joi.string(), + chainToViewConversionFactor: Joi.number() +}) + const schema = Joi.object({ id: Joi.string().lowercase(), title: Joi.string(), @@ -32,6 +38,9 @@ const schema = Joi.object({ action_proposal: Joi.boolean(), default: Joi.boolean(), stakingDenom: Joi.string().uppercase(), + coinLookup: Joi.array() + .items(coinLookup) + .optional(), enabled: Joi.boolean(), experimental: Joi.boolean().optional(), icon: Joi.string().optional(),