简体   繁体   English

使用 python/web3 将资产发送到 Binance 地址时出错

[英]Error sending assets to Binance address with python/web3

I am trying to send BNB from a Trust-Wallet with a python script to my binance account.我正在尝试将带有 python 脚本的 Trust-Wallet 中的 BNB 发送到我的币安账户。

Simple function:简单功能:

def send_bnb(to_public, from_public, from_secret, amount):
    nonce = web3.eth.getTransactionCount(from_public)
    tx = {
        'chainId': 97,
        'to': to_public,
        'nonce': nonce,
        'value': web3.toWei( amount,'ether'),
        'gas': 21000,
        'gasPrice': web3.toWei('50','gwei')
    }
    signed_tx = web3.eth.account.signTransaction( tx, from_secret)
    return web3.eth.sendRawTransaction(signed_tx.rawTransaction)

This works fine between two Trust-Wallet addresses, but fails if I try to send to my binance address.这在两个 Trust-Wallet 地址之间运行良好,但如果我尝试发送到我的币安地址则失败。

Error is:错误是:

TypeError: Transaction had invalid fields: {'to': <to_public>} TypeError:交易有无效字段:{'to': <to_public>}

The cause may be connected to the use of upper and lower case letters in the address, as I get the same error, if I convert the working Trust-Wallet address to lower case.原因可能与地址中使用大写和小写字母有关,如果我将工作的 Trust-Wallet 地址转换为小写,我会得到同样的错误。 But I found no hints on that so far.但到目前为止,我没有发现任何提示。

After some more research I found the solution in the Web3.py Documentation .经过更多研究后,我在Web3.py 文档中找到了解决方案。 To me, it's not clearly stated there, that addresses must have an EIP55 checksum.对我来说,那里没有明确说明,地址必须有 EIP55 校验和。 But as the API provides functions to check and create these, I thought I give it a try, and it worked!但由于 API 提供了检查和创建这些功能的功能,我想我试一试,它成功了!

So after adding an additional line at the beginning of the function:所以在函数的开头添加了一行之后:

to_public=web3.toChecksumAddress(to_public)

it worked.有效。

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

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