Skip to main content

Why does web3py transtactions not work in my code?

from web3 import Web3 
import requests

*i have also connect to a node provider before this function.

def buy_coin_token():
    from_acc = str(input("Sender acc:"))
    private_key = str(input("Senders private key:"))
    to_acc = str(input("Receiver acc/contract address:"))
    amount_to_send = input("Amount to send:")
    gas = int(input("Gas:"))
    gas_price = int(input("Gas price:"))
    nonce = w3.eth.getTransactionCount(from_acc)
    tx = {
        'nonce': nonce,
        'to': to_acc,
        'value': amount_to_send,
        'gas': gas,
        'gasPrice': gas_price
    }
    signed_tx = w3.eth.account.signTransaction(tx, private_key)
    tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
    print("Your transactions has is "+(tx_hash))

I am trying to make a transaction on the bsc and i keep bumping into the same error:

TypeError: Transaction had invalid fields: {'value': '0.005'}

I have turning it into and int, float, and str, and I also though of doing the toWei function but I am afraid that it wont work and that it will send more that its supposed to.

I also want to test it into a bsc test network



source https://stackoverflow.com/questions/74955000/why-does-web3py-transtactions-not-work-in-my-code

Comments