Skip to content

Commit

Permalink
Ana/427 polkadot single validator query (#440)
Browse files Browse the repository at this point in the history
* save validators with operatoraddress as key

* add check to wait to fetch validators

* Update lib/resolvers.js

Co-Authored-By: Fabian <[email protected]>

* Update lib/resolvers.js

Co-Authored-By: Fabian <[email protected]>

* lint

* fix delegations

Co-authored-by: Fabian <[email protected]>
  • Loading branch information
Bitcoinera and faboweb authored Mar 11, 2020
1 parent dc7d0e6 commit 67d825f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/block-listeners/polkadot-node-subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class PolkadotNodeSubscription {
this.store.update({
height: blockHeight,
block,
validators: this.sessionValidators
validators: this.getValidatorMap(this.sessionValidators)
})

// For each transaction listed in a block we extract the relevant addresses. This is published to the network.
Expand All @@ -160,7 +160,7 @@ class PolkadotNodeSubscription {
}
}

async getValidatorMap(validators) {
getValidatorMap(validators) {
const validatorMap = _.keyBy(validators, 'operatorAddress')
return validatorMap
}
Expand Down
24 changes: 16 additions & 8 deletions lib/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,25 @@ async function validator(_, { networkId, operatorAddress }, { dataSources }) {
networkId
).getValidatorInfoByAddress(operatorAddress)

const validator = localStore(dataSources, networkId).validators[
operatorAddress
]
// here first we check if validators in localStore are an empty object
// this is because sometimes it takes time to fetch them on startup
if (Object.keys(localStore(dataSources, networkId).validators).length > 0) {
const validator = localStore(dataSources, networkId).validators[
operatorAddress
]

if (!validator) {
throw new UserInputError(
`The validator ${operatorAddress} was was found within the network ${networkId}`
if (!validator) {
throw new UserInputError(
`The validator ${operatorAddress} was not found within the network ${networkId}`
)
}

return enrichValidator(validatorInfo, validator)
} else {
throw new Error(
`Validators have not been loaded yet for the network ${networkId}`
)
}

return enrichValidator(validatorInfo, validator)
}

function delegation(
Expand Down
6 changes: 4 additions & 2 deletions lib/source/polkadotV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,11 @@ class polkadotAPI {
delegatorAddress,
validatorsDictionary
) {
if (!Array.isArray(validatorsDictionary)) return []
const validatorsArray = Object.keys(validatorsDictionary).map(
key => validatorsDictionary[key]
)
let delegations = []
validatorsDictionary.forEach(validator => {
validatorsArray.forEach(validator => {
validator.nominations.forEach(nomination => {
if (delegatorAddress === nomination.who) {
delegations.push(
Expand Down

0 comments on commit 67d825f

Please sign in to comment.