Web3.py Transactions – Transactions Not Visible on Ethereum Blockchain with Web3.py

pythonweb3.py

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 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. Thank you.

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()))

Best Answer

Your nonce is going to be absurdly high. The nonce for an account starts at 0 and increases with each outgoing transaction. You can get the current correct nonce via web3.eth.getTransactionCount.

Your gas limit is also quite high: 2.5 million. At 60 gwei and recent ether prices, consuming that much gas would cost a couple hundred US dollars. Now, your transaction probably won't actually consume that much gas, but it would be much safer to specify a more reasonable number. For a simple transfer between accounts, 21,000 should do. If you're sending ether to a contract that does some computation, consider using 100,000.