Skip to content

Commit

Permalink
various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
josepot committed Nov 27, 2024
1 parent 01a3ee7 commit b46bf07
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .papi/descriptors/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.0-autogenerated.5839969737303539935",
"version": "0.1.0-autogenerated.12920092453677198285",
"name": "@polkadot-api/descriptors",
"files": [
"dist"
Expand Down
2 changes: 1 addition & 1 deletion src/api/validators/chain/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SmWorker from "polkadot-api/smoldot/worker?worker"
import { dot, dotPpl } from "@polkadot-api/descriptors"
const worker = new SmWorker()

const smoldot = startFromWorker(worker)
const smoldot = startFromWorker(worker, { forbidWs: true })
const chain = smoldot.addChain({ chainSpec: chainSpecRelay })

const dotClient = createClient(getSmProvider(chain))
Expand Down
14 changes: 3 additions & 11 deletions src/api/validators/getPoints.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import type { SS58String } from "polkadot-api"
import { dotApi } from "./chain"

const N_ERAS = 83

export const getEraPoints = async () => {
const currentEra = await dotApi.query.Staking.CurrentEra.getValue()

const previousEras = Array(N_ERAS)
.fill(null)
.map((_, idx) => currentEra! - idx - 1)

const allEraPoints = await Promise.all(
previousEras.map((x) => dotApi.query.Staking.ErasRewardPoints.getValue(x)),
)
const allEraPoints = (
await dotApi.query.Staking.ErasRewardPoints.getEntries()
).map((x) => x.value)

return async (validators: SS58String[]) => {
let totalScorers = 0
Expand Down
25 changes: 10 additions & 15 deletions src/api/validators/getSelfStake.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { type SS58String } from "polkadot-api"
import { dotApi } from "./chain"

const getFormattedSelfStake =
(era: number) => async (validator: SS58String) => {
const selfStakeRaw = await dotApi.query.Staking.ErasStakers.getValue(
era,
export const getOwnStakes = async () =>
Object.fromEntries(
(
await dotApi.query.Staking.ErasStakersOverview.getEntries(
(await dotApi.query.Staking.ActiveEra.getValue())!.index,
)
).map(({ keyArgs: [, validator], value: { own } }) => [
validator,
)
const { own } = selfStakeRaw
return Number(BigInt(own) / 1_000_000n) / 10_000 // Expressed in DOT (with 4 decimals)
}

export const getSelfStake = async (validators: string[]) => {
const activeEra = await dotApi.query.Staking.ActiveEra.getValue()

return Promise.all(validators.map(getFormattedSelfStake(activeEra!.index)))
}
Number(own / 1_000_000n) / 10_000, // Expressed in DOT (with 4 decimals)
]),
)
5 changes: 3 additions & 2 deletions src/api/validators/getValidators.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { getComissions } from "./getCommissions"
import { getClusterSizes } from "./getClusterSizes"
import { getEraPoints } from "./getPoints"
import { getSelfStake } from "./getSelfStake"
import { getOwnStakes } from "./getSelfStake"
import { getVotes } from "./getVotes"
import { dotApi } from "./chain"

export const getValidators = async () => {
const fnPromise = getEraPoints()
const ownStakesP = getOwnStakes()
const validators = await dotApi.query.Session.Validators.getValue()

const [comissions, clusterSizes, points, votes, selfStake] =
Expand All @@ -17,7 +18,7 @@ export const getValidators = async () => {
getEraPointsValidators(validators),
),
getVotes(validators),
getSelfStake(validators),
ownStakesP.then((ownStakes) => validators.map((x) => ownStakes[x] || 0)),
])

const result = Object.fromEntries(
Expand Down
8 changes: 1 addition & 7 deletions whitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import {
} from "@polkadot-api/descriptors"

const dotWhitelist: DotWhitelistEntry[] = [
"query.Staking.Validators",
"query.Staking.CurrentEra",
"query.Staking.ErasRewardPoints",
"query.Staking.ErasStakers",
"query.Staking.ActiveEra",
"query.Staking.Nominators",
"query.Staking.Validators",
"query.Staking.*",
"query.Session.Validators",
]

Expand Down

0 comments on commit b46bf07

Please sign in to comment.