I am currently developing a service that uses the 0x API to swap tokens on the Binance Smart Chain (BSC). I am trying to enable swaps that involve BNB, the native token of BSC. (For example: sell BNB, buy CAKE)
In the context of Ethereum, when trading ERC20 tokens, I understand that I need to approve the 0x contract to spend tokens on behalf of the user. This approval is done by calling the
approve function of the ERC20 token contract before executing the trade. However, for trading ETH, the native asset of the Ethereum network, this approval step is not required.
Given that BNB is the native asset of BSC, I am unclear about whether I need to call the
approve function when trading BNB. More specifically, I am unsure about which version of BNB I should be using for these trades:
- BNB, as the native token of BSC, similar to ETH on Ethereum.
- WBNB, which is a wrapped version of BNB and operates as a BEP20 token.
In the case of trading BNB, would I need to call the
approve function for the 0x contract to spend the user's BNB? If I should use WBNB instead, would this require the user to manually wrap their BNB to WBNB before trading?
Furthermore, I have encountered a
"execution reverted" error when trying to execute a swap involving BNB. The transaction parameters seem to be correct, and the gas price and gas limit are set to reasonable values. However the value I retrieve through the quote API is 0. I am wondering if this error could be related to the usage of BNB vs WBNB or to the approval of the 0x contract.
All in all, when I go the WBNB route, I don't find balance (because the wallet has BNB which is not yet wrapped), and I'm not sure how to do it with BNB through the 0x API.
If I want to swap, should I first wrap BNB, then swap? It's a bit ambiguous to me what common practice is on exchanges, etc. behind the scenes.
I would appreciate any insights or suggestions on how to correctly set up a BNB swap using the 0x API on BSC.