-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOtterMaticLPBondDepository.test.js
441 lines (380 loc) · 13.1 KB
/
OtterMaticLPBondDepository.test.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
const { ethers, timeAndMine } = require('hardhat')
const { expect } = require('chai')
const { parseEther, parseUnits } = require('@ethersproject/units')
const { deployUniswap, getPair } = require('./helpers/uniswap')
describe('OtterMaticBondLPDepository', function () {
// Large number for approval for DAI
const largeApproval = '100000000000000000000000000000000'
// What epoch will be first epoch
const firstEpochNumber = '0'
// How many seconds are in each epoch
const epochLength = 86400 / 3
// Ethereum 0 address, used when toggling changes in treasury
const zeroAddress = '0x0000000000000000000000000000000000000000'
// Initial staking index
const initialIndex = '1737186817'
const initialRewardRate = '5000'
let // Used as default deployer for contracts, asks as owner of contracts.
deployer,
// Used as the default user for deposits and trade. Intended to be the default regular user.
depositor,
dao,
clam,
sClam,
dai,
treasury,
staking,
bond,
firstEpochTime,
oracle,
lp,
uniRouter
beforeEach(async function () {
;[deployer, depositor, dao] = await ethers.getSigners()
firstEpochTime = (await deployer.provider.getBlock()).timestamp - 100
const Oracle = await ethers.getContractFactory('AggregatorV3Mock')
oracle = await Oracle.deploy()
const CLAM = await ethers.getContractFactory('OtterClamERC20')
clam = await CLAM.deploy()
await clam.setVault(deployer.address)
const DAI = await ethers.getContractFactory('DAI')
dai = await DAI.deploy(0)
const StakedCLAM = await ethers.getContractFactory('StakedOtterClamERC20')
sClam = await StakedCLAM.deploy()
const Treasury = await ethers.getContractFactory('OtterTreasury')
treasury = await Treasury.deploy(
clam.address,
dai.address,
zeroAddress,
zeroAddress,
0
)
const Staking = await ethers.getContractFactory('OtterStaking')
staking = await Staking.deploy(
clam.address,
sClam.address,
epochLength,
firstEpochNumber,
firstEpochTime
)
const StakingWarmup = await ethers.getContractFactory('OtterStakingWarmup')
const stakingWarmup = await StakingWarmup.deploy(
staking.address,
sClam.address
)
const StakingDistributor = await ethers.getContractFactory(
'OtterStakingDistributor'
)
const stakingDistributor = await StakingDistributor.deploy(
treasury.address,
clam.address,
epochLength,
firstEpochTime
)
await stakingDistributor.addRecipient(staking.address, initialRewardRate)
const BondingCalculator = await ethers.getContractFactory(
'OtterBondingCalculator'
)
const bondingCalculator = await BondingCalculator.deploy(clam.address)
const { factory, router } = await deployUniswap(deployer)
uniRouter = router
await factory.createPair(dai.address, clam.address)
const lpAddress = factory.getPair(dai.address, clam.address)
lp = getPair(lpAddress, deployer)
const Bond = await ethers.getContractFactory('OtterMaticLPBondDepository')
bond = await Bond.deploy(
clam.address,
sClam.address,
lp.address,
treasury.address,
staking.address,
bondingCalculator.address,
dao.address,
oracle.address
)
await sClam.initialize(staking.address)
await sClam.setIndex(initialIndex)
await staking.setContract('0', stakingDistributor.address)
await staking.setContract('1', stakingWarmup.address)
await clam.setVault(treasury.address)
// queue and toggle deployer reserve depositor
await treasury.queue('0', deployer.address)
await treasury.toggle('0', deployer.address, zeroAddress)
await treasury.queue('5', lp.address)
await treasury.toggle('5', lp.address, bondingCalculator.address)
await treasury.queue('8', bond.address)
await treasury.toggle('8', bond.address, zeroAddress)
await treasury.queue('8', stakingDistributor.address)
await treasury.toggle('8', stakingDistributor.address, zeroAddress)
await dai.approve(treasury.address, largeApproval)
await lp.approve(bond.address, largeApproval)
await lp.connect(depositor).approve(bond.address, largeApproval)
await dai.approve(router.address, largeApproval)
await clam.approve(router.address, largeApproval)
await dai.connect(depositor).approve(router.address, largeApproval)
await clam.connect(depositor).approve(router.address, largeApproval)
// mint 1,000,000 DAI for testing
await dai.mint(deployer.address, parseEther(String(100 * 10000)))
await dai.transfer(depositor.address, parseEther('10000'))
// deposit to mint 25,000 CLAM
await treasury.deposit(
parseEther('100000'),
dai.address,
parseUnits('75000', 9)
)
await clam.transfer(depositor.address, parseUnits('50', 9))
})
describe('adjust', function () {
it('should able to adjust with bcv <= 40', async function () {
const bcv = 38
const bondVestingLength = 10
const minBondPrice = 400 // bond price = $4
const maxBondPayout = 1000 // 1000 = 1% of CLAM total supply
const maxBondDebt = '8000000000000000'
const initialBondDebt = 0
await bond.initializeBondTerms(
bcv,
bondVestingLength,
minBondPrice,
maxBondPayout, // Max bond payout,
maxBondDebt,
initialBondDebt
)
await bond.setAdjustment(true, 1, 50, 0)
const adjustment = await bond.adjustment()
expect(adjustment[0]).to.be.true
expect(adjustment[1]).to.eq(1)
expect(adjustment[2]).to.eq(50)
expect(adjustment[3]).to.eq(0)
})
it('should failed to adjust with too large increment', async function () {
const bcv = 100
const bondVestingLength = 10
const minBondPrice = 400 // bond price = $4
const maxBondPayout = 1000 // 1000 = 1% of CLAM total supply
const maxBondDebt = '8000000000000000'
const initialBondDebt = 0
await bond.initializeBondTerms(
bcv,
bondVestingLength,
minBondPrice,
maxBondPayout, // Max bond payout,
maxBondDebt,
initialBondDebt
)
await expect(bond.setAdjustment(true, 3, 50, 0)).to.be.revertedWith(
'Increment too large'
)
})
it('should be able to adjust with normal increment', async function () {
const bcv = 100
const bondVestingLength = 10
const minBondPrice = 400 // bond price = $4
const maxBondPayout = 1000 // 1000 = 1% of CLAM total supply
const maxBondDebt = '8000000000000000'
const initialBondDebt = 0
await bond.initializeBondTerms(
bcv,
bondVestingLength,
minBondPrice,
maxBondPayout, // Max bond payout,
maxBondDebt,
initialBondDebt
)
await bond.setAdjustment(false, 2, 80, 3)
const adjustment = await bond.adjustment()
expect(adjustment[0]).to.be.false
expect(adjustment[1]).to.eq(2)
expect(adjustment[2]).to.eq(80)
expect(adjustment[3]).to.eq(3)
})
})
describe('payout', function () {
it('should return correct payout', async function () {
// $50
await uniRouter.addLiquidity(
dai.address,
clam.address,
parseEther('500'),
parseUnits('10', 9),
0,
0,
deployer.address,
1000000000000
)
await oracle.setRoundData(0, parseUnits('2.2', 8), 0, 1, 0)
const bcv = 300
const bondVestingLength = 10
const minBondPrice = 150
const maxBondPayout = 1000 // 1000 = 1% of CLAM total supply
const maxBondDebt = '1000000000000000000'
const initialBondDebt = 0
await bond.initializeBondTerms(
bcv,
bondVestingLength,
minBondPrice,
maxBondPayout, // Max bond payout,
maxBondDebt,
initialBondDebt
)
expect(await bond.bondPrice()).to.eq('150')
const lpBalance = await lp.balanceOf(deployer.address)
const lpValue = await treasury.valueOfToken(lp.address, lpBalance)
expect(await bond.payoutFor(lpValue)).to.eq(parseUnits('94.280904156', 9))
})
})
describe('priceInUSD', function () {
it('should return correct price in usd', async function () {
// $50
await uniRouter.addLiquidity(
dai.address,
clam.address,
parseEther('500'),
parseUnits('10', 9),
0,
0,
deployer.address,
1000000000000
)
await oracle.setRoundData(0, parseUnits('2.2', 8), 0, 1, 0)
const bcv = 300
const bondVestingLength = 10
const minBondPrice = 150
const maxBondPayout = 1000 // 1000 = 1% of CLAM total supply
const maxBondDebt = '1000000000000000000'
const initialBondDebt = 0
await bond.initializeBondTerms(
bcv,
bondVestingLength,
minBondPrice,
maxBondPayout, // Max bond payout,
maxBondDebt,
initialBondDebt
)
expect(await bond.bondPriceInUSD()).to.eq(
parseEther('23.33452377937213661')
)
})
})
describe('deposit', function () {
beforeEach(async function () {
// $50
await uniRouter.addLiquidity(
dai.address,
clam.address,
parseEther('500'),
parseUnits('10', 9),
0,
0,
deployer.address,
1000000000000
)
await oracle.setRoundData(0, parseUnits('2.2', 8), 0, 1, 0)
})
it('failed to redeem not fully vested bond', async function () {
const bcv = 300
const bondVestingLength = 10
const minBondPrice = 150
const maxBondPayout = 1000 // 1000 = 1% of CLAM total supply
const maxBondDebt = '1000000000000000000'
const initialBondDebt = 0
await bond.initializeBondTerms(
bcv,
bondVestingLength,
minBondPrice,
maxBondPayout, // Max bond payout,
maxBondDebt,
initialBondDebt
)
const lpBalance = await lp.balanceOf(deployer.address)
await bond.deposit(lpBalance, largeApproval, deployer.address)
const bondInfo = await bond.bondInfo(deployer.address)
expect(bondInfo.payout).to.eq(parseUnits('94.280904156', 9))
expect(bondInfo.vesting).to.eq(10)
expect(bondInfo.pricePaid).to.eq(parseEther('23.334523779372136610'))
expect(bondInfo.gonsPayout).to.eq(
'2183396573481281471879252372173323493526061427090569544909976060441540496'
)
await timeAndMine.setTimeIncrease(2)
await expect(bond.redeem(deployer.address, false)).to.be.revertedWith(
'not fully vested'
)
})
it('should redeem sCLAM when vested fully', async function () {
const bcv = 300
const bondVestingLength = 15
const minBondPrice = 400 // bond price = 4 MATIC
const maxBondPayout = 10000 // 1000 = 1% of CLAM total supply
const maxBondDebt = '8000000000000000'
const initialBondDebt = 0
await bond.initializeBondTerms(
bcv,
bondVestingLength,
minBondPrice,
maxBondPayout, // Max bond payout,
maxBondDebt,
initialBondDebt
)
await oracle.setRoundData(0, parseUnits('2.2', 8), 0, 1, 0)
const lpBalance = await lp.balanceOf(deployer.address)
await bond.deposit(lpBalance, largeApproval, deployer.address)
await timeAndMine.setTimeIncrease(432001)
await staking.rebase()
await expect(() =>
bond.redeem(deployer.address, false)
).to.changeTokenBalance(sClam, deployer, parseUnits('160.532115752', 9))
})
it('should deploy twice and redeem sCLAM when vested fully', async function () {
await uniRouter
.connect(depositor)
.addLiquidity(
dai.address,
clam.address,
parseEther('250'),
parseUnits('5', 9),
0,
0,
depositor.address,
1000000000000
)
const bcv = 300
const bondVestingLength = 15
const minBondPrice = 5000 // bond price = $50
const maxBondPayout = 10000 // 1000 = 1% of CLAM total supply
const maxBondDebt = '8000000000000000'
const initialBondDebt = 0
await bond.initializeBondTerms(
bcv,
bondVestingLength,
minBondPrice,
maxBondPayout, // Max bond payout,
maxBondDebt,
initialBondDebt
)
await bond.deposit(
(await lp.balanceOf(deployer.address)).div(2),
largeApproval,
deployer.address
)
const balance = await lp.balanceOf(depositor.address)
await bond
.connect(depositor)
.deposit(balance, largeApproval, depositor.address)
await timeAndMine.setTimeIncrease(86400)
await staking.rebase()
await bond.deposit(
await lp.balanceOf(deployer.address),
largeApproval,
deployer.address
)
await timeAndMine.setTimeIncrease(432001)
await staking.rebase()
await expect(() =>
bond.redeem(deployer.address, false)
).to.changeTokenBalance(sClam, deployer, '191639081228')
await expect(() =>
bond.redeem(depositor.address, false)
).to.changeTokenBalance(sClam, depositor, '189524252460')
})
})
})