1

Is it possible for a smart contract to swap using 0x without receiving the calldata from outside?

I know how to make my deployed contract to swap with 0x by creating a fillQuote function and passing the data received from an HTTP API request as the arguments of that function (basically what is explained in the 0x docs).

What I am struggling with is to make my deployed contract to initiate a 0x swap without me having to make an external HTTP API request. That is, the smart contract determines internally (by some solidity computation) the token it needs to sell (tokenIn), the token it wants to buy (tokenOut) and the amount it wants to sell (amountIn). As a result, this information was not previously known or accessible by the external keeper. Can then the smart contract initiate a 0x swap with that information and without access to the external API data (like it could do with say a Uniswap V2 router)?

Or alternatively, is there anyway that the smart contract can internally re-create (encode) the SwapCallData?

2
  • Can you give more detail on what you're asking?
    – Ryan Sea
    Sep 3, 2022 at 22:27
  • I tried to expand a little bit. Found this related question (ethereum.stackexchange.com/questions/121666/…), which shares some of the same problematic. Couldn't find any mentions in the docs of the shouldSellEntireBalance=true cited in one of the answers; so not sure if that could actually help me too.
    – lewa
    Sep 3, 2022 at 23:24

1 Answer 1

1

Looking at source source code, ZRX Asset Swapper uses an optimizer fetching samples from each onchain source and then breakdown to create SwapCallData to retrieve it on the API. Fetching amount quotes from sources is by using these set of contracts here: https://github.com/0xProject/protocol/tree/development/packages/asset-swapper/contracts/src and then they use Asset Swapper classes to find the best route based on the samples.

I guess inserting all this logic on solidity at the moment is not feasible, maybe you can start using Uniswap V2 and V3 and choose the best of these ones on your solidity contract. It really depends on the set of tokens that you want to swap, stablecoins have more liquidity on UniV3 and Curve, others maybe others source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.