This repository was archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUnlockNativeToken.jsx
112 lines (103 loc) · 3.23 KB
/
UnlockNativeToken.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { BigNumber } from "@ethersproject/bignumber";
import styles from "../styles/Home.module.css";
import { useNotification, Button, Input, Typography } from "@web3uikit/core";
import { formatUnits, parseUnits } from "@ethersproject/units";
import contractAddresses from "../contractAddresses.json";
import {
useAccount,
useNetwork,
useContract,
useProvider,
useSigner,
} from "wagmi";
import { useState, useEffect } from "react";
import votingEscrowContract from "../contracts/VotingEscrow.json";
export default function WithdrawNativeToken(props) {
const { address, isConnected } = useAccount();
const { chain } = useNetwork();
const [unlockTime, setUnlockTime] = useState(0);
const provider = useProvider();
const { data: signer } = useSigner();
const [unlockLoading, setUnlockLoading] = useState(false);
var addresses = contractAddresses[chain ? chain.id : 1];
const dispatch = useNotification();
const votingEscrowProvider = useContract({
contractInterface: votingEscrowContract.abi,
addressOrName: addresses.VotingEscrow,
signerOrProvider: provider,
});
const votingEscrowSigner = useContract({
contractInterface: votingEscrowContract.abi,
addressOrName: addresses.VotingEscrow,
signerOrProvider: signer,
});
async function getUnlockTime() {
const updatedUnlockTime = await votingEscrowProvider.getLock(props.lockId);
console.log(
"updatedUnlockTime:",
BigNumber.from(updatedUnlockTime.end).toNumber()
);
setUnlockTime(BigNumber.from(updatedUnlockTime.end).toNumber());
}
useEffect(() => {
if (isConnected) {
addresses = contractAddresses[chain.id];
}
}, [isConnected]);
useEffect(() => {
if (isConnected && props.lockId) {
console.log("Getting unlock time", props.lockId);
getUnlockTime();
}
}, [props.lockId]);
const handleUnlockSuccess = async function () {
props.updateUI();
props.setVisibility(false);
dispatch({
type: "success",
message: "Please wait for transaction confirmation.",
title: "Withdrawal Successful!",
position: "bottomL",
});
};
return (
<div>
{unlockTime > Date.now() / 1000 ? (
<div className="flex flex-col items-center m-8">
<Typography variant="subtitle2">
Locked until {new Date(unlockTime * 1000).toLocaleString()}
</Typography>
</div>
) : (
<div className="m-8">
<Button
text="UNLOCK"
theme="secondary"
isFullWidth
loadingProps={{
spinnerColor: "#000000",
spinnerType: "loader",
direction: "right",
size: "24",
}}
loadingText=""
isLoading={unlockLoading}
disabled={unlockTime == 0}
onClick={async function () {
try {
setUnlockLoading(true);
const tx = await votingEscrowSigner.withdraw(props.lockId);
await tx.wait(1);
handleUnlockSuccess();
} catch (error) {
console.log(error);
} finally {
setUnlockLoading(false);
}
}}
/>
</div>
)}
</div>
);
}