From f31619322b836cfd6b57bfbcc7a3764aa39854b1 Mon Sep 17 00:00:00 2001 From: Fabian Date: Sun, 15 Mar 2020 10:29:14 -0500 Subject: [PATCH] Fabo/improve delegations (#455) * improve delegations * removed timer * add hack for accounts with no nominations Co-authored-by: Bitcoinera --- lib/source/polkadotV0-source.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/source/polkadotV0-source.js b/lib/source/polkadotV0-source.js index a10b34a544..4f528d41b4 100644 --- a/lib/source/polkadotV0-source.js +++ b/lib/source/polkadotV0-source.js @@ -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 } }