@@ -12,6 +12,8 @@ import { setInterimRewards } from "../store/rewards"
1212import { selectStakingProviders } from "../store/staking"
1313import { BigNumber } from "ethers"
1414import { Zero } from "@ethersproject/constants"
15+ import { useMulticall } from "../web3/hooks/useMulticall"
16+ import { ContractCall } from "../threshold-ts/multicall"
1517
1618interface StakingRewards {
1719 [ stakingProvider : string ] : string
@@ -25,6 +27,16 @@ export const useFetchStakingRewards = () => {
2527 )
2628 const dispatch = useDispatch ( )
2729
30+ const cumulativeClaimedCalls : ContractCall [ ] = stakingProviders . map (
31+ ( stakingProvider ) => ( {
32+ address : merkleDropContract ! . address ,
33+ interface : merkleDropContract ! . interface ! ,
34+ method : "cumulativeClaimed" ,
35+ args : [ stakingProvider ] ,
36+ } )
37+ )
38+ const fetchCumulativeClaims = useMulticall ( cumulativeClaimedCalls )
39+
2840 useEffect ( ( ) => {
2941 const fetch = async ( ) => {
3042 if (
@@ -43,56 +55,48 @@ export const useFetchStakingRewards = () => {
4355 // See https://github.com/threshold-network/token-dashboard/issues/765
4456 // - Note also that TACo rewards now accrue on each block. They can be
4557 // calculated via TACoApp.availableRewards(address _stakingProvider)
46- const claimedEvents = await getContractPastEvents ( merkleDropContract , {
47- eventName : "Claimed" ,
48- fromBlock : DEPLOYMENT_BLOCK ,
49- filterParams : [ stakingProviders ] ,
50- } )
5158
52- const claimedAmountToStakingProvider = claimedEvents . reduce (
53- (
54- reducer : { [ stakingProvider : string ] : string } ,
55- event
56- ) : { [ stakingProvider : string ] : string } => {
57- const stakingProvider = getAddress (
58- event . args ?. stakingProvider as string
59- )
60- const prevAmount = BigNumber . from ( reducer [ stakingProvider ] || Zero )
61- reducer [ stakingProvider ] = prevAmount
62- . add ( event . args ?. amount as string )
63- . toString ( )
64- return reducer
59+ const cumulativeClaimedResults = await fetchCumulativeClaims ( )
60+ console . log ( cumulativeClaimedResults )
61+
62+ const claimedAmountToStakingProvider = stakingProviders . reduce (
63+ ( acc , stakingProvider , index ) => {
64+ acc [ getAddress ( stakingProvider ) ] =
65+ cumulativeClaimedResults [ index ] . toString ( )
66+ return acc
6567 } ,
66- { }
68+ { } as { [ stakingProvider : string ] : string }
6769 )
70+ console . log ( claimedAmountToStakingProvider )
6871
69- const claimedRewardsInCurrentMerkleRoot = new Set (
70- claimedEvents
71- . filter ( ( _ ) => _ . args ?. merkleRoot === rewardsData . merkleRoot )
72- . map ( ( _ ) => getAddress ( _ . args ?. stakingProvider as string ) )
73- )
72+ // const claimedRewardsInCurrentMerkleRoot = new Set(
73+ // claimedEvents
74+ // .filter((_) => _.args?.merkleRoot === rewardsData.merkleRoot)
75+ // .map((_) => getAddress(_.args?.stakingProvider as string))
76+ // )
7477
7578 const stakingRewards : StakingRewards = { }
7679 for ( const stakingProvider of stakingProviders ) {
77- if (
78- ! rewardsData . claims . hasOwnProperty ( stakingProvider ) ||
79- claimedRewardsInCurrentMerkleRoot . has ( stakingProvider )
80- ) {
80+ if ( ! ( stakingProvider in ( rewardsData as RewardsJSONData ) . claims ) ) {
8181 // If the JSON file doesn't contain proofs for a given staking
82- // provider it means this staking provider has no Merkle rewards -
83- // we can skip this iteration.
84- // TODO: ^ But there's going to be TACo rewards
85-
86- // If the `Claimed` event exists with a current merkle
87- // root for a given staking provider it means that rewards have
88- // already been claimed - we can skip this iteration.
89- // TODO: ^ Same, there can be TACo rewards
82+ // provider it means this staking provider has no Merkle rewards
9083 continue
9184 }
92-
9385 const { amount } = ( rewardsData as RewardsJSONData ) . claims [
9486 stakingProvider
9587 ]
88+ const claimedAmount =
89+ claimedAmountToStakingProvider [ stakingProvider ] || "0"
90+ if ( BigNumber . from ( amount ) . eq ( BigNumber . from ( claimedAmount ) ) ) {
91+ // if the claimed amount is equal to the amount of rewards available, then skip
92+ continue
93+ }
94+ // TODO: ^ But there's going to be TACo rewards
95+
96+ // If the `Claimed` event exists with a current merkle
97+ // root for a given staking provider it means that rewards have
98+ // already been claimed - we can skip this iteration.
99+ // TODO: ^ Same, there can be TACo rewards
96100 const claimableAmount = BigNumber . from ( amount ) . sub (
97101 claimedAmountToStakingProvider [ stakingProvider ] || Zero
98102 )
@@ -108,5 +112,12 @@ export const useFetchStakingRewards = () => {
108112 }
109113
110114 fetch ( )
111- } , [ stakingProviders , merkleDropContract , hasFetched , isFetching , dispatch ] )
115+ } , [
116+ stakingProviders ,
117+ merkleDropContract ,
118+ hasFetched ,
119+ isFetching ,
120+ dispatch ,
121+ fetchCumulativeClaims ,
122+ ] )
112123}
0 commit comments