1

I am trying to send BNB from a Trust-Wallet with a python script to my binance account.

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.

Error is:

TypeError: Transaction had invalid fields: {'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. But I found no hints on that so far.

TylerH
  • 20,799
  • 66
  • 75
  • 101
maufer
  • 23
  • 5

1 Answers1

1

After some more research I found the solution in the Web3.py Documentation. To me, it's not clearly stated there, that addresses must have an EIP55 checksum. But as the API provides functions to check and create these, I thought I give it a try, and it worked!

So after adding an additional line at the beginning of the function:

to_public=web3.toChecksumAddress(to_public)

it worked.

maufer
  • 23
  • 5