-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
189 lines (164 loc) · 4.18 KB
/
index.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const { ethers, Contract, Wallet, BigNumber } = require('ethers');
const commandLineUsage = require('command-line-usage');
const commandLineArgs = require('command-line-args');
const { parseUnits } = require('ethers/lib/utils');
const vaultABI = require('./abis/vault.json');
const erc20ABI = require('./abis/erc20.json');
const usage = commandLineUsage([
{
header: 'Backend deposit/withdrawal helper',
content:
'Backend application to assist with deposit/withdrawal calls on the Sandclock Vault contract',
},
{
header: 'Options',
optionList: [
{
name: 'action',
type: String,
description: 'deposit/withdraw/claim/generateYield/claimYield',
},
{
name: 'depositId',
type: Number,
description: 'depositId for withdraw action',
},
{
name: 'help',
alias: 'h',
description: 'print this usage guide',
},
],
},
]);
const options = commandLineArgs([
{
name: 'action',
type: String,
},
{
name: 'depositId',
type: Number,
},
{
name: 'help',
alias: 'h',
},
]);
if (options.help) {
console.log(usage);
process.exit(0);
}
let provider = new ethers.providers.WebSocketProvider(process.env.RPC_URL, {
chainId: 3,
});
const wallet = Wallet.fromMnemonic(process.env.MNEMONIC).connect(provider);
const testWallet = Wallet.fromMnemonic(process.env.TEST_MNEMONIC).connect(
provider,
);
(async () => {
const walletAddress = await wallet.getAddress();
const testWalletAddress = await testWallet.getAddress();
const underlyingContract = new Contract(
process.env.UNDERLYING_ADDRESS,
erc20ABI,
wallet,
);
const vaultContract = new Contract(
process.env.VAULT_ADDRESS,
vaultABI,
wallet,
);
switch (options.action) {
case 'withdraw':
if (options.depositId) {
await withdraw([parseInt(options.depositId)]);
break;
}
console.log(usage);
break;
case 'deposit':
await deposit();
break;
case 'claim':
await claim();
break;
case 'generateYield':
await generateYield();
break;
case 'claimYield':
await claimYield();
break;
case 'fundTestWallet':
await fundTestWallet();
break;
default:
console.log(usage);
}
provider.destroy();
async function deposit() {
await (
await underlyingContract
.connect(testWallet)
.approve(process.env.VAULT_ADDRESS, parseUnits('1000', 18))
).wait();
console.log('approve transaction mined');
await (
await vaultContract.connect(testWallet).deposit({
amount: parseUnits('1000', 18),
inputToken: underlyingContract.address,
lockDuration: 1,
claims: [
{
beneficiary: testWalletAddress,
pct: 1000,
data: 0,
},
{
beneficiary: await vaultContract.treasury(),
pct: 9000,
data: 0x46e0b937,
},
],
name: 'deposit-withdraw script test foundation',
slippage: BigNumber.from('1'),
})
).wait();
console.log('deposit transaction mined');
}
async function generateYield() {
await (
await underlyingContract.mint(
vaultContract.address,
parseUnits('2000', 18),
)
).wait();
console.log('generateYield transaction mined');
}
async function claimYield() {
await (
await vaultContract
.connect(testWallet)
.claimYield(await vaultContract.treasury())
).wait();
console.log('claimYield transaction mined');
}
async function withdraw(depositIds) {
await (
await vaultContract
.connect(testWallet)
.partialWithdraw(testWalletAddress, depositIds, [parseUnits('150')])
).wait();
console.log('withdraw transaction mined');
}
async function claim() {
await (await vaultContract.claimYield(walletAddress)).wait();
console.log('claimYield transaction mined');
}
async function fundTestWallet() {
await (
await underlyingContract.mint(testWalletAddress, parseUnits('10000', 18))
).wait();
console.log('fundTestWallet transaction mined');
}
})();