-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.1.OpynVaultWithdrawals.spec.js
140 lines (128 loc) · 6.82 KB
/
5.1.OpynVaultWithdrawals.spec.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
const { ethers } = require("hardhat");
const { expect } = require("chai");
describe("OpynVault Withdrawals", function () {
let OpynVault, vault, owner, addr1, addr2;
let WETH, WETH_ADDRESS;
beforeEach(async function () {
[owner, addr1, addr2] = await ethers.getSigners();
WETH = await ethers.getContractFactory("ERC20Mintable");
weth = await WETH.deploy("WETH", "WETH");
WETH_ADDRESS = await weth.getAddress();
OpynVault = await ethers.getContractFactory("OpynVault");
vault = await OpynVault.deploy(WETH_ADDRESS, 5000);
await weth.mint(await owner.getAddress(), 1000);
await weth.approve(await vault.getAddress(), 1000);
await vault.depositRewards(500);
});
it("should allow user to withdraw after single deposit", async function () {
await weth.mint(await addr1.getAddress(), 1000);
await weth.connect(addr1).approve(await vault.getAddress(), 500);
await vault.connect(addr1).deposit(500);
await ethers.provider.send("evm_increaseTime", [365 * 24 * 60 * 60]);
await ethers.provider.send("evm_mine", []);
const initialBalance = await weth.balanceOf(await addr1.getAddress());
await vault.connect(addr1).withdraw();
const finalBalance = await weth.balanceOf(await addr1.getAddress());
expect(finalBalance).to.be.above(initialBalance);
});
it("should allow user to withdraw after multiple deposits", async function () {
await weth.mint(await addr1.getAddress(), 1000);
await weth.connect(addr1).approve(await vault.getAddress(), 500);
await vault.connect(addr1).deposit(200);
await vault.connect(addr1).deposit(300);
await ethers.provider.send("evm_increaseTime", [365 * 24 * 60 * 60]);
await ethers.provider.send("evm_mine", []);
const initialBalance = await weth.balanceOf(await addr1.getAddress());
await vault.connect(addr1).withdraw();
const finalBalance = await weth.balanceOf(await addr1.getAddress());
expect(finalBalance).to.be.above(initialBalance);
});
it("should allow user to withdraw after reward calculation", async function () {
await weth.mint(await addr1.getAddress(), 1000);
await weth.connect(addr1).approve(await vault.getAddress(), 500);
await vault.connect(addr1).deposit(500);
await ethers.provider.send("evm_increaseTime", [5 * 24 * 60 * 60]);
await ethers.provider.send("evm_mine", []);
const reward = await vault.calculateReward(await addr1.getAddress());
const initialBalance = await weth.balanceOf(await addr1.getAddress());
await vault.connect(addr1).withdraw();
const finalBalance = await weth.balanceOf(await addr1.getAddress());
expect(finalBalance).to.be.above(initialBalance + reward);
});
it("should allow withdraw immediately after deposit", async function () {
await weth.mint(await addr1.getAddress(), 1000);
await weth.connect(addr1).approve(await vault.getAddress(), 500);
await vault.connect(addr1).deposit(500);
await vault.connect(addr1).withdraw();
const finalBalance = await weth.balanceOf(await addr1.getAddress());
expect(finalBalance).to.equal(1000);
});
it("should handle withdraw after partial reward pool depletion", async function () {
await weth.mint(await owner.getAddress(), 100 * 1000);
await weth.connect(owner).approve(await vault.getAddress(), 100 * 1000);
await vault.depositRewards(100 * 1000);
await weth.mint(await addr1.getAddress(), 1000);
await weth.mint(await addr2.getAddress(), 1000);
await weth.connect(addr1).approve(await vault.getAddress(), 500);
await vault.connect(addr1).deposit(500);
await weth.connect(addr2).approve(await vault.getAddress(), 500);
await vault.connect(addr2).deposit(500);
await ethers.provider.send("evm_increaseTime", [200 * 24 * 60 * 60]); // Increase time by 1 y * 60ear
await ethers.provider.send("evm_mine", []);
await vault.connect(addr1).withdraw();
const finalBalance = await weth.balanceOf(await addr1.getAddress());
expect(finalBalance).to.be.above(1000); // User should get back deposit + rewards
});
it("should handle multiple users withdrawing simultaneously", async function () {
await weth.mint(await addr1.getAddress(), 1000);
await weth.mint(await addr2.getAddress(), 1000);
await weth.connect(addr1).approve(await vault.getAddress(), 500);
await vault.connect(addr1).deposit(500);
await weth.connect(addr2).approve(await vault.getAddress(), 500);
await vault.connect(addr2).deposit(500);
await ethers.provider.send("evm_increaseTime", [365 * 24 * 60 * 60]); // Increase time by 1 y * 60ear
await ethers.provider.send("evm_mine", []);
await vault.connect(addr1).withdraw();
await vault.connect(addr2).withdraw();
const finalBalance1 = await weth.balanceOf(await addr1.getAddress());
const finalBalance2 = await weth.balanceOf(await addr2.getAddress());
expect(finalBalance1).to.be.above(1000);
expect(finalBalance2).to.be.above(1000);
});
it("should handle users withdrawing after deposits and owner adding more rewards", async function () {
await weth.mint(await addr1.getAddress(), 1000);
await weth.mint(await addr2.getAddress(), 1000);
await weth.connect(addr1).approve(await vault.getAddress(), 500);
await vault.connect(addr1).deposit(500);
await weth.connect(addr2).approve(await vault.getAddress(), 500);
await vault.connect(addr2).deposit(500);
await vault.depositRewards(500);
await ethers.provider.send("evm_increaseTime", [365 * 24 * 60 * 60]);
await ethers.provider.send("evm_mine", []);
await vault.connect(addr1).withdraw();
await vault.connect(addr2).withdraw();
const finalBalance1 = await weth.balanceOf(await addr1.getAddress());
const finalBalance2 = await weth.balanceOf(await addr2.getAddress());
expect(finalBalance1).to.be.above(1000);
expect(finalBalance2).to.be.above(1000);
});
it("should handle users withdrawing, depositing again, and then withdrawing", async function () {
await weth.mint(await addr1.getAddress(), 1000);
await weth.mint(await addr2.getAddress(), 1000);
const initialBalance1 = await weth.balanceOf(addr1);
await weth.connect(addr1).approve(await vault.getAddress(), 500);
await vault.connect(addr1).deposit(500);
await weth.connect(addr2).approve(await vault.getAddress(), 500);
await vault.connect(addr2).deposit(500);
await ethers.provider.send("evm_increaseTime", [365 * 24 * 60 * 60]);
await ethers.provider.send("evm_mine", []);
await vault.connect(addr1).withdraw();
await weth.connect(addr1).approve(await vault.getAddress(), 500);
await vault.connect(addr1).deposit(500);
await ethers.provider.send("evm_increaseTime", [365 * 24 * 60 * 60]);
await ethers.provider.send("evm_mine", []);
await vault.connect(addr1).withdraw();
const finalBalance1 = await weth.balanceOf(await addr1.getAddress());
expect(finalBalance1).to.be.above(1000);
});
});