Skip to content

Commit

Permalink
fix: outdated active validators
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Apr 23, 2024
1 parent cb5e7b7 commit 525f4bf
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 145 deletions.
6 changes: 3 additions & 3 deletions apps/portal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"@emotion/styled": "^11.11.0",
"@polkadot-onboard/core": "^1.1.0",
"@polkadot-onboard/injected-wallets": "^1.1.0",
"@polkadot/api": "^10.12.6",
"@polkadot/api-contract": "^10.12.6",
"@polkadot/extension-dapp": "^0.46.6",
"@polkadot/api": "^10.13.1",
"@polkadot/api-contract": "^10.13.1",
"@polkadot/extension-dapp": "^0.47.1",
"@polkawallet/bridge": "^0.1.5-38",
"@recoiljs/refine": "^0.1.1",
"@sentry/react": "^7.85.0",
Expand Down
45 changes: 42 additions & 3 deletions apps/portal/src/domains/fastUnstake/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,47 @@ export type WorkerFunction = typeof getExposure

export type ExposureRecord = Record<number, Record<string, boolean>>

// DEPRECATION: Paged Rewards
//
// Temporary until paged rewards migration has completed on all networks. Wait 84 eras from Polkadot
// start: 1420 + 84 = 1504, when full history depth will be moved over to new paged rewards storage.
const pagedRewardsActiveEras: Record<`0x${string}`, number> = {
// Polkadot
'0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3': 1420,
// Kusama
'0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe': 6514,
// Westend
'0xe143f23803ac50e8f6f8e62695d1ce9e4e1d68aa36c1cd2cfd15340213f3423e': 7167,
}

const getEraExposeds = async (api: ApiPromise, era: number) => {
const pagedRewardsActiveEra = pagedRewardsActiveEras[api.genesisHash.toHex()]
if (pagedRewardsActiveEra === undefined || pagedRewardsActiveEra < era) {
const stakers = await api.query.staking.erasStakers.entries(era)

const exposeds = stakers
.map(staker => staker[1])
.flatMap(staker => staker.others.map(other => other.who.toString()))

return new Set(exposeds)
}

const stakerKeys = await api.query.staking.erasStakersOverview.keys(era)
const stakers = await Promise.all(
stakerKeys
.map(key => key.args[1])
.map(async address => await api.query.staking.erasStakersPaged.entries(era, address))
)

const exposeds = stakers
.flat()
.map(staker => staker[1])
.filter(staker => staker.isSome)
.flatMap(staker => staker.unwrap().others.map(other => other.who.toString()))

return new Set(exposeds)
}

const generateExposure = async function* (
endpoint: string,
activeEra: number,
Expand All @@ -26,9 +67,7 @@ const generateExposure = async function* (
const addressesToCheck = encodedAddresses.filter(address => !(address in eraExposure))

if (addressesToCheck.length > 0) {
const exposeds = await api.query.staking.erasStakers
.entries(era)
.then(x => new Set(x.flatMap(([_, exposure]) => exposure.others.flatMap(({ who }) => who.toString()))))
const exposeds = await getEraExposeds(api, era)

for (const address of addressesToCheck) {
eraExposure[address] = exposeds.has(address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const usePoolStakes = <T extends Account | Account[]>(account: T) => {
)

const _eraStakers = useRecoilValue(useEraStakersState(activeEra.unwrapOrDefault().index))
const eraStakers = useMemo(() => new Set(_eraStakers.map(x => x[0].args[1].toHuman())), [_eraStakers])
const eraStakers = useMemo(() => new Set(_eraStakers.map(x => x.toString())), [_eraStakers])

const pools = useMemo(
() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export const eraStakersState = selectorFamily({
async ({ get }) => {
const api = get(substrateApiState(endpoint))

return await api.query.staking.erasStakers.entries(era)
const stakers = await (api.query.staking.erasStakersOverview ?? api.query.staking.erasStakers).keys(era)

return stakers.map(staker => staker.args[1])
},
cachePolicy_UNSTABLE: { eviction: 'most-recent' },
// NOTE: polkadot.js returned codec object includes reference to the registry
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
"turbo": "^1.11.2"
},
"resolutions": {
"@polkadot/api-augment": "^10.12.6",
"@polkadot/api-derive": "^10.12.6",
"@polkadot/api-augment": "^10.13.1",
"@polkadot/api-derive": "^10.13.1",
"@polkadot/keyring": "^12.6.2",
"@polkadot/rpc-augment": "^10.12.6",
"@polkadot/types": "^10.12.6",
"@polkadot/types-augment": "^10.12.6",
"@polkadot/types-codec": "^10.12.6",
"@polkadot/types-create": "^10.12.6",
"@polkadot/types-known": "^10.12.6",
"@polkadot/rpc-augment": "^10.13.1",
"@polkadot/types": "^10.13.1",
"@polkadot/types-augment": "^10.13.1",
"@polkadot/types-codec": "^10.13.1",
"@polkadot/types-create": "^10.13.1",
"@polkadot/types-known": "^10.13.1",
"@polkadot/util": "^12.6.2",
"@polkadot/util-crypto": "^12.6.2",
"@polkadot/wasm-crypto": "^7.3.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/nft/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"devDependencies": {
"@acala-network/types": "^5.1.2",
"@graphql-codegen/cli": "^5.0.0",
"@polkadot/api": "^10.12.6",
"@polkadot/api": "^10.13.1",
"prettier": "^2.8.8",
"typescript": "^5.0.2",
"viem": "^1.20.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/react-polkadot-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"check-types": "tsc --noEmit"
},
"devDependencies": {
"@polkadot/api": "^10.12.6",
"@polkadot/api": "^10.13.1",
"@talismn/eslint-config": "workspace:^",
"@talismn/tsconfig": "workspace:^",
"@types/react": "^18.2.57",
Expand Down
Loading

0 comments on commit 525f4bf

Please sign in to comment.