From da542c66f7c17156ac3411401da993c2b7d5e810 Mon Sep 17 00:00:00 2001 From: macket Date: Fri, 11 Aug 2023 12:15:01 +0400 Subject: [PATCH 1/4] feat: leveragePriceImpact --- src/llammas/LlammaTemplate.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/llammas/LlammaTemplate.ts b/src/llammas/LlammaTemplate.ts index beab319..02a2461 100644 --- a/src/llammas/LlammaTemplate.ts +++ b/src/llammas/LlammaTemplate.ts @@ -93,6 +93,7 @@ export class LlammaTemplate { createLoanHealth: (collateral: number | string, debt: number | string, range: number, full?: boolean, address?: string) => Promise, createLoanIsApproved: (collateral: number | string) => Promise, createLoanApprove: (collateral: number | string) => Promise, + priceImpact: (collateral: number | string, debt: number | string) => Promise, createLoan: (collateral: number | string, debt: number | string, range: number, slippage?: number) => Promise, estimateGas: { createLoanApprove: (collateral: number | string) => Promise, @@ -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), @@ -1527,6 +1529,21 @@ export class LlammaTemplate { return ethers.utils.formatUnits(_health); } + public async leveragePriceImpact(collateral: number | string, debt: number | string): Promise { + 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 { if (await this.loanExists()) throw Error("Loan already created"); this._checkRange(range); From f182bccaf126aa2470f204aeac6c2476d940b92b Mon Sep 17 00:00:00 2001 From: macket Date: Fri, 11 Aug 2023 12:18:07 +0400 Subject: [PATCH 2/4] docs: leveragePriceImpact --- README.md | 2 ++ test/readme.test.ts | 1 + 2 files changed, 3 insertions(+) diff --git a/README.md b/README.md index e74f474..0d03bff 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/test/readme.test.ts b/test/readme.test.ts index 5e979b5..86d4a46 100644 --- a/test/readme.test.ts +++ b/test/readme.test.ts @@ -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 From dbf2192dc4bae3ca1aec865b3e931db4876347ea Mon Sep 17 00:00:00 2001 From: macket Date: Fri, 11 Aug 2023 12:24:14 +0400 Subject: [PATCH 3/4] fix: mul maxRecv by 0.999 --- src/llammas/LlammaTemplate.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/llammas/LlammaTemplate.ts b/src/llammas/LlammaTemplate.ts index 02a2461..4d74556 100644 --- a/src/llammas/LlammaTemplate.ts +++ b/src/llammas/LlammaTemplate.ts @@ -1299,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]); @@ -1329,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); @@ -1361,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, From 1e810bf8b2cc018aaca539b223a2c849eabc707d Mon Sep 17 00:00:00 2001 From: macket Date: Fri, 11 Aug 2023 12:25:55 +0400 Subject: [PATCH 4/4] build: v1.3.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 77a6c81..6949333 100644 --- a/package.json +++ b/package.json @@ -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",