From b68cb68ed4ee8e0c9b24be56ee70dcce2247ce05 Mon Sep 17 00:00:00 2001 From: Philippe Schommers Date: Mon, 2 Oct 2023 20:33:05 +0200 Subject: [PATCH] feat: implement max redeem --- src/components/Form/Form.tsx | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/components/Form/Form.tsx b/src/components/Form/Form.tsx index 803f2e5..a1709aa 100644 --- a/src/components/Form/Form.tsx +++ b/src/components/Form/Form.tsx @@ -29,7 +29,6 @@ enum Actions { ApproveWXDAI, DepositWXDAI, ApproveSDAI, - RedeemXDAI, WithdrawWXDAI, WithdrawXDAI, } @@ -53,10 +52,13 @@ const Form: React.FC = () => { } // Token input - const { address, depositAllowance, withdrawAllowance } = account; + const { address, depositAllowance, withdrawAllowance, sharesBalance } = account; const [tokenInput, setTokenInput] = useState<{ token: Token; balance: bigint; max: bigint }>(); const isNative = tokenInput?.token.name === "xDAI"; const amount = tokenInput?.balance ?? 0n; + const amountIsMax = tokenInput?.balance === tokenInput?.max; + + console.log({ sharesBalance }); // Toggles const [isDeposit, setIsDeposit] = useState(true); @@ -147,22 +149,12 @@ const Form: React.FC = () => { }).config, ); - const redeemXDAI = useContractWrite( - usePrepareContractWrite({ - address: VAULT_ROUTER_ADDRESS, - abi: VaultAdapter, - functionName: "redeemXDAI", - args: [amount, receiver], - enabled: action.action === Actions.RedeemXDAI, - }).config, - ); - const withdrawWXDAI = useContractWrite( usePrepareContractWrite({ address: VAULT_ROUTER_ADDRESS, abi: VaultAdapter, - functionName: "withdraw", - args: [amount, receiver], + functionName: amountIsMax ? "redeem" : "withdraw", + args: [amountIsMax ? sharesBalance.value : amount, receiver], enabled: action.action === Actions.WithdrawWXDAI, }).config, ); @@ -171,8 +163,8 @@ const Form: React.FC = () => { usePrepareContractWrite({ address: VAULT_ROUTER_ADDRESS, abi: VaultAdapter, - functionName: "withdrawXDAI", - args: [amount, receiver], + functionName: amountIsMax ? "redeemXDAI" : "withdrawXDAI", + args: [amountIsMax ? sharesBalance.value : amount, receiver], enabled: action.action === Actions.WithdrawXDAI, }).config, ); @@ -224,7 +216,6 @@ const Form: React.FC = () => { [Actions.ApproveWXDAI]: approveWXDAI, [Actions.DepositWXDAI]: depositWXDAI, [Actions.ApproveSDAI]: approveSDAI, - [Actions.RedeemXDAI]: redeemXDAI, [Actions.WithdrawWXDAI]: withdrawWXDAI, [Actions.WithdrawXDAI]: withdrawXDAI, }[action.action];