2020-06-09 09:11:19 +00:00
import {
APPROVAL_AMOUNT_LENDING_POOL_CORE ,
2020-06-10 08:31:33 +00:00
MOCK_ETH_ADDRESS ,
2020-06-10 11:46:42 +00:00
AAVE_REFERRAL ,
MAX_UINT_AMOUNT ,
ZERO_ADDRESS ,
2020-06-09 09:11:19 +00:00
} from "../helpers/constants" ;
2020-06-10 15:01:32 +00:00
import { convertToCurrencyDecimals } from "../helpers/contracts-helpers" ;
2020-06-09 09:11:19 +00:00
import { expect } from "chai" ;
2020-06-10 15:01:32 +00:00
import { ethers } from "ethers" ;
2020-06-10 11:46:42 +00:00
import { RateMode } from "../helpers/types" ;
2020-06-10 15:01:32 +00:00
import { makeSuite , TestEnv } from "./helpers/make-suite" ;
2020-06-08 12:03:40 +00:00
2020-06-10 15:01:32 +00:00
makeSuite ( "AToken: Transfer" , ( testEnv : TestEnv ) = > {
2020-06-09 09:11:19 +00:00
it ( "User 0 deposits 1000 DAI, transfers to user 1" , async ( ) = > {
2020-06-10 15:01:32 +00:00
const { users , pool , core , _dai , _aDai } = testEnv ;
2020-06-09 09:11:19 +00:00
await _dai
2020-06-10 15:01:32 +00:00
. connect ( users [ 0 ] . signer )
2020-06-09 09:11:19 +00:00
. mint ( await convertToCurrencyDecimals ( _dai . address , "1000" ) ) ;
2020-06-08 12:03:40 +00:00
2020-06-09 09:11:19 +00:00
await _dai
2020-06-10 15:01:32 +00:00
. connect ( users [ 0 ] . signer )
. approve ( core . address , APPROVAL_AMOUNT_LENDING_POOL_CORE ) ;
2020-06-08 12:03:40 +00:00
2020-06-09 09:11:19 +00:00
//user 1 deposits 1000 DAI
const amountDAItoDeposit = await convertToCurrencyDecimals (
_dai . address ,
"1000"
) ;
2020-06-08 12:03:40 +00:00
2020-06-10 15:01:32 +00:00
await pool
. connect ( users [ 0 ] . signer )
2020-06-09 09:11:19 +00:00
. deposit ( _dai . address , amountDAItoDeposit , "0" ) ;
2020-06-08 12:03:40 +00:00
2020-06-09 14:36:33 +00:00
await _aDai
2020-06-10 15:01:32 +00:00
. connect ( users [ 0 ] . signer )
. transfer ( users [ 1 ] . address , amountDAItoDeposit ) ;
2020-06-09 14:36:33 +00:00
2020-06-10 15:01:32 +00:00
const fromBalance = await _aDai . balanceOf ( users [ 0 ] . address ) ;
const toBalance = await _aDai . balanceOf ( users [ 1 ] . address ) ;
2020-06-09 14:36:33 +00:00
expect ( fromBalance . toString ( ) ) . to . be . equal (
"0" ,
"Invalid from balance after transfer"
) ;
expect ( toBalance . toString ( ) ) . to . be . equal (
amountDAItoDeposit . toString ( ) ,
"Invalid to balance after transfer"
) ;
2020-06-09 09:11:19 +00:00
} ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 08:31:33 +00:00
it ( "User 1 redirects interest to user 2, transfers 500 DAI back to user 0" , async ( ) = > {
2020-06-10 15:01:32 +00:00
const { users , _aDai , _dai } = testEnv ;
2020-06-10 08:31:33 +00:00
await _aDai
2020-06-10 15:01:32 +00:00
. connect ( users [ 1 ] . signer )
. redirectInterestStream ( users [ 2 ] . address ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 08:31:33 +00:00
const aDAIRedirected = await convertToCurrencyDecimals (
_dai . address ,
"1000"
) ;
2020-06-08 12:03:40 +00:00
2020-06-10 08:31:33 +00:00
const aDAItoTransfer = await convertToCurrencyDecimals ( _dai . address , "500" ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 08:31:33 +00:00
const user2RedirectedBalanceBefore = await _aDai . getRedirectedBalance (
2020-06-10 15:01:32 +00:00
users [ 2 ] . address
2020-06-10 08:31:33 +00:00
) ;
expect ( user2RedirectedBalanceBefore . toString ( ) ) . to . be . equal (
aDAIRedirected ,
"Invalid redirected balance for user 2 before transfer"
) ;
2020-06-08 12:03:40 +00:00
2020-06-10 08:31:33 +00:00
await _aDai
2020-06-10 15:01:32 +00:00
. connect ( users [ 1 ] . signer )
. transfer ( users [ 0 ] . address , aDAItoTransfer ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 08:31:33 +00:00
const user2RedirectedBalanceAfter = await _aDai . getRedirectedBalance (
2020-06-10 15:01:32 +00:00
users [ 2 ] . address
2020-06-10 08:31:33 +00:00
) ;
const user1RedirectionAddress = await _aDai . getInterestRedirectionAddress (
2020-06-10 15:01:32 +00:00
users [ 1 ] . address
2020-06-10 08:31:33 +00:00
) ;
2020-06-08 12:03:40 +00:00
2020-06-10 08:31:33 +00:00
expect ( user2RedirectedBalanceAfter . toString ( ) ) . to . be . equal (
aDAItoTransfer ,
"Invalid redirected balance for user 2 after transfer"
) ;
expect ( user1RedirectionAddress . toString ( ) ) . to . be . equal (
2020-06-10 15:01:32 +00:00
users [ 2 ] . address ,
2020-06-10 08:31:33 +00:00
"Invalid redirection address for user 1"
) ;
} ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 08:31:33 +00:00
it ( "User 0 transfers back to user 1" , async ( ) = > {
2020-06-10 15:01:32 +00:00
const { users , _aDai , _dai } = testEnv ;
2020-06-10 08:31:33 +00:00
const aDAItoTransfer = await convertToCurrencyDecimals ( _dai . address , "500" ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 08:31:33 +00:00
await _aDai
2020-06-10 15:01:32 +00:00
. connect ( users [ 0 ] . signer )
. transfer ( users [ 1 ] . address , aDAItoTransfer ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 08:31:33 +00:00
const user2RedirectedBalanceAfter = await _aDai . getRedirectedBalance (
2020-06-10 15:01:32 +00:00
users [ 2 ] . address
2020-06-10 08:31:33 +00:00
) ;
2020-06-08 12:03:40 +00:00
2020-06-10 15:01:32 +00:00
const user1BalanceAfter = await _aDai . balanceOf ( users [ 1 ] . address ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 08:31:33 +00:00
expect ( user2RedirectedBalanceAfter . toString ( ) ) . to . be . equal (
user1BalanceAfter . toString ( ) ,
"Invalid redirected balance for user 2 after transfer"
) ;
} ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 08:31:33 +00:00
it ( "User 0 deposits 1 ETH and user tries to borrow, but the aTokens received as a transfer are not available as collateral (revert expected)" , async ( ) = > {
2020-06-10 15:01:32 +00:00
const { users , pool } = testEnv ;
await pool
. connect ( users [ 0 ] . signer )
2020-06-10 10:31:08 +00:00
. deposit ( MOCK_ETH_ADDRESS , ethers . utils . parseEther ( "1.0" ) , "0" , {
value : ethers.utils.parseEther ( "1.0" ) ,
} ) ;
2020-06-10 11:46:42 +00:00
await expect (
2020-06-10 15:01:32 +00:00
pool
. connect ( users [ 1 ] . signer )
2020-06-10 11:46:42 +00:00
. borrow (
MOCK_ETH_ADDRESS ,
ethers . utils . parseEther ( "0.1" ) ,
RateMode . Stable ,
AAVE_REFERRAL
) ,
"The collateral balance is 0"
) . to . be . revertedWith ( "The collateral balance is 0" ) ;
2020-06-10 08:31:33 +00:00
} ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
it ( "User 1 sets the DAI as collateral and borrows, tries to transfer everything back to user 0 (revert expected)" , async ( ) = > {
2020-06-10 15:01:32 +00:00
const { users , pool , _aDai , _dai } = testEnv ;
await pool
. connect ( users [ 1 ] . signer )
2020-06-10 11:46:42 +00:00
. setUserUseReserveAsCollateral ( _dai . address , true ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
const aDAItoTransfer = await convertToCurrencyDecimals (
_dai . address ,
"1000"
) ;
2020-06-08 12:03:40 +00:00
2020-06-10 15:01:32 +00:00
await pool
. connect ( users [ 1 ] . signer )
2020-06-10 11:46:42 +00:00
. borrow (
MOCK_ETH_ADDRESS ,
ethers . utils . parseEther ( "0.1" ) ,
RateMode . Stable ,
AAVE_REFERRAL
) ;
await expect (
2020-06-10 15:01:32 +00:00
_aDai . connect ( users [ 1 ] . signer ) . transfer ( users [ 0 ] . address , aDAItoTransfer ) ,
2020-06-10 11:46:42 +00:00
"Transfer cannot be allowed."
) . to . be . revertedWith ( "Transfer cannot be allowed." ) ;
} ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
it ( "User 0 tries to transfer 0 balance (revert expected)" , async ( ) = > {
2020-06-10 15:01:32 +00:00
const { users , pool , _aDai , _dai } = testEnv ;
2020-06-10 11:46:42 +00:00
await expect (
2020-06-10 15:01:32 +00:00
_aDai . connect ( users [ 0 ] . signer ) . transfer ( users [ 1 ] . address , "0" ) ,
2020-06-10 11:46:42 +00:00
"Transferred amount needs to be greater than zero"
) . to . be . revertedWith ( "Transferred amount needs to be greater than zero" ) ;
} ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
it ( "User 1 repays the borrow, transfers aDAI back to user 0" , async ( ) = > {
2020-06-10 15:01:32 +00:00
const { users , pool , _aDai , _dai } = testEnv ;
await pool
. connect ( users [ 1 ] . signer )
. repay ( MOCK_ETH_ADDRESS , MAX_UINT_AMOUNT , users [ 1 ] . address , {
2020-06-10 11:46:42 +00:00
value : ethers.utils.parseEther ( "1" ) ,
} ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
const aDAItoTransfer = await convertToCurrencyDecimals (
_aDai . address ,
"1000"
) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
await _aDai
2020-06-10 15:01:32 +00:00
. connect ( users [ 1 ] . signer )
. transfer ( users [ 0 ] . address , aDAItoTransfer ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
const user2RedirectedBalanceAfter = await _aDai . getRedirectedBalance (
2020-06-10 15:01:32 +00:00
users [ 2 ] . address
2020-06-10 11:46:42 +00:00
) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
const user1RedirectionAddress = await _aDai . getInterestRedirectionAddress (
2020-06-10 15:01:32 +00:00
users [ 1 ] . address
2020-06-10 11:46:42 +00:00
) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
expect ( user2RedirectedBalanceAfter . toString ( ) ) . to . be . equal (
"0" ,
"Invalid redirected balance for user 2 after transfer"
) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
expect ( user1RedirectionAddress . toString ( ) ) . to . be . equal (
ZERO_ADDRESS ,
"Invalid redirected address for user 1"
) ;
} ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
it ( "User 0 redirects interest to user 2, transfers 500 aDAI to user 1. User 1 redirects to user 3. User 0 transfers another 100 aDAI" , async ( ) = > {
2020-06-10 15:01:32 +00:00
const { users , pool , _aDai , _dai } = testEnv ;
2020-06-10 11:46:42 +00:00
let aDAItoTransfer = await convertToCurrencyDecimals ( _aDai . address , "500" ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
await _aDai
2020-06-10 15:01:32 +00:00
. connect ( users [ 0 ] . signer )
. redirectInterestStream ( users [ 2 ] . address ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
await _aDai
2020-06-10 15:01:32 +00:00
. connect ( users [ 0 ] . signer )
. transfer ( users [ 1 ] . address , aDAItoTransfer ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
await _aDai
2020-06-10 15:01:32 +00:00
. connect ( users [ 1 ] . signer )
. redirectInterestStream ( users [ 3 ] . address ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
aDAItoTransfer = await convertToCurrencyDecimals ( _aDai . address , "100" ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
await _aDai
2020-06-10 15:01:32 +00:00
. connect ( users [ 0 ] . signer )
. transfer ( users [ 1 ] . address , aDAItoTransfer ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
const user2RedirectedBalanceAfter = await _aDai . getRedirectedBalance (
2020-06-10 15:01:32 +00:00
users [ 2 ] . address
2020-06-10 11:46:42 +00:00
) ;
const user3RedirectedBalanceAfter = await _aDai . getRedirectedBalance (
2020-06-10 15:01:32 +00:00
users [ 3 ] . address
2020-06-10 11:46:42 +00:00
) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
const expectedUser2Redirected = await convertToCurrencyDecimals (
_aDai . address ,
"400"
) ;
const expectedUser3Redirected = await convertToCurrencyDecimals (
_aDai . address ,
"600"
) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
expect ( user2RedirectedBalanceAfter . toString ( ) ) . to . be . equal (
expectedUser2Redirected ,
"Invalid redirected balance for user 2 after transfer"
) ;
expect ( user3RedirectedBalanceAfter . toString ( ) ) . to . be . equal (
expectedUser3Redirected ,
"Invalid redirected balance for user 3 after transfer"
) ;
} ) ;
2020-06-09 09:11:19 +00:00
} ) ;