-
Notifications
You must be signed in to change notification settings - Fork 9
/
repay-full-borrow-example.js
218 lines (173 loc) · 8.41 KB
/
repay-full-borrow-example.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const assert = require('assert');
const { TASK_NODE_CREATE_SERVER } = require('hardhat/builtin-tasks/task-names');
const hre = require('hardhat');
const ethers = require('ethers');
const { resetForkedChain } = require('./common.js');
const networks = require('./addresses.json');
const net = hre.config.cometInstance;
const jsonRpcUrl = 'http://127.0.0.1:8545';
const providerUrl = hre.config.networks.hardhat.forking.url;
const blockNumber = hre.config.networks.hardhat.forking.blockNumber;
const cometAbi = [
'event Supply(address indexed from, address indexed dst, uint256 amount)',
'function supply(address asset, uint amount)',
'function withdraw(address asset, uint amount)',
'function balanceOf(address account) returns (uint256)',
'function borrowBalanceOf(address account) returns (uint256)',
'function collateralBalanceOf(address account, address asset) external view returns (uint128)',
];
const wethAbi = [
'function deposit() payable',
'function balanceOf(address) returns (uint)',
'function approve(address, uint) returns (bool)',
'function transfer(address, uint)',
];
const stdErc20Abi = [
'function approve(address, uint) returns (bool)',
'function transfer(address, uint)',
];
const myContractAbi = [
'function supply(address asset, uint amount) public',
'function withdraw(address asset, uint amount) public',
'function repayFullBorrow(address baseAsset) public',
];
let jsonRpcServer, deployment, cometAddress, myContractFactory, baseAssetAddress, wethAddress;
const mnemonic = hre.network.config.accounts.mnemonic;
const addresses = [];
const privateKeys = [];
for (let i = 0; i < 20; i++) {
const wallet = new ethers.Wallet.fromMnemonic(mnemonic, `m/44'/60'/0'/0/${i}`);
addresses.push(wallet.address);
privateKeys.push(wallet._signingKey().privateKey);
}
describe("Repay an entire Compound III account's borrow", function () {
before(async () => {
console.log('\n Running a hardhat local evm fork of a public net...\n');
jsonRpcServer = await hre.run(TASK_NODE_CREATE_SERVER, {
hostname: '127.0.0.1',
port: 8545,
provider: hre.network.provider
});
await jsonRpcServer.listen();
baseAssetAddress = networks[net].USDC;
usdcAddress = baseAssetAddress;
cometAddress = networks[net].comet;
wethAddress = networks[net].WETH;
myContractFactory = await hre.ethers.getContractFactory('MyContract');
});
beforeEach(async () => {
await resetForkedChain(hre, providerUrl, blockNumber);
deployment = await myContractFactory.deploy(cometAddress);
});
after(async () => {
await jsonRpcServer.close();
});
it('Repays an entire borrow without missing latest block interest using JS', async () => {
const provider = new ethers.providers.JsonRpcProvider(jsonRpcUrl);
const signer = provider.getSigner(addresses[0]);
const comet = new ethers.Contract(cometAddress, cometAbi, signer);
const weth = new ethers.Contract(wethAddress, wethAbi, signer);
const usdc = new ethers.Contract(usdcAddress, stdErc20Abi, signer);
const baseAssetMantissa = 1e6; // USDC has 6 decimal places
let tx = await weth.deposit({ value: ethers.utils.parseEther('10') });
await tx.wait(1);
console.log('\tApproving Comet to move WETH collateral...');
tx = await weth.approve(cometAddress, ethers.constants.MaxUint256);
await tx.wait(1);
console.log('\tSending initial supply to Compound...');
tx = await comet.supply(wethAddress, ethers.utils.parseEther('10'));
await tx.wait(1);
// Accounts cannot hold a borrow smaller than baseBorrowMin (100 USDC).
const borrowSize = 1000;
console.log('\tExecuting initial borrow of the base asset from Compound...');
console.log('\tBorrow size:', borrowSize);
// Do borrow
tx = await comet.withdraw(usdcAddress, (borrowSize * baseAssetMantissa).toString());
await tx.wait(1);
let borrowBalance = await comet.callStatic.borrowBalanceOf(addresses[0]);
console.log('\tBorrow Balance initial', +borrowBalance.toString() / baseAssetMantissa);
// accrue some interest
console.log('\tFast forwarding 100 blocks to accrue some borrower interest...');
await advanceBlockHeight(100);
borrowBalance = await comet.callStatic.borrowBalanceOf(addresses[0]);
console.log('\tBorrow Balance after some interest accrued', +borrowBalance.toString() / baseAssetMantissa);
// For example purposes, get extra USDC so we can pay off the
// original borrow plus the accrued borrower interest
await seedWithBaseToken(addresses[0], 5);
tx = await usdc.approve(cometAddress, ethers.constants.MaxUint256);
await tx.wait(1);
console.log('\tRepaying the entire borrow...');
tx = await comet.supply(usdcAddress, ethers.constants.MaxUint256);
await tx.wait(1);
borrowBalance = await comet.callStatic.borrowBalanceOf(addresses[0]);
console.log('\tBorrow Balance after full repayment', +borrowBalance.toString() / baseAssetMantissa);
});
it('Repays an entire borrow without missing latest block interest using Solidity', async () => {
const me = addresses[0];
const provider = new ethers.providers.JsonRpcProvider(jsonRpcUrl);
const signer = provider.getSigner(me);
const comet = new ethers.Contract(cometAddress, cometAbi, signer);
const MyContract = new ethers.Contract(deployment.address, myContractAbi, signer);
const weth = new ethers.Contract(wethAddress, wethAbi, signer);
const wethMantissa = 1e18; // WETH and ETH have 18 decimal places
const usdc = new ethers.Contract(baseAssetAddress, stdErc20Abi, signer);
const baseAssetMantissa = 1e6; // USDC has 6 decimal places
let tx = await weth.deposit({ value: ethers.utils.parseEther('10') });
await tx.wait(1);
console.log('\tTransferring WETH to MyContract to use as collateral...');
tx = await weth.transfer(MyContract.address, ethers.utils.parseEther('10'));
await tx.wait(1);
console.log('\tSending initial supply to Compound...');
tx = await MyContract.supply(wethAddress, ethers.utils.parseEther('10'));
await tx.wait(1);
// Accounts cannot hold a borrow smaller than baseBorrowMin (100 USDC).
const borrowSize = 1000;
console.log('\tExecuting initial borrow of the base asset from Compound...');
console.log('\tBorrow size:', borrowSize);
// Do borrow
tx = await MyContract.withdraw(usdcAddress, (borrowSize * baseAssetMantissa).toString());
await tx.wait(1);
// accrue some interest
console.log('\tFast forwarding 100 blocks to accrue some borrower interest...');
await advanceBlockHeight(100);
borrowBalance = await comet.callStatic.borrowBalanceOf(MyContract.address);
console.log('\tBorrow Balance after some interest accrued', +borrowBalance.toString() / baseAssetMantissa);
// For example purposes, get extra USDC so we can pay off the
// original borrow plus the accrued borrower interest
await seedWithBaseToken(MyContract.address, 5);
console.log('\tRepaying the entire borrow...');
tx = await MyContract.repayFullBorrow(usdcAddress);
await tx.wait(1);
borrowBalance = await comet.callStatic.borrowBalanceOf(MyContract.address);
console.log('\tBorrow Balance after full repayment', +borrowBalance.toString() / baseAssetMantissa);
});
});
async function advanceBlockHeight(blocks) {
const txns = [];
for (let i = 0; i < blocks; i++) {
txns.push(hre.network.provider.send('evm_mine'));
}
await Promise.all(txns);
}
// Test account index 9 uses Comet to borrow and then seed the toAddress with tokens
async function seedWithBaseToken(toAddress, amt) {
const baseTokenDecimals = 6; // USDC
const provider = new ethers.providers.JsonRpcProvider(jsonRpcUrl);
const signer = provider.getSigner(addresses[9]);
const comet = new ethers.Contract(cometAddress, cometAbi, signer);
const weth = new ethers.Contract(wethAddress, wethAbi, signer);
const usdc = new ethers.Contract(usdcAddress, stdErc20Abi, signer);
let tx = await weth.deposit({ value: ethers.utils.parseEther('10') });
await tx.wait(1);
tx = await weth.approve(cometAddress, ethers.constants.MaxUint256);
await tx.wait(1);
tx = await comet.supply(wethAddress, ethers.utils.parseEther('10'));
await tx.wait(1);
// baseBorrowMin is 1000 USDC
tx = await comet.withdraw(usdcAddress, (1000 * 1e6).toString());
await tx.wait(1);
// transfer from this account to the main test account (0th)
tx = await usdc.transfer(toAddress, (amt * 1e6).toString());
await tx.wait(1);
return;
}