Skip to content

Commit

Permalink
feat: implement new rewards withdrawal feature
Browse files Browse the repository at this point in the history
  • Loading branch information
creed-victor committed Sep 20, 2023
1 parent eb53160 commit 9ea3b0f
Show file tree
Hide file tree
Showing 3 changed files with 911 additions and 191 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useCallback, useMemo } from 'react';
import React, { FC, useCallback, useMemo, useState } from 'react';

import { Contract } from 'ethers';
import { t } from 'i18next';
Expand Down Expand Up @@ -32,6 +32,7 @@ const MAX_NEXT_POSITIVE_CHECKPOINT = 75;

export const WithdrawAllFees: FC<WithdrawFeeProps> = ({ fees, refetch }) => {
const { account } = useAccount();
const [loading, setLoading] = useState(false);
const { setTransactions, setIsOpen, setTitle } = useTransactionContext();

const { checkMaintenance, States } = useMaintenance();
Expand All @@ -56,6 +57,7 @@ export const WithdrawAllFees: FC<WithdrawFeeProps> = ({ fees, refetch }) => {
if (!feeSharing) {
return;
}
setLoading(true);

const claimable = fees.filter(fee => decimalic(fee.value).gt(0));

Expand All @@ -70,16 +72,33 @@ export const WithdrawAllFees: FC<WithdrawFeeProps> = ({ fees, refetch }) => {
hasFees: result.hasFees,
})),
),
).then(result => result.filter(fee => fee.hasSkippedCheckpoints));

console.log({ checkpoints });
).then(result => result.filter(fee => fee.hasFees));

if (checkpoints.length === 0) {
// todo: show error message about impossibility to withdraw
console.warn('No checkpoints to withdraw');
setLoading(false);
return;
}

const nonRbtcRegular = checkpoints
.filter(
item => !isBtcBasedToken(item.token) && !item.hasSkippedCheckpoints,
)
.map(item => item.contractAddress);

const rbtcRegular = checkpoints
.filter(
item => isBtcBasedToken(item.token) && !item.hasSkippedCheckpoints,
)
.map(item => item.contractAddress);

const tokensWithSkippedCheckpoints = checkpoints
.filter(item => item.hasSkippedCheckpoints)
.map(item => ({
tokenAddress: item.contractAddress,
fromCheckpoint: item.startFrom,
}));

// END: Fetch checkpoints

const transactions: Transaction[] = [];
Expand All @@ -91,10 +110,11 @@ export const WithdrawAllFees: FC<WithdrawFeeProps> = ({ fees, refetch }) => {
request: {
type: TransactionType.signTransaction,
contract: feeSharing,
fnName: 'withdrawStartingFromCheckpoints',
fnName: 'claimAllCollectedFees',
args: [
claimable.map(({ contractAddress }) => contractAddress),
claimable.map(({ startFrom }) => startFrom),
nonRbtcRegular,
rbtcRegular,
tokensWithSkippedCheckpoints,
MAX_CHECKPOINTS,
account,
],
Expand All @@ -106,6 +126,7 @@ export const WithdrawAllFees: FC<WithdrawFeeProps> = ({ fees, refetch }) => {
setTransactions(transactions);
setTitle(txTitle);
setIsOpen(true);
setLoading(false);
}, [
account,
feeSharing,
Expand All @@ -122,7 +143,8 @@ export const WithdrawAllFees: FC<WithdrawFeeProps> = ({ fees, refetch }) => {
style={ButtonStyle.secondary}
text={t(translations.rewardPage.stabilityPool.actions.withdrawAll)}
onClick={onSubmit}
disabled={isClaimDisabled}
disabled={isClaimDisabled || loading}
loading={loading}
className="w-full lg:w-auto whitespace-nowrap"
dataAttribute="rewards-withdraw"
/>
Expand Down Expand Up @@ -180,3 +202,7 @@ async function getNextPositiveCheckpoint(
hasSkippedCheckpoints: false,
};
}

function isBtcBasedToken(token: SupportedTokens) {
return [SupportedTokens.rbtc, SupportedTokens.wrbtc].includes(token);
}
132 changes: 47 additions & 85 deletions packages/contracts/src/abis/feeSharing.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,53 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "address[]",
"name": "_nonRbtcTokensRegularWithdraw",
"type": "address[]"
},
{
"internalType": "address[]",
"name": "_rbtcTokensRegularWithdraw",
"type": "address[]"
},
{
"components": [
{
"internalType": "address",
"name": "tokenAddress",
"type": "address"
},
{
"internalType": "uint256",
"name": "fromCheckpoint",
"type": "uint256"
}
],
"internalType": "struct FeeSharingCollectorStorage.TokenWithSkippedCheckpointsWithdraw[]",
"name": "_tokensWithSkippedCheckpoints",
"type": "tuple[]"
},
{
"internalType": "uint32",
"name": "_maxCheckpoints",
"type": "uint32"
},
{
"internalType": "address",
"name": "_receiver",
"type": "address"
}
],
"name": "claimAllCollectedFees",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
Expand Down Expand Up @@ -807,91 +854,6 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "address[]",
"name": "_tokens",
"type": "address[]"
},
{
"internalType": "uint32",
"name": "_maxCheckpoints",
"type": "uint32"
},
{
"internalType": "address",
"name": "_receiver",
"type": "address"
}
],
"name": "withdrawRbtcTokens",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "address[]",
"name": "_tokens",
"type": "address[]"
},
{
"internalType": "uint256[]",
"name": "_fromCheckpoints",
"type": "uint256[]"
},
{
"internalType": "uint32",
"name": "_maxCheckpoints",
"type": "uint32"
},
{
"internalType": "address",
"name": "_receiver",
"type": "address"
}
],
"name": "withdrawRbtcTokensStartingFromCheckpoint",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "address[]",
"name": "_tokens",
"type": "address[]"
},
{
"internalType": "uint256[]",
"name": "_fromCheckpoints",
"type": "uint256[]"
},
{
"internalType": "uint32",
"name": "_maxCheckpoints",
"type": "uint32"
},
{
"internalType": "address",
"name": "_receiver",
"type": "address"
}
],
"name": "withdrawStartingFromCheckpoints",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
Expand Down
Loading

0 comments on commit 9ea3b0f

Please sign in to comment.