From 84d6f95d037e1135dab49d7685d978f2767cc2fe Mon Sep 17 00:00:00 2001 From: tiltom Date: Thu, 25 May 2023 21:15:46 +0200 Subject: [PATCH] Fix checkpoints calculations (#2545) --- .../hooks/useGetFeesEarnedClaimAmount.ts | 76 ++++++++++++------- .../hooks/useGetNextPositiveCheckpoint.ts | 20 +++-- 2 files changed, 61 insertions(+), 35 deletions(-) diff --git a/src/app/pages/RewardPage/hooks/useGetFeesEarnedClaimAmount.ts b/src/app/pages/RewardPage/hooks/useGetFeesEarnedClaimAmount.ts index a9ea1353b..cc5519df8 100644 --- a/src/app/pages/RewardPage/hooks/useGetFeesEarnedClaimAmount.ts +++ b/src/app/pages/RewardPage/hooks/useGetFeesEarnedClaimAmount.ts @@ -7,6 +7,7 @@ import { useSelector } from 'react-redux'; import { selectWalletProvider } from 'app/containers/WalletProvider/selectors'; import { calculateAssetValue } from 'utils/helpers'; import { bignumber } from 'mathjs'; +import { contractReader } from 'utils/sovryn/contract-reader'; export interface IEarnedFee { asset: Asset; contractAddress: string; @@ -51,37 +52,58 @@ export const useGetFeesEarnedClaimAmount = () => { }; }; -const defaultEarnedFees: IEarnedFee[] = [ - { - asset: Asset.RBTC, - contractAddress: getContract('RBTC_lending').address, - value: '0', - rbtcValue: 0, - }, - { - asset: Asset.SOV, - contractAddress: getContract('SOV_token').address, - value: '0', - rbtcValue: 0, - }, - { - asset: Asset.MYNT, - contractAddress: getContract('MYNT_token').address, - value: '0', - rbtcValue: 0, - }, - { - asset: Asset.ZUSD, - contractAddress: getContract('ZUSD_token').address, - value: '0', - rbtcValue: 0, - }, -]; - const useGetFeesEarned = () => { const address = useAccount(); const blockSync = useBlockSync(); const [loading, setLoading] = useState(false); + const [RBTCDummyAddress, setRBTCDummyAddress] = useState( + '0xeabd29be3c3187500df86a2613c6470e12f2d77d', + ); + + useEffect(() => { + const getRbtcDummyAddress = async () => { + contractReader + .call( + 'feeSharingProxy', + 'RBTC_DUMMY_ADDRESS_FOR_CHECKPOINT', + [], + ) + .then(result => setRBTCDummyAddress(result)); + }; + + getRbtcDummyAddress().then(); + }, []); + + const defaultEarnedFees: IEarnedFee[] = useMemo( + () => [ + { + asset: Asset.RBTC, + contractAddress: RBTCDummyAddress, + value: '0', + rbtcValue: 0, + }, + { + asset: Asset.SOV, + contractAddress: getContract('SOV_token').address, + value: '0', + rbtcValue: 0, + }, + { + asset: Asset.MYNT, + contractAddress: getContract('MYNT_token').address, + value: '0', + rbtcValue: 0, + }, + { + asset: Asset.ZUSD, + contractAddress: getContract('ZUSD_token').address, + value: '0', + rbtcValue: 0, + }, + ], + [RBTCDummyAddress], + ); + const [earnedFees, setEarnedFees] = useState(defaultEarnedFees); const feeSharingProxyContract = getContract('feeSharingProxy'); diff --git a/src/app/pages/RewardPage/hooks/useGetNextPositiveCheckpoint.ts b/src/app/pages/RewardPage/hooks/useGetNextPositiveCheckpoint.ts index b378c4a30..bad947ea5 100644 --- a/src/app/pages/RewardPage/hooks/useGetNextPositiveCheckpoint.ts +++ b/src/app/pages/RewardPage/hooks/useGetNextPositiveCheckpoint.ts @@ -18,12 +18,15 @@ export const useGetNextPositiveCheckpoint = ( const [userCheckpoint, setUserCheckpoint] = useState(); const updateNextPositiveCheckpoint = useCallback(async () => { - let index = 1; + const processedCheckpoints = await contractReader.call( + 'feeSharingProxy', + 'processedCheckpoints', + [address, contractAddress], + ); - while ( - totalTokenCheckpoints >= - MAX_NEXT_POSITIVE_CHECKPOINT * (index - 1) - ) { + let userNextUnprocessedCheckpoint = processedCheckpoints; + + while (userNextUnprocessedCheckpoint < totalTokenCheckpoints) { const { hasFees, checkpointNum, @@ -34,11 +37,13 @@ export const useGetNextPositiveCheckpoint = ( [ address, contractAddress, - MAX_NEXT_POSITIVE_CHECKPOINT * (index - 1), - MAX_NEXT_POSITIVE_CHECKPOINT * index, + userNextUnprocessedCheckpoint, + MAX_NEXT_POSITIVE_CHECKPOINT, ], ); + userNextUnprocessedCheckpoint = Number(checkpointNum); + if (!!hasFees) { return setUserCheckpoint({ checkpointNum, @@ -46,7 +51,6 @@ export const useGetNextPositiveCheckpoint = ( hasSkippedCheckpoints, }); } - index++; } setUserCheckpoint({