The transactions created by web3 (python) aren't reflected on the Ethereum blockchain

Question:

I’m trying to use web3.py with infura.io to send ethereum tokens from one address to another.
This returns to me a transaction ID. But it just never goes on to the blockchain when I check it. I have used high gas amount but it still doesn’t work (The transaction id isn’t present on the blockchain as per etherscan.io and also as per web3.py functions)

I tried a few other ways of signing the transaction but they didn’t work either.

Please help me make this work.

import web3
import time
w = web3.Web3(web3.HTTPProvider('https://mainnet.infura.io/12345678'))

# gas example
gas_limit = 250000
gas_price = 60

transaction = {
    'to':to_addr,
    'from':from_addr,
    'value':int(eth_amount*(10**18)),
    'gas':gas_limit,
    'gasPrice':int(gas_price*(10**9)),
    'chainId':1,
    'nonce':int(time.time())
    }
signed_transaction = w.eth.account.signTransaction(transaction, key)
transaction_id = w.eth.sendRawTransaction(signed_transaction.rawTransaction)

print ('nhttps://etherscan.io/tx/{0}'.format(transaction_id.hex()))
Asked By: aste123

||

Answers:

Your nonce is incorrect. This is not supposed to be a random value, but a scalar used to identify the number of transactions that have occurred from this account. This is used to make sure transactions are executed in the correct order they were submitted from the sender’s address. If you have a gap in your nonce, the transaction with the higher nonce won’t be executed until that gap is filled. Using time for nonce pretty much guarantees your transaction will never get mined.

You can use nonce = w.eth.getTransactionCount('from_addr') to get the correct nonce value. Note that the nonce is 0-based.

Answered By: Adam Kipnis
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.