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

Posted

技术标签:

【中文标题】Web3 签名交易问题找到非十六进制数字(Python)【英文标题】:Web3 Signing transaction issue Non-hexadecimal digit found (Python) 【发布时间】:2022-01-05 03:15:36 【问题描述】:

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

代码:

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

这是我收到的错误:

【问题讨论】:

请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。 【参考方案1】:

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

进入metamask,点击角落里的3个按钮,然后点击账号 详细信息和导出密钥。

【讨论】:

以上是关于Web3 签名交易问题找到非十六进制数字(Python)的主要内容,如果未能解决你的问题,请参考以下文章

web3j - 从签名交易中获取交易详情(金额、gas 价格、gas 限制)

关于web3.js中与交易发送交易签名智能合约函数调用相关api的理解

本地链使用web3js发送签名交易

使用 Trezor(硬件钱包)将签名交易发送到 Ropsten 或 Truffle 开发网络

使用 web3.js 从交易中获取 USDC(或任何非 ETH 值)

Web3.js sendSignedTransaction 给出“错误:无法检查交易收据”