0

I try to make a swap on BSC using 0x API. All my transaction FAILS (Warning! Error encountered during contract execution [execution reverted]). There shouldn't be any problems with allowance, I can swap using metamask.

Please tell me what is wrong with my code.

Here is my code:

self.bsc = "https://bsc-dataseed.binance.org/"
        self.web3 = Web3(Web3.HTTPProvider(self.bsc))

        key = 'MY_KEY'
        account = self.web3.toChecksumAddress('MY_ACCOUNT')
        nonce = self.web3.eth.getTransactionCount(account)
            
        tx = {
            'nonce': nonce, 
            'gas': int(res['gas']), 
            'gasPrice': int(res['gasPrice']), 
            'from': account,
            'to': self.web3.toChecksumAddress(res['to']), 
            'data': res['data'], 
            'value': int(res['value'])}
            
        signed_tx = self.web3.eth.account.sign_transaction(tx, key)
        tx_hash = self.web3.eth.sendRawTransaction(signed_tx.rawTransaction)

Where 'res' is a result of GET /swap/v1/quote Example res:

{'chainId': 56, 'price': '0.011004538', 'guaranteedPrice': '0.010894493', 'to': '0xdef1c0ded9bec7f1a1670819833240f027b25eff', 'data': '0x415565b00000000000000000000000008ac76a51cc950d9822d68b83fe1ad97b32cd580d000000000000000000000000fa54ff1a158b5189ebba6ae130ced6bbd3aea76e000000000000000000000000000000000000000000000001158e460913d00000000000000000000000000000000000000000000000000000000000000cfcbc4f00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000004200000000000000000000000000000000000000000000000000000000000000780000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000360000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008ac76a51cc950d9822d68b83fe1ad97b32cd580d000000000000000000000000e9e7cea3dedca5984780bafc599bd69add087d560000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000032000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000001158e460913d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000134b79626572446d6d0000000000000000000000000000000000000000000000000000000000000001158e460913d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000010000000000000000000000000078df70615ffc8066cc0887917f2cd72092c86409000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000001000000000000000000000000542717158a059bd23832c3fde7d90b86ea3bea7200000000000000000000000000000000000000000000000000000000000000020000000000000000000000008ac76a51cc950d9822d68b83fe1ad97b32cd580d000000000000000000000000e9e7cea3dedca5984780bafc599bd69add087d560000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e9e7cea3dedca5984780bafc599bd69add087d56000000000000000000000000fa54ff1a158b5189ebba6ae130ced6bbd3aea76e000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000002c000000000000000000000000000000000000000000000000000000000000002c000000000000000000000000000000000000000000000000000000000000002a0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000250616e63616b65537761705632000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000cfcbc4e000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000002000000000000000000000000e9e7cea3dedca5984780bafc599bd69add087d56000000000000000000000000fa54ff1a158b5189ebba6ae130ced6bbd3aea76e000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000030000000000000000000000008ac76a51cc950d9822d68b83fe1ad97b32cd580d000000000000000000000000e9e7cea3dedca5984780bafc599bd69add087d56000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000000000000000000000000000000000000000000000869584cd0000000000000000000000001000000000000000000000000000000000000011000000000000000000000000000000000000000000000089fc176b5f61f271a9', 'value': '0', 'gas': '261000', 'estimatedGas': '261000', 'gasPrice': '5000000000', 'protocolFee': '0', 'minimumProtocolFee': '0', 'buyTokenAddress': '0xfa54ff1a158b5189ebba6ae130ced6bbd3aea76e', 'sellTokenAddress': '0x8ac76a51cc950d9822d68b83fe1ad97b32cd580d', 'buyAmount': '220090779', 'sellAmount': '20000000000000000000', 'sources': 
[{'name': 'BakerySwap', 'proportion': '0'}, {'name': 'Belt', 'proportion': '0'}, {'name': 'DODO', 'proportion': '0'}, {'name': 'DODO_V2', 'proportion': '0'}, {'name': 'Ellipsis', 'proportion': '0'}, {'name': 'Mooniswap', 'proportion': '0'}, {'proportion': '1', 'intermediateToken': '0xe9e7cea3dedca5984780bafc599bd69add087d56', 'hops': ['KyberDMM', 'PancakeSwap_V2'], 'name': 'MultiHop'}, {'name': 'Nerve', 'proportion': '0'}, {'name': 'PancakeSwap', 'proportion': '0'}, {'name': 'PancakeSwap_V2', 'proportion': '0'}, {'name': 'SushiSwap', 'proportion': '0'}, {'name': 'Smoothy', 'proportion': '0'}, {'name': 'ApeSwap', 'proportion': '0'}, {'name': 'CafeSwap', 'proportion': '0'}, {'name': 'CheeseSwap', 'proportion': '0'}, {'name': 'JulSwap', 'proportion': '0'}, {'name': 'LiquidityProvider', 'proportion': '0'}, {'name': 'WaultSwap', 'proportion': '0'}, {'name': 'FirebirdOneSwap', 'proportion': '0'}, {'name': 'JetSwap', 'proportion': '0'}, {'name': 'ACryptoS', 'proportion': '0'}, {'name': 'KyberDMM', 'proportion': '0'}], 'orders': [{'makerToken': '0xe9e7cea3dedca5984780bafc599bd69add087d56', 'takerToken': '0x8ac76a51cc950d9822d68b83fe1ad97b32cd580d', 'makerAmount': '0', 'takerAmount': '20000000000000000000', 'fillData': {'poolsPath': ['0x542717158a059bd23832c3fde7d90b86ea3bea72'], 'router': '0x78df70615ffc8066cc0887917f2cd72092c86409', 'tokenAddressPath': ['0x8ac76a51cc950d9822d68b83fe1ad97b32cd580d', '0xe9e7cea3dedca5984780bafc599bd69add087d56']}, 'source': 'KyberDMM', 'sourcePathId': '', 'type': 0}, {'makerToken': '0xfa54ff1a158b5189ebba6ae130ced6bbd3aea76e', 'takerToken': '0xe9e7cea3dedca5984780bafc599bd69add087d56', 'makerAmount': '220090779', 'takerAmount': '115792089237316195423570985008687907853269984665640564039457584007913129639935', 'fillData': {'tokenAddressPath': ['0xe9e7cea3dedca5984780bafc599bd69add087d56', '0xfa54ff1a158b5189ebba6ae130ced6bbd3aea76e'], 'router': '0x10ed43c718714eb63d5aa57b78b54704e256024e'}, 'source': 'PancakeSwap_V2', 'sourcePathId': '', 'type': 0}], 'allowanceTarget': '0xdef1c0ded9bec7f1a1670819833240f027b25eff', 'sellTokenToEthRate': '316.286956340046373442', 'buyTokenToEthRate': '0'}

1 Answer 1

1

It's hard to determine from the information provided.

One common problem is that since you're not providing a takerAddress to 0xAPI then the gas estimate is not accurate for your particular address.

Either provide the takerAddress to 0xAPI or perform your own gas estimate.

See the docs on the gas field here, https://0x.org/docs/api#response-1.

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.