From 83ba6ec0508467d8cd1defb9090400f7ea3ffe7c Mon Sep 17 00:00:00 2001 From: "johnnyji.dev" Date: Wed, 6 Mar 2024 09:42:06 +0000 Subject: [PATCH 01/13] add some api(claimedReward, UsedFee). - After completing transaction, need to retrieve a final claimed reward and used fee. --- .../dapp-staking/pending-rewards/index.ts | 91 ++++++++++++++++++- packages/sdk-core/src/modules/units/index.ts | 18 ++++ 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts index d9b0a327..142e737a 100644 --- a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts +++ b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts @@ -3,7 +3,7 @@ import { Perbill } from '@polkadot/types/interfaces'; import { ApiPromise } from '@polkadot/api'; import { ethers } from 'ethers'; import { Codec } from '@polkadot/types/types'; -import { hasProperty, truncate } from '@astar-network/astar-sdk-core'; +import { commaStrToBigInt, hasProperty, truncate } from '@astar-network/astar-sdk-core'; export interface RewardDistributionConfig extends Struct { readonly baseTreasuryPercent: Perbill; @@ -251,3 +251,92 @@ export const estimatePendingRewards = async ({ api, return { stakerPendingRewards: 0 }; } }; + + + + +/** + * Memo: + * This method returns claimed reward amount by stakerAddress and blockHeight that have the reward event. + * After StkingV3, Two kinds of rewards exist. + * - Staker Reward : When "Build&Earn" comes after the "Voting", stakers can get the reward by staking on dApps. + * - Bonus Reward : During the "Voting" subperiod makes the staker eligible for bonus rewards. + */ +export const claimedReward = async ( + { + api, staker, height + } : + { + api: ApiPromise, staker: string, height: BigInt + }): 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 = (await apiAt.query.system.events()).toArray(); + + let claimedReward = BigInt('0'); + const method = 'Reward'; + const section = 'dappStaking'; + + allRecords.map((e) => { + if (e.toHuman()?.event?.data?.account == staker && + e.toHuman()?.event?.method == method && + e.toHuman()?.event?.section == section) { + let tmpAmount = e.toHuman().event?.data?.amount; + let tmpAmountBigInt = commaStrToBigInt(tmpAmount); + + claimedReward += tmpAmountBigInt; + + } else { + claimedReward = claimedReward; + } + }) + + return { claimedRewards: Number(claimedReward) }; + + } catch (error) { + console.log(error); + throw error; + } +} + +/** + * Memo: + * This method returns usedfee amount by Address and blockHeight that have the actualFee. + */ +export const UsedFee = async ( + { + api, address, height + } : + { + api: ApiPromise, address: string, height: BigInt + }): 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 = (await apiAt.query.system.events()).toArray(); + + let usedFee = BigInt('0'); + allRecords.map((e) => { + if (e.toHuman()?.event?.data?.who == address) { + let tmpUsedFee = e.toHuman().event?.data?.actualFee; + let tmpUsedFeeBigInt = commaStrToBigInt(tmpUsedFee); + + usedFee = tmpUsedFeeBigInt; + + } else { + usedFee = usedFee; + } + }) + + return { usedFee: Number(usedFee) }; + + } catch (error) { + console.log(error); + throw error; + } +} \ No newline at end of file diff --git a/packages/sdk-core/src/modules/units/index.ts b/packages/sdk-core/src/modules/units/index.ts index b798aebe..f216917b 100644 --- a/packages/sdk-core/src/modules/units/index.ts +++ b/packages/sdk-core/src/modules/units/index.ts @@ -80,3 +80,21 @@ export const formatNumber = (value: number, digits: number): string => { return item ? (value / item.value).toFixed(digits).replace(rx, '$1') + item.symbol : '0'; }; + + +export function commaStrToBigInt(_str: string) { + + let _strToStr = String(_str); + + let _noCommaStr = _strToStr.replace(/,/g, ''); + + let rlt = BigInt("0"); + + if (_noCommaStr === "undefined") { + console.log(' _noCommaStr :undefined '); + } else { + rlt = BigInt(_noCommaStr); + } + + return rlt; +} \ No newline at end of file From af8b38f04c5899766e44dd0dc0374087748ff17d Mon Sep 17 00:00:00 2001 From: "johnnyji.dev" Date: Thu, 7 Mar 2024 14:55:34 +0000 Subject: [PATCH 02/13] update claimedReward & usedFee param (address to extrinsicHash) --- .../dapp-staking/pending-rewards/index.ts | 174 +++++++++++------- 1 file changed, 104 insertions(+), 70 deletions(-) diff --git a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts index 142e737a..978b01b0 100644 --- a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts +++ b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts @@ -85,10 +85,10 @@ const getClaimableEraRange = (era: number, currentEra: number): number[] => { const formatGeneralStakerInfo = ({ eraTvls, currentEra, generalStakerInfo }: { - currentEra: number; - eraTvls: EraTvl[]; - generalStakerInfo: [StorageKey, Codec][]; -}): { stakerInfo: StakerInfo[]; stakedEras: number[] } => { + currentEra: number; + eraTvls: EraTvl[]; + generalStakerInfo: [StorageKey, Codec][]; + }): { stakerInfo: StakerInfo[]; stakedEras: number[] } => { const stakerInfo: StakerInfo[] = []; const stakedEras: number[] = []; @@ -125,12 +125,12 @@ const estimateEraTokenIssuances = async ({ blockHeight, stakedEras, currentEra, blocksPerEra }: { - blockHeight: number; - api: ApiPromise; - stakedEras: number[]; - currentEra: number; - blocksPerEra: number; -}): Promise => { + blockHeight: number; + api: ApiPromise; + stakedEras: number[]; + currentEra: number; + blocksPerEra: number; + }): Promise => { const eraTokenIssuances: { era: number; eraTokenIssuance: number }[] = []; const block7EraAgo = blockHeight - blocksPerEra * 7; const [hash, currentIssuance] = await Promise.all([ @@ -169,12 +169,12 @@ const formatStakerPendingRewards = ({ stakerInfo, eraTokenIssuances, eraRewards, rewardsDistributionConfig }: { - stakerInfo: StakerInfo[]; - eraTvls: EraTvl[]; - eraTokenIssuances: EraTokenIssuances[]; - eraRewards: number; - rewardsDistributionConfig: DistributionConfig; -}) => { + stakerInfo: StakerInfo[]; + eraTvls: EraTvl[]; + eraTokenIssuances: EraTokenIssuances[]; + eraRewards: number; + rewardsDistributionConfig: DistributionConfig; + }) => { return stakerInfo.map((it) => { const totalStaked = eraTvls[it.era].tvlLocked; const { baseStakerPercent, adjustablePercent, idealDappsStakingTvl } = rewardsDistributionConfig; @@ -199,9 +199,9 @@ const formatStakerPendingRewards = ({ stakerInfo, // In other words, as the number of unclaimed eras increases, the difference increases (but it shouldn't be too far away). export const estimatePendingRewards = async ({ api, walletAddress }: { - api: ApiPromise; - walletAddress: string; -}): Promise<{ stakerPendingRewards: number }> => { + api: ApiPromise; + walletAddress: string; + }): Promise<{ stakerPendingRewards: number }> => { try { const [eraInfo, generalStakerInfo, blockHeight, blocksPerEra, rawBlockRewards, rewardsDistributionConfig] = await Promise.all([ @@ -257,86 +257,120 @@ export const estimatePendingRewards = async ({ api, /** * Memo: - * This method returns claimed reward amount by stakerAddress and blockHeight that have the reward event. + * This method returns claimed reward amount by extrinsicHash and blockHeight that have the reward event. * After StkingV3, Two kinds of rewards exist. * - Staker Reward : When "Build&Earn" comes after the "Voting", stakers can get the reward by staking on dApps. * - Bonus Reward : During the "Voting" subperiod makes the staker eligible for bonus rewards. */ export const claimedReward = async ( { - api, staker, height - } : - { - api: ApiPromise, staker: string, height: BigInt - }): Promise<{claimedRewards: number}> => { + 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 = (await apiAt.query.system.events()).toArray(); + 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(); + + // 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) => + record.phase.isApplyExtrinsic && + record.phase.asApplyExtrinsic.eq(extrinsicIndex) + ); - let claimedReward = BigInt('0'); - const method = 'Reward'; - const section = 'dappStaking'; + const method = 'Reward'; + const section = 'dappStaking'; + let claimedReward = BigInt('0'); - allRecords.map((e) => { - if (e.toHuman()?.event?.data?.account == staker && - e.toHuman()?.event?.method == method && - e.toHuman()?.event?.section == section) { - let tmpAmount = e.toHuman().event?.data?.amount; - let tmpAmountBigInt = commaStrToBigInt(tmpAmount); + extrinsicEvents.map((e) => { + if (e.toHuman()?.event?.method == method && + e.toHuman()?.event?.section == section) { + let tmpAmount = e.toHuman().event?.data?.amount; + let tmpAmountBigInt = commaStrToBigInt(tmpAmount); - claimedReward += tmpAmountBigInt; + claimedReward += tmpAmountBigInt; - } else { - claimedReward = claimedReward; - } - }) + } else { + claimedReward = claimedReward; + } + }) - return { claimedRewards: Number(claimedReward) }; + return { claimedRewards: Number(claimedReward) }; } catch (error) { - console.log(error); - throw error; + console.log(error); + throw error; } } /** * Memo: - * This method returns usedfee amount by Address and blockHeight that have the actualFee. + * This method returns usedfee amount by extrinsicHash and blockHeight that have the actualFee. */ export const UsedFee = async ( { - api, address, height - } : - { - api: ApiPromise, address: string, height: BigInt - }): Promise<{usedFee: number}> => { + 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 = (await apiAt.query.system.events()).toArray(); - - let usedFee = BigInt('0'); - allRecords.map((e) => { - if (e.toHuman()?.event?.data?.who == address) { - let tmpUsedFee = e.toHuman().event?.data?.actualFee; - let tmpUsedFeeBigInt = commaStrToBigInt(tmpUsedFee); - - usedFee = tmpUsedFeeBigInt; - - } else { - usedFee = usedFee; - } + 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(); + + // 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) => + record.phase.isApplyExtrinsic && + record.phase.asApplyExtrinsic.eq(extrinsicIndex) + ); + + const method = 'TransactionFeePaid'; + const section = 'transactionPayment'; + + let usedFee = BigInt('0'); + + extrinsicEvents.map((e) => { + if (e.toHuman()?.event?.method == method && + e.toHuman()?.event?.section == section) { + let tmpUsedFee = e.toHuman().event?.data?.actualFee; + let tmpUsedFeeBigInt = commaStrToBigInt(tmpUsedFee); + + usedFee = tmpUsedFeeBigInt; + + } else { + usedFee = usedFee; + } }) return { usedFee: Number(usedFee) }; } catch (error) { - console.log(error); - throw error; + console.log(error); + throw error; } } \ No newline at end of file From 77dc1cec6fb9c32a32c7820c0aacc1cecdb08e24 Mon Sep 17 00:00:00 2001 From: "johnnyji.dev" Date: Wed, 29 May 2024 00:52:00 +0000 Subject: [PATCH 03/13] update claimedReward(Claim_staker_rewards & Claim_bonus_reward) --- .../src/modules/dapp-staking/pending-rewards/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts index 978b01b0..b313c4b2 100644 --- a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts +++ b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts @@ -291,12 +291,14 @@ export const claimedReward = async ( record.phase.asApplyExtrinsic.eq(extrinsicIndex) ); - const method = 'Reward'; + const methodRwd = 'Reward'; + const methodBns = 'BonusReward'; const section = 'dappStaking'; let claimedReward = BigInt('0'); - extrinsicEvents.map((e) => { - if (e.toHuman()?.event?.method == method && + extrinsicEvents.map((e, idx) => { + console.log(e.toHuman(), idx); + if ((e.toHuman()?.event?.method == methodRwd || e.toHuman()?.event?.method == methodBns) && e.toHuman()?.event?.section == section) { let tmpAmount = e.toHuman().event?.data?.amount; let tmpAmountBigInt = commaStrToBigInt(tmpAmount); From 3f0e8e5daf7c00c118a6f2211f806b7853593323 Mon Sep 17 00:00:00 2001 From: "johnnyji.dev" Date: Sun, 23 Jun 2024 07:57:51 +0000 Subject: [PATCH 04/13] fix: version change 0.2.8 => 0.3.0 on package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 05bb4c42..139436ec 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "url": "https://github.com/AstarNetwork/astar.js.git" }, "sideEffects": false, - "version": "0.2.8", + "version": "0.3.0", "workspaces": [ "packages/*", "examples/*" From b44464da39dc143df93807a92e314b142afe2d96 Mon Sep 17 00:00:00 2001 From: "johnnyji.dev" Date: Mon, 8 Jul 2024 23:24:12 +0900 Subject: [PATCH 05/13] fix : yarn lint - modification --- .../dapp-staking/pending-rewards/index.ts | 93 ++++++++----------- 1 file changed, 38 insertions(+), 55 deletions(-) diff --git a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts index b313c4b2..8cb1dc84 100644 --- a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts +++ b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts @@ -1,6 +1,6 @@ import { StorageKey, Struct } from '@polkadot/types'; import { Perbill } from '@polkadot/types/interfaces'; -import { ApiPromise } from '@polkadot/api'; +import { ApiPromise, HttpProvider } from '@polkadot/api'; import { ethers } from 'ethers'; import { Codec } from '@polkadot/types/types'; import { commaStrToBigInt, hasProperty, truncate } from '@astar-network/astar-sdk-core'; @@ -85,10 +85,10 @@ const getClaimableEraRange = (era: number, currentEra: number): number[] => { const formatGeneralStakerInfo = ({ eraTvls, currentEra, generalStakerInfo }: { - currentEra: number; - eraTvls: EraTvl[]; - generalStakerInfo: [StorageKey, Codec][]; - }): { stakerInfo: StakerInfo[]; stakedEras: number[] } => { + currentEra: number; + eraTvls: EraTvl[]; + generalStakerInfo: [StorageKey, Codec][]; +}): { stakerInfo: StakerInfo[]; stakedEras: number[] } => { const stakerInfo: StakerInfo[] = []; const stakedEras: number[] = []; @@ -125,12 +125,12 @@ const estimateEraTokenIssuances = async ({ blockHeight, stakedEras, currentEra, blocksPerEra }: { - blockHeight: number; - api: ApiPromise; - stakedEras: number[]; - currentEra: number; - blocksPerEra: number; - }): Promise => { + blockHeight: number; + api: ApiPromise; + stakedEras: number[]; + currentEra: number; + blocksPerEra: number; +}): Promise => { const eraTokenIssuances: { era: number; eraTokenIssuance: number }[] = []; const block7EraAgo = blockHeight - blocksPerEra * 7; const [hash, currentIssuance] = await Promise.all([ @@ -169,12 +169,12 @@ const formatStakerPendingRewards = ({ stakerInfo, eraTokenIssuances, eraRewards, rewardsDistributionConfig }: { - stakerInfo: StakerInfo[]; - eraTvls: EraTvl[]; - eraTokenIssuances: EraTokenIssuances[]; - eraRewards: number; - rewardsDistributionConfig: DistributionConfig; - }) => { + stakerInfo: StakerInfo[]; + eraTvls: EraTvl[]; + eraTokenIssuances: EraTokenIssuances[]; + eraRewards: number; + rewardsDistributionConfig: DistributionConfig; +}) => { return stakerInfo.map((it) => { const totalStaked = eraTvls[it.era].tvlLocked; const { baseStakerPercent, adjustablePercent, idealDappsStakingTvl } = rewardsDistributionConfig; @@ -199,9 +199,9 @@ const formatStakerPendingRewards = ({ stakerInfo, // In other words, as the number of unclaimed eras increases, the difference increases (but it shouldn't be too far away). export const estimatePendingRewards = async ({ api, walletAddress }: { - api: ApiPromise; - walletAddress: string; - }): Promise<{ stakerPendingRewards: number }> => { + api: ApiPromise; + walletAddress: string; +}): Promise<{ stakerPendingRewards: number }> => { try { const [eraInfo, generalStakerInfo, blockHeight, blocksPerEra, rawBlockRewards, rewardsDistributionConfig] = await Promise.all([ @@ -252,9 +252,6 @@ export const estimatePendingRewards = async ({ api, } }; - - - /** * Memo: * This method returns claimed reward amount by extrinsicHash and blockHeight that have the reward event. @@ -263,14 +260,11 @@ export const estimatePendingRewards = async ({ api, * - Bonus Reward : During the "Voting" subperiod makes the staker eligible for bonus rewards. */ export const claimedReward = async ( + { api, extrinsicHash, height }: { - api, extrinsicHash, height - }: - { - api: ApiPromise, extrinsicHash: string, height: number - }): Promise<{ claimedRewards: number }> => { + 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); @@ -298,39 +292,32 @@ export const claimedReward = async ( extrinsicEvents.map((e, idx) => { console.log(e.toHuman(), idx); - if ((e.toHuman()?.event?.method == methodRwd || e.toHuman()?.event?.method == methodBns) && - e.toHuman()?.event?.section == section) { - let tmpAmount = e.toHuman().event?.data?.amount; - let tmpAmountBigInt = commaStrToBigInt(tmpAmount); + if ((e.toHuman()?.event?.method === methodRwd || e.toHuman()?.event?.method === methodBns) && + e.toHuman()?.event?.section === section) { + const tmpAmount = e.toHuman().event?.data?.amount; + const tmpAmountBigInt = commaStrToBigInt(tmpAmount); claimedReward += tmpAmountBigInt; - - } else { - claimedReward = claimedReward; } - }) + }); 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 UsedFee = async ( + { api, extrinsicHash, height }: { - api, extrinsicHash, height - }: - { - api: ApiPromise, extrinsicHash: string, height: number - }): Promise<{ usedFee: number }> => { + 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); @@ -357,22 +344,18 @@ export const UsedFee = async ( let usedFee = BigInt('0'); extrinsicEvents.map((e) => { - if (e.toHuman()?.event?.method == method && - e.toHuman()?.event?.section == section) { - let tmpUsedFee = e.toHuman().event?.data?.actualFee; - let tmpUsedFeeBigInt = commaStrToBigInt(tmpUsedFee); + if (e.toHuman()?.event?.method === method && + e.toHuman()?.event?.section === section) { + const tmpUsedFee = e.toHuman().event?.data?.actualFee; + const tmpUsedFeeBigInt = commaStrToBigInt(tmpUsedFee); usedFee = tmpUsedFeeBigInt; - - } else { - usedFee = usedFee; } - }) + }); return { usedFee: Number(usedFee) }; - } catch (error) { console.log(error); throw error; } -} \ No newline at end of file +}; From 8b229d503aa57b78db53742446c39041a3246ba0 Mon Sep 17 00:00:00 2001 From: "johnnyji.dev" Date: Mon, 8 Jul 2024 23:45:49 +0900 Subject: [PATCH 06/13] fix : remove toHuman() & commaStrToBigInt() --- .../dapp-staking/pending-rewards/index.ts | 19 ++++++------------- packages/sdk-core/src/modules/units/index.ts | 18 ------------------ 2 files changed, 6 insertions(+), 31 deletions(-) diff --git a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts index 8cb1dc84..24479ded 100644 --- a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts +++ b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts @@ -291,13 +291,9 @@ export const claimedReward = async ( let claimedReward = BigInt('0'); extrinsicEvents.map((e, idx) => { - console.log(e.toHuman(), idx); - if ((e.toHuman()?.event?.method === methodRwd || e.toHuman()?.event?.method === methodBns) && - e.toHuman()?.event?.section === section) { - const tmpAmount = e.toHuman().event?.data?.amount; - const tmpAmountBigInt = commaStrToBigInt(tmpAmount); - - claimedReward += tmpAmountBigInt; + if ((e.event?.method === methodRwd || e.event?.method === methodBns) && + e.event?.section === section) { + claimedReward += e.event?.data?.amount; } }); @@ -344,12 +340,9 @@ export const UsedFee = async ( let usedFee = BigInt('0'); extrinsicEvents.map((e) => { - if (e.toHuman()?.event?.method === method && - e.toHuman()?.event?.section === section) { - const tmpUsedFee = e.toHuman().event?.data?.actualFee; - const tmpUsedFeeBigInt = commaStrToBigInt(tmpUsedFee); - - usedFee = tmpUsedFeeBigInt; + if (e.event?.method === method && + e.event?.section === section) { + usedFee = e.event?.data?.actualFee; } }); diff --git a/packages/sdk-core/src/modules/units/index.ts b/packages/sdk-core/src/modules/units/index.ts index f216917b..b798aebe 100644 --- a/packages/sdk-core/src/modules/units/index.ts +++ b/packages/sdk-core/src/modules/units/index.ts @@ -80,21 +80,3 @@ export const formatNumber = (value: number, digits: number): string => { return item ? (value / item.value).toFixed(digits).replace(rx, '$1') + item.symbol : '0'; }; - - -export function commaStrToBigInt(_str: string) { - - let _strToStr = String(_str); - - let _noCommaStr = _strToStr.replace(/,/g, ''); - - let rlt = BigInt("0"); - - if (_noCommaStr === "undefined") { - console.log(' _noCommaStr :undefined '); - } else { - rlt = BigInt(_noCommaStr); - } - - return rlt; -} \ No newline at end of file From 6a75c9aba4340b77979fe9fb59d43f641373fdd7 Mon Sep 17 00:00:00 2001 From: "johnnyji.dev" Date: Sun, 21 Jul 2024 17:13:03 +0900 Subject: [PATCH 07/13] fix : add DAppReward on claimedReward & update README.md --- README.md | 2 ++ .../modules/dapp-staking/pending-rewards/index.ts | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2a561de5..6b5ea4b7 100644 --- a/README.md +++ b/README.md @@ -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. `claimedReward`: Returns claimed reward amount by extrinsicHash and blockHeight that have the reward event. +62. `UsedFee`: Returns usedfee amount by extrinsicHash and blockHeight that have the actualFee. diff --git a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts index 24479ded..b1dfbb33 100644 --- a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts +++ b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts @@ -256,8 +256,11 @@ export const estimatePendingRewards = async ({ api, * 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 : When "Build&Earn" comes after the "Voting", stakers can get the reward by staking on dApps. - * - Bonus Reward : During the "Voting" subperiod makes the staker eligible for bonus rewards. + * - 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 claimedReward = async ( { api, extrinsicHash, height }: @@ -286,12 +289,13 @@ export const claimedReward = async ( ); const methodRwd = 'Reward'; - const methodBns = 'BonusReward'; + const methodBnsRwd = 'BonusReward'; + const methodDappRwd = 'DAppReward'; const section = 'dappStaking'; let claimedReward = BigInt('0'); extrinsicEvents.map((e, idx) => { - if ((e.event?.method === methodRwd || e.event?.method === methodBns) && + if ((e.event?.method === methodRwd || e.event?.method === methodBnsRwd || e.event?.method === methodDappRwd) && e.event?.section === section) { claimedReward += e.event?.data?.amount; } From 239e1622b0c59b4a7cfa3770e97ebef810cdd3d8 Mon Sep 17 00:00:00 2001 From: "johnnyji.dev" Date: Sun, 21 Jul 2024 17:15:25 +0900 Subject: [PATCH 08/13] fix : update UsedFee() to getUsedFee() --- .../sdk-core/src/modules/dapp-staking/pending-rewards/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts index b1dfbb33..5c5364d0 100644 --- a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts +++ b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts @@ -312,7 +312,7 @@ export const claimedReward = async ( * Memo: * This method returns usedfee amount by extrinsicHash and blockHeight that have the actualFee. */ -export const UsedFee = async ( +export const getUsedFee = async ( { api, extrinsicHash, height }: { api: ApiPromise, extrinsicHash: string, height: number From e0b1ce0986638cad0da1db54ea23fa309d604a97 Mon Sep 17 00:00:00 2001 From: "johnnyji.dev" Date: Sun, 21 Jul 2024 17:16:22 +0900 Subject: [PATCH 09/13] fix : update UsedFee() to getUsedFee() on README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6b5ea4b7..2895836c 100644 --- a/README.md +++ b/README.md @@ -260,4 +260,4 @@ Here is the list of functions and their brief explanations: 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. `claimedReward`: Returns claimed reward amount by extrinsicHash and blockHeight that have the reward event. -62. `UsedFee`: Returns usedfee amount by extrinsicHash and blockHeight that have the actualFee. +62. `getUsedFee`: Returns usedfee amount by extrinsicHash and blockHeight that have the actualFee. From 77c3d69db3fd27ba2236a9cf7ec51eebc5d14cc0 Mon Sep 17 00:00:00 2001 From: "johnnyji.dev" Date: Sun, 21 Jul 2024 17:19:20 +0900 Subject: [PATCH 10/13] fix : Update claimedReward to getClaimedReward --- README.md | 2 +- .../sdk-core/src/modules/dapp-staking/pending-rewards/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2895836c..7f4ee8bd 100644 --- a/README.md +++ b/README.md @@ -259,5 +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. `claimedReward`: Returns claimed reward amount by extrinsicHash and blockHeight that have the reward event. +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. diff --git a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts index 5c5364d0..49ecc85a 100644 --- a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts +++ b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts @@ -262,7 +262,7 @@ export const estimatePendingRewards = async ({ api, * During the "Voting" subperiod makes the staker eligible for bonus rewards. * - Dapp Reward : Dapp reward has been paid out to a beneficiary. */ -export const claimedReward = async ( +export const getClaimedReward = async ( { api, extrinsicHash, height }: { api: ApiPromise, extrinsicHash: string, height: number From 29a523f7763693f3867804eefdeb1772fe8a81b8 Mon Sep 17 00:00:00 2001 From: "johnnyji.dev" Date: Tue, 23 Jul 2024 00:00:52 +0900 Subject: [PATCH 11/13] fix : update build-error(yarn polkadot-exec-tsc --build tsconfig.build.json) --- .../dapp-staking/pending-rewards/index.ts | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts index 49ecc85a..ddd14b0e 100644 --- a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts +++ b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts @@ -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; @@ -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( @@ -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) ); @@ -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; } }); @@ -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(); // Find the extrinsic index by matching the extrinsic hash const extrinsicIndex = signedBlock.block.extrinsics.findIndex( @@ -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) ); @@ -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; } }); From 0c90c289d24f8754a62ed80f98d524de17ff87f2 Mon Sep 17 00:00:00 2001 From: "johnnyji.dev" Date: Mon, 29 Jul 2024 16:07:11 +0900 Subject: [PATCH 12/13] fix : apply > on system.events & delete any --- .../dapp-staking/pending-rewards/index.ts | 75 ++++++++++--------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts index ddd14b0e..4572d278 100644 --- a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts +++ b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts @@ -1,9 +1,11 @@ -import { StorageKey, Struct } from '@polkadot/types'; +import { StorageKey, Struct, Vec } from '@polkadot/types'; import { EventRecord, Perbill } from '@polkadot/types/interfaces'; import { ApiPromise } from '@polkadot/api'; import { ethers } from 'ethers'; import { Codec } from '@polkadot/types/types'; import { hasProperty, truncate } from '@astar-network/astar-sdk-core'; +// import { FrameSystemEventRecord } from '@polkadot/types/lookup'; +// import { FrameSystemEventRecord } from '@polkadot/types/interfaces/system' export interface RewardDistributionConfig extends Struct { readonly baseTreasuryPercent: Perbill; @@ -85,10 +87,10 @@ const getClaimableEraRange = (era: number, currentEra: number): number[] => { const formatGeneralStakerInfo = ({ eraTvls, currentEra, generalStakerInfo }: { - currentEra: number; - eraTvls: EraTvl[]; - generalStakerInfo: [StorageKey, Codec][]; -}): { stakerInfo: StakerInfo[]; stakedEras: number[] } => { + currentEra: number; + eraTvls: EraTvl[]; + generalStakerInfo: [StorageKey, Codec][]; + }): { stakerInfo: StakerInfo[]; stakedEras: number[] } => { const stakerInfo: StakerInfo[] = []; const stakedEras: number[] = []; @@ -125,12 +127,12 @@ const estimateEraTokenIssuances = async ({ blockHeight, stakedEras, currentEra, blocksPerEra }: { - blockHeight: number; - api: ApiPromise; - stakedEras: number[]; - currentEra: number; - blocksPerEra: number; -}): Promise => { + blockHeight: number; + api: ApiPromise; + stakedEras: number[]; + currentEra: number; + blocksPerEra: number; + }): Promise => { const eraTokenIssuances: { era: number; eraTokenIssuance: number }[] = []; const block7EraAgo = blockHeight - blocksPerEra * 7; const [hash, currentIssuance] = await Promise.all([ @@ -169,12 +171,12 @@ const formatStakerPendingRewards = ({ stakerInfo, eraTokenIssuances, eraRewards, rewardsDistributionConfig }: { - stakerInfo: StakerInfo[]; - eraTvls: EraTvl[]; - eraTokenIssuances: EraTokenIssuances[]; - eraRewards: number; - rewardsDistributionConfig: DistributionConfig; -}) => { + stakerInfo: StakerInfo[]; + eraTvls: EraTvl[]; + eraTokenIssuances: EraTokenIssuances[]; + eraRewards: number; + rewardsDistributionConfig: DistributionConfig; + }) => { return stakerInfo.map((it) => { const totalStaked = eraTvls[it.era].tvlLocked; const { baseStakerPercent, adjustablePercent, idealDappsStakingTvl } = rewardsDistributionConfig; @@ -199,9 +201,9 @@ const formatStakerPendingRewards = ({ stakerInfo, // In other words, as the number of unclaimed eras increases, the difference increases (but it shouldn't be too far away). export const estimatePendingRewards = async ({ api, walletAddress }: { - api: ApiPromise; - walletAddress: string; -}): Promise<{ stakerPendingRewards: number }> => { + api: ApiPromise; + walletAddress: string; + }): Promise<{ stakerPendingRewards: number }> => { try { const [eraInfo, generalStakerInfo, blockHeight, blocksPerEra, rawBlockRewards, rewardsDistributionConfig] = await Promise.all([ @@ -264,14 +266,15 @@ export const estimatePendingRewards = async ({ api, */ export const getClaimedReward = async ( { api, extrinsicHash, height }: - { - api: ApiPromise, extrinsicHash: string, height: number - }): Promise<{ claimedRewards: number }> => { + { + 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(); + const allRecords = await apiAt.query.system.events>(); + // Find the extrinsic index by matching the extrinsic hash const extrinsicIndex = signedBlock.block.extrinsics.findIndex( @@ -283,7 +286,7 @@ export const getClaimedReward = async ( } // Get events associated with the extrinsic - const extrinsicEvents = allRecords.filter((record: EventRecord) => + const extrinsicEvents = allRecords.filter((record) => record.phase.isApplyExtrinsic && record.phase.asApplyExtrinsic.eq(extrinsicIndex) ); @@ -294,11 +297,11 @@ export const getClaimedReward = async ( const section = 'dappStaking'; let claimedReward = BigInt('0'); - extrinsicEvents.map((e: EventRecord) => { + extrinsicEvents.map((e) => { 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; + const amountIdx = e.event?.data.length - 1; + claimedReward = claimedReward + BigInt(e.event?.data[amountIdx].toString()); } }); @@ -315,14 +318,14 @@ export const getClaimedReward = async ( */ export const getUsedFee = async ( { api, extrinsicHash, height }: - { - api: ApiPromise, extrinsicHash: string, height: number - }): Promise<{ usedFee: number }> => { + { + 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(); + const allRecords = await apiAt.query.system.events>(); // Find the extrinsic index by matching the extrinsic hash const extrinsicIndex = signedBlock.block.extrinsics.findIndex( @@ -334,7 +337,7 @@ export const getUsedFee = async ( } // Get events associated with the extrinsic - const extrinsicEvents = allRecords.filter((record: EventRecord) => + const extrinsicEvents = allRecords.filter((record) => record.phase.isApplyExtrinsic && record.phase.asApplyExtrinsic.eq(extrinsicIndex) ); @@ -344,10 +347,10 @@ export const getUsedFee = async ( let usedFee = BigInt('0'); - extrinsicEvents.map((e: EventRecord) => { + extrinsicEvents.map((e) => { if (e.event?.method === method && e.event?.section === section) { - const eData: any = e.event?.data; - usedFee += eData?.actualFee; + const actualFeeIdx = e.event?.data.length - 2; + usedFee = usedFee + BigInt(e.event?.data[actualFeeIdx].toString()); } }); From 49820fa7320198ac5a5b04bb5753e2f1f9551ab6 Mon Sep 17 00:00:00 2001 From: "johnnyji.dev" Date: Mon, 29 Jul 2024 16:14:22 +0900 Subject: [PATCH 13/13] fix : delete the unusing comments --- .../sdk-core/src/modules/dapp-staking/pending-rewards/index.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts index 4572d278..07c8f275 100644 --- a/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts +++ b/packages/sdk-core/src/modules/dapp-staking/pending-rewards/index.ts @@ -4,8 +4,6 @@ import { ApiPromise } from '@polkadot/api'; import { ethers } from 'ethers'; import { Codec } from '@polkadot/types/types'; import { hasProperty, truncate } from '@astar-network/astar-sdk-core'; -// import { FrameSystemEventRecord } from '@polkadot/types/lookup'; -// import { FrameSystemEventRecord } from '@polkadot/types/interfaces/system' export interface RewardDistributionConfig extends Struct { readonly baseTreasuryPercent: Perbill;