2020-06-09 09:11:19 +00:00
import {
2020-06-20 23:40:03 +00:00
APPROVAL_AMOUNT_LENDING_POOL ,
2020-06-10 11:46:42 +00:00
AAVE_REFERRAL ,
MAX_UINT_AMOUNT ,
ZERO_ADDRESS ,
2020-07-13 08:54:08 +00:00
} from '../helpers/constants' ;
import { convertToCurrencyDecimals } from '../helpers/contracts-helpers' ;
import { expect } from 'chai' ;
import { ethers } from 'ethers' ;
import { RateMode , ProtocolErrors } from '../helpers/types' ;
import { makeSuite , TestEnv } from './helpers/make-suite' ;
makeSuite ( 'AToken: Transfer' , ( testEnv : TestEnv ) = > {
2020-06-12 08:39:42 +00:00
const {
INVALID_FROM_BALANCE_AFTER_TRANSFER ,
INVALID_TO_BALANCE_AFTER_TRANSFER ,
INVALID_REDIRECTED_BALANCE_BEFORE_TRANSFER ,
INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER ,
INVALID_REDIRECTION_ADDRESS ,
2020-09-02 15:54:34 +00:00
// ZERO_COLLATERAL,
TRANSFER_AMOUNT_NOT_GT_0 ,
COLLATERAL_BALANCE_IS_0 ,
TRANSFER_NOT_ALLOWED ,
2020-06-12 08:39:42 +00:00
} = ProtocolErrors ;
2020-07-13 08:54:08 +00:00
it ( 'User 0 deposits 1000 DAI, transfers to user 1' , async ( ) = > {
const { users , pool , dai , aDai } = testEnv ;
2020-06-10 15:01:32 +00:00
2020-07-13 08:54:08 +00:00
await dai . connect ( users [ 0 ] . signer ) . mint ( await convertToCurrencyDecimals ( dai . address , '1000' ) ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
await dai . connect ( users [ 0 ] . signer ) . approve ( pool . address , APPROVAL_AMOUNT_LENDING_POOL ) ;
2020-06-08 12:03:40 +00:00
2020-06-09 09:11:19 +00:00
//user 1 deposits 1000 DAI
2020-07-13 08:54:08 +00:00
const amountDAItoDeposit = await convertToCurrencyDecimals ( dai . address , '1000' ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
await pool . connect ( users [ 0 ] . signer ) . deposit ( dai . address , amountDAItoDeposit , '0' ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
await aDai . connect ( users [ 0 ] . signer ) . transfer ( users [ 1 ] . address , amountDAItoDeposit ) ;
2020-06-09 14:36:33 +00:00
2020-06-12 07:41:30 +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
2020-07-13 08:54:08 +00:00
expect ( fromBalance . toString ( ) ) . to . be . equal ( '0' , INVALID_FROM_BALANCE_AFTER_TRANSFER ) ;
2020-06-09 14:36:33 +00:00
expect ( toBalance . toString ( ) ) . to . be . equal (
amountDAItoDeposit . toString ( ) ,
2020-06-12 08:39:42 +00:00
INVALID_TO_BALANCE_AFTER_TRANSFER
2020-06-09 14:36:33 +00:00
) ;
2020-06-09 09:11:19 +00:00
} ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
it ( 'User 1 redirects interest to user 2, transfers 500 DAI back to user 0' , async ( ) = > {
2020-06-12 07:41:30 +00:00
const { users , aDai , dai } = testEnv ;
2020-07-13 08:54:08 +00:00
await aDai . connect ( users [ 1 ] . signer ) . redirectInterestStream ( users [ 2 ] . address ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
const aDAIRedirected = await convertToCurrencyDecimals ( dai . address , '1000' ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
const aDAItoTransfer = await convertToCurrencyDecimals ( dai . address , '500' ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
const user2RedirectedBalanceBefore = await aDai . getRedirectedBalance ( users [ 2 ] . address ) ;
2020-06-10 08:31:33 +00:00
expect ( user2RedirectedBalanceBefore . toString ( ) ) . to . be . equal (
aDAIRedirected ,
2020-06-12 08:39:42 +00:00
INVALID_REDIRECTED_BALANCE_BEFORE_TRANSFER
2020-06-10 08:31:33 +00:00
) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
await aDai . connect ( users [ 1 ] . signer ) . transfer ( users [ 0 ] . address , aDAItoTransfer ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
const user2RedirectedBalanceAfter = await aDai . getRedirectedBalance ( users [ 2 ] . address ) ;
const user1RedirectionAddress = await aDai . getInterestRedirectionAddress ( 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 (
aDAItoTransfer ,
2020-06-12 08:39:42 +00:00
INVALID_REDIRECTED_BALANCE_BEFORE_TRANSFER
2020-06-10 08:31:33 +00:00
) ;
expect ( user1RedirectionAddress . toString ( ) ) . to . be . equal (
2020-06-10 15:01:32 +00:00
users [ 2 ] . address ,
2020-06-12 08:39:42 +00:00
INVALID_REDIRECTION_ADDRESS
2020-06-10 08:31:33 +00:00
) ;
} ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
it ( 'User 0 transfers back to user 1' , async ( ) = > {
2020-08-13 11:06:23 +00:00
const { users , aDai , dai , weth } = testEnv ;
2020-07-13 08:54:08 +00:00
const aDAItoTransfer = await convertToCurrencyDecimals ( dai . address , '500' ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
await aDai . connect ( users [ 0 ] . signer ) . transfer ( users [ 1 ] . address , aDAItoTransfer ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
const user2RedirectedBalanceAfter = await aDai . getRedirectedBalance ( users [ 2 ] . address ) ;
2020-06-08 12:03:40 +00:00
2020-06-12 07:41:30 +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 ( ) ,
2020-06-12 08:39:42 +00:00
INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER
2020-06-10 08:31:33 +00:00
) ;
} ) ;
2020-06-08 12:03:40 +00:00
2020-08-13 17:25:29 +00:00
it ( 'User 0 deposits 1 WETH and user 1 tries to borrow, but the aTokens received as a transfer are not available as collateral (revert expected)' , async ( ) = > {
2020-08-13 11:06:23 +00:00
const { users , pool , weth } = testEnv ;
2020-06-10 15:01:32 +00:00
2020-08-13 17:25:29 +00:00
await weth . connect ( users [ 0 ] . signer ) . mint ( await convertToCurrencyDecimals ( weth . address , '1' ) ) ;
await weth . connect ( users [ 0 ] . signer ) . approve ( pool . address , APPROVAL_AMOUNT_LENDING_POOL ) ;
2020-09-02 15:54:34 +00:00
await pool . connect ( users [ 0 ] . signer ) . deposit ( weth . address , ethers . utils . parseEther ( '1.0' ) , '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-09-09 12:33:05 +00:00
. borrow (
weth . address ,
ethers . utils . parseEther ( '0.1' ) ,
RateMode . Stable ,
AAVE_REFERRAL ,
users [ 1 ] . address
) ,
2020-09-02 15:54:34 +00:00
COLLATERAL_BALANCE_IS_0
) . to . be . revertedWith ( COLLATERAL_BALANCE_IS_0 ) ;
2020-06-10 08:31:33 +00:00
} ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
it ( 'User 1 sets the DAI as collateral and borrows, tries to transfer everything back to user 0 (revert expected)' , async ( ) = > {
2020-08-13 11:06:23 +00:00
const { users , pool , aDai , dai , weth } = testEnv ;
2020-07-13 08:54:08 +00:00
await pool . connect ( users [ 1 ] . signer ) . setUserUseReserveAsCollateral ( dai . address , true ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +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-09-09 12:33:05 +00:00
. borrow (
weth . address ,
ethers . utils . parseEther ( '0.1' ) ,
RateMode . Stable ,
AAVE_REFERRAL ,
users [ 1 ] . address
) ;
2020-06-10 11:46:42 +00:00
await expect (
2020-06-12 07:41:30 +00:00
aDai . connect ( users [ 1 ] . signer ) . transfer ( users [ 0 ] . address , aDAItoTransfer ) ,
2020-09-02 15:54:34 +00:00
TRANSFER_NOT_ALLOWED
) . to . be . revertedWith ( TRANSFER_NOT_ALLOWED ) ;
2020-06-10 11:46:42 +00:00
} ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
it ( 'User 0 tries to transfer 0 balance (revert expected)' , async ( ) = > {
2020-08-13 11:06:23 +00:00
const { users , pool , aDai , dai , weth } = testEnv ;
2020-06-10 11:46:42 +00:00
await expect (
2020-07-13 08:54:08 +00:00
aDai . connect ( users [ 0 ] . signer ) . transfer ( users [ 1 ] . address , '0' ) ,
2020-09-02 15:54:34 +00:00
TRANSFER_AMOUNT_NOT_GT_0
) . to . be . revertedWith ( TRANSFER_AMOUNT_NOT_GT_0 ) ;
2020-06-10 11:46:42 +00:00
} ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
it ( 'User 1 repays the borrow, transfers aDAI back to user 0' , async ( ) = > {
2020-08-13 11:06:23 +00:00
const { users , pool , aDai , dai , weth } = testEnv ;
2020-09-02 15:54:34 +00:00
2020-08-13 17:25:29 +00:00
await weth . connect ( users [ 1 ] . signer ) . mint ( await convertToCurrencyDecimals ( weth . address , '2' ) ) ;
await weth . connect ( users [ 1 ] . signer ) . approve ( pool . address , APPROVAL_AMOUNT_LENDING_POOL ) ;
2020-09-02 15:54:34 +00:00
2020-06-10 15:01:32 +00:00
await pool
. connect ( users [ 1 ] . signer )
2020-08-13 17:25:29 +00:00
. repay ( weth . address , MAX_UINT_AMOUNT , RateMode . Stable , users [ 1 ] . address ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
const aDAItoTransfer = await convertToCurrencyDecimals ( aDai . address , '1000' ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
await aDai . connect ( users [ 1 ] . signer ) . transfer ( users [ 0 ] . address , aDAItoTransfer ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
const user2RedirectedBalanceAfter = await aDai . getRedirectedBalance ( users [ 2 ] . address ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
const user1RedirectionAddress = await aDai . getInterestRedirectionAddress ( users [ 1 ] . address ) ;
2020-06-08 12:03:40 +00:00
2020-06-10 11:46:42 +00:00
expect ( user2RedirectedBalanceAfter . toString ( ) ) . to . be . equal (
2020-07-13 08:54:08 +00:00
'0' ,
2020-06-12 08:39:42 +00:00
INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER
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 ( user1RedirectionAddress . toString ( ) ) . to . be . equal (
ZERO_ADDRESS ,
2020-06-12 08:39:42 +00:00
INVALID_REDIRECTION_ADDRESS
2020-06-10 11:46:42 +00:00
) ;
} ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +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-08-13 11:06:23 +00:00
const { users , pool , aDai , dai , weth } = testEnv ;
2020-06-10 15:01:32 +00:00
2020-07-13 08:54:08 +00:00
let aDAItoTransfer = await convertToCurrencyDecimals ( aDai . address , '500' ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
await aDai . connect ( users [ 0 ] . signer ) . redirectInterestStream ( users [ 2 ] . address ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
await aDai . connect ( users [ 0 ] . signer ) . transfer ( users [ 1 ] . address , aDAItoTransfer ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
await aDai . connect ( users [ 1 ] . signer ) . redirectInterestStream ( users [ 3 ] . address ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
aDAItoTransfer = await convertToCurrencyDecimals ( aDai . address , '100' ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
await aDai . connect ( users [ 0 ] . signer ) . transfer ( users [ 1 ] . address , aDAItoTransfer ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +00:00
const user2RedirectedBalanceAfter = await aDai . getRedirectedBalance ( users [ 2 ] . address ) ;
const user3RedirectedBalanceAfter = await aDai . getRedirectedBalance ( users [ 3 ] . address ) ;
2020-06-08 12:03:40 +00:00
2020-07-13 08:54:08 +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 ,
2020-06-12 08:39:42 +00:00
INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER
2020-06-10 11:46:42 +00:00
) ;
expect ( user3RedirectedBalanceAfter . toString ( ) ) . to . be . equal (
expectedUser3Redirected ,
2020-06-12 08:39:42 +00:00
INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER
2020-06-10 11:46:42 +00:00
) ;
} ) ;
2020-07-14 12:43:43 +00:00
it ( 'User 1 transfers the whole amount to himself' , async ( ) = > {
const { users , pool , aDai , dai } = testEnv ;
const user1BalanceBefore = await aDai . balanceOf ( users [ 1 ] . address ) ;
await aDai . connect ( users [ 1 ] . signer ) . transfer ( users [ 1 ] . address , user1BalanceBefore ) ;
const user1BalanceAfter = await aDai . balanceOf ( users [ 1 ] . address ) ;
expect ( user1BalanceAfter . toString ( ) ) . to . be . equal (
user1BalanceBefore ,
INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER
) ;
} ) ;
2020-06-09 09:11:19 +00:00
} ) ;