Skip to content

Commit

Permalink
fix: Fix errors in atoken repay tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmtzinf committed Sep 7, 2021
1 parent 80b2685 commit d056de8
Showing 1 changed file with 22 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { expect } from 'chai';
import { utils } from 'ethers';
import { MAX_UINT_AMOUNT } from '../helpers/constants';
import { setBlocktime, timeLatest } from '../helpers/misc-utils';
import { TestEnv, makeSuite } from './helpers/make-suite';
import { MAX_UINT_AMOUNT, MAX_SUPPLY_CAP } from '../helpers/constants';
import { ProtocolErrors } from '../helpers/types';
import { MintableERC20, WETH9Mocked } from '../types';
import { parseEther } from '@ethersproject/units';
import { BigNumber } from '@ethersproject/bignumber';
import { setBlocktime, timeLatest, waitForTx } from '../helpers/misc-utils';
import { zeroPad } from '@ethersproject/bytes';

const { expect } = require('chai');

makeSuite('Repay with aTokens', (testEnv: TestEnv) => {
makeSuite('AToken: Repay', (testEnv: TestEnv) => {
before('User 0 deposits 100 DAI, user 1 deposits 1 WETH, borrows 50 DAI', async () => {
const {
weth,
Expand All @@ -18,22 +13,22 @@ makeSuite('Repay with aTokens', (testEnv: TestEnv) => {
users: [user0, user1],
} = testEnv;

const daiAmount = parseEther('100');
const wethAmount = parseEther('1');
const daiAmount = utils.parseEther('100');
const wethAmount = utils.parseEther('1');
await dai.connect(user0.signer).mint(daiAmount);
await weth.connect(user1.signer).mint(wethAmount);

await dai.connect(user0.signer).approve(pool.address, MAX_UINT_AMOUNT);
await weth.connect(user1.signer).approve(pool.address, MAX_UINT_AMOUNT);

await waitForTx(
await expect(
await pool.connect(user0.signer).deposit(dai.address, daiAmount, user0.address, 0)
);
await waitForTx(
await expect(
await pool.connect(user1.signer).deposit(weth.address, wethAmount, user1.address, 0)
);

await waitForTx(
await expect(
await pool.connect(user1.signer).borrow(dai.address, daiAmount.div(2), 2, 0, user1.address)
);
});
Expand All @@ -44,7 +39,7 @@ makeSuite('Repay with aTokens', (testEnv: TestEnv) => {
dai,
users: [, user1],
} = testEnv;
const repayAmount = parseEther('25');
const repayAmount = utils.parseEther('25');

await expect(
pool.connect(user1.address).repayWithATokens(dai.address, repayAmount, 2, user1.address)
Expand All @@ -60,30 +55,25 @@ makeSuite('Repay with aTokens', (testEnv: TestEnv) => {
users: [user0, user1],
} = testEnv;

const repayAmount = parseEther('25');
const repayAmount = utils.parseEther('25');

await waitForTx(await aDai.connect(user0.signer).transfer(user1.address, repayAmount));
await expect(await aDai.connect(user0.signer).transfer(user1.address, repayAmount));

const time = await timeLatest();

await setBlocktime(time.plus(1).toNumber());
await setBlocktime(time.add(1).toNumber());

const balanceBefore: BigNumber = await aDai.balanceOf(user1.address, { blockTag: 'pending' });
const balanceBefore = await aDai.balanceOf(user1.address, { blockTag: 'pending' });
const debtBefore = await variableDebtDai.balanceOf(user1.address, { blockTag: 'pending' });

await waitForTx(
await expect(
await pool.connect(user1.signer).repayWithATokens(dai.address, repayAmount, 2, user1.address)
);

const balanceAfter = await aDai.balanceOf(user1.address);
const debtAfter = await variableDebtDai.balanceOf(user1.address);

expect(balanceAfter.toString()).to.be.bignumber.almostEqual(
balanceBefore.sub(repayAmount).toString()
);
expect(debtAfter.toString()).to.be.bignumber.almostEqual(
debtBefore.sub(repayAmount).toString()
);
expect(balanceAfter).to.be.closeTo(balanceBefore.sub(repayAmount), 2);
expect(debtAfter).to.be.closeTo(debtBefore.sub(repayAmount), 2);
});

it('User 0 repays the rest of the debt on behalf of user 1 using aTokens', async () => {
Expand All @@ -97,21 +87,19 @@ makeSuite('Repay with aTokens', (testEnv: TestEnv) => {

const time = await timeLatest();

await setBlocktime(time.plus(1).toNumber());
await setBlocktime(time.add(1).toNumber());

const balanceBefore: BigNumber = await aDai.balanceOf(user0.address, { blockTag: 'pending' });
const balanceBefore = await aDai.balanceOf(user0.address, { blockTag: 'pending' });
const debtBefore = await variableDebtDai.balanceOf(user1.address, { blockTag: 'pending' });

await waitForTx(
await expect(
await pool.connect(user0.signer).repayWithATokens(dai.address, debtBefore, 2, user1.address)
);

const balanceAfter = await aDai.balanceOf(user0.address);
const debtAfter = await variableDebtDai.balanceOf(user1.address);

expect(balanceAfter.toString()).to.be.bignumber.almostEqual(
balanceBefore.sub(debtBefore).toString()
);
expect(balanceAfter).to.be.closeTo(balanceBefore.sub(debtBefore), 2);
expect(debtAfter).to.be.equal(0);
});
});

0 comments on commit d056de8

Please sign in to comment.