-
Notifications
You must be signed in to change notification settings - Fork 9
/
borrow.ts
34 lines (26 loc) · 1.52 KB
/
borrow.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
import { getContractsFactory } from '../../../src/index';
/*
* Note that to borrow, you must have a certain amount of assets lent to the compound protocol.
* You must also explicitly enable those assets to be used as collateral
*/
const collateralTokenName = 'cDAI';
const borrowTokenName = 'USDC';
const compoundTokenName = 'cUSDC';
const borrowAmount = 1e5;
const compoundComptroller = getContractsFactory('eth').getContract('CompoundComptroller').instance();
const compoundCollateralTokenContract = getContractsFactory('eth').getContract('Compound').instance(collateralTokenName);
// First we need to "enter the market" for our collateral token. This assumes that we already have some cTokens to use
let { data, amount } = compoundComptroller.methods()
.enterMarkets.call({ cTokens: [compoundCollateralTokenContract.address] });
console.log(`\nTo enter ${collateralTokenName} as collateral for borrowing, send:`);
console.log(`Data: ${data}`);
console.log(`Amount: ${amount} ETH`);
console.log(`To: ${compoundComptroller.address}`);
// Once the above tx is confirmed, we can borrow our desired underlying asset
const compoundTokenContract = getContractsFactory('eth').getContract('Compound').instance(compoundTokenName);
({ data, amount } = compoundTokenContract.methods()
.borrow.call({ borrowAmount: borrowAmount.toString(10) }));
console.log(`\nTo borrow ${borrowAmount} ${borrowTokenName} from compound, send:`);
console.log(`Data: ${data}`);
console.log(`Amount: ${amount} ETH`);
console.log(`To: ${compoundTokenContract.address}`);