Skip to content

Commit

Permalink
feat: add at query param for staking-payouts
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikGul committed Feb 1, 2024
1 parent cc80227 commit 552b6df
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
20 changes: 11 additions & 9 deletions src/controllers/accounts/AccountsStakingPayoutsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { ApiPromise } from '@polkadot/api';
import type { ApiPromise } from '@polkadot/api';
import type { ApiDecoration } from '@polkadot/api/types';
import { Option } from '@polkadot/types';
import BN from 'bn.js';
import { RequestHandler } from 'express';
Expand Down Expand Up @@ -95,10 +96,12 @@ export default class AccountsStakingPayoutsController extends AbstractController
* @param res Express Response
*/
private getStakingPayoutsByAccountId: RequestHandler<IAddressParam> = async (
{ params: { address }, query: { depth, era, unclaimedOnly } },
{ params: { address }, query: { depth, era, unclaimedOnly, at } },
res,
): Promise<void> => {
const { hash, eraArg, currentEra } = await this.getEraAndHash(this.verifyAndCastOr('era', era, undefined));
const hash = await this.getHashFromAt(at);
const apiAt = await this.api.at(hash);
const { eraArg, currentEra } = await this.getEraAndHash(apiAt, this.verifyAndCastOr('era', era, undefined));

const unclaimedOnlyArg = unclaimedOnly === 'false' ? false : true;

Expand All @@ -111,15 +114,15 @@ export default class AccountsStakingPayoutsController extends AbstractController
eraArg,
unclaimedOnlyArg,
currentEra,
apiAt,
),
);
};

private async getEraAndHash(era?: number) {
const [hash, activeEraOption, currentEraMaybeOption] = await Promise.all([
this.api.rpc.chain.getFinalizedHead(),
this.api.query.staking.activeEra(),
this.api.query.staking.currentEra(),
private async getEraAndHash(apiAt: ApiDecoration<'promise'>, era?: number) {
const [activeEraOption, currentEraMaybeOption] = await Promise.all([
apiAt.query.staking.activeEra(),
apiAt.query.staking.currentEra(),
]);

if (activeEraOption.isNone) {
Expand Down Expand Up @@ -148,7 +151,6 @@ export default class AccountsStakingPayoutsController extends AbstractController
}

return {
hash,
eraArg: era === undefined ? activeEra - 1 : era,
currentEra,
};
Expand Down
32 changes: 28 additions & 4 deletions src/services/accounts/AccountsStakingPayoutsService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,29 @@ describe('AccountsStakingPayoutsService', () => {

describe('Correct succesfull responses', () => {
it('Should work when ApiPromise works', async () => {
const res = await stakingPayoutsService.fetchAccountStakingPayout(blockHash, nominator, 1, 533, true, 534);
const res = await stakingPayoutsService.fetchAccountStakingPayout(
blockHash,
nominator,
1,
533,
true,
534,
mockHistoricApi,
);

expect(sanitizeNumbers(res)).toStrictEqual(stakingPayoutsResponse);
});

it('Should work when unclaimed is false', async () => {
const res = await stakingPayoutsService.fetchAccountStakingPayout(blockHash, nominator, 1, 533, false, 534);
const res = await stakingPayoutsService.fetchAccountStakingPayout(
blockHash,
nominator,
1,
533,
false,
534,
mockHistoricApi,
);

expect(sanitizeNumbers(res)).toStrictEqual(stakingPayoutsResponse);
});
Expand Down Expand Up @@ -207,7 +223,15 @@ describe('AccountsStakingPayoutsService', () => {
describe('Correct errors', () => {
it('Should throw an error when the depth is greater than the historyDepth', () => {
const serviceCall = async () => {
await stakingPayoutsService.fetchAccountStakingPayout(blockHash, nominator, 85, 533, true, 534);
await stakingPayoutsService.fetchAccountStakingPayout(
blockHash,
nominator,
85,
533,
true,
534,
mockHistoricApi,
);
};

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Expand All @@ -216,7 +240,7 @@ describe('AccountsStakingPayoutsService', () => {

it('Should throw an error inputted era and historydepth is invalid', () => {
const serviceCall = async () => {
await stakingPayoutsService.fetchAccountStakingPayout(blockHash, nominator, 1, 400, true, 534);
await stakingPayoutsService.fetchAccountStakingPayout(blockHash, nominator, 1, 400, true, 534, mockHistoricApi);
};

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Expand Down
3 changes: 1 addition & 2 deletions src/services/accounts/AccountsStakingPayoutsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ export class AccountsStakingPayoutsService extends AbstractService {
era: number,
unclaimedOnly: boolean,
currentEra: number,
historicApi: ApiDecoration<'promise'>,
): Promise<IAccountStakingPayouts> {
const { api } = this;
const historicApi = await api.at(hash);

const { number } = await api.rpc.chain.getHeader(hash);

/**
Expand Down

0 comments on commit 552b6df

Please sign in to comment.