Skip to content

Commit a625e19

Browse files
chore: review comments #506
1 parent 582e34e commit a625e19

File tree

3 files changed

+8
-17
lines changed

3 files changed

+8
-17
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,9 +1206,9 @@ import curve from "@curvefi/api";
12061206
await curve.router.getBestRouteAndOutput('DAI', 'CRV', '1000');
12071207
12081208
// Then get calldata
1209-
const calldata = await curve.router.swapCalldata('DAI', 'CRV', '1000', 0.5);
1210-
// OR const calldata = await curve.router.swapCalldata('0x6B175474E89094C44Da98b954EedeAC495271d0F', '0xD533a949740bb3306d119CC777fa900bA034cd52', '1000', 0.5);
1211-
console.log(calldata);
1209+
const { data, to, from, amount } = await curve.router.populateSwap('DAI', 'CRV', '1000', 0.5);
1210+
// OR const tx = await curve.router.populateSwap('0x6B175474E89094C44Da98b954EedeAC495271d0F', '0xD533a949740bb3306d119CC777fa900bA034cd52', '1000', 0.5);
1211+
console.log(data);
12121212
// 0x8f726f1c000000000000000000000000...
12131213
})()
12141214
```

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
swapApprove,
1919
swapEstimateGas,
2020
swap,
21-
swapCalldata,
21+
populateSwap,
2222
getSwappedAmount,
2323
} from "./router.js";
2424
import { Curve } from "./curve.js";
@@ -354,7 +354,7 @@ export const createCurve = () => {
354354
isApproved: swapIsApproved.bind(_curve),
355355
approve: swapApprove.bind(_curve),
356356
swap: swap.bind(_curve),
357-
swapCalldata: swapCalldata.bind(_curve),
357+
populateSwap: populateSwap.bind(_curve),
358358
getSwappedAmount: getSwappedAmount.bind(_curve),
359359
estimateGas: {
360360
approve: swapApproveEstimateGas.bind(_curve),

src/router.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import BigNumber from "bignumber.js";
2-
import {ethers} from "ethers";
2+
import {ethers, TransactionLike} from "ethers";
33
import {type Curve, OLD_CHAINS} from "./curve.js";
44
import {IChainId, IDict, IRoute, IRouteOutputAndCost, IRouteStep} from "./interfaces";
55
import {
@@ -459,7 +459,7 @@ export async function swap(this: Curve, inputCoin: string, outputCoin: string, a
459459
}
460460
}
461461

462-
export async function swapCalldata(this: Curve, inputCoin: string, outputCoin: string, amount: number | string, slippage = 0.5): Promise<string> {
462+
export async function populateSwap(this: Curve, inputCoin: string, outputCoin: string, amount: number | string, slippage = 0.5): Promise<TransactionLike> {
463463
console.log(inputCoin, outputCoin, amount, slippage);
464464
const [inputCoinAddress, outputCoinAddress] = _getCoinAddresses.call(this, inputCoin, outputCoin);
465465
const [inputCoinDecimals, outputCoinDecimals] = _getCoinDecimals.call(this, inputCoinAddress, outputCoinAddress);
@@ -476,16 +476,7 @@ export async function swapCalldata(this: Curve, inputCoin: string, outputCoin: s
476476
const _minRecvAmount = fromBN(minRecvAmountBN, outputCoinDecimals);
477477

478478
const contract = this.contracts[this.constants.ALIASES.router].contract;
479-
const value = isEth(inputCoinAddress) ? _amount : this.parseUnits("0");
480-
481-
let populatedTx;
482-
if (_pools) {
483-
populatedTx = await contract.exchange.populateTransaction(_route, _swapParams, _amount, _minRecvAmount, _pools, { value });
484-
} else {
485-
populatedTx = await contract.exchange.populateTransaction(_route, _swapParams, _amount, _minRecvAmount, { value });
486-
}
487-
488-
return populatedTx.data as string;
479+
return await contract.exchange.populateTransaction(...[_route, _swapParams, _amount, _minRecvAmount, ..._pools ? [_pools] : [], { value: isEth(inputCoinAddress) ? _amount : this.parseUnits("0") }])
489480
}
490481

491482
export async function getSwappedAmount(this: Curve, tx: ethers.ContractTransactionResponse, outputCoin: string): Promise<string> {

0 commit comments

Comments
 (0)