Skip to content

Commit 6b97212

Browse files
committed
Calculate available rewards using contract instead of events
Rewards are cumulative, so anything that has already been claimed needs to be subtracted to get the current available amount. This commit gets the claimed amount by calling `cumulativeClaimed` on the merkle contract. This is more reliable than scanning for claimed events.
1 parent a6f087e commit 6b97212

File tree

3 files changed

+52
-41
lines changed

3 files changed

+52
-41
lines changed

src/hooks/useFetchStakingRewards.ts

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { setInterimRewards } from "../store/rewards"
1212
import { selectStakingProviders } from "../store/staking"
1313
import { BigNumber } from "ethers"
1414
import { Zero } from "@ethersproject/constants"
15+
import { useMulticall } from "../web3/hooks/useMulticall"
16+
import { ContractCall } from "../threshold-ts/multicall"
1517

1618
interface 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
}

src/web3/hooks/useClaimMerkleRewardsTransaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const useClaimMerkleRewardsTransaction = (
3636

3737
// TODO:
3838
// - This only signals no Merkle rewards, but there may be TACo rewards
39-
// - We can still call the new Merkle contract with a claim with an empty
39+
// - We can still call the new Merkle contract with a claim with an empty
4040
// merkle proof, signalling to not try to claim Merkle rewards. This
4141
// will still try to claim TACo rewards automatically.
4242
if (availableRewardsToClaim.length === 0) {

src/web3/hooks/useMerkleDropContract.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const CONTRACT_ADDRESSESS = {
1212
// https://etherscan.io/address/0xea7ca290c7811d1cc2e79f8d706bd05d8280bd37
1313
[ChainID.Ethereum.valueOf().toString()]:
1414
"0xeA7CA290c7811d1cC2e79f8d706bD05d8280BD37",
15-
// https://sepolia.etherscan.io/address/0x4621a14bbB5a53f79Ea532bdc032b8ACc383B153
15+
// https://sepolia.etherscan.io/address/0xBF807283ef74616065A5595ACa49b25A569A33c6
1616
[ChainID.Sepolia.valueOf().toString()]:
17-
"0x4621a14bbB5a53f79Ea532bdc032b8ACc383B153",
17+
"0xBF807283ef74616065A5595ACa49b25A569A33c6",
1818
// TODO: Set local address- how to resolve it in local network?
1919
[ChainID.Localhost.valueOf().toString()]: AddressZero,
2020
} as Record<string, string>

0 commit comments

Comments
 (0)