Skip to content

Commit

Permalink
feat: add at query param for staking-payouts (#1388)
Browse files Browse the repository at this point in the history
* feat: add `at` query param for staking-payouts

* add docs

* rewrite derive to fit historic data

* hack for tests

* add new mock data

* export mock data

* add single new test

* fix test

* update test to reflect validator and nominator

* add extract axposure test

* extractExposure for nom and val

* extractTotalValidatorRewardPoints

* error test

* another error test

* deriveNominatedExposures

* replace original file

* finalize tests
  • Loading branch information
TarikGul committed Feb 6, 2024
1 parent 1bd0cda commit a8b96ff
Show file tree
Hide file tree
Showing 11 changed files with 1,550 additions and 372 deletions.
2 changes: 1 addition & 1 deletion docs/dist/app.bundle.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions docs/src/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,15 @@ paths:
schema:
format: SS58
type: string
- name: at
in: query
description: Block at which to query staking payouts.
required: false
schema:
type: string
description: Block height (as a non-negative integer) or hash
(as a hex string).
format: unsignedInteger or $hex
- name: depth
in: query
description: The number of eras to query for payouts of. Must be less
Expand Down
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
Loading

0 comments on commit a8b96ff

Please sign in to comment.