-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deposit.jsx
98 lines (86 loc) · 2.56 KB
/
Deposit.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
import React, { useState } from "react";
import { ethers } from "ethers";
import yearnSdk from "../sdk";
import initWallet from "../wallet";
import CONSTANTS from "../constants";
const Deposit = () => {
const [loading, setLoading] = useState(false);
const approveDaiDeposit = 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.DAI;
// 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.approveDeposit(
accountAddress,
vaultAddress,
tokenAddress,
amount
);
console.log(tx);
setLoading(false);
} catch (error) {
console.error(error);
setLoading(false);
}
};
const depositToDaiVault = 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 deposit (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();
// Deposit DAI to DAI or FTM to FTM
// NOTE The deposit will fail if amount > allowance (the token was not approved before)
try {
const tx = await yearnSdk.vaults.deposit(
vaultAddress,
tokenAddress,
amount,
accountAddress,
{
slippage: slippageTolerance,
}
);
console.log(tx);
setLoading(false);
} catch (error) {
console.error(error);
setLoading(false);
}
};
return (
<div>
<h2>Approve</h2>
<section>
<p>Approve DAI permanently</p>
<button onClick={approveDaiDeposit} disabled={loading}>
{loading ? "Loading" : "Approve DAI"}
</button>
</section>
<h2>Deposit</h2>
<section>
<p>
Deposit to 1 DAI to DAI vault (will fail if token is not approved)
</p>
<button onClick={depositToDaiVault} disabled={loading}>
{loading ? "Loading" : "Deposit DAI"}
</button>
</section>
</div>
);
};
export default Deposit;