Skip to content

Commit

Permalink
fix(StakingInfo): add historicApi to staking-info (#741)
Browse files Browse the repository at this point in the history
* add historicApi to staking-info

* lint
  • Loading branch information
TarikGul committed Nov 1, 2021
1 parent 0ec6473 commit bb679ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
25 changes: 15 additions & 10 deletions src/services/accounts/AccountsStakingInfoService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { ApiPromise } from '@polkadot/api';
import { ApiDecoration } from '@polkadot/api/types';
import { Option } from '@polkadot/types';
import { AccountId, Hash, StakingLedger } from '@polkadot/types/interfaces';
import { BadRequest, InternalServerError } from 'http-errors';
Expand Down Expand Up @@ -43,16 +44,20 @@ const payeeAt = (_hash: Hash, _address: string) =>
const slashingSpansAt = (_hash: Hash, _address: string) =>
Promise.resolve().then(() => polkadotRegistry.createType('SlashingSpans'));

const mockApi = {
...defaultMockApi,
const historicApi = {
query: {
staking: {
bonded: { at: bondedAt },
ledger: { at: ledgerAt },
payee: { at: payeeAt },
slashingSpans: { at: slashingSpansAt },
bonded: bondedAt,
ledger: ledgerAt,
payee: payeeAt,
slashingSpans: slashingSpansAt,
},
},
} as unknown as ApiDecoration<'promise'>;

const mockApi = {
...defaultMockApi,
at: (_hash: Hash) => historicApi,
} as unknown as ApiPromise;

const accountStakingInfoService = new AccountsStakingInfoService(mockApi);
Expand All @@ -71,7 +76,7 @@ describe('AccountsStakingInfoService', () => {
});

it('throws a 400 when the given address is not a stash', async () => {
(mockApi.query.staking.bonded as any).at = () =>
(historicApi.query.staking.bonded as any) = () =>
Promise.resolve().then(() =>
polkadotRegistry.createType('Option<AccountId>', null)
);
Expand All @@ -85,11 +90,11 @@ describe('AccountsStakingInfoService', () => {
new BadRequest('The address NotStash is not a stash address.')
);

(mockApi.query.staking.bonded as any).at = bondedAt;
(historicApi.query.staking.bonded as any) = bondedAt;
});

it('throws a 404 when the staking ledger cannot be found', async () => {
(mockApi.query.staking.ledger as any).at = () =>
(historicApi.query.staking.ledger as any) = () =>
Promise.resolve().then(() =>
polkadotRegistry.createType('Option<StakingLedger>', null)
);
Expand All @@ -105,7 +110,7 @@ describe('AccountsStakingInfoService', () => {
)
);

(mockApi.query.staking.ledger as any).at = ledgerAt;
(historicApi.query.staking.ledger as any) = ledgerAt;
});
});
});
9 changes: 5 additions & 4 deletions src/services/accounts/AccountsStakingInfoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ export class AccountsStakingInfoService extends AbstractService {
stash: string
): Promise<IAccountStakingInfo> {
const { api } = this;
const historicApi = await api.at(hash);

const [header, controllerOption] = await Promise.all([
api.rpc.chain.getHeader(hash),
api.query.staking.bonded.at(hash, stash), // Option<AccountId> representing the controller
historicApi.query.staking.bonded(stash), // Option<AccountId> representing the controller
]);

const at = {
Expand All @@ -35,9 +36,9 @@ export class AccountsStakingInfoService extends AbstractService {

const [stakingLedgerOption, rewardDestination, slashingSpansOption] =
await Promise.all([
api.query.staking.ledger.at(hash, controller),
api.query.staking.payee.at(hash, stash),
api.query.staking.slashingSpans.at(hash, stash),
historicApi.query.staking.ledger(controller),
historicApi.query.staking.payee(stash),
historicApi.query.staking.slashingSpans(stash),
]);

const stakingLedger = stakingLedgerOption.unwrapOr(null);
Expand Down

0 comments on commit bb679ed

Please sign in to comment.