Skip to content

Commit 695c5c1

Browse files
authored
Merge pull request #42 from curvefi/st-crvUSD
st-crvUSD enhanced approvals
2 parents 1ff0994 + 1f6cc87 commit 695c5c1

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@curvefi/lending-api",
3-
"version": "2.3.4",
3+
"version": "2.3.5",
44
"description": "JavaScript library for Curve Lending",
55
"main": "lib/index.js",
66
"author": "Macket",

src/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ import {
2020
maxDeposit,
2121
previewDeposit,
2222
depositIsApproved,
23+
depositAllowance,
2324
depositApproveEstimateGas,
2425
depositApprove,
2526
depositEstimateGas,
2627
deposit,
2728
maxMint,
2829
previewMint,
2930
mintIsApproved,
31+
mintAllowance,
3032
mintApproveEstimateGas,
3133
mintApprove,
3234
mintEstimateGas,
@@ -89,11 +91,13 @@ const lending = {
8991
maxDeposit,
9092
previewDeposit,
9193
depositIsApproved,
94+
depositAllowance,
9295
depositApprove,
9396
deposit,
9497
maxMint,
9598
previewMint,
9699
mintIsApproved,
100+
mintAllowance,
97101
mintApprove,
98102
mint,
99103
maxWithdraw,

src/markets/MarketConstructor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { OneWayMarketTemplate} from "./OneWayMarketTemplate.js";
2-
import {lending} from "../lending";
2+
import { lending } from "../lending.js";
33

44
export const getOneWayMarket = (oneWayMarketId: string): OneWayMarketTemplate => {
55
const marketData = lending.constants.ONE_WAY_MARKETS[oneWayMarketId];

src/st-crvUSD.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import {
44
ensureAllowanceEstimateGas,
55
formatUnits,
66
hasAllowance,
7+
getAllowance,
78
parseUnits,
89
smartNumber,
910
getBalances,
11+
MAX_ALLOWANCE,
1012
} from "./utils.js";
1113
import { lending } from "./lending.js";
1214
import { TAmount, TGas } from "./interfaces.js";
@@ -67,12 +69,16 @@ export const depositIsApproved = async(assets: TAmount): Promise<boolean> => {
6769
return await hasAllowance([lending.constants.ALIASES.crvUSD], [assets], lending.signerAddress, lending.constants.ALIASES.st_crvUSD);
6870
}
6971

72+
export const depositAllowance = async(): Promise<string[]> => {
73+
return await getAllowance([lending.constants.ALIASES.crvUSD], lending.signerAddress, lending.constants.ALIASES.st_crvUSD);
74+
}
75+
7076
export const depositApproveEstimateGas = async (assets: TAmount): Promise<TGas> => {
7177
return await ensureAllowanceEstimateGas([lending.constants.ALIASES.crvUSD], [assets], lending.constants.ALIASES.st_crvUSD);
7278
}
7379

74-
export const depositApprove = async (assets: TAmount): Promise<string[]> => {
75-
return await ensureAllowance([lending.constants.ALIASES.crvUSD], [assets], lending.constants.ALIASES.st_crvUSD);
80+
export const depositApprove = async (assets: TAmount, isMax = true): Promise<string[]> => {
81+
return await ensureAllowance([lending.constants.ALIASES.crvUSD], [assets], lending.constants.ALIASES.st_crvUSD, isMax);
7682
}
7783

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

129+
export const mintAllowance = async (): Promise<string[]> => {
130+
const assets = await getAllowance([lending.constants.ALIASES.crvUSD], lending.signerAddress, lending.constants.ALIASES.st_crvUSD);
131+
try {
132+
return [await convertToShares(assets[0])]
133+
} catch (e) {
134+
if (parseUnits(assets[0]) === MAX_ALLOWANCE) return [lending.formatUnits(MAX_ALLOWANCE)];
135+
throw e;
136+
}
137+
}
138+
123139
export const mintApproveEstimateGas = async (shares: TAmount): Promise<TGas> => {
124140
const assets = await previewMint(shares);
125141
return await ensureAllowanceEstimateGas([lending.constants.ALIASES.crvUSD], [assets], lending.constants.ALIASES.st_crvUSD);
126142
}
127143

128-
export const mintApprove = async (shares: TAmount): Promise<string[]> => {
144+
export const mintApprove = async (shares: TAmount, isMax = true): Promise<string[]> => {
129145
const assets = await previewMint(shares);
130-
return await ensureAllowance([lending.constants.ALIASES.crvUSD], [assets], lending.constants.ALIASES.st_crvUSD);
146+
return await ensureAllowance([lending.constants.ALIASES.crvUSD], [assets], lending.constants.ALIASES.st_crvUSD, isMax);
131147
}
132148

133149
const _mint = async (shares: TAmount, estimateGas = false): Promise<string | TGas> => {

src/utils.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { _getUsdPricesFromApi } from "./external-api.js";
77
import { lending } from "./lending.js";
88
import { JsonFragment } from "ethers/lib.esm";
99
import { L2Networks } from "./constants/L2Networks.js";
10+
import memoize from "memoizee";
1011

1112
export const MAX_ALLOWANCE = BigInt("115792089237316195423570985008687907853269984665640564039457584007913129639935"); // 2**256 - 1
1213
export const MAX_ACTIVE_BAND = BigInt("57896044618658097711785492504343953926634992332820282019728792003956564819967"); // 2**255 - 1
@@ -186,12 +187,11 @@ export const getBalances = async (coins: string[], address = ""): Promise<string
186187
return _balances.map((_b, i: number ) => formatUnits(_b, decimals[i]));
187188
}
188189

189-
export const _getAllowance = async (coins: string[], address: string, spender: string): Promise<bigint[]> => {
190+
export const _getAllowance = memoize(async (coins: string[], address: string, spender: string): Promise<bigint[]> => {
190191
const _coins = [...coins]
191192
const ethIndex = getEthIndex(_coins);
192193
if (ethIndex !== -1) {
193194
_coins.splice(ethIndex, 1);
194-
195195
}
196196

197197
let allowance: bigint[];
@@ -202,13 +202,18 @@ export const _getAllowance = async (coins: string[], address: string, spender: s
202202
allowance = await lending.multicallProvider.all(contractCalls);
203203
}
204204

205-
206205
if (ethIndex !== -1) {
207206
allowance.splice(ethIndex, 0, MAX_ALLOWANCE);
208207
}
209208

210209
return allowance;
211-
}
210+
},
211+
{
212+
promise: true,
213+
maxAge: 5 * 1000, // 5s
214+
primitive: true,
215+
length: 3,
216+
});
212217

213218
// coins can be either addresses or symbols
214219
export const getAllowance = async (coins: string[], address: string, spender: string): Promise<string[]> => {

0 commit comments

Comments
 (0)