Skip to content

Commit

Permalink
add borrow liquidity endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
janndriessen committed Jun 30, 2021
1 parent 4a6bb1a commit c509197
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
48 changes: 48 additions & 0 deletions api/api/borrow/liquidity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { BigNumber } from "bignumber.js";
import { ethers, providers } from "ethers";
import { VercelRequest, VercelResponse } from "@vercel/node";

const contractAddress = "0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B";
const provider = new providers.JsonRpcProvider("http://localhost:8545");

// Comptroller
const abi = [
// Read-Only Functions
"function getAccountLiquidity(address account) view returns (uint, uint, uint)",
"function markets(address cTokenAddress) view returns (bool, uint, bool)"
];

export default async (req: VercelRequest, res: VercelResponse) => {
if (req.body === undefined || req.body.amount === undefined) {
res.status(400).json({ error: "No borrow amount provided." });
return;
}

const amount = req.body.amount;
const cTokenAddress = "0x39AA39c021dfbaE8faC545936693aC917d5E7563";
const contract = new ethers.Contract(contractAddress, abi, provider);

const market = await contract.markets(cTokenAddress);
const { 1: collateralFactorMantissa } = market;

let collateralFactor = new BigNumber(collateralFactorMantissa.toString());
collateralFactor = collateralFactor.shiftedBy(-18);
const collateralFactorSafetyAdjusted = collateralFactor
.minus(0.05)
.toNumber();
console.log(
amount,
collateralFactorMantissa,
collateralFactor.toNumber(),
collateralFactorSafetyAdjusted
);

// amount divided by collateral factor minus safety of 5%
// we would never want to get our user liquidated
// (probably there is a better way to calc this)
const collateral = amount / collateralFactorSafetyAdjusted;
res.json({
borrowAmount: amount,
collateralNeeded: Math.ceil(collateral)
});
};
1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"dependencies": {
"@compound-finance/compound-js": "^0.2.13",
"bignumber.js": "^9.0.1",
"ethers": "^5.3.1",
"web3": "^1.3.6"
},
Expand Down

0 comments on commit c509197

Please sign in to comment.