-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathdiscount-borrow.test.ts
480 lines (376 loc) · 20.4 KB
/
discount-borrow.test.ts
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import hre from 'hardhat';
import { expect } from 'chai';
import { BigNumber } from 'ethers';
import './helpers/math/wadraymath';
import { makeSuite, TestEnv } from './helpers/make-suite';
import { timeLatest, setBlocktime, mine, evmSnapshot, evmRevert } from '../helpers/misc-utils';
import { ONE_YEAR, MAX_UINT, ZERO_ADDRESS, oneRay, PERCENTAGE_FACTOR } from '../helpers/constants';
import { ghoReserveConfig } from '../helpers/config';
import { calcCompoundedInterest, calcDiscountRate } from './helpers/math/calculations';
import { getTxCostAndTimestamp } from './helpers/helpers';
import { printVariableDebtTokenEvents } from './helpers/tokenization-events';
makeSuite('Gho Discount Borrow Flow', (testEnv: TestEnv) => {
let ethers;
let collateralAmount;
let borrowAmount;
let startTime;
let oneYearLater;
let rcpt, tx;
let discountRate, ghoDiscountedPerDiscountToken, minDiscountTokenBalance;
before(async () => {
ethers = hre.ethers;
collateralAmount = ethers.utils.parseUnits('1000.0', 18);
borrowAmount = ethers.utils.parseUnits('1000.0', 18);
const { users, aaveToken, stakedAave, discountRateStrategy, faucetOwner } = testEnv;
// Fetch discount rate strategy parameters
[discountRate, ghoDiscountedPerDiscountToken, minDiscountTokenBalance] = await Promise.all([
discountRateStrategy.DISCOUNT_RATE(),
discountRateStrategy.GHO_DISCOUNTED_PER_DISCOUNT_TOKEN(),
discountRateStrategy.MIN_DISCOUNT_TOKEN_BALANCE(),
]);
// Transfers 10 stkAave (discountToken) to User 2
const stkAaveAmount = ethers.utils.parseUnits('10.0', 18);
const approveAaveAmount = ethers.utils.parseUnits('1000.0', 18);
await aaveToken.connect(users[1].signer).approve(stakedAave.address, approveAaveAmount);
await stakedAave.connect(users[1].signer).stake(users[1].address, stkAaveAmount);
});
it('User 1: Deposit WETH and Borrow GHO', async function () {
const { users, pool, weth, gho, variableDebtToken } = testEnv;
await weth.connect(users[0].signer).approve(pool.address, collateralAmount);
await pool
.connect(users[0].signer)
.deposit(weth.address, collateralAmount, users[0].address, 0);
tx = await pool
.connect(users[0].signer)
.borrow(gho.address, borrowAmount, 2, 0, users[0].address);
await expect(tx)
.to.emit(variableDebtToken, 'Transfer')
.withArgs(ZERO_ADDRESS, users[0].address, borrowAmount)
.to.emit(variableDebtToken, 'Mint')
.withArgs(users[0].address, users[0].address, borrowAmount, 0, oneRay);
expect(await variableDebtToken.getDiscountPercent(users[0].address)).to.be.eq(0);
expect(await gho.balanceOf(users[0].address)).to.be.equal(borrowAmount);
expect(await variableDebtToken.getBalanceFromInterest(users[0].address)).to.be.equal(0);
expect(await variableDebtToken.balanceOf(users[0].address)).to.be.equal(borrowAmount);
});
it('User 1: Increase time by 1 year and check interest accrued', async function () {
const { users, gho, variableDebtToken, pool } = testEnv;
const poolData = await pool.getReserveData(gho.address);
startTime = BigNumber.from(poolData.lastUpdateTimestamp);
const variableBorrowIndex = poolData.variableBorrowIndex;
oneYearLater = startTime.add(BigNumber.from(ONE_YEAR));
await setBlocktime(oneYearLater.toNumber());
await mine(); // Mine block to increment time in underlying chain as well
const multiplier = calcCompoundedInterest(
ghoReserveConfig.INTEREST_RATE,
await timeLatest(),
startTime
);
const expIndex = variableBorrowIndex.rayMul(multiplier);
const user1ExpectedBalance = (await variableDebtToken.scaledBalanceOf(users[0].address)).rayMul(
expIndex
);
const user1Year1Debt = await variableDebtToken.balanceOf(users[0].address);
expect(await gho.balanceOf(users[0].address)).to.be.equal(borrowAmount);
expect(user1Year1Debt).to.be.eq(user1ExpectedBalance);
expect(await variableDebtToken.getBalanceFromInterest(users[0].address)).to.be.equal(0);
});
it('User 2: After 1 year Deposit WETH and Borrow GHO', async function () {
const { users, pool, weth, gho, variableDebtToken, stakedAave } = testEnv;
const { lastUpdateTimestamp: ghoLastUpdateTimestamp, variableBorrowIndex } =
await pool.getReserveData(gho.address);
const discountPercentBefore = await variableDebtToken.getDiscountPercent(users[1].address);
await weth.connect(users[1].signer).approve(pool.address, collateralAmount);
await pool
.connect(users[1].signer)
.deposit(weth.address, collateralAmount, users[1].address, 0);
tx = await pool
.connect(users[1].signer)
.borrow(gho.address, borrowAmount, 2, 0, users[1].address);
rcpt = await tx.wait();
const { txTimestamp } = await getTxCostAndTimestamp(rcpt);
const multiplier = calcCompoundedInterest(
ghoReserveConfig.INTEREST_RATE,
txTimestamp,
BigNumber.from(ghoLastUpdateTimestamp)
);
const expIndex = variableBorrowIndex.rayMul(multiplier);
const discountTokenBalance = await stakedAave.balanceOf(users[1].address);
const discountPercent = calcDiscountRate(
discountRate,
ghoDiscountedPerDiscountToken,
minDiscountTokenBalance,
borrowAmount,
discountTokenBalance
);
await expect(tx)
.to.emit(variableDebtToken, 'Transfer')
.withArgs(ZERO_ADDRESS, users[1].address, borrowAmount)
.to.emit(variableDebtToken, 'Mint')
.withArgs(users[1].address, users[1].address, borrowAmount, 0, expIndex)
.to.emit(variableDebtToken, 'DiscountPercentUpdated')
.withArgs(users[1].address, discountPercentBefore, discountPercent);
expect(await variableDebtToken.getDiscountPercent(users[1].address)).to.be.eq(discountPercent);
expect(await gho.balanceOf(users[1].address)).to.be.equal(borrowAmount);
expect(await variableDebtToken.getBalanceFromInterest(users[1].address)).to.be.equal(0);
expect(await variableDebtToken.balanceOf(users[1].address)).to.be.equal(borrowAmount);
});
it('User 2: Wait 1 more year and borrow less GHO than discount accrued', async function () {
const snapId = await evmSnapshot();
const { users, pool, weth, gho, variableDebtToken, stakedAave } = testEnv;
const debtBalanceBeforeTimeskip = await variableDebtToken.balanceOf(users[1].address);
const { lastUpdateTimestamp, variableBorrowIndex } = await pool.getReserveData(gho.address);
const twoYearsLater = startTime.add(BigNumber.from(ONE_YEAR).mul(2));
await setBlocktime(twoYearsLater.toNumber());
await mine(); // Mine block to increment time in underlying chain as well
const balanceBeforeBorrow = await gho.balanceOf(users[1].address);
const debtBalanceAfterTimeskip = await variableDebtToken.balanceOf(users[1].address);
const debtIncrease = debtBalanceAfterTimeskip.sub(debtBalanceBeforeTimeskip);
const discountPercent = calcDiscountRate(
discountRate,
ghoDiscountedPerDiscountToken,
minDiscountTokenBalance,
borrowAmount,
await stakedAave.balanceOf(users[1].address)
);
const expectedDiscount = debtIncrease.mul(discountPercent).div(PERCENTAGE_FACTOR);
expect(expectedDiscount).to.be.gt(1);
await expect(pool.connect(users[1].signer).borrow(gho.address, 1, 2, 0, users[1].address)).to
.not.be.reverted;
const balanceAfterBorrow = await gho.balanceOf(users[1].address);
expect(balanceAfterBorrow).to.eq(balanceBeforeBorrow.add(1));
await evmRevert(snapId);
});
it('User 1: Increase time by 1 more year and borrow more GHO', async function () {
const { users, gho, variableDebtToken, pool, stakedAave } = testEnv;
const user1BeforeDebt = await variableDebtToken.scaledBalanceOf(users[0].address);
const { lastUpdateTimestamp, variableBorrowIndex } = await pool.getReserveData(gho.address);
const user1ScaledBefore = await variableDebtToken.scaledBalanceOf(users[0].address);
const user2ScaledBefore = await variableDebtToken.scaledBalanceOf(users[1].address);
// Updating the timestamp for the borrow to be one year later
oneYearLater = BigNumber.from(lastUpdateTimestamp).add(BigNumber.from(ONE_YEAR));
await setBlocktime(oneYearLater.toNumber());
tx = await pool
.connect(users[0].signer)
.borrow(gho.address, borrowAmount, 2, 0, users[0].address);
rcpt = await tx.wait();
const { txTimestamp } = await getTxCostAndTimestamp(rcpt);
const multiplier = calcCompoundedInterest(
ghoReserveConfig.INTEREST_RATE,
txTimestamp,
BigNumber.from(lastUpdateTimestamp)
);
const expIndex = variableBorrowIndex.rayMul(multiplier);
const borrowedAmountScaled = borrowAmount.rayDiv(expIndex);
const user1ExpectedBalance = user1ScaledBefore.add(borrowedAmountScaled).rayMul(expIndex);
const amount = user1ExpectedBalance.sub(borrowAmount);
const user1BalanceIncrease = amount.sub(borrowAmount);
const user2ExpectedBalanceNoDiscount = user2ScaledBefore.rayMul(expIndex);
const user2BalanceIncrease = user2ExpectedBalanceNoDiscount.sub(borrowAmount);
const user2DiscountTokenBalance = await stakedAave.balanceOf(users[1].address);
const user2DiscountPercent = calcDiscountRate(
discountRate,
ghoDiscountedPerDiscountToken,
minDiscountTokenBalance,
borrowAmount,
user2DiscountTokenBalance
);
const user2ExpectedDiscount = user2BalanceIncrease
.mul(user2DiscountPercent)
.div(PERCENTAGE_FACTOR);
const user2ExpectedBalance = user2ExpectedBalanceNoDiscount.sub(user2ExpectedDiscount);
await expect(tx)
.to.emit(variableDebtToken, 'Transfer')
.withArgs(ZERO_ADDRESS, users[0].address, amount)
.to.emit(variableDebtToken, 'Mint')
.withArgs(users[0].address, users[0].address, amount, user1BalanceIncrease, expIndex)
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');
const user1Debt = await variableDebtToken.balanceOf(users[0].address);
const user2Debt = await variableDebtToken.balanceOf(users[1].address);
expect(await variableDebtToken.getDiscountPercent(users[0].address)).to.be.eq(0);
expect(await variableDebtToken.getDiscountPercent(users[1].address)).to.be.eq(
user2DiscountPercent
);
expect(await gho.balanceOf(users[0].address)).to.be.equal(borrowAmount.add(borrowAmount));
expect(await gho.balanceOf(users[1].address)).to.be.equal(borrowAmount);
expect(user1Debt).to.be.eq(user1ExpectedBalance);
expect(user2Debt).to.be.closeTo(user2ExpectedBalance, 1);
const balanceIncrease = user1Debt.sub(borrowAmount).sub(user1BeforeDebt);
expect(await variableDebtToken.getBalanceFromInterest(users[0].address)).to.be.equal(
balanceIncrease
);
});
it('User 2: Receive GHO from User 1 and Repay Debt', async function () {
const { users, gho, variableDebtToken, aToken, pool, stakedAave, treasuryAddress } = testEnv;
await gho.connect(users[0].signer).transfer(users[1].address, borrowAmount);
await gho.connect(users[1].signer).approve(pool.address, MAX_UINT);
const { lastUpdateTimestamp, variableBorrowIndex } = await pool.getReserveData(gho.address);
const user1ScaledBefore = await variableDebtToken.scaledBalanceOf(users[0].address);
const user2ScaledBefore = await variableDebtToken.scaledBalanceOf(users[1].address);
const user2DiscountPercentBefore = await variableDebtToken.getDiscountPercent(users[1].address);
expect(await variableDebtToken.getBalanceFromInterest(users[1].address)).to.be.equal(0);
tx = await pool.connect(users[1].signer).repay(gho.address, MAX_UINT, 2, users[1].address);
rcpt = await tx.wait();
const { txTimestamp } = await getTxCostAndTimestamp(rcpt);
const multiplier = calcCompoundedInterest(
ghoReserveConfig.INTEREST_RATE,
txTimestamp,
BigNumber.from(lastUpdateTimestamp)
);
const expIndex = variableBorrowIndex.rayMul(multiplier);
const user1ExpectedBalance = user1ScaledBefore.rayMul(expIndex);
const user2ExpectedBalanceNoDiscount = user2ScaledBefore.rayMul(expIndex);
const user2BalanceIncrease = user2ExpectedBalanceNoDiscount.sub(borrowAmount);
const user2DiscountTokenBalance = await stakedAave.balanceOf(users[1].address);
const user2ExpectedDiscount = user2BalanceIncrease
.mul(user2DiscountPercentBefore)
.div(PERCENTAGE_FACTOR);
const user2ExpectedBalance = user2ExpectedBalanceNoDiscount.sub(user2ExpectedDiscount);
const user2ExpectedInterest = user2BalanceIncrease.sub(user2ExpectedDiscount);
expect(await variableDebtToken.getBalanceFromInterest(users[1].address)).to.be.equal(0);
const user2DiscountPercent = calcDiscountRate(
discountRate,
ghoDiscountedPerDiscountToken,
minDiscountTokenBalance,
BigNumber.from(0),
user2DiscountTokenBalance
);
await expect(tx)
.to.emit(variableDebtToken, 'Transfer')
.withArgs(users[1].address, ZERO_ADDRESS, borrowAmount)
.to.emit(variableDebtToken, 'Burn')
.withArgs(users[1].address, ZERO_ADDRESS, borrowAmount, user2ExpectedInterest, expIndex)
.to.emit(variableDebtToken, 'DiscountPercentUpdated')
.withArgs(users[1].address, user2DiscountPercentBefore, user2DiscountPercent);
const user1Debt = await variableDebtToken.balanceOf(users[0].address);
const user2Debt = await variableDebtToken.balanceOf(users[1].address);
expect(await variableDebtToken.getDiscountPercent(users[1].address)).to.be.eq(
user2DiscountPercent
);
expect(await gho.balanceOf(users[0].address)).to.be.equal(borrowAmount);
expect(await gho.balanceOf(users[1].address)).to.be.equal(
borrowAmount.mul(2).sub(user2ExpectedBalance)
);
expect(user1Debt).to.be.eq(user1ExpectedBalance);
// TODO: update to zero
expect(user2Debt).to.be.eq(0);
expect(await gho.balanceOf(aToken.address)).to.be.eq(user2ExpectedInterest);
expect(await variableDebtToken.getBalanceFromInterest(users[1].address)).to.be.equal(0);
});
it('User 3: Deposit some ETH and borrow GHO', async function () {
const { users, pool, weth, gho, variableDebtToken } = testEnv;
const { lastUpdateTimestamp: ghoLastUpdateTimestamp, variableBorrowIndex } =
await pool.getReserveData(gho.address);
await weth.connect(users[2].signer).approve(pool.address, collateralAmount);
await pool
.connect(users[2].signer)
.deposit(weth.address, collateralAmount, users[2].address, 0);
tx = await pool
.connect(users[2].signer)
.borrow(gho.address, borrowAmount.mul(3), 2, 0, users[2].address);
rcpt = await tx.wait();
const { txTimestamp } = await getTxCostAndTimestamp(rcpt);
const multiplier = calcCompoundedInterest(
ghoReserveConfig.INTEREST_RATE,
txTimestamp,
BigNumber.from(ghoLastUpdateTimestamp)
);
const expIndex = variableBorrowIndex.rayMul(multiplier);
await expect(tx)
.to.emit(variableDebtToken, 'Transfer')
.withArgs(ZERO_ADDRESS, users[2].address, borrowAmount.mul(3))
.to.emit(variableDebtToken, 'Mint')
.withArgs(users[2].address, users[2].address, borrowAmount.mul(3), 0, expIndex)
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');
expect(await variableDebtToken.getDiscountPercent(users[2].address)).to.be.eq(0);
expect(await gho.balanceOf(users[2].address)).to.be.equal(borrowAmount.mul(3));
expect(await variableDebtToken.getBalanceFromInterest(users[2].address)).to.be.equal(0);
expect(await variableDebtToken.balanceOf(users[2].address)).to.be.equal(borrowAmount.mul(3));
});
it('User 1: Repay 100 wei of GHO Debt', async function () {
const { users, gho, variableDebtToken, aToken, pool, treasuryAddress } = testEnv;
const repayAmount = BigNumber.from('100'); // 100 wei
await gho.connect(users[0].signer).approve(pool.address, MAX_UINT);
const { lastUpdateTimestamp, variableBorrowIndex } = await pool.getReserveData(gho.address);
const user1ScaledBefore = await variableDebtToken.scaledBalanceOf(users[0].address);
const aTokenGhoBalanceBefore = await gho.balanceOf(aToken.address);
const user1AccruedInterestBefore = await variableDebtToken.getBalanceFromInterest(
users[0].address
);
tx = await pool.connect(users[0].signer).repay(gho.address, repayAmount, 2, users[0].address);
rcpt = await tx.wait();
const { txTimestamp } = await getTxCostAndTimestamp(rcpt);
const multiplier = calcCompoundedInterest(
ghoReserveConfig.INTEREST_RATE,
txTimestamp,
BigNumber.from(lastUpdateTimestamp)
);
const expIndex = variableBorrowIndex.rayMul(multiplier);
const user1ExpectedBalance = user1ScaledBefore.rayMul(expIndex);
const user1ExpectedInterest = user1ExpectedBalance.sub(borrowAmount.mul(2));
const user1ExpectedBalanceIncrease = user1ExpectedInterest.sub(user1AccruedInterestBefore);
const expectedATokenGhoBalance = aTokenGhoBalanceBefore.add(repayAmount);
const amount = user1ExpectedBalanceIncrease.sub(repayAmount);
await expect(tx)
.to.emit(variableDebtToken, 'Transfer')
.withArgs(ZERO_ADDRESS, users[0].address, amount)
.to.emit(variableDebtToken, 'Mint')
.withArgs(users[0].address, users[0].address, amount, user1ExpectedBalanceIncrease, expIndex)
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');
expect(await variableDebtToken.getDiscountPercent(users[0].address)).to.be.eq(0);
expect(await variableDebtToken.balanceOf(users[0].address)).to.be.eq(
user1ExpectedBalance.sub(repayAmount)
);
expect(await variableDebtToken.getBalanceFromInterest(users[0].address)).to.be.equal(
user1AccruedInterestBefore.add(user1ExpectedBalanceIncrease).sub(repayAmount)
);
expect(await gho.balanceOf(aToken.address)).to.be.eq(expectedATokenGhoBalance);
});
it('User 1: Receive some GHO from User 3 and Repay Debt', async function () {
const { users, gho, variableDebtToken, aToken, pool, treasuryAddress } = testEnv;
await gho.connect(users[2].signer).transfer(users[0].address, borrowAmount.mul(3));
await gho.connect(users[0].signer).approve(pool.address, MAX_UINT);
const { lastUpdateTimestamp, variableBorrowIndex } = await pool.getReserveData(gho.address);
const user1ScaledBefore = await variableDebtToken.scaledBalanceOf(users[0].address);
const aTokenGhoBalanceBefore = await gho.balanceOf(aToken.address);
const user1AccruedInterestBefore = await variableDebtToken.getBalanceFromInterest(
users[0].address
);
tx = await pool.connect(users[0].signer).repay(gho.address, MAX_UINT, 2, users[0].address);
rcpt = await tx.wait();
const { txTimestamp } = await getTxCostAndTimestamp(rcpt);
const multiplier = calcCompoundedInterest(
ghoReserveConfig.INTEREST_RATE,
txTimestamp,
BigNumber.from(lastUpdateTimestamp)
);
const expIndex = variableBorrowIndex.rayMul(multiplier);
const user1ExpectedBalance = user1ScaledBefore.rayMul(expIndex);
const user1ExpectedInterest = user1ExpectedBalance.sub(borrowAmount.mul(2));
const user1ExpectedBalanceIncrease = user1ExpectedInterest.sub(user1AccruedInterestBefore);
const expectedATokenGhoBalance = aTokenGhoBalanceBefore.add(user1ExpectedInterest);
const amount = user1ExpectedBalance.sub(user1ExpectedBalanceIncrease);
await expect(tx)
.to.emit(variableDebtToken, 'Transfer')
.withArgs(users[0].address, ZERO_ADDRESS, amount)
.to.emit(variableDebtToken, 'Burn')
.withArgs(users[0].address, ZERO_ADDRESS, amount, user1ExpectedBalanceIncrease, expIndex)
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');
expect(await variableDebtToken.getDiscountPercent(users[0].address)).to.be.eq(0);
expect(await variableDebtToken.balanceOf(users[0].address)).to.be.eq(0);
expect(await variableDebtToken.getBalanceFromInterest(users[0].address)).to.be.equal(0);
expect(await gho.balanceOf(aToken.address)).to.be.eq(expectedATokenGhoBalance);
});
it('Distribute fees to treasury', async function () {
const { aToken, gho, treasuryAddress } = testEnv;
const aTokenBalance = await gho.balanceOf(aToken.address);
expect(aTokenBalance).to.not.be.equal(0);
expect(await gho.balanceOf(treasuryAddress)).to.be.equal(0);
const tx = await aToken.distributeFeesToTreasury();
await expect(tx)
.to.emit(aToken, 'FeesDistributedToTreasury')
.withArgs(treasuryAddress, gho.address, aTokenBalance);
expect(await gho.balanceOf(aToken.address)).to.be.equal(0);
expect(await gho.balanceOf(treasuryAddress)).to.be.equal(aTokenBalance);
});
});