Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Add final rewards+final used fee #75

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
27 changes: 14 additions & 13 deletions packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { StorageKey, Struct } from '@polkadot/types';
import { Perbill } from '@polkadot/types/interfaces';
import { ApiPromise, HttpProvider } from '@polkadot/api';
import { EventRecord, Perbill } from '@polkadot/types/interfaces';
import { ApiPromise } from '@polkadot/api';
import { ethers } from 'ethers';
import { Codec } from '@polkadot/types/types';
import { commaStrToBigInt, hasProperty, truncate } from '@astar-network/astar-sdk-core';
import { hasProperty, truncate } from '@astar-network/astar-sdk-core';

export interface RewardDistributionConfig extends Struct {
readonly baseTreasuryPercent: Perbill;
Expand Down Expand Up @@ -271,7 +271,7 @@ export const getClaimedReward = async (
const blockHash = await api.rpc.chain.getBlockHash(height);
const signedBlock = await api.rpc.chain.getBlock(blockHash);
const apiAt = await api.at(signedBlock.block.header.hash);
const allRecords = (await apiAt.query.system.events()).toArray();
const allRecords: any = await apiAt.query.system.events();

// Find the extrinsic index by matching the extrinsic hash
const extrinsicIndex = signedBlock.block.extrinsics.findIndex(
Expand All @@ -283,7 +283,7 @@ export const getClaimedReward = async (
}

// Get events associated with the extrinsic
const extrinsicEvents = allRecords.filter((record) =>
const extrinsicEvents = allRecords.filter((record: EventRecord) =>
record.phase.isApplyExtrinsic &&
record.phase.asApplyExtrinsic.eq(extrinsicIndex)
);
Expand All @@ -294,10 +294,11 @@ export const getClaimedReward = async (
const section = 'dappStaking';
let claimedReward = BigInt('0');

extrinsicEvents.map((e, idx) => {
extrinsicEvents.map((e: EventRecord) => {
if ((e.event?.method === methodRwd || e.event?.method === methodBnsRwd || e.event?.method === methodDappRwd) &&
e.event?.section === section) {
claimedReward += e.event?.data?.amount;
const eData: any = e.event?.data;
claimedReward += eData?.amount;
}
});

Expand All @@ -321,7 +322,7 @@ export const getUsedFee = async (
const blockHash = await api.rpc.chain.getBlockHash(height);
const signedBlock = await api.rpc.chain.getBlock(blockHash);
const apiAt = await api.at(signedBlock.block.header.hash);
const allRecords = (await apiAt.query.system.events()).toArray();
const allRecords: any = await apiAt.query.system.events();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use any Applicable to multiple locations within the code
https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bobo-k2
BTW, Property 'map' does not exist on type 'Codec'.
The type of 'allRecords' is Codec.
I tried to iterate the data by using 'filter' or 'map'. But that make build error(yarn polkadot-exec-tsc --build tsconfig.build.json).

How can I handle this? Can you suggest any reference docs?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codec is a base type from which all polkadot.js types are inherited. System.events are of type Vec<FrameSystemEventRecord> so you can change your line to

const allRecords = await api.query.system.events<Vec<FrameSystemEventRecord>>();

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When '<Vec>' applied, can not import the member like below error message.
packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts:7:10 - error TS2305: Module '"@polkadot/types/lookup"' has no exported member 'FrameSystemEventRecord'.

Alternative, I used the 'EventRecord' and got properties(actualFee, amount) by index.


// Find the extrinsic index by matching the extrinsic hash
const extrinsicIndex = signedBlock.block.extrinsics.findIndex(
Expand All @@ -333,7 +334,7 @@ export const getUsedFee = async (
}

// Get events associated with the extrinsic
const extrinsicEvents = allRecords.filter((record) =>
const extrinsicEvents = allRecords.filter((record: EventRecord) =>
record.phase.isApplyExtrinsic &&
record.phase.asApplyExtrinsic.eq(extrinsicIndex)
);
Expand All @@ -343,10 +344,10 @@ export const getUsedFee = async (

let usedFee = BigInt('0');

extrinsicEvents.map((e) => {
if (e.event?.method === method &&
e.event?.section === section) {
usedFee = e.event?.data?.actualFee;
extrinsicEvents.map((e: EventRecord) => {
if (e.event?.method === method && e.event?.section === section) {
const eData: any = e.event?.data;
usedFee += eData?.actualFee;
}
});

Expand Down
Loading