Skip to content

Commit

Permalink
Add denom decimal places to networks.js (#434)
Browse files Browse the repository at this point in the history
* lint

* add decimals places, refactor POLKADOT_CONVERSION

* add to schema

* add to tests

* refactor with @faboweb suggestions

* fix

* fix

* fix

* cleanup

* apply suggestions

* fix balances

* add factor properly to network

Co-authored-by: Bitcoinera <[email protected]>
  • Loading branch information
mariopino and Bitcoinera authored Mar 12, 2020
1 parent 67d825f commit 8af69f2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
10 changes: 9 additions & 1 deletion data/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
15 changes: 7 additions & 8 deletions lib/reducers/polkadotV0-reducers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const BigNumber = require('bignumber.js')

const POLKADOT_CONVERSION = 1000000000000

function blockReducer(
networkId,
blockHeight,
Expand Down Expand Up @@ -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)
}
}
Expand Down
7 changes: 7 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ const typeDefs = gql`
type: String
}
type coinLookup {
chainDenom: String!
viewDenom: String!
chainToViewConversionFactor: Int!
}
type Network {
id: String
title: String
Expand Down Expand Up @@ -138,6 +144,7 @@ const typeDefs = gql`
action_proposal: Boolean
default: Boolean
stakingDenom: String
coinLookup: [coinLookup]
enabled: Boolean
icon: String
slug: String
Expand Down
8 changes: 5 additions & 3 deletions lib/source/polkadotV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
)
}
})
Expand Down
9 changes: 9 additions & 0 deletions tests/network-configs.test.js
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 8af69f2

Please sign in to comment.