diff --git a/package.json b/package.json index 399a6b6..dfcc028 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@curvefi/lending-api", - "version": "2.3.4", + "version": "2.3.5", "description": "JavaScript library for Curve Lending", "main": "lib/index.js", "author": "Macket", diff --git a/src/index.ts b/src/index.ts index 31dfea5..e43ef83 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,6 +20,7 @@ import { maxDeposit, previewDeposit, depositIsApproved, + depositAllowance, depositApproveEstimateGas, depositApprove, depositEstimateGas, @@ -27,6 +28,7 @@ import { maxMint, previewMint, mintIsApproved, + mintAllowance, mintApproveEstimateGas, mintApprove, mintEstimateGas, @@ -89,11 +91,13 @@ const lending = { maxDeposit, previewDeposit, depositIsApproved, + depositAllowance, depositApprove, deposit, maxMint, previewMint, mintIsApproved, + mintAllowance, mintApprove, mint, maxWithdraw, diff --git a/src/markets/MarketConstructor.ts b/src/markets/MarketConstructor.ts index 0171755..0da5779 100644 --- a/src/markets/MarketConstructor.ts +++ b/src/markets/MarketConstructor.ts @@ -1,5 +1,5 @@ import { OneWayMarketTemplate} from "./OneWayMarketTemplate.js"; -import {lending} from "../lending"; +import { lending } from "../lending.js"; export const getOneWayMarket = (oneWayMarketId: string): OneWayMarketTemplate => { const marketData = lending.constants.ONE_WAY_MARKETS[oneWayMarketId]; diff --git a/src/st-crvUSD.ts b/src/st-crvUSD.ts index 9a0a23a..61be982 100644 --- a/src/st-crvUSD.ts +++ b/src/st-crvUSD.ts @@ -4,9 +4,11 @@ import { ensureAllowanceEstimateGas, formatUnits, hasAllowance, + getAllowance, parseUnits, smartNumber, getBalances, + MAX_ALLOWANCE, } from "./utils.js"; import { lending } from "./lending.js"; import { TAmount, TGas } from "./interfaces.js"; @@ -67,12 +69,16 @@ export const depositIsApproved = async(assets: TAmount): Promise => { return await hasAllowance([lending.constants.ALIASES.crvUSD], [assets], lending.signerAddress, lending.constants.ALIASES.st_crvUSD); } +export const depositAllowance = async(): Promise => { + return await getAllowance([lending.constants.ALIASES.crvUSD], lending.signerAddress, lending.constants.ALIASES.st_crvUSD); +} + export const depositApproveEstimateGas = async (assets: TAmount): Promise => { return await ensureAllowanceEstimateGas([lending.constants.ALIASES.crvUSD], [assets], lending.constants.ALIASES.st_crvUSD); } -export const depositApprove = async (assets: TAmount): Promise => { - return await ensureAllowance([lending.constants.ALIASES.crvUSD], [assets], lending.constants.ALIASES.st_crvUSD); +export const depositApprove = async (assets: TAmount, isMax = true): Promise => { + return await ensureAllowance([lending.constants.ALIASES.crvUSD], [assets], lending.constants.ALIASES.st_crvUSD, isMax); } const _deposit = async (assets: TAmount, estimateGas = false): Promise => { @@ -120,14 +126,24 @@ export const mintIsApproved = async (shares: TAmount): Promise => { return await hasAllowance([lending.constants.ALIASES.crvUSD], [assets], lending.signerAddress, lending.constants.ALIASES.st_crvUSD); } +export const mintAllowance = async (): Promise => { + const assets = await getAllowance([lending.constants.ALIASES.crvUSD], lending.signerAddress, lending.constants.ALIASES.st_crvUSD); + try { + return [await convertToShares(assets[0])] + } catch (e) { + if (parseUnits(assets[0]) === MAX_ALLOWANCE) return [lending.formatUnits(MAX_ALLOWANCE)]; + throw e; + } +} + export const mintApproveEstimateGas = async (shares: TAmount): Promise => { const assets = await previewMint(shares); return await ensureAllowanceEstimateGas([lending.constants.ALIASES.crvUSD], [assets], lending.constants.ALIASES.st_crvUSD); } -export const mintApprove = async (shares: TAmount): Promise => { +export const mintApprove = async (shares: TAmount, isMax = true): Promise => { const assets = await previewMint(shares); - return await ensureAllowance([lending.constants.ALIASES.crvUSD], [assets], lending.constants.ALIASES.st_crvUSD); + return await ensureAllowance([lending.constants.ALIASES.crvUSD], [assets], lending.constants.ALIASES.st_crvUSD, isMax); } const _mint = async (shares: TAmount, estimateGas = false): Promise => { diff --git a/src/utils.ts b/src/utils.ts index 7ab0948..0f9d761 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -7,6 +7,7 @@ import { _getUsdPricesFromApi } from "./external-api.js"; import { lending } from "./lending.js"; import { JsonFragment } from "ethers/lib.esm"; import { L2Networks } from "./constants/L2Networks.js"; +import memoize from "memoizee"; export const MAX_ALLOWANCE = BigInt("115792089237316195423570985008687907853269984665640564039457584007913129639935"); // 2**256 - 1 export const MAX_ACTIVE_BAND = BigInt("57896044618658097711785492504343953926634992332820282019728792003956564819967"); // 2**255 - 1 @@ -186,12 +187,11 @@ export const getBalances = async (coins: string[], address = ""): Promise formatUnits(_b, decimals[i])); } -export const _getAllowance = async (coins: string[], address: string, spender: string): Promise => { +export const _getAllowance = memoize(async (coins: string[], address: string, spender: string): Promise => { const _coins = [...coins] const ethIndex = getEthIndex(_coins); if (ethIndex !== -1) { _coins.splice(ethIndex, 1); - } let allowance: bigint[]; @@ -202,13 +202,18 @@ export const _getAllowance = async (coins: string[], address: string, spender: s allowance = await lending.multicallProvider.all(contractCalls); } - if (ethIndex !== -1) { allowance.splice(ethIndex, 0, MAX_ALLOWANCE); } return allowance; -} +}, +{ + promise: true, + maxAge: 5 * 1000, // 5s + primitive: true, + length: 3, +}); // coins can be either addresses or symbols export const getAllowance = async (coins: string[], address: string, spender: string): Promise => {