Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The result of AlpharRouter.route may be estimated to be less than the number specified in EXACT. #458

Open
0skgc opened this issue Dec 13, 2023 · 0 comments

Comments

@0skgc
Copy link

0skgc commented Dec 13, 2023

  • I'm submitting a ...
    [x] bug report
    [ ] feature request
    [ ] question about the decisions made in the repository
    [ ] question about how to use this project

  • Summary
    When estimating route in AlphaRouter, the number of swaps specified in EXACT will be different if the swaps are split so that multiple Pools are used (truncation probably occurs for each split Swap, and the total number of swaps due to the split Swaps is less than EXACT).

  • Other information (e.g. detailed explanation, stack traces, related issues, suggestions how to fix, links for us to have context, eg. StackOverflow, personal fork, etc.)
    Reproduced code (It can be confirmed that the value of route.trade.swap is off due to an error. Also, the byte code of route.methodParameters.calldata is off as well as route.trade.swap)

Note that the following code is an example for TradeType.EXACT_OUTPUT, but the same misalignment occurred for TradeType.EXACT_INPUT.

        const provider = // provider
        const recipientAddress = // recipient address
        const networkId = 137 // polygon
        const buyTokenAddress = "0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619" // WETH
        const buyTokenDecimals = 18
        const buyAmountWei = "1464983244737305125"
        const sellTokenAddress = "0x0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270" // WMATIC
        const sellTokenDecimals = 18

        const router = new AlphaRouter({
          chainId: networkId,
          provider,
        })
        const route = await  router.route(
          CurrencyAmount.fromRawAmount(
            new Token(networkId, buyTokenAddress, buyTokenDecimals),
            buyAmountWei
          ),
          new Token(networkId, sellTokenAddress, sellTokenDecimals),
          TradeType.EXACT_OUTPUT,
          {
            recipient: recipientAddress,
            slippageTolerance: new Percent(3,100),
            deadline: Math.floor(Date.now() / 1000) + 60 * 15,
            type: SwapType.SWAP_ROUTER_02,
          },
          {
            minSplits:2 // NOTE: Set the split to 2 or more to reproduce the problem.
          }
        )
        if (route == undefined) {
          throw new Error("Swap route not found.")
        }
        let sumSellAmount = new BigNumberJS(0)
        let sumBuyAmount = new BigNumberJS(0)
        route.trade.swaps.forEach(it => {
          const sellAmount= it.inputAmount.quotient.toString()
          const buyAmount = it.outputAmount.quotient.toString()
          sumSellAmount = sumSellAmount.plus(sellAmount)
          sumBuyAmount = sumBuyAmount.plus(buyAmount)
        })

        let sellAmount = route.trade.inputAmount.quotient.toString()
        let buyAmount = route.trade.outputAmount.quotient.toString()
        console.log("debug quote amount", {
            splits: route.trade.swaps.length,
            pools: route.trade.swaps.map(swap => swap.route.pools.map(pool => pool)),
            diffBuyAmount: sumBuyAmount.minus(buyAmount).toString(), // NOTE: If "splits" is greater than 1, the value specified in EXACT is often off, probably due to truncation errors caused by swapping by each pool.
            requiredBuyAmount: buyAmountWei,
            quoteSellAmount: sumSellAmount.toString(),
            quoteBuyAmount: sumBuyAmount.toString(),
          }
        )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant