Skip to content

Commit

Permalink
Claim withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
philipjames44 committed Jun 15, 2024
1 parent b98a1ba commit 129c6e5
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 9 deletions.
38 changes: 38 additions & 0 deletions src/components/v1/DelayWithdrawClaim.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// src/components/v1/DelayWithdrawClaim.tsx

import React, { useEffect, useState } from "react";
import { Box, Text, VStack, Button } from "@chakra-ui/react";
import { useBoringVaultV1 } from "../../contexts/v1/BoringVaultContextV1";
import { Token } from "../../types";
import { useEthersSigner } from "../../hooks/ethers";

interface DelayWithdrawClaimProps {
token: Token;
unixSecondsReadyToClaim: number;
}

const DelayWithdrawClaim: React.FC<DelayWithdrawClaimProps> = ({
token,
unixSecondsReadyToClaim,
}) => {
const { delayWithdrawComplete } = useBoringVaultV1();
const signer = useEthersSigner();

// Check if the user can claim the delayed withdraw based on the seconds to claim
// If the current time is greater than the unixSecondsReadyToClaim, the user can claim
const canClaim = Date.now() / 1000 > unixSecondsReadyToClaim;

return (
<Button
mt={4}
onClick={() => delayWithdrawComplete(signer!, token)}
isDisabled={!signer || !canClaim}
outline={"1px solid black"}
colorScheme={"green"}
>
Claim
</Button>
);
};

export default DelayWithdrawClaim;
21 changes: 12 additions & 9 deletions src/components/v1/PendingDelayedWithdraws.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect, useState } from "react";
import { Box, HStack, Text, VStack } from "@chakra-ui/react";
import { useBoringVaultV1 } from "../../contexts/v1/BoringVaultContextV1";
import DelayWithdrawCancelButton from "./DelayWithdrawCancelButton";
import { Token } from "../../types";
import DelayWithdrawClaim from "./DelayWithdrawClaim";
import { useEthersSigner } from "../../hooks/ethers";

interface PendingDelayedWithdrawsProps {
Expand Down Expand Up @@ -44,13 +44,8 @@ const PendingDelayedWithdraws: React.FC<PendingDelayedWithdrawsProps> = ({
outline={"1px solid black"}
borderRadius={"1em"}
>
<HStack
key={index}
alignItems={"flex-start"}
>
<VStack
alignItems={"flex-start"}
>
<HStack key={index} alignItems={"flex-start"}>
<VStack alignItems={"flex-start"}>
<Text>
<strong>Shares:</strong> {delayWithdrawStatus.shares}
</Text>
Expand All @@ -76,7 +71,15 @@ const PendingDelayedWithdraws: React.FC<PendingDelayedWithdrawsProps> = ({
{delayWithdrawStatus.token.displayName}
</Text>
</VStack>
<DelayWithdrawCancelButton token={delayWithdrawStatus.token} />
<VStack paddingLeft="1em">
<DelayWithdrawClaim
token={delayWithdrawStatus.token}
unixSecondsReadyToClaim={delayWithdrawStatus.maturity}
/>
<DelayWithdrawCancelButton
token={delayWithdrawStatus.token}
/>
</VStack>
</HStack>
</Box>
);
Expand Down
92 changes: 92 additions & 0 deletions src/contexts/v1/BoringVaultContextV1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ interface BoringVaultV1ContextProps {
signer: JsonRpcSigner,
tokenOut: Token
) => Promise<WithdrawStatus>;
delayWithdrawComplete: (
signer: JsonRpcSigner,
tokenOut: Token
) => Promise<WithdrawStatus>;
depositStatus: DepositStatus;
withdrawStatus: WithdrawStatus;
isBoringV1ContextReady: boolean;
Expand Down Expand Up @@ -816,6 +820,93 @@ export const BoringVaultV1Provider: React.FC<{

}, [delayWithdrawEthersContract, userAddress, decimals, ethersProvider, isBoringV1ContextReady]);


const delayWithdrawComplete = useCallback(
async (signer: JsonRpcSigner, tokenOut: Token) => {
if (
!delayWithdrawEthersContract ||
!isBoringV1ContextReady ||
!userAddress ||
!decimals ||
!signer
) {
console.error("Contracts or user not ready to complete withdraw", {
delayWithdrawEthersContract,
isBoringV1ContextReady,
userAddress,
decimals,
signer,
});

setWithdrawStatus({
initiated: false,
loading: false,
success: false,
error: "Contracts or user not ready",
});

return withdrawStatus;
}

try {
const delayWithdrawContractWithSigner = new Contract(
delayWithdrawContract!,
BoringDelayWithdrawContractABI,
signer
);

console.log("Completing delay withdraw ...");

setWithdrawStatus({
initiated: true,
loading: true,
});

const completeTx = await delayWithdrawContractWithSigner.completeWithdraw(
tokenOut.address,
userAddress
);

// Wait for confirmation
const completeReceipt: ContractTransactionReceipt = await completeTx.wait();

console.log("Withdraw Completed in tx: ", completeReceipt);

if (!completeReceipt.hash) {
console.error("Withdraw Complete failed");
setWithdrawStatus({
initiated: false,
loading: false,
success: false,
error: "Withdraw Complete reverted",
});
return withdrawStatus;
}

console.log("Withdraw Complete hash: ", completeReceipt.hash);

// Set status
setWithdrawStatus({
initiated: false,
loading: false,
success: true,
tx_hash: completeReceipt.hash,
});

} catch (error: any) {
console.error("Error completing withdraw", error);
setWithdrawStatus({
initiated: false,
loading: false,
success: false,
error: (error as Error).message,
});
return withdrawStatus;
}
return withdrawStatus;
}, [delayWithdrawEthersContract, userAddress, decimals, ethersProvider, isBoringV1ContextReady]);


return (
<BoringVaultV1Context.Provider
value={{
Expand All @@ -839,6 +930,7 @@ export const BoringVaultV1Provider: React.FC<{
delayWithdraw,
delayWithdrawStatuses,

Check failure on line 931 in src/contexts/v1/BoringVaultContextV1.tsx

View workflow job for this annotation

GitHub Actions / build

Type '(signer: JsonRpcSigner) => Promise<({ allowThirdPartyToComplete: any; maxLoss: number; maturity: number; shares: number; exchangeRateAtTimeOfRequest: number; token: Token; } | null)[]>' is not assignable to type '(signer: JsonRpcSigner) => Promise<DelayWithdrawStatus[]>'.
delayWithdrawCancel,
delayWithdrawComplete,
depositStatus,
withdrawStatus,
isBoringV1ContextReady,
Expand Down

0 comments on commit 129c6e5

Please sign in to comment.