diff --git a/src/controllers/accounts/AccountsStakingPayoutsController.ts b/src/controllers/accounts/AccountsStakingPayoutsController.ts index bd35a1209..59abd2bcc 100644 --- a/src/controllers/accounts/AccountsStakingPayoutsController.ts +++ b/src/controllers/accounts/AccountsStakingPayoutsController.ts @@ -14,7 +14,8 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -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'; @@ -95,10 +96,12 @@ export default class AccountsStakingPayoutsController extends AbstractController * @param res Express Response */ private getStakingPayoutsByAccountId: RequestHandler = async ( - { params: { address }, query: { depth, era, unclaimedOnly } }, + { params: { address }, query: { depth, era, unclaimedOnly, at } }, res, ): Promise => { - 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; @@ -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) { @@ -148,7 +151,6 @@ export default class AccountsStakingPayoutsController extends AbstractController } return { - hash, eraArg: era === undefined ? activeEra - 1 : era, currentEra, }; diff --git a/src/services/accounts/AccountsStakingPayoutsService.spec.ts b/src/services/accounts/AccountsStakingPayoutsService.spec.ts index 42f5c3804..ed5ff5d83 100644 --- a/src/services/accounts/AccountsStakingPayoutsService.spec.ts +++ b/src/services/accounts/AccountsStakingPayoutsService.spec.ts @@ -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); }); @@ -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 @@ -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 diff --git a/src/services/accounts/AccountsStakingPayoutsService.ts b/src/services/accounts/AccountsStakingPayoutsService.ts index eccfc0f86..03494b0c5 100644 --- a/src/services/accounts/AccountsStakingPayoutsService.ts +++ b/src/services/accounts/AccountsStakingPayoutsService.ts @@ -70,10 +70,9 @@ export class AccountsStakingPayoutsService extends AbstractService { era: number, unclaimedOnly: boolean, currentEra: number, + historicApi: ApiDecoration<'promise'>, ): Promise { const { api } = this; - const historicApi = await api.at(hash); - const { number } = await api.rpc.chain.getHeader(hash); /**