Web3.py: TypeError: Transaction had invalid fields when i send a raw transaction to contract address

Created on 25 Apr 2018  ·  6Comments  ·  Source: ethereum/web3.py

  • Version: v4.0.0
  • Python: 3.6.4
  • OS: win 10

What was wrong?

I'm trying to sign and broadcast a raw transaction(to send 0.x ETH) to contract address. If i send 1 ETH it's working.

Code:

import time
from web3 import Web3, HTTPProvider
#import contract_abi

contract_address     = '0x86148aA32d278ZZZZZZ9866bFd545a0B1d70fAD'
wallet_private_key   = 'e56ec09e254d33dZZZZZZe6bfc86fb760b3bee2b51a00e892716ee902d2438'
wallet_address       = '0xcC6CaE5E121c6ZZZZZZ343509E859b804320cF7'

w3 = Web3(HTTPProvider('https://ropsten.infura.io/HcekpL4KhjR4L0pCa4o8'))

w3.eth.enable_unaudited_features()

print(w3.eth.blockNumber);

#contract = w3.eth.contract(address = contract_address, abi = contract_abi.abi)

def send_ether_to_contract(amount_in_ether):

    amount_in_wei = amount_in_ether * 1000000000000000000;

    nonce = w3.eth.getTransactionCount(wallet_address)

    txn_dict = {
            'to': contract_address,
            'value': amount_in_wei,
            'gas': 195000,
            'gasPrice': w3.toWei('11', 'gwei'),
            'nonce': nonce,
            'chainId': 3
    }

    signed_txn = w3.eth.account.signTransaction(txn_dict, wallet_private_key)

    txn_hash = w3.eth.sendRawTransaction(signed_txn.rawTransaction)

    txn_receipt = None

    count = 0
    while txn_receipt is None and (count < 30):

        txn_receipt = w3.eth.getTransactionReceipt(txn_hash)

        print(txn_receipt)

        time.sleep(10)


    if txn_receipt is None:
        return {'status': 'failed', 'error': 'timeout'}

    return {'status': 'added', 'txn_receipt': txn_receipt}

if __name__ == "__main__":

    send_ether_to_contract(0.0001)

Error:

  File "1send.py", line 55, in <module>
    send_ether_to_contract(0.0001)
  File "1send.py", line 32, in send_ether_to_contract
    signed_txn = w3.eth.account.signTransaction(txn_dict, wallet_private_key)
  File "C:\Users\ZZZZZZZZZ\AppData\Local\Programs\Python\Python36\lib\site-packages\eth_utils\decorators.py", line 17, in _wrapper
    return self.method(obj, *args, **kwargs)
  File "C:\Users\ZZZZZZZZZ\AppData\Local\Programs\Python\Python36\lib\site-packages\eth_account\account.py", line 422, in signTransaction
    ) = sign_transaction_dict(account._key_obj, transaction_dict)
  File "C:\Users\ZZZZZZZZZ\AppData\Local\Programs\Python\Python36\lib\site-packages\eth_account\internal\signing.py", line 23, in sign_transaction_dict
    unsigned_transaction = serializable_unsigned_transaction_from_dict(transaction_dict)
  File "C:\Users\ZZZZZZZZZ\AppData\Local\Programs\Python\Python36\lib\site-packages\eth_account\internal\transactions.py", line 35, in serializable_unsigned_transaction_from_dict
    assert_valid_fields(transaction_dict)
  File "C:\Users\ZZZZZZZZZ\AppData\Local\Programs\Python\Python36\lib\site-packages\eth_account\internal\transactions.py", line 140, in assert_valid_fields
    raise TypeError("Transaction had invalid fields: %r" % invalid)
TypeError: Transaction had invalid fields: {'value': 100000000000000.0}

From this tutorial:
https://hackernoon.com/ethereum-smart-contracts-in-python-a-comprehensive-ish-guide-771b03990988

Thanks,
Sebi

All 6 comments

Dont manually convert a value to wei. Use the api for this.

value = w3.toWei(value, 'ether')

Or if you're dead set on manually converting it, make sure you use

amount_in_wei = int(amount_in_ether * 1000000000000000000)

But again, you should use the api...

@dangell7 's answer is correct. wei should not be passed in as a float.

oh, i got it now, thank you both

why i got ValueError: {'code': -32010, 'message': 'Invalid chain id.'}
after run above program?

coding=utf-8

import web3
from web3 import Web3, HTTPProvider
import json
import rlp
from ethereum.transactions import Transaction

class OperationKovan:

def __init__(self):
    self.url = 'https://kovan.infura.io'
    self.w3 = Web3(HTTPProvider(self.url))

    # 前端充值
    self.R1_CONTRACT_ADDRESS = '0x97b2F257e51Da5bD16f6Ec34A575C366ffB745C2'
    self.R1_CONTRACT_PATH  = '/Users/renyiguang/Project/py3interface/dataconfig/r1ABI.json'
    with open(self.R1_CONTRACT_PATH) as load_f:
        load_dict = json.load(load_f)
        # print(load_dict)
    self.wallet_address = '0xDecF8962727Cd0465e8228001DCDEa46BA624C2c'
    self.wallet_private_key = '0ae83deffeec590205bcf633802548b1464a41c542dfb87c13606afbc53b4fee'

    # 创建R1对象(合约对象)
    self.R1 = self.w3.eth.contract(address=self.R1_CONTRACT_ADDRESS, abi=load_dict)

    self.w3.eth.enable_unaudited_features()

# 获得区块高度
def get_block_num(self):
    blockNum = self.w3.eth.blockNumber
    return blockNum

# 通过合约对象的xx方法获取到充值所需的数据
def get_recharge_data(self):
    pass

def send_ether_to_contract(self,amount_in_ether):

    # amount_in_wei = amount_in_ether * 1000000000000000000;
    amount_in_wei = self.w3.toWei(amount_in_ether, 'ether')

    nonce = self.w3.eth.getTransactionCount(self.wallet_address)

    txn_dict = {
            'to': self.R1_CONTRACT_ADDRESS,
            'value': amount_in_wei,
            'gas': 195000,
            'gasPrice': self.w3.toWei('11', 'gwei'),
            'nonce': nonce,
            'chainId': 3
    }

    signed_txn = self.w3.eth.account.signTransaction(txn_dict, self.wallet_private_key)
    txn_hash = self.w3.eth.sendRawTransaction(signed_txn.rawTransaction)
    txn_receipt = None
    count = 0
    while txn_receipt is None and (count < 30):
        txn_receipt = self.w3.eth.getTransactionReceipt(txn_hash)
        print(txn_receipt)
        time.sleep(10)

    if txn_receipt is None:
        return {'status': 'failed', 'error': 'timeout'}

    return {'status': 'added', 'txn_receipt': txn_receipt}

if __name__ == '__main__':
kovan = OperationKovan()
blockNum = kovan.get_block_num()
print(blockNum)
# recharge
kovan.send_ether_to_contract(0.0001)

@FrancisJen Your issue doesn't seem related to the issue discussed in this thread!
Either create a new issue or hop on to the gittter channel or ask for help on stackexchange.

I'll still try to answer your question:
remove chainId from your tx_dict and try again. If you send an incorrect chainId your transaction will error out. Its best not to send one.

Was this page helpful?
0 / 5 - 0 ratings