generated from 0xDaksh/solidity-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
02_borrow.test.ts
57 lines (48 loc) · 2.07 KB
/
02_borrow.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { ComptrollerInterface, CTokenInterface, ERC20 } from "../../typechain";
import { useChai } from "../utils";
import { compoundFixture } from "./utils/fixtures";
import { waffle } from "hardhat";
const expect = useChai();
describe("Compound: Borrow and Payback", function () {
let wallet: SignerWithAddress;
let USDC: ERC20;
let DAI: ERC20;
let cUSDC: CTokenInterface;
let cDAI: CTokenInterface;
let comptroller: ComptrollerInterface;
before(async function () {
// go read the code for this fixture to understand
// how we get the wallet, erc20 USDC token, ctoken of USDC, etc.
({ wallet, USDC, DAI, cUSDC, cDAI, comptroller } = await waffle.loadFixture(compoundFixture));
});
it("Compound: deposit USDC as collateral and borrow DAI", async () => {
// approve cUSDC to take out 1000 USDC from wallet
await USDC.approve(cUSDC.address, 1000);
// deposit 1000 USDC tokens and get the cUSDC in return
await cUSDC.mint(1000);
// set USDC as collateral
const markets = [cUSDC.address];
await comptroller.enterMarkets(markets);
// using that collateral, let's borrow some DAI
// but we can only borrow ~80% of what our collateral is
await cDAI.borrow(799);
// check balanceOf DAI
expect(await DAI.balanceOf(wallet.address)).to.eq(799);
});
it("Compound: check the borrow balance of DAI", async () => {
const bal = await cDAI.callStatic.borrowBalanceCurrent(wallet.address);
expect(bal).to.eq(799);
});
it("Compound: repay the borrowed DAI", async () => {
// approve cDAI to take out 799 DAI from your account
await DAI.approve(cDAI.address, 799);
// repay the borrowed amount
await cDAI.repayBorrow(799);
// note: there's also a repayBorrowBehalf that allows you to repay someone else's debt
// cDAI borrowBalanceCurrent should be 0 now
expect(await cDAI.callStatic.borrowBalanceCurrent(wallet.address)).to.eq(0);
// DAI balance should be 0 now
expect(await DAI.balanceOf(wallet.address)).to.eq(0);
});
});