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()))
up vote 2 down vote accepted

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.

  • Thanks. The gas value is actually a typo in the question. I meant it to be 250,000. Recipient is a smart contract. – aste123 Jan 16 at 6:24
  • In that case, your gas limit is pretty reasonable. – smarx Jan 16 at 6:43

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.