Skip to content

Commit

Permalink
Merge pull request #11 from curvefi/leverage
Browse files Browse the repository at this point in the history
Leverage: price impact
  • Loading branch information
Macket authored Aug 11, 2023
2 parents acd8686 + 1e810bf commit 2c03437
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,8 @@ import crvusd from "@curvefi/stablecoin-api";
// 203.0010181561119221
await llamma.leverage.createLoanHealth(1, 1000, 5, false); // NOT FULL
// 3.6596075146233826
await llamma.leverage.priceImpact(1, 1000);
// 0.0007 %

await llamma.leverage.createLoanIsApproved(1);
// false
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@curvefi/stablecoin-api",
"version": "1.3.2",
"version": "1.3.3",
"description": "JavaScript library for Curve Stablecoin",
"main": "lib/index.js",
"author": "Macket",
Expand Down
29 changes: 23 additions & 6 deletions src/llammas/LlammaTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class LlammaTemplate {
createLoanHealth: (collateral: number | string, debt: number | string, range: number, full?: boolean, address?: string) => Promise<string>,
createLoanIsApproved: (collateral: number | string) => Promise<boolean>,
createLoanApprove: (collateral: number | string) => Promise<string[]>,
priceImpact: (collateral: number | string, debt: number | string) => Promise<string>,
createLoan: (collateral: number | string, debt: number | string, range: number, slippage?: number) => Promise<string>,
estimateGas: {
createLoanApprove: (collateral: number | string) => Promise<number>,
Expand Down Expand Up @@ -168,6 +169,7 @@ export class LlammaTemplate {
createLoanHealth: this.leverageCreateLoanHealth.bind(this),
createLoanIsApproved: this.createLoanIsApproved.bind(this),
createLoanApprove: this.createLoanApprove.bind(this),
priceImpact: this.leveragePriceImpact.bind(this),
createLoan: this.leverageCreateLoan.bind(this),
estimateGas: {
createLoanApprove: this.createLoanApproveEstimateGas.bind(this),
Expand Down Expand Up @@ -1297,8 +1299,8 @@ export class LlammaTemplate {
calls.push(crvusd.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, range, i));
}
const _res: ethers.BigNumber[][] = await crvusd.multicallProvider.all(calls);
const _maxBorrowable = _res.map((r) => r[0]);
const _maxCollateral = _res.map((r) => r[1]);
const _maxBorrowable = _res.map((r) => r[0].mul(999).div(1000));
const _maxCollateral = _res.map((r) => r[1].mul(999).div(1000));
const routeIdx = this._getBestIdx(_maxCollateral);

const maxBorrowable = crvusd.formatUnits(_maxBorrowable[routeIdx]);
Expand Down Expand Up @@ -1327,8 +1329,8 @@ export class LlammaTemplate {
const res: IDict<{ maxBorrowable: string, maxCollateral: string, leverage: string, routeIdx: number }> = {};
for (let N = this.minBands; N <= this.maxBands; N++) {
const _res = _rawRes.splice(0, 5);
const _maxBorrowable = _res.map((r) => r[0]);
const _maxCollateral = _res.map((r) => r[1]);
const _maxBorrowable = _res.map((r) => r[0].mul(999).div(1000));
const _maxCollateral = _res.map((r) => r[1].mul(999).div(1000));
const routeIdx = this._getBestIdx(_maxCollateral);
const maxBorrowable = crvusd.formatUnits(_maxBorrowable[routeIdx]);
const maxCollateral = crvusd.formatUnits(_maxCollateral[routeIdx], this.collateralDecimals);
Expand Down Expand Up @@ -1359,8 +1361,8 @@ export class LlammaTemplate {

const res: IDict<{ maxBorrowable: string, maxCollateral: string, leverage: string }> = {};
for (let N = this.minBands; N <= this.maxBands; N++) {
const maxBorrowable = crvusd.formatUnits(_res[N - this.minBands][0]);
const maxCollateral = crvusd.formatUnits(_res[N - this.minBands][1], this.collateralDecimals);
const maxBorrowable = crvusd.formatUnits(_res[N - this.minBands][0].mul(999).div(1000));
const maxCollateral = crvusd.formatUnits(_res[N - this.minBands][1].mul(999).div(1000), this.collateralDecimals);
res[N] = {
maxBorrowable,
maxCollateral,
Expand Down Expand Up @@ -1527,6 +1529,21 @@ export class LlammaTemplate {
return ethers.utils.formatUnits(_health);
}

public async leveragePriceImpact(collateral: number | string, debt: number | string): Promise<string> {
const x_BN = BN(debt);
const small_x_BN = BN(100);
const { _collateral, routeIdx } = await this._leverageCreateLoanCollateral(collateral, debt);
const _y = _collateral.sub(parseUnits(collateral, this.collateralDecimals));
const _small_y = await crvusd.contracts[this.leverageZap].contract.get_collateral(fromBN(small_x_BN), routeIdx);
const y_BN = toBN(_y, this.collateralDecimals);
const small_y_BN = toBN(_small_y, this.collateralDecimals);
const rateBN = y_BN.div(x_BN);
const smallRateBN = small_y_BN.div(small_x_BN);
if (rateBN.gt(smallRateBN)) return "0.0";

return BN(1).minus(rateBN.div(smallRateBN)).times(100).toFixed(4);
}

private async _leverageCreateLoan(collateral: number | string, debt: number | string, range: number, slippage: number, estimateGas: boolean): Promise<string | number> {
if (await this.loanExists()) throw Error("Loan already created");
this._checkRange(range);
Expand Down
1 change: 1 addition & 0 deletions test/readme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ const leverageTest = async () => {
console.log(await llamma.leverage.createLoanPrices(1, 1000, 5));
console.log(await llamma.leverage.createLoanHealth(1, 1000, 5)); // FULL
console.log(await llamma.leverage.createLoanHealth(1, 1000, 5, false)); // NOT FULL
console.log(await llamma.leverage.priceImpact(1, 1000));

console.log(await llamma.leverage.createLoanIsApproved(1));
// false
Expand Down

0 comments on commit 2c03437

Please sign in to comment.