Skip to content

Commit

Permalink
Fabo/improve delegations (#455)
Browse files Browse the repository at this point in the history
* improve delegations

* removed timer

* add hack for accounts with no nominations

Co-authored-by: Bitcoinera <anagjordano@gmail.com>
  • Loading branch information
faboweb and Bitcoinera authored Mar 15, 2020
1 parent 668635b commit f316193
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions lib/source/polkadotV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,29 @@ class polkadotAPI {
delegatorAddress,
validatorsDictionary
) {
const validatorsArray = Object.keys(validatorsDictionary).map(
key => validatorsDictionary[key]
const api = await this.getAPI()
const nominations = JSON.parse(
JSON.stringify(await api.query.staking.nominators(delegatorAddress))
)
if (!nominations) return [] // HACK not all accounts do have nominations
const allNominations = nominations.reduce(
(all, { targets }) => all.concat(targets),
[]
)
let delegations = []
validatorsArray.forEach(validator => {
validator.nominations.forEach(nomination => {
if (delegatorAddress === nomination.who) {
delegations.push(
this.reducers.delegationReducer(this.network, nomination, validator)
)
}
let delegations = allNominations
.map(validatorAddress => {
const validator = validatorsDictionary[validatorAddress]
if (!validator) return undefined // HACK until we load all validators
const nomination = validator
? validator.nominations.find(({ who }) => who === delegatorAddress)
: undefined
return this.reducers.delegationReducer(
this.network,
nomination,
validator
)
})
})
.filter(x => !!x) //filter out not existing validators
return delegations
}
}
Expand Down

0 comments on commit f316193

Please sign in to comment.