Skip to content

Commit

Permalink
Merge pull request #42 from curvefi/st-crvUSD
Browse files Browse the repository at this point in the history
st-crvUSD enhanced approvals
  • Loading branch information
Macket authored Oct 23, 2024
2 parents 1ff0994 + 1f6cc87 commit 695c5c1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import {
maxDeposit,
previewDeposit,
depositIsApproved,
depositAllowance,
depositApproveEstimateGas,
depositApprove,
depositEstimateGas,
deposit,
maxMint,
previewMint,
mintIsApproved,
mintAllowance,
mintApproveEstimateGas,
mintApprove,
mintEstimateGas,
Expand Down Expand Up @@ -89,11 +91,13 @@ const lending = {
maxDeposit,
previewDeposit,
depositIsApproved,
depositAllowance,
depositApprove,
deposit,
maxMint,
previewMint,
mintIsApproved,
mintAllowance,
mintApprove,
mint,
maxWithdraw,
Expand Down
2 changes: 1 addition & 1 deletion src/markets/MarketConstructor.ts
Original file line number Diff line number Diff line change
@@ -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];
Expand Down
24 changes: 20 additions & 4 deletions src/st-crvUSD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -67,12 +69,16 @@ export const depositIsApproved = async(assets: TAmount): Promise<boolean> => {
return await hasAllowance([lending.constants.ALIASES.crvUSD], [assets], lending.signerAddress, lending.constants.ALIASES.st_crvUSD);
}

export const depositAllowance = async(): Promise<string[]> => {
return await getAllowance([lending.constants.ALIASES.crvUSD], lending.signerAddress, lending.constants.ALIASES.st_crvUSD);
}

export const depositApproveEstimateGas = async (assets: TAmount): Promise<TGas> => {
return await ensureAllowanceEstimateGas([lending.constants.ALIASES.crvUSD], [assets], lending.constants.ALIASES.st_crvUSD);
}

export const depositApprove = async (assets: TAmount): Promise<string[]> => {
return await ensureAllowance([lending.constants.ALIASES.crvUSD], [assets], lending.constants.ALIASES.st_crvUSD);
export const depositApprove = async (assets: TAmount, isMax = true): Promise<string[]> => {
return await ensureAllowance([lending.constants.ALIASES.crvUSD], [assets], lending.constants.ALIASES.st_crvUSD, isMax);
}

const _deposit = async (assets: TAmount, estimateGas = false): Promise<string | TGas> => {
Expand Down Expand Up @@ -120,14 +126,24 @@ export const mintIsApproved = async (shares: TAmount): Promise<boolean> => {
return await hasAllowance([lending.constants.ALIASES.crvUSD], [assets], lending.signerAddress, lending.constants.ALIASES.st_crvUSD);
}

export const mintAllowance = async (): Promise<string[]> => {
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<TGas> => {
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<string[]> => {
export const mintApprove = async (shares: TAmount, isMax = true): Promise<string[]> => {
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<string | TGas> => {
Expand Down
13 changes: 9 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -186,12 +187,11 @@ export const getBalances = async (coins: string[], address = ""): Promise<string
return _balances.map((_b, i: number ) => formatUnits(_b, decimals[i]));
}

export const _getAllowance = async (coins: string[], address: string, spender: string): Promise<bigint[]> => {
export const _getAllowance = memoize(async (coins: string[], address: string, spender: string): Promise<bigint[]> => {
const _coins = [...coins]
const ethIndex = getEthIndex(_coins);
if (ethIndex !== -1) {
_coins.splice(ethIndex, 1);

}

let allowance: bigint[];
Expand All @@ -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<string[]> => {
Expand Down

0 comments on commit 695c5c1

Please sign in to comment.