100 Python Pancake 裸交易方法swapExactETHForTokens一直失败,如何把bnb购买数量传入(Sodility是可以实现的)

from datetime import datetime
from time import time

from web3 import Web3
from web3.middleware import geth_poa_middleware  # Needed for Binance

from config import PANCAKE_ROUTER_V2_ADDRESS, WBNB_ADDRESS, PANCAKE_ROUTER_V2_ABI, ACCOUNT_PK, ACCOUNT_ADDRESS

web3 = Web3(Web3.HTTPProvider('https://bsc-dataseed1.binance.org:443'))
web3.middleware_onion.inject(geth_poa_middleware, layer=0)  # Again, this is needed for Binance, not Ethirium

def buy(bnb_amount, amountOutMin, token_address, router, gwei, gas):
    token_address = web3.toChecksumAddress(token_address)
    router = web3.toChecksumAddress(router)
    bot = web3.eth.contract(address=router, abi=PANCAKE_ROUTER_V2_ABI) 

    path = [WBNB_ADDRESS, token_address]
    print("path", path)
    tx = bot.functions.swapETHForExactTokens(amountOutMin, path, ACCOUNT_ADDRESS, (int(time()) + 1000)).buildTransaction({
            'gas': gas,
            'gasPrice': web3.toWei(gwei, 'gwei'),
            'from': token_address,
            'nonce': 1
        })
    print("buy tx-------->>>", tx)
    signPromise = web3.eth.signTransaction(tx, ACCOUNT_PK)
    web3.eth.sendRawTransaction(signPromise.rawTransaction)

if __name__ == '__main__':
    CAKE_ROUTER_V2 = Web3.toChecksumAddress(PANCAKE_ROUTER_V2_ADDRESS)
    token_address = web3.toChecksumAddress('0x26193c7fa4354ae49ec53ea2cebc513dc39a10aa')
    router = web3.toChecksumAddress(PANCAKE_ROUTER_V2_ADDRESS)
    buy('100000000000000000', '600000000000000000000', token_address, router, 330000, 5000000000)

目前报错:

ValueError: {'code': -32602, 'message': 'invalid argument 0: json: cannot unmarshal non-string into Go struct field SendTxArgs.chainId of type *hexutil.Big'}

1、bnb_amount bnb购买数量如何传入 2、报错是什么原因,目前看感觉是返回的tx的json格式不对

buy tx-------->>> {'value': 0, 'chainId': 56, 'gas': 5000000000, 'gasPrice': 330000000000000, 'nonce': 1, 'to': '0x10ED43C718714eb63d5aA57B78B54704E256024E', 'data': '0xfb3bdb4100000000000000000000000000000000000000000000002086ac3510526000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000aa18b438a62c6878335a17f60bf17bc7e56b72c500000000000000000000000000000000000000000000000000000000627777800000000000000000000000000000000000000000000000000000000000000002000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c00000000000000000000000026193c7fa4354ae49ec53ea2cebc513dc39a10aa'}
请先 登录 后评论

最佳答案 2022-05-10 23:12

buildTransaction 后面的结构中加入 value 字段, 这个字段表示交易所需的 ETH 数量。

请先 登录 后评论

其它 1 个回答

Niko.Tesla
请先 登录 后评论
  • 3 关注
  • 0 收藏,4949 浏览
  • hunter666 提出于 2022-05-08 15:46