-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBonding.test.js
345 lines (289 loc) · 10.7 KB
/
Bonding.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
const { ethers, timeAndMine } = require('hardhat')
const { expect } = require('chai')
const { BigNumber } = require('@ethersproject/bignumber')
const { formatUnits, formatEther } = require('@ethersproject/units')
describe('Bonding', 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 = '1000000000'
const daoAddr = '0x176311b81309240a8700BCC6129D5dF85087358D'
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.
clam,
sClam,
dai,
treasury,
staking,
stakingHelper,
daiBond,
firstEpochTime
beforeEach(async function () {
deployer = await ethers.getSigner()
firstEpochTime = (await deployer.provider.getBlock()).timestamp - 100
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 DAIBond = await ethers.getContractFactory('OtterBondDepository')
daiBond = await DAIBond.deploy(
clam.address,
dai.address,
treasury.address,
daoAddr,
zeroAddress
)
const Staking = await ethers.getContractFactory('OtterStaking')
staking = await Staking.deploy(
clam.address,
sClam.address,
epochLength,
firstEpochNumber,
firstEpochTime
)
// Deploy staking helper
const StakingHelper = await ethers.getContractFactory('OtterStakingHelper')
stakingHelper = await StakingHelper.deploy(staking.address, clam.address)
const StakingWarmup = await ethers.getContractFactory('OtterStakingWarmup')
const stakingWarmup = await StakingWarmup.deploy(
staking.address,
sClam.address
)
await sClam.initialize(staking.address)
await sClam.setIndex(initialIndex)
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('0', daiBond.address)
await treasury.toggle('0', daiBond.address, zeroAddress)
await daiBond.setStaking(stakingHelper.address, true)
await clam.approve(stakingHelper.address, largeApproval)
await dai.approve(treasury.address, largeApproval)
await dai.approve(daiBond.address, largeApproval)
// mint 1,000,000 DAI for testing
await dai.mint(
deployer.address,
BigNumber.from(100 * 10000).mul(BigNumber.from(10).pow(18))
)
})
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 daoFee = 10000 // DAO fee for bond
const maxBondDebt = '8000000000000000'
const initialBondDebt = 0
await daiBond.initializeBondTerms(
bcv,
bondVestingLength,
minBondPrice,
maxBondPayout, // Max bond payout,
daoFee,
maxBondDebt,
initialBondDebt
)
await daiBond.setAdjustment(true, 1, 50, 0)
const adjustment = await daiBond.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 daoFee = 10000 // DAO fee for bond
const maxBondDebt = '8000000000000000'
const initialBondDebt = 0
await daiBond.initializeBondTerms(
bcv,
bondVestingLength,
minBondPrice,
maxBondPayout, // Max bond payout,
daoFee,
maxBondDebt,
initialBondDebt
)
await expect(daiBond.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 daoFee = 10000 // DAO fee for bond
const maxBondDebt = '8000000000000000'
const initialBondDebt = 0
await daiBond.initializeBondTerms(
bcv,
bondVestingLength,
minBondPrice,
maxBondPayout, // Max bond payout,
daoFee,
maxBondDebt,
initialBondDebt
)
await daiBond.setAdjustment(false, 2, 80, 3)
const adjustment = await daiBond.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('deposit', function () {
it('should get vested fully', async function () {
await treasury.deposit(
BigNumber.from(10000).mul(BigNumber.from(10).pow(18)),
dai.address,
BigNumber.from(7500).mul(BigNumber.from(10).pow(9))
)
const bcv = 300
const bondVestingLength = 10
const minBondPrice = 400 // bond price = $4
const maxBondPayout = 1000 // 1000 = 1% of CLAM total supply
const daoFee = 10000 // DAO fee for bond
const maxBondDebt = '8000000000000000'
const initialBondDebt = 0
await daiBond.initializeBondTerms(
bcv,
bondVestingLength,
minBondPrice,
maxBondPayout, // Max bond payout,
daoFee,
maxBondDebt,
initialBondDebt
)
let bondPrice = await daiBond.bondPriceInUSD()
console.log('bond price: ' + formatEther(bondPrice))
let depositAmount = BigNumber.from(100).mul(BigNumber.from(10).pow(18))
await daiBond.deposit(depositAmount, largeApproval, deployer.address)
const prevDAOReserve = await clam.balanceOf(daoAddr)
expect(prevDAOReserve).to.eq(
BigNumber.from(25).mul(BigNumber.from(10).pow(9))
)
console.log('dao balance: ' + formatUnits(prevDAOReserve, 9))
await timeAndMine.setTimeIncrease(2)
await expect(() =>
daiBond.redeem(deployer.address, false)
).to.changeTokenBalance(
clam,
deployer,
BigNumber.from(5).mul(BigNumber.from(10).pow(9))
)
// bond 2nd times
bondPrice = await daiBond.bondPriceInUSD()
console.log('bond price: ' + formatEther(bondPrice))
depositAmount = BigNumber.from(100).mul(BigNumber.from(10).pow(18))
await daiBond.deposit(depositAmount, largeApproval, deployer.address)
console.log(
'dao balance: ' + formatUnits(await clam.balanceOf(daoAddr), 9)
)
expect(await clam.balanceOf(daoAddr)).to.eq('35834236186')
await timeAndMine.setTimeIncrease(20)
await expect(() =>
daiBond.redeem(deployer.address, false)
).to.changeTokenBalance(clam, deployer, '30834236186')
})
it('should get vested partially', async function () {
await treasury.deposit(
BigNumber.from(10000).mul(BigNumber.from(10).pow(18)),
dai.address,
BigNumber.from(7500).mul(BigNumber.from(10).pow(9))
)
const bcv = 300
const bondVestingLength = 10
const minBondPrice = 400 // bond price = $4
const maxBondPayout = 1000 // 1000 = 1% of CLAM total supply
const daoFee = 10000 // DAO fee for bond
const maxBondDebt = '8000000000000000'
const initialBondDebt = 0
await daiBond.initializeBondTerms(
bcv,
bondVestingLength,
minBondPrice,
maxBondPayout, // Max bond payout,
daoFee,
maxBondDebt,
initialBondDebt
)
const bondPrice = await daiBond.bondPriceInUSD()
const depositAmount = BigNumber.from(100).mul(BigNumber.from(10).pow(18))
const totalClam = depositAmount
.div(bondPrice)
.mul(BigNumber.from(10).pow(9))
await daiBond.deposit(depositAmount, largeApproval, deployer.address)
// vested 20%
await timeAndMine.setTimeIncrease(2)
await expect(() =>
daiBond.redeem(deployer.address, false)
).to.changeTokenBalance(clam, deployer, totalClam.div(5))
// fully vested, get rest 80%
await timeAndMine.setTimeIncrease(10)
await expect(() =>
daiBond.redeem(deployer.address, false)
).to.changeTokenBalance(clam, deployer, totalClam - totalClam.div(5))
})
it('should staked directly', async function () {
await treasury.deposit(
BigNumber.from(10000).mul(BigNumber.from(10).pow(18)),
dai.address,
BigNumber.from(7500).mul(BigNumber.from(10).pow(9))
)
const bcv = 300
const bondVestingLength = 10
const minBondPrice = 400 // bond price = $4
const maxBondPayout = 1000 // 1000 = 1% of CLAM total supply
const daoFee = 10000 // DAO fee for bond
const maxBondDebt = '8000000000000000'
const initialBondDebt = 0
await daiBond.initializeBondTerms(
bcv,
bondVestingLength,
minBondPrice,
maxBondPayout, // Max bond payout,
daoFee,
maxBondDebt,
initialBondDebt
)
let bondPrice = await daiBond.bondPriceInUSD()
console.log('bond price: ' + formatEther(bondPrice))
let depositAmount = BigNumber.from(100).mul(BigNumber.from(10).pow(18))
await daiBond.deposit(depositAmount, largeApproval, deployer.address)
const prevDAOReserve = await clam.balanceOf(daoAddr)
expect(prevDAOReserve).to.eq(
BigNumber.from(25).mul(BigNumber.from(10).pow(9))
)
console.log('dao balance: ' + formatUnits(prevDAOReserve, 9))
await timeAndMine.setTimeIncrease(2)
await daiBond.redeem(deployer.address, true)
expect(await sClam.balanceOf(deployer.address)).to.eq('5000000000')
})
})
})