Reputation: 51
Found a code where swap goes through exact pool on polygon uniswap v3. Need to change the method to use different pools in router, depending on price. Now all transactions use ExactOutputSingle. If someone knows how to change that, please help
async split(signer: Signer): Promise<void> {
const address = await signer.getAddress();
const { buyToken, sellToken, buyAmount } = await this.calculateSplitPurchase(address);
if (buyAmount.lessThan(0) || buyAmount.equalTo(0)) {
return;
}
console.log(`Buying ${buyAmount.toFixed()} ${buyToken.name} with ${sellToken.name}`);
const route = new Route([this.pool], sellToken, buyToken);
const amountIn = await this.quoter.callStatic.quoteExactOutputSingle(
sellToken.isNative ? WETH_ADDRESS : sellToken.address,
buyToken.isNative ? WETH_ADDRESS : buyToken.address,
this.pool.fee,
buyAmount.quotient.toString(10),
0,
);
const trade = Trade.createUncheckedTrade({
route,
tradeType: TradeType.EXACT_OUTPUT,
inputAmount: CurrencyAmount.fromRawAmount(sellToken, amountIn.toString()),
outputAmount: CurrencyAmount.fromRawAmount(buyToken, buyAmount.quotient.toString(10)),
});
const params = SwapRouter.swapCallParameters([trade], {
slippageTolerance: new Percent(2, 1000),
recipient: address,
deadline: ethers.constants.MaxUint256.toString(),
});
await this.swap(signer, params);
}
async swap(signer: Signer, params: MethodParameters): Promise<void> {
const tx = await signer.sendTransaction({
to: this.router.address,
from: await signer.getAddress(),
data: params.calldata,
value: params.value,
//gasPrice: await getFastGasPrice(),
//gasLimit: 1300000,
});
await tx.wait(3);
}
Upvotes: 0
Views: 971
Reputation: 154
You would have two choices, you could search for and gather data from the pools that you want to trade, then you could build paths and use the quoter to get best returns, Or you could use the Uniswap auto router code (what the UI uses to get best price)
https://docs.uniswap.org/sdk/guides/auto-router
Upvotes: -1