Web3 签名交易问题找到非十六进制数字 (Python)

Web3 Signing transaction issue Non-hexadecimal digit found (Python)

我正在尝试完成在 binance smart chain 中购买代币的交易,但在签名交易部分出现错误,我无法解决。

代码:

PancakeABI = open('pancakeABI','r').read().replace('\n','')
bsc="https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))
contract_id = web3.toChecksumAddress(tokaddr)

#Checking balance for test
balance = web3.eth.get_balance(sender_address)
humanReadable = web3.fromWei(balance,'ether')
print(humanReadable)

#Setup the contract
contract = web3.eth.contract(address=router_address, abi=PancakeABI)
nonce = web3.eth.get_transaction_count(sender_address)
start = time.time()
print(web3.toWei('0.02','ether'))

#Send transaction
pancakeswap2_txn = contract.functions.swapExactETHForTokens(
0, # here setup the minimum destination token you want to have, you can do some math, or you can put a 0 if you don't want to care
[spend,contract_id],
sender_address,
(int(time.time()) + 1000000)
).buildTransaction({
'from': sender_address,
'value': web3.toWei(0.02,'ether'),#This is the Token(BNB) amount you want to Swap from
'gas': 250000,
'gasPrice': web3.toWei('5','gwei'),
'nonce': nonce,
})

#Sign Transaction
signed_txn = web3.eth.account.sign_transaction(pancakeswap2_txn, private_key=private)
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(web3.toHex(tx_token))

这是我收到的错误:

问题已解决: 使用种子短语作为私钥。 实际私钥可以这样获取:

  • 转到 metamask,单击角落的 3 个按钮,然后单击帐户 详细信息和导出密钥。