Python

在 python 中的 BSC 上的每個 0x API 事務交換上恢復執行

  • January 31, 2022

我嘗試使用 0x API 在 BSC 上進行交換。我所有的交易都失敗了(警告!在合約執行過程中遇到錯誤

$$ execution reverted $$)。津貼應該沒有任何問題,我可以使用元遮罩進行交換。 請告訴我我的程式碼有什麼問題。

這是我的程式碼:

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)

其中 ‘res’ 是 GET /swap/v1/quote 範例 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'}

從提供的資訊很難確定。

一個常見的問題是,由於您沒有提供takerAddress0xAPI,因此氣體估算值對於您的特定地址不准確。

要麼提供takerAddress給 0xAPI,要麼執行您自己的 gas 估算。

gas在此處查看該領域的文件, https://0x.org/docs/api#response-1

引用自:https://ethereum.stackexchange.com/questions/120082