Web3.py – How to Send BEP20 Tokens in Web3.py

erc-20web3.py

def send_token_from_contract_in_bsc(tx_amount, tx_bsc_addr):
    web3_BSC = Web3(Web3.HTTPProvider(bscTestnet_url))
    contract_bsc_addr = web3_BSC.toChecksumAddress('0x74d4c3f8620c69C68a2144D4e1B4B7EE660cBA4D')
    bep20_addr_USDC = web3_BSC.toChecksumAddress('0x64544969ed7ebf5f083679233325356ebe738930')
    contract_bsc = web3_BSC.eth.contract(address = contract_bsc_addr, abi = contractAbi)
    bsc_Nonce = web3_BSC.eth.getTransactionCount(bscAccount)
    contract_method_transferToken = contract_bsc.functions.transferToken(bep20_addr_USDC, web3_BSC.toWei(tx_amount, 'wei'), web3_BSC.toChecksumAddress(tx_bsc_addr))
    contract_method_transferToken_build_tx = contract_method_transferToken.buildTransaction({
      'chainId': 97,
      'nonce': bsc_Nonce,
      'from': web3_BSC.toChecksumAddress(bscAccount),
      'value': web3_BSC.toWei(tx_amount, 'wei'),
      'gas': 21000,
      'gasPrice': web3_BSC.toWei('33', 'gwei'),
    })
    bsc_sign_tx_transferToken = web3_BSC.eth.account.signTransaction(contract_method_transferToken_build_tx, private_key = PRIVATE_KEY)
    bscTxHash = web3_BSC.eth.sendRawTransaction(bsc_sign_tx_transferToken.rawTransaction)

Hello guys! I try to send BEP20 tokens in BSC, All the work fine, tx is subscribes, but doesnt pushed in Testnet, problem come in last line:

bscTxHash = web3_BSC.eth.sendRawTransaction(bsc_sign_tx_transferToken.rawTransaction)

Error: ValueError: {'code': -32000, 'message': 'invalid sender'}

If somebody have idea, I will be glad to see 🙂

Best Answer

I solved my problem, problem was in bscTestnet_url wrong node url.

Related Topic