Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix api.derive.staking.eraExposure [WIP] #5813

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# CHANGELOG

## 10.12.2 Mar 11, 2024

Contributed:

- fix cache key (rpc-provider) (Thanks to https://github.com/ermalkaleci)

Changes:

- Update to latest Substrate metadata


## 10.12.1 Mar 4, 2024

Contributed:
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
14 Nikos Kontakis Bump substrate connect to 0.7.16 (#5307)
11 Ian He HttpProvider support clone() (#3949)
10 Axel Chalon Make Enum/Tuple constructor use value directly if instance (#1954)
9 Tarik Gul Update docs in types-support for retrieving metadata for typegen (#5809)
10 Tarik Gul 10.12.2 (#5814)
6 Andreea Eftene Update check for contract instantiation (#5699)
5 Chevdor Fix metadata package link (#3458)
5 Jake Naviasky Fixing approvalFlagsToBools. (#1250)
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
},
"sideEffects": false,
"type": "module",
"version": "10.12.2-2-x",
"version": "10.12.2",
"versions": {
"git": "10.12.2-2-x",
"npm": "10.12.1"
"git": "10.12.2",
"npm": "10.12.2"
},
"workspaces": [
"packages/*"
Expand Down
12 changes: 6 additions & 6 deletions packages/api-augment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
"./packageDetect.cjs"
],
"type": "module",
"version": "10.12.2-2-x",
"version": "10.12.2",
"main": "index.js",
"dependencies": {
"@polkadot/api-base": "10.12.2-2-x",
"@polkadot/rpc-augment": "10.12.2-2-x",
"@polkadot/types": "10.12.2-2-x",
"@polkadot/types-augment": "10.12.2-2-x",
"@polkadot/types-codec": "10.12.2-2-x",
"@polkadot/api-base": "10.12.2",
"@polkadot/rpc-augment": "10.12.2",
"@polkadot/types": "10.12.2",
"@polkadot/types-augment": "10.12.2",
"@polkadot/types-codec": "10.12.2",
"@polkadot/util": "^12.6.2",
"tslib": "^2.6.2"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/api-augment/src/packageInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

// Do not edit, auto-generated by @polkadot/dev

export const packageInfo = { name: '@polkadot/api-augment', path: 'auto', type: 'auto', version: '10.12.2-2-x' };
export const packageInfo = { name: '@polkadot/api-augment', path: 'auto', type: 'auto', version: '10.12.2' };
6 changes: 3 additions & 3 deletions packages/api-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
"./packageDetect.cjs"
],
"type": "module",
"version": "10.12.2-2-x",
"version": "10.12.2",
"main": "index.js",
"dependencies": {
"@polkadot/rpc-core": "10.12.2-2-x",
"@polkadot/types": "10.12.2-2-x",
"@polkadot/rpc-core": "10.12.2",
"@polkadot/types": "10.12.2",
"@polkadot/util": "^12.6.2",
"rxjs": "^7.8.1",
"tslib": "^2.6.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/api-base/src/packageInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

// Do not edit, auto-generated by @polkadot/dev

export const packageInfo = { name: '@polkadot/api-base', path: 'auto', type: 'auto', version: '10.12.2-2-x' };
export const packageInfo = { name: '@polkadot/api-base', path: 'auto', type: 'auto', version: '10.12.2' };
16 changes: 8 additions & 8 deletions packages/api-contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
"./packageDetect.cjs"
],
"type": "module",
"version": "10.12.2-2-x",
"version": "10.12.2",
"main": "index.js",
"dependencies": {
"@polkadot/api": "10.12.2-2-x",
"@polkadot/api-augment": "10.12.2-2-x",
"@polkadot/types": "10.12.2-2-x",
"@polkadot/types-codec": "10.12.2-2-x",
"@polkadot/types-create": "10.12.2-2-x",
"@polkadot/api": "10.12.2",
"@polkadot/api-augment": "10.12.2",
"@polkadot/types": "10.12.2",
"@polkadot/types-codec": "10.12.2",
"@polkadot/types-create": "10.12.2",
"@polkadot/util": "^12.6.2",
"@polkadot/util-crypto": "^12.6.2",
"rxjs": "^7.8.1",
"tslib": "^2.6.2"
},
"devDependencies": {
"@polkadot/api-augment": "10.12.2-2-x",
"@polkadot/api-augment": "10.12.2",
"@polkadot/keyring": "^12.6.2",
"@polkadot/types-support": "10.12.2-2-x"
"@polkadot/types-support": "10.12.2"
}
}
2 changes: 1 addition & 1 deletion packages/api-contract/src/packageInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

// Do not edit, auto-generated by @polkadot/dev

export const packageInfo = { name: '@polkadot/api-contract', path: 'auto', type: 'auto', version: '10.12.2-2-x' };
export const packageInfo = { name: '@polkadot/api-contract', path: 'auto', type: 'auto', version: '10.12.2' };
24 changes: 12 additions & 12 deletions packages/api-derive/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@
"./packageDetect.cjs"
],
"type": "module",
"version": "10.12.2-2-x",
"version": "10.12.2",
"main": "index.js",
"dependencies": {
"@polkadot/api": "10.12.2-2-x",
"@polkadot/api-augment": "10.12.2-2-x",
"@polkadot/api-base": "10.12.2-2-x",
"@polkadot/rpc-core": "10.12.2-2-x",
"@polkadot/types": "10.12.2-2-x",
"@polkadot/types-codec": "10.12.2-2-x",
"@polkadot/api": "10.12.2",
"@polkadot/api-augment": "10.12.2",
"@polkadot/api-base": "10.12.2",
"@polkadot/rpc-core": "10.12.2",
"@polkadot/types": "10.12.2",
"@polkadot/types-codec": "10.12.2",
"@polkadot/util": "^12.6.2",
"@polkadot/util-crypto": "^12.6.2",
"rxjs": "^7.8.1",
"tslib": "^2.6.2"
},
"devDependencies": {
"@polkadot/api": "10.12.2-2-x",
"@polkadot/api-augment": "10.12.2-2-x",
"@polkadot/rpc-augment": "10.12.2-2-x",
"@polkadot/rpc-provider": "10.12.2-2-x",
"@polkadot/types-support": "10.12.2-2-x"
"@polkadot/api": "10.12.2",
"@polkadot/api-augment": "10.12.2",
"@polkadot/rpc-augment": "10.12.2",
"@polkadot/rpc-provider": "10.12.2",
"@polkadot/types-support": "10.12.2"
}
}
2 changes: 1 addition & 1 deletion packages/api-derive/src/packageInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

// Do not edit, auto-generated by @polkadot/dev

export const packageInfo = { name: '@polkadot/api-derive', path: 'auto', type: 'auto', version: '10.12.2-2-x' };
export const packageInfo = { name: '@polkadot/api-derive', path: 'auto', type: 'auto', version: '10.12.2' };
55 changes: 44 additions & 11 deletions packages/api-derive/src/staking/erasExposure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

import type { Observable } from 'rxjs';
import type { StorageKey } from '@polkadot/types';
import type { AccountId, EraIndex } from '@polkadot/types/interfaces';
import type { PalletStakingExposure } from '@polkadot/types/lookup';
import type { DeriveApi, DeriveEraExposure, DeriveEraNominatorExposure, DeriveEraValidatorExposure } from '../types.js';
import type { Option, StorageKey, u32 } from '@polkadot/types';
import type { AccountId, AccountId32, EraIndex } from '@polkadot/types/interfaces';
import type { PalletStakingExposure, SpStakingExposurePage } from '@polkadot/types/lookup';
import type { DeriveApi, DeriveEraExposurePaged, DeriveEraNominatorExposure, DeriveEraValidatorExposure, DeriveEraValidatorExposurePaged } from '../types.js';

import { map, of } from 'rxjs';

Expand All @@ -14,10 +14,12 @@ import { getEraCache, setEraCache } from './cache.js';
import { combineEras, erasHistoricApply, singleEra } from './util.js';

type KeysAndExposures = [StorageKey<[EraIndex, AccountId]>, PalletStakingExposure][];
type KeysAndExposuresPaged = [StorageKey<[u32, AccountId32, u32]>, Option<SpStakingExposurePage>][];

const CACHE_KEY = 'eraExposure';

function mapStakers (era: EraIndex, stakers: KeysAndExposures): DeriveEraExposure {
// Legacy usage for erasStakersClipped. Giving support for compatibility.
function mapStakersClipped (era: EraIndex, stakers: KeysAndExposures): DeriveEraExposurePaged {
const nominators: DeriveEraNominatorExposure = {};
const validators: DeriveEraValidatorExposure = {};

Expand All @@ -37,15 +39,46 @@ function mapStakers (era: EraIndex, stakers: KeysAndExposures): DeriveEraExposur
return { era, nominators, validators };
}

export function _eraExposure (instanceId: string, api: DeriveApi): (era: EraIndex, withActive?: boolean) => Observable<DeriveEraExposure> {
return memo(instanceId, (era: EraIndex, withActive = false): Observable<DeriveEraExposure> => {
const [cacheKey, cached] = getEraCache<DeriveEraExposure>(CACHE_KEY, era, withActive);
function mapStakersPaged (era: EraIndex, stakers: KeysAndExposuresPaged): DeriveEraExposurePaged {
const nominators: DeriveEraNominatorExposure = {};
const validators: DeriveEraValidatorExposurePaged = {};

stakers.forEach(([key, exposureOpt]): void => {
if (exposureOpt.isSome) {
const validatorId = key.args[1].toString();
const exposure = exposureOpt.unwrap();

validators[validatorId] = exposure;

exposure.others.forEach(({ who }, validatorIndex): void => {
const nominatorId = who.toString();

nominators[nominatorId] = nominators[nominatorId] || [];
nominators[nominatorId].push({ validatorId, validatorIndex });
});
}
});

return { era, nominators, validators };
}

/**
* erasStakersClipped will be deprecated and replaced with erasStakersPaged. Therefore support is given for both
* storage queries until erasStakersClipped has been completely out of use.
*/
export function _eraExposure (instanceId: string, api: DeriveApi): (era: EraIndex, withActive?: boolean) => Observable<DeriveEraExposurePaged> {
return memo(instanceId, (era: EraIndex, withActive = false): Observable<DeriveEraExposurePaged> => {
const [cacheKey, cached] = getEraCache<DeriveEraExposurePaged>(CACHE_KEY, era, withActive);

return cached
? of(cached)
: api.query.staking.erasStakersClipped.entries(era).pipe(
map((r) => setEraCache(cacheKey, withActive, mapStakers(era, r)))
);
: api.query.staking.erasStakersPaged
? api.query.staking.erasStakersPaged.entries<Option<SpStakingExposurePage>>(era).pipe(
map((r) => setEraCache(cacheKey, withActive, mapStakersPaged(era, r)))
)
: api.query.staking.erasStakersClipped.entries(era).pipe(
map((r) => setEraCache(cacheKey, withActive, mapStakersClipped(era, r)))
);
});
}

Expand Down
4 changes: 2 additions & 2 deletions packages/api-derive/src/staking/stakerExposure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type { Observable } from 'rxjs';
import type { EraIndex } from '@polkadot/types/interfaces';
import type { DeriveApi } from '../types.js';
import type { DeriveEraValidatorExposure, DeriveStakerExposure } from './types.js';
import type { DeriveEraValidatorExposurePaged, DeriveStakerExposure } from './types.js';

import { map, switchMap } from 'rxjs';

Expand All @@ -19,7 +19,7 @@ export function _stakerExposures (instanceId: string, api: DeriveApi): (accountI
stakerIds.map((stakerId) =>
exposures.map(({ era, nominators: allNominators, validators: allValidators }): DeriveStakerExposure => {
const isValidator = !!allValidators[stakerId];
const validators: DeriveEraValidatorExposure = {};
const validators: DeriveEraValidatorExposurePaged = {};
const nominating = allNominators[stakerId] || [];

if (isValidator) {
Expand Down
16 changes: 13 additions & 3 deletions packages/api-derive/src/staking/stakerRewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type { Observable } from 'rxjs';
import type { u32, Vec } from '@polkadot/types';
import type { AccountId, EraIndex } from '@polkadot/types/interfaces';
import type { PalletStakingStakingLedger } from '@polkadot/types/lookup';
import type { PalletStakingExposure, PalletStakingStakingLedger, SpStakingExposurePage } from '@polkadot/types/lookup';
import type { BN } from '@polkadot/util';
import type { DeriveApi, DeriveEraPoints, DeriveEraPrefs, DeriveEraRewards, DeriveEraValPoints, DeriveEraValPrefs, DeriveStakerExposure, DeriveStakerReward, DeriveStakerRewardValidator } from '../types.js';
import type { DeriveStakingQuery } from './types.js';
Expand Down Expand Up @@ -38,7 +38,11 @@ function parseRewards (api: DeriveApi, stashId: AccountId, [erasPoints, erasPref
Object.entries(eraValidators).forEach(([validatorId, exposure]): void => {
const valPoints = allValPoints[validatorId] || BN_ZERO;
const valComm = allValPrefs[validatorId]?.commission.unwrap() || BN_ZERO;
const expTotal = exposure.total?.unwrap() || BN_ZERO;
const expTotal = (exposure as PalletStakingExposure).total
? (exposure as PalletStakingExposure).total?.unwrap()
: (exposure as SpStakingExposurePage).pageTotal
? (exposure as SpStakingExposurePage).pageTotal?.unwrap()
: BN_ZERO;
let avail = BN_ZERO;
let value: BN | undefined;

Expand All @@ -49,7 +53,13 @@ function parseRewards (api: DeriveApi, stashId: AccountId, [erasPoints, erasPref
let staked: BN;

if (validatorId === stakerId) {
staked = exposure.own.unwrap();
if ((exposure as PalletStakingExposure).own) {
staked = (exposure as PalletStakingExposure).own.unwrap();
} else {
const expAccount = exposure.others.find(({ who }) => who.toString() === validatorId);

staked = expAccount ? expAccount.value.unwrap() : BN_ZERO;
}
} else {
const stakerExp = exposure.others.find(({ who }) => who.eq(stakerId));

Expand Down
12 changes: 10 additions & 2 deletions packages/api-derive/src/staking/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import type { AccountId, Balance, EraIndex, RewardPoint } from '@polkadot/types/interfaces';
import type { PalletStakingExposure, PalletStakingRewardDestination, PalletStakingStakingLedger, PalletStakingValidatorPrefs } from '@polkadot/types/lookup';
import type { PalletStakingExposure, PalletStakingRewardDestination, PalletStakingStakingLedger, PalletStakingValidatorPrefs, SpStakingExposurePage } from '@polkadot/types/lookup';
import type { BN } from '@polkadot/util';
import type { DeriveSessionIndexes } from '../session/types.js';

Expand Down Expand Up @@ -55,18 +55,26 @@ export type DeriveEraNominatorExposure = Record<string, DeriveEraExposureNominat

export type DeriveEraValidatorExposure = Record<string, PalletStakingExposure>;

export type DeriveEraValidatorExposurePaged = Record<string, SpStakingExposurePage | PalletStakingExposure>;

export interface DeriveEraExposure {
era: EraIndex;
nominators: DeriveEraNominatorExposure;
validators: DeriveEraValidatorExposure;
}

export interface DeriveEraExposurePaged {
era: EraIndex;
nominators: DeriveEraNominatorExposure;
validators: DeriveEraValidatorExposurePaged;
}

export interface DeriveStakerExposure {
era: EraIndex;
isEmpty: boolean;
isValidator: boolean;
nominating: DeriveEraExposureNominating[];
validators: DeriveEraValidatorExposure;
validators: DeriveEraValidatorExposurePaged;
}

export interface DeriveStakerPrefs {
Expand Down
28 changes: 14 additions & 14 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@
"./packageDetect.cjs"
],
"type": "module",
"version": "10.12.2-2-x",
"version": "10.12.2",
"main": "index.js",
"dependencies": {
"@polkadot/api-augment": "10.12.2-2-x",
"@polkadot/api-base": "10.12.2-2-x",
"@polkadot/api-derive": "10.12.2-2-x",
"@polkadot/api-augment": "10.12.2",
"@polkadot/api-base": "10.12.2",
"@polkadot/api-derive": "10.12.2",
"@polkadot/keyring": "^12.6.2",
"@polkadot/rpc-augment": "10.12.2-2-x",
"@polkadot/rpc-core": "10.12.2-2-x",
"@polkadot/rpc-provider": "10.12.2-2-x",
"@polkadot/types": "10.12.2-2-x",
"@polkadot/types-augment": "10.12.2-2-x",
"@polkadot/types-codec": "10.12.2-2-x",
"@polkadot/types-create": "10.12.2-2-x",
"@polkadot/types-known": "10.12.2-2-x",
"@polkadot/rpc-augment": "10.12.2",
"@polkadot/rpc-core": "10.12.2",
"@polkadot/rpc-provider": "10.12.2",
"@polkadot/types": "10.12.2",
"@polkadot/types-augment": "10.12.2",
"@polkadot/types-codec": "10.12.2",
"@polkadot/types-create": "10.12.2",
"@polkadot/types-known": "10.12.2",
"@polkadot/util": "^12.6.2",
"@polkadot/util-crypto": "^12.6.2",
"eventemitter3": "^5.0.1",
"rxjs": "^7.8.1",
"tslib": "^2.6.2"
},
"devDependencies": {
"@polkadot/api-augment": "10.12.2-2-x",
"@polkadot/types-support": "10.12.2-2-x"
"@polkadot/api-augment": "10.12.2",
"@polkadot/types-support": "10.12.2"
}
}
2 changes: 1 addition & 1 deletion packages/api/src/packageInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

// Do not edit, auto-generated by @polkadot/dev

export const packageInfo = { name: '@polkadot/api', path: 'auto', type: 'auto', version: '10.12.2-2-x' };
export const packageInfo = { name: '@polkadot/api', path: 'auto', type: 'auto', version: '10.12.2' };
Loading
Loading