From ce052c669f73fd31039e6e789c10429275bc8a79 Mon Sep 17 00:00:00 2001 From: The3D Date: Fri, 7 May 2021 20:04:24 +0200 Subject: [PATCH] refactor: partially fixed mint to treasury tests --- .../test-aave/helpers/utils/calculations.ts | 2 +- .../test-aave/mint-to-treasury.spec.ts | 43 +++++++++++++------ .../test-amm/helpers/utils/calculations.ts | 2 +- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/test-suites/test-aave/helpers/utils/calculations.ts b/test-suites/test-aave/helpers/utils/calculations.ts index de3d31f9..db633d51 100644 --- a/test-suites/test-aave/helpers/utils/calculations.ts +++ b/test-suites/test-aave/helpers/utils/calculations.ts @@ -1196,7 +1196,7 @@ const calcLinearInterest = ( return cumulatedInterest; }; -const calcCompoundedInterest = ( +export const calcCompoundedInterest = ( rate: BigNumber, currentTimestamp: BigNumber, lastUpdateTimestamp: BigNumber diff --git a/test-suites/test-aave/mint-to-treasury.spec.ts b/test-suites/test-aave/mint-to-treasury.spec.ts index 29813520..44cf7e33 100644 --- a/test-suites/test-aave/mint-to-treasury.spec.ts +++ b/test-suites/test-aave/mint-to-treasury.spec.ts @@ -1,12 +1,15 @@ import { makeSuite, TestEnv } from './helpers/make-suite'; import { ProtocolErrors, RateMode } from '../../helpers/types'; -import { APPROVAL_AMOUNT_LENDING_POOL, oneEther, ONE_YEAR } from '../../helpers/constants'; +import { APPROVAL_AMOUNT_LENDING_POOL, oneEther, ONE_YEAR, RAY } from '../../helpers/constants'; import { convertToCurrencyDecimals } from '../../helpers/contracts-helpers'; import { parseEther, parseUnits } from 'ethers/lib/utils'; import { BigNumber } from 'bignumber.js'; import { MockFlashLoanReceiver } from '../../types/MockFlashLoanReceiver'; import { getMockFlashLoanReceiver } from '../../helpers/contracts-getters'; -import { advanceTimeAndBlock, waitForTx } from '../../helpers/misc-utils'; +import { advanceTimeAndBlock, timeLatest, waitForTx } from '../../helpers/misc-utils'; +import './helpers/utils/math'; +import { calcCompoundedInterest } from './helpers/utils/calculations'; +import {getTxCostAndTimestamp } from './helpers/actions'; const { expect } = require('chai'); @@ -24,11 +27,10 @@ makeSuite('Mint to treasury', (testEnv: TestEnv) => { }); it('User 0 deposits 1000 DAI. Borrower borrows 100 DAI. Clock moved forward one year. Calculates and verifies the amount earned by the treasury', async () => { - const { users, pool, dai, aDai, configurator } = testEnv; + const { users, pool, dai, helpersContract } = testEnv; const amountDAItoDeposit = await convertToCurrencyDecimals(dai.address, '1000'); const amountDAItoBorrow = await convertToCurrencyDecimals(dai.address, '100'); - await dai.connect(users[0].signer).mint(amountDAItoDeposit); @@ -38,25 +40,40 @@ makeSuite('Mint to treasury', (testEnv: TestEnv) => { .connect(users[0].signer) .deposit(dai.address, amountDAItoDeposit, users[0].address, '0'); - await pool + const borrowTx = await waitForTx(await pool .connect(users[0].signer) - .borrow(dai.address, amountDAItoBorrow, RateMode.Variable, '0', users[0].address); + .borrow(dai.address, amountDAItoBorrow, RateMode.Variable, '0', users[0].address)); + const {txTimestamp : lastUpdateTimestamp} = await getTxCostAndTimestamp(borrowTx); + + + const { + currentLiquidityRate: liquidityRate, + currentVariableBorrowRate: variableBorrowRate, + } = await pool.getReserveData(dai.address); + const { reserveFactor } = await helpersContract.getReserveConfigurationData(dai.address); await advanceTimeAndBlock(parseInt(ONE_YEAR)); - await dai.connect(users[0].signer).mint(amountDAItoDeposit); - await pool + const depositTx = await waitForTx(await pool .connect(users[0].signer) - .deposit(dai.address, amountDAItoDeposit, users[0].address, '0'); + .deposit(dai.address, amountDAItoDeposit, users[0].address, '0')); - const reserveData = await pool.getReserveData(dai.address); + + const {txTimestamp : currTimestamp} = await getTxCostAndTimestamp(depositTx); - console.log(reserveData.accruedToTreasury.toString()); + const interestNormalized = calcCompoundedInterest(new BigNumber(variableBorrowRate.toString()), currTimestamp, lastUpdateTimestamp).minus(RAY); + + const expectedReserveFactor = new BigNumber(amountDAItoBorrow.toString()) + .rayMul(interestNormalized) + .times(reserveFactor.toString()) + .div(10000); + + const reserveData = await pool.getReserveData(dai.address); + + console.log(reserveData.accruedToTreasury.toString(), expectedReserveFactor.toString()); }); - - }); diff --git a/test-suites/test-amm/helpers/utils/calculations.ts b/test-suites/test-amm/helpers/utils/calculations.ts index e1574656..310df15b 100644 --- a/test-suites/test-amm/helpers/utils/calculations.ts +++ b/test-suites/test-amm/helpers/utils/calculations.ts @@ -1195,7 +1195,7 @@ const calcLinearInterest = ( return cumulatedInterest; }; -const calcCompoundedInterest = ( +export const calcCompoundedInterest = ( rate: BigNumber, currentTimestamp: BigNumber, lastUpdateTimestamp: BigNumber