Skip to content

Commit

Permalink
Fix checkpoints calculations (#2545)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiltom authored May 25, 2023
1 parent 02a93b3 commit 84d6f95
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 35 deletions.
76 changes: 49 additions & 27 deletions src/app/pages/RewardPage/hooks/useGetFeesEarnedClaimAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string>(
'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');

Expand Down
20 changes: 12 additions & 8 deletions src/app/pages/RewardPage/hooks/useGetNextPositiveCheckpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ export const useGetNextPositiveCheckpoint = (
const [userCheckpoint, setUserCheckpoint] = useState<UserCheckpoint>();

const updateNextPositiveCheckpoint = useCallback(async () => {
let index = 1;
const processedCheckpoints = await contractReader.call<number>(
'feeSharingProxy',
'processedCheckpoints',
[address, contractAddress],
);

while (
totalTokenCheckpoints >=
MAX_NEXT_POSITIVE_CHECKPOINT * (index - 1)
) {
let userNextUnprocessedCheckpoint = processedCheckpoints;

while (userNextUnprocessedCheckpoint < totalTokenCheckpoints) {
const {
hasFees,
checkpointNum,
Expand All @@ -34,19 +37,20 @@ 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,
hasFees,
hasSkippedCheckpoints,
});
}
index++;
}

setUserCheckpoint({
Expand Down

0 comments on commit 84d6f95

Please sign in to comment.