zeta-chain / toolkit

A library of ZetaChain helper utilities, contracts, and Hardhat tasks for smart-contract development
MIT License
42 stars 21 forks source link

Swap helpers to always swap through ZETA #153

Closed fadeev closed 3 weeks ago

fadeev commented 3 months ago

We have liquidity pools on testnet with zero liquidity in them. For example USDC Sepolia/tBTC. Our swap helpers try to swap using a direct pair and if it doesn't exist they swap through ZETA.

This may work in production, where pools actually have liquidity and direct pair is preferable, but on testnet, where we have pools with zero liquidity, it becomes impossible to swap between certain tokens, because the helper first tries to use a zero liquidity direct pool.

I propose changing the helpers to always swap through ZETA to ensure that all swaps go through.

https://github.com/zeta-chain/toolkit/blob/d952cd7b67526c218e07030263d0d5413ab0e64c/contracts/SwapHelperLib.sol

fadeev commented 3 months ago

@andresaiello wdyt?

fadeev commented 3 months ago

Important to handle edge case, where one of the tokens (input or output) is ZETA. If we swap directly without checking for that, Uniswap will return "identical addresses" error.

fadeev commented 2 months ago

Once this is done, update the example contracts and share txs of successful transfers using this helper.

fadeev commented 2 months ago

Keep the current functions under names appending "Directly" like "swapExactTokensForTokensDirectly".

New function (always through ZETA): swapExactTokensForTokens Current functionality: swapExactTokensForTokensDirectly

fadeev commented 2 months ago

Maybe there is a better way where it first tries directly, and if fails (not enough liquidity), then through ZETA.

lukema95 commented 2 months ago

Isn't that already implemented here?🤔

https://github.com/zeta-chain/toolkit/blob/d952cd7b67526c218e07030263d0d5413ab0e64c/contracts/SwapHelperLib.sol#L102-L112

fadeev commented 2 months ago

@lukema95 the problem is when a pool exists, but there is no liquidity there. For example, for Ethereum USDC and BNB USDC a pool exists, but swap fails, because there is no liquidity.

lukema95 commented 2 months ago

@lukema95 the problem is when a pool exists, but there is no liquidity there. For example, for Ethereum USDC and BNB USDC a pool exists, but swap fails, because there is no liquidity.

Got it. I would like to complete it.