Skip to content

Commit 1fd5d22

Browse files
committed
refactor: refactor populateApprove
1 parent 5eb7068 commit 1fd5d22

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ import curve from "@curvefi/api";
184184

185185
// Get populated approve transactions (without executing)
186186
const approveTxs = await curve.populateApprove(["DAI", "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"], ['1000', '1000'], spender);
187+
// OR with custom user address (for API):
188+
// await curve.populateApprove(["DAI", "USDC"], ['1000', '1000'], spender, false, '0x...userAddress');
187189
// Returns array of TransactionLike objects (may include reset to 0 if needed for some tokens)
188190
console.log(approveTxs);
189191
// [
@@ -1204,7 +1206,6 @@ import curve from "@curvefi/api";
12041206
// Get populated transactions for approve (without executing)
12051207
const approveTxs = await curve.router.populateApprove('DAI', 1000);
12061208
// OR const approveTxs = await curve.router.populateApprove('0x6B175474E89094C44Da98b954EedeAC495271d0F', 1000);
1207-
// OR with custom spender: await curve.router.populateApprove('DAI', 1000, '0x...customSpender');
12081209
console.log(approveTxs);
12091210
// [{ to: '0x6B17...', data: '0x...', ... }]
12101211
// Returns array of TransactionLike objects

src/router.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
getTxCostsUsd,
2121
hasAllowance,
2222
isEth,
23-
MAX_ALLOWANCE,
2423
parseUnits,
2524
populateApprove,
2625
runWorker,
@@ -409,9 +408,8 @@ export async function swapApprove(this: Curve, inputCoin: string, amount: number
409408
return await ensureAllowance.call(this, [inputCoin], [amount], this.constants.ALIASES.router);
410409
}
411410

412-
export async function swapPopulateApprove(this: Curve, inputCoin: string, amount: number | string, spender?: string, isMax = true): Promise<TransactionLike[]> {
413-
const routerAddress = this.contracts[this.constants.ALIASES.router].contract.target as string;
414-
return await populateApprove.call(this, [inputCoin], [amount], spender || routerAddress, isMax);
411+
export async function swapPopulateApprove(this: Curve, inputCoin: string, amount: number | string, isMax = true): Promise<TransactionLike[]> {
412+
return await populateApprove.call(this, [inputCoin], [amount], this.constants.ALIASES.router, isMax);
415413
}
416414

417415
export async function swapEstimateGas(this: Curve, inputCoin: string, outputCoin: string, amount: number | string): Promise<number | number[]> {

src/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,14 @@ export async function ensureAllowance(this: Curve, coins: string[], amounts: (nu
310310
return await _ensureAllowance.call(this, coinAddresses, _amounts, spender, isMax)
311311
}
312312

313-
export async function populateApprove(this: Curve, coins: string[], amounts: (number | string)[], spender: string, isMax = true): Promise<TransactionLike[]> {
313+
export async function populateApprove(this: Curve, coins: string[], amounts: (number | string)[], spender: string, isMax = true, userAddress?: string): Promise<TransactionLike[]> {
314314
const coinAddresses = _getCoinAddresses.call(this, coins);
315315
const decimals = _getCoinDecimals.call(this, coinAddresses);
316316
const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
317317

318-
const address = this.signerAddress;
318+
const address = userAddress || this.signerAddress;
319+
if (!address) throw Error("User address is not defined. Pass userAddress parameter.");
320+
319321
const allowance = await _getAllowance.call(this, coinAddresses, address, spender);
320322

321323
const transactions: TransactionLike[] = [];

0 commit comments

Comments
 (0)