【问题标题】:Uniswap V3 routerUniswap V3 路由器
【发布时间】:2022-11-04 02:11:13
【问题描述】:

找到了一个代码,其中交换通过多边形 uniswap v3 上的确切池。需要更改方法以在路由器中使用不同的池,具体取决于价格。现在所有事务都使用 ExactOutputSingle。如果有人知道如何改变它,请帮助

  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);
  }

【问题讨论】:

    标签: typescript ethers.js uniswap


    【解决方案1】:

    您将有两个选择,您可以搜索并从您想要交易的池中收集数据,然后您可以构建路径并使用报价器来获得最佳回报,或者您可以使用 Uniswap 自动路由器代码(UI 使用以获得最优惠的价格)

    https://docs.uniswap.org/sdk/guides/auto-router

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-28
      • 2022-08-29
      • 2022-10-26
      • 1970-01-01
      • 2016-11-25
      • 2017-11-02
      • 2022-07-29
      相关资源
      最近更新 更多