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
Show file tree
Hide file tree
Changes from 12 commits
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,5 @@ Here is the list of functions and their brief explanations:
59. `getUnit`: Returns the corresponding unit from the `arrUnitPrefixes` array.
60. `nFormatter`: Returns a string representation of a number in a more readable format.
61. `formatNumber`: Formats a number by adding weight prefix and considering decimal places.
62. `getClaimedReward`: Returns claimed reward amount by extrinsicHash and blockHeight that have the reward event.
62. `getUsedFee`: Returns usedfee amount by extrinsicHash and blockHeight that have the actualFee.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"url": "https://github.com/AstarNetwork/astar.js.git"
},
"sideEffects": false,
"version": "0.2.9",
"version": "0.3.0",
"workspaces": [
"packages/*",
"examples/*"
Expand Down
108 changes: 107 additions & 1 deletion packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StorageKey, Struct } from '@polkadot/types';
import { Perbill } from '@polkadot/types/interfaces';
import { EventRecord, Perbill } from '@polkadot/types/interfaces';
import { ApiPromise } from '@polkadot/api';
import { ethers } from 'ethers';
import { Codec } from '@polkadot/types/types';
Expand Down Expand Up @@ -251,3 +251,109 @@ export const estimatePendingRewards = async ({ api,
return { stakerPendingRewards: 0 };
}
};

/**
* Memo:
* This method returns claimed reward amount by extrinsicHash and blockHeight that have the reward event.
* After StkingV3, Two kinds of rewards exist.
* - Staker Reward : Account has claimed some stake rewards.
* When "Build&Earn" comes after the "Voting", stakers can get the reward by staking on dApps.
* - Bonus Reward : Bonus reward has been paid out to a loyal staker.
* During the "Voting" subperiod makes the staker eligible for bonus rewards.
* - Dapp Reward : Dapp reward has been paid out to a beneficiary.
*/
export const getClaimedReward = async (
{ api, extrinsicHash, height }:
{
api: ApiPromise, extrinsicHash: string, height: number
}): Promise<{ claimedRewards: number }> => {
try {
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: any = await apiAt.query.system.events();

// Find the extrinsic index by matching the extrinsic hash
const extrinsicIndex = signedBlock.block.extrinsics.findIndex(
(ex) => ex.hash.toString() === extrinsicHash
);

if (extrinsicIndex === -1) {
throw new Error('Extrinsic not found in the block');
}

// Get events associated with the extrinsic
const extrinsicEvents = allRecords.filter((record: EventRecord) =>
record.phase.isApplyExtrinsic &&
record.phase.asApplyExtrinsic.eq(extrinsicIndex)
);

const methodRwd = 'Reward';
const methodBnsRwd = 'BonusReward';
const methodDappRwd = 'DAppReward';
const section = 'dappStaking';
let claimedReward = BigInt('0');

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

return { claimedRewards: Number(claimedReward) };
} catch (error) {
console.log(error);
throw error;
}
};

/**
* Memo:
* This method returns usedfee amount by extrinsicHash and blockHeight that have the actualFee.
*/
export const getUsedFee = async (
{ api, extrinsicHash, height }:
{
api: ApiPromise, extrinsicHash: string, height: number
}): Promise<{ usedFee: number }> => {
try {
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: 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(
(ex) => ex.hash.toString() === extrinsicHash
);

if (extrinsicIndex === -1) {
throw new Error('Extrinsic not found in the block');
}

// Get events associated with the extrinsic
const extrinsicEvents = allRecords.filter((record: EventRecord) =>
record.phase.isApplyExtrinsic &&
record.phase.asApplyExtrinsic.eq(extrinsicIndex)
);

const method = 'TransactionFeePaid';
const section = 'transactionPayment';

let usedFee = BigInt('0');

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

return { usedFee: Number(usedFee) };
} catch (error) {
console.log(error);
throw error;
}
};
Loading