-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWithdraw.jsx
135 lines (119 loc) · 3.67 KB
/
Withdraw.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React, { useState } from "react";
import { ethers } from "ethers";
import yearnSdk from "../sdk";
import initWallet from "../wallet";
import CONSTANTS from "../constants";
const Withdraw = () => {
const [loading, setLoading] = useState(false);
const approveWithdrawFromDaiToUSDC = async () => {
setLoading(true);
const wallet = await initWallet();
// User account address
const accountAddress = await wallet.getAddress();
// DAI Vault address
const vaultAddress = CONSTANTS.VAULT_ADDRESSES.DAI;
// DAI Token address
const tokenAddress = CONSTANTS.TOKEN_ADDRESSES.USDC;
// The amount to approve, in this case, we use the MAX_UINT256 constant to approve permanently
const amount = CONSTANTS.MAX_UINT256;
try {
const tx = await yearnSdk.vaults.approveWithdraw(
accountAddress,
vaultAddress,
tokenAddress,
amount
);
console.log(tx);
setLoading(false);
} catch (error) {
console.error(error);
setLoading(false);
}
};
const withdrawDaiVault = async () => {
setLoading(true);
const wallet = await initWallet();
// DAI Vault address
const vaultAddress = CONSTANTS.VAULT_ADDRESSES.DAI;
// DAI Token address
const tokenAddress = CONSTANTS.TOKEN_ADDRESSES.DAI;
// Amount to withdraw (1 DAI), we use ethers to format it
const amount = ethers.utils.parseUnits("1", 18);
// Slippage tolerance (1%)
const slippageTolerance = 0.01;
// User account address
const accountAddress = await wallet.getAddress();
// NOTE The withdraw will fail if amount > allowance (the token was not approved before)
try {
const tx = await yearnSdk.vaults.withdraw(
vaultAddress,
tokenAddress,
amount,
accountAddress,
{
slippage: slippageTolerance,
}
);
console.log(tx);
setLoading(false);
} catch (error) {
console.error(error);
setLoading(false);
}
};
const withdrawUsdcFromDai = async () => {
setLoading(true);
const wallet = await initWallet();
// DAI Vault address
const vaultAddress = CONSTANTS.VAULT_ADDRESSES.DAI;
// DAI Token address
const tokenAddress = CONSTANTS.TOKEN_ADDRESSES.USDC;
// Amount to withdraw (1 DAI), we use ethers to format it
const amount = ethers.utils.parseUnits("1", 18);
// Slippage tolerance (1%)
const slippageTolerance = 0.01;
// User account address
const accountAddress = await wallet.getAddress();
// NOTE The withdraw will fail if amount > allowance (the token was not approved before)
try {
const tx = await yearnSdk.vaults.withdraw(
vaultAddress,
tokenAddress,
amount,
accountAddress,
{
slippage: slippageTolerance,
}
);
console.log(tx);
setLoading(false);
} catch (error) {
console.error(error);
setLoading(false);
}
};
return (
<div>
<h2>Withdraw</h2>
<section>
<p>Approve Withdraw from DAI vault to USDC token (ZapOut)</p>
<button onClick={approveWithdrawFromDaiToUSDC} disabled={loading}>
{loading ? "Loading" : "Approve Withdraw"}
</button>
</section>
<section>
<p>Withdraw 1 DAI from DAI vault</p>
<button onClick={withdrawDaiVault} disabled={loading}>
{loading ? "Loading" : "Withdraw DAI"}
</button>
</section>
<section>
<p>Withdraw 1 USDC from DAI vault</p>
<button onClick={withdrawUsdcFromDai} disabled={loading}>
{loading ? "Loading" : "Withdraw USDC"}
</button>
</section>
</div>
);
};;
export default Withdraw;