I want to swap from 0xdb25f211ab05b1c97d595516f45794528a807ad8(EURS) to 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48 (USDC) but the sellAmount is not known upfront and is only known during contract execution.

I tried building the swap data using https://api.0x.org/swap/v1/quote?sellToken=0xdb25f211ab05b1c97d595516f45794528a807ad8&buyToken=USDC&sellAmount=277000. Passing it to the contract works if the contract has more tokens than the sellAmount.

How do I create swap data when the value of sellAmount is not known?

Edit: I don't know the buyAmount as well, I want to sell everything that the wallet owns.

2 Answers 2


You need to specify either one of the sellAmount or buyAmount, or both. Since you don't have the sellAmount you need to provide the buyAmount. Link to relevant API Docs

  • Sadly in this case I don't have access to either the sellAmount or buyAmount. I want to sell everything that the wallet owns at that point.
    – Kunal Jain
    Feb 16, 2022 at 2:24
  • Like I said, you need to provide at least one of the two. You can get the balance of the wallet by using a web3 library like \eb3.js, ethers.js, or web3.py. web3js.readthedocs.io/en/v1.2.11/web3-eth.html#getbalance
    – jstep
    Feb 16, 2022 at 4:04
  • Don't forget to subtract a bit from the wallet's balance for gas and fees.
    – jstep
    Feb 16, 2022 at 4:14
  • I am aware of what the API requires, but my constraints don't allow me to know the buy or sell token amount in advance. Also, any web3 library is of no use because the balances change during the contract execution when I require swapping the tokens. Zapper ZapIn uses ZeroEx contract to do the swapping when they don't know about the sell or buy token in advance but I am not sure how the tx data is created. etherscan.io/tx/…
    – Kunal Jain
    Feb 21, 2022 at 13:03

There is another flag shouldSellEntireBalance which if set to true would sell the entire balance up to the amount specified in sellAmount.

So in the above case, I had to do was ensure that sellAmount is greater than the amount in the wallet's possession and set shouldSellEntireBalance=true while fetching swap data.

Your Answer

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

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