Skip to content

Commit

Permalink
[Polkadot] Add delegations query (#435)
Browse files Browse the repository at this point in the history
* first step, simplified query is working

* lint

* Get validator info from val dictionary

* cleanup, fix comission

* fix

* faster solution

* remove types we dont need in store

* @ALEKSEI suggestions

* fix

* fix

* simplify as @imbeone suggested

* simplify, thanks @Bitcoinera!

* re adding array check

* delegationReducer

* fix
  • Loading branch information
mariopino authored Mar 11, 2020
1 parent b8a2203 commit 011b5c0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/reducers/polkadotV0-reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function validatorReducer(network, validator) {
uptimePercentage: undefined,
tokens: validator.tokens,
commissionUpdateTime: undefined,
commission: validator.validatorPrefs.commission / 100000000,
commission: validator.validatorPrefs.commission / 10000000,
maxCommission: undefined,
maxChangeCommission: undefined,
status: `ACTIVE`, // We are fetching current session active validators only (not intentions)
Expand All @@ -53,7 +53,8 @@ function validatorReducer(network, validator) {
selfStake:
(
BigNumber(validator.exposure.own).toNumber() / POLKADOT_CONVERSION
).toFixed(9) || 0
).toFixed(9) || 0,
nominations: validator.nominations
}
}

Expand All @@ -69,8 +70,20 @@ function balanceReducer(balance) {
]
}

function delegationReducer(delegation, validator) {
return {
validatorAddress: validator.operatorAddress,
delegatorAddress: delegation.who,
validator,
amount: BigNumber(delegation.value)
.dividedBy(10 ** 12) // FIXME: Replace by constant
.toFixed(9)
}
}

module.exports = {
blockReducer,
validatorReducer,
balanceReducer
balanceReducer,
delegationReducer
}
21 changes: 21 additions & 0 deletions lib/source/polkadotV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ class polkadotAPI {
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.nominations = JSON.parse(
JSON.stringify(validator.exposure.others)
) // Added for faster delegations search
} else {
validator.votingPower = 0
}
Expand Down Expand Up @@ -188,6 +191,24 @@ class polkadotAPI {
const accountBalances = await api.query.staking.bonded(delegatorAddress) // or stashId?
console.log('ACCOUNT', JSON.stringify(accountBalances))
}

async getDelegationsForDelegatorAddress(
delegatorAddress,
validatorsDictionary
) {
if (!Array.isArray(validatorsDictionary)) return []
let delegations = []
validatorsDictionary.forEach(validator => {
validator.nominations.forEach(nomination => {
if (delegatorAddress === nomination.who) {
delegations.push(
this.reducers.delegationReducer(nomination, validator)
)
}
})
})
return delegations
}
}

module.exports = polkadotAPI

0 comments on commit 011b5c0

Please sign in to comment.