簡體   English   中英

使用 python/web3 將資產發送到 Binance 地址時出錯

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

我正在嘗試將帶有 python 腳本的 Trust-Wallet 中的 BNB 發送到我的幣安賬戶。

簡單功能:

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)

這在兩個 Trust-Wallet 地址之間運行良好,但如果我嘗試發送到我的幣安地址則失敗。

錯誤是:

TypeError:交易有無效字段:{'to': <to_public>}

原因可能與地址中使用大寫和小寫字母有關,如果我將工作的 Trust-Wallet 地址轉換為小寫,我會得到同樣的錯誤。 但到目前為止,我沒有發現任何提示。

經過更多研究后,我在Web3.py 文檔中找到了解決方案。 對我來說,那里沒有明確說明,地址必須有 EIP55 校驗和。 但由於 API 提供了檢查和創建這些功能的功能,我想我試一試,它成功了!

所以在函數的開頭添加了一行之后:

to_public=web3.toChecksumAddress(to_public)

有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM