繁体   English   中英

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

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

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

代码:

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))

这是我收到的错误: 在此处输入图像描述

已解决的问题:使用种子短语作为私钥。 可以通过以下方式获取实际的私钥:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM