2021-05-06 14:21:55 +00:00
import { makeSuite , TestEnv } from './helpers/make-suite' ;
2021-05-08 16:53:56 +00:00
import { RateMode } from '../../helpers/types' ;
import { APPROVAL_AMOUNT_LENDING_POOL , ONE_YEAR } from '../../helpers/constants' ;
2021-05-06 14:21:55 +00:00
import { convertToCurrencyDecimals } from '../../helpers/contracts-helpers' ;
import { BigNumber } from 'bignumber.js' ;
2021-05-08 16:53:56 +00:00
import { advanceTimeAndBlock , waitForTx } from '../../helpers/misc-utils' ;
2021-05-07 18:04:24 +00:00
import './helpers/utils/math' ;
2021-05-06 14:21:55 +00:00
const { expect } = require ( 'chai' ) ;
makeSuite ( 'Mint to treasury' , ( testEnv : TestEnv ) = > {
2021-06-03 09:09:48 +00:00
it ( 'User 0 deposits 1000 DAI. Borrower borrows 100 DAI. Clock moved forward one year. Calculates and verifies the amount accrued to the treasury' , async ( ) = > {
2021-05-07 18:04:24 +00:00
const { users , pool , dai , helpersContract } = testEnv ;
2021-05-06 14:21:55 +00:00
const amountDAItoDeposit = await convertToCurrencyDecimals ( dai . address , '1000' ) ;
const amountDAItoBorrow = await convertToCurrencyDecimals ( dai . address , '100' ) ;
2021-05-08 16:53:56 +00:00
await waitForTx ( await dai . connect ( users [ 0 ] . signer ) . mint ( amountDAItoDeposit ) ) ;
2021-05-06 14:21:55 +00:00
// user 0 deposits 1000 DAI
2021-05-08 16:53:56 +00:00
await waitForTx (
await dai . connect ( users [ 0 ] . signer ) . approve ( pool . address , APPROVAL_AMOUNT_LENDING_POOL )
) ;
await waitForTx (
await pool
. connect ( users [ 0 ] . signer )
. deposit ( dai . address , amountDAItoDeposit , users [ 0 ] . address , '0' )
) ;
await waitForTx (
await pool
. connect ( users [ 0 ] . signer )
. borrow ( dai . address , amountDAItoBorrow , RateMode . Variable , '0' , users [ 0 ] . address )
) ;
2021-05-06 14:21:55 +00:00
2021-05-07 18:04:24 +00:00
const { reserveFactor } = await helpersContract . getReserveConfigurationData ( dai . address ) ;
await advanceTimeAndBlock ( parseInt ( ONE_YEAR ) ) ;
2021-05-06 14:21:55 +00:00
2021-05-08 16:53:56 +00:00
await waitForTx ( await dai . connect ( users [ 0 ] . signer ) . mint ( amountDAItoDeposit ) ) ;
2021-05-06 14:21:55 +00:00
2021-05-08 16:53:56 +00:00
await waitForTx (
await pool
. connect ( users [ 0 ] . signer )
. deposit ( dai . address , amountDAItoDeposit , users [ 0 ] . address , '0' )
) ;
2021-05-06 14:21:55 +00:00
2021-05-08 16:53:56 +00:00
const { liquidityIndex , variableBorrowIndex } = await pool . getReserveData ( dai . address ) ;
2021-05-06 14:21:55 +00:00
2021-05-08 16:53:56 +00:00
const amountBorrowedBN = new BigNumber ( amountDAItoBorrow . toString ( ) ) ;
const liquidityIndexBN = new BigNumber ( liquidityIndex . toString ( ) ) ;
const variableBorrowIndexBN = new BigNumber ( variableBorrowIndex . toString ( ) ) ;
2021-05-06 14:21:55 +00:00
2021-05-08 16:53:56 +00:00
const expectedAccruedToTreasury = amountBorrowedBN
. rayMul ( variableBorrowIndexBN )
. minus ( amountBorrowedBN )
2021-05-07 18:04:24 +00:00
. times ( reserveFactor . toString ( ) )
2021-05-08 16:53:56 +00:00
. div ( 10000 )
. rayDiv ( liquidityIndexBN )
. toFixed ( 0 ) ;
2021-05-07 18:04:24 +00:00
2021-05-08 16:53:56 +00:00
const { accruedToTreasury } = await pool . getReserveData ( dai . address ) ;
2021-05-07 18:04:24 +00:00
2021-05-08 16:53:56 +00:00
expect ( accruedToTreasury . toString ( ) ) . to . be . bignumber . almostEqual (
expectedAccruedToTreasury ,
2021-06-03 09:09:48 +00:00
'Invalid amount accrued to the treasury'
) ;
} ) ;
it ( 'Mints the accrued to the treasury' , async ( ) = > {
const { users , pool , dai , aDai , helpersContract } = testEnv ;
const treasuryAddress = await aDai . RESERVE_TREASURY_ADDRESS ( ) ;
const { accruedToTreasury } = await pool . getReserveData ( dai . address ) ;
await waitForTx ( await pool . connect ( users [ 0 ] . signer ) . mintToTreasury ( [ dai . address ] ) ) ;
const normalizedIncome = await pool . getReserveNormalizedIncome ( dai . address ) ;
const treasuryBalance = await aDai . balanceOf ( treasuryAddress ) ;
const expectedTreasuryBalance = new BigNumber ( accruedToTreasury . toString ( ) ) . rayMul (
new BigNumber ( normalizedIncome . toString ( ) )
2021-05-08 16:53:56 +00:00
) ;
2021-06-03 09:09:48 +00:00
expect ( treasuryBalance . toString ( ) ) . to . be . bignumber . almostEqual ( expectedTreasuryBalance , "Invalid treasury balance after minting" ) ;
2021-05-07 18:04:24 +00:00
} ) ;
2021-05-06 14:21:55 +00:00
} ) ;