3

I'm developing a website using python 3.6, Django 2.1.1, Solidity and web3.py v4. I want to add the transaction to ropsten testnet but the transactions are never getting confirmed. Here is the code:

amount_in_wei = w3.toWei(questionEtherValue,'ether')
nonce=w3.eth.getTransactionCount(w3.toChecksumAddress(questionairAddress))+1

txn_dict = {
     'to': contractAddress,
     'value': amount_in_wei,
     'gas': 2000000,
     'gasPrice': w3.toWei('70', 'gwei'),
     'nonce': nonce,
     'chainId': 3
}
signed_txn = account.signTransaction(txn_dict)
txn_hash = w3.eth.sendRawTransaction(signed_txn.rawTransaction)

try:
    txn_receipt = w3.eth.waitForTransactionReceipt(txn_hash, timeout=300)
except Exception:
    return {'status': 'failed', 'error': 'timeout'}
else:
    return {'status': 'success', 'receipt': txn_receipt}
TylerH
  • 20,799
  • 66
  • 75
  • 101
yasaman.h
  • 81
  • 8
  • 2M gas is a lot, about a quarter of a block currently. Do you actually need all that gas? If not, you might have more luck getting transactions included with a lower gas limit. – carver Sep 17 '18 at 22:52
  • I reduced gas limit to 30K but tx_receipt is still None! – yasaman.h Sep 19 '18 at 08:30
  • Is `questionairAddress == w3.eth.account.privateKeyToAccount(wallet_private_key).address`? – carver Sep 19 '18 at 15:34
  • It is a 20 byte MetaMask wallet address – yasaman.h Sep 19 '18 at 19:42
  • I suspect you are calculating the nonce with the wrong account – carver Sep 20 '18 at 16:34
  • 1
    I removed +1 in nonce calculation, it works! – yasaman.h Sep 20 '18 at 18:51
  • The last edit to the question made the code invalid. `account` is no longer set up for the line `account.signTransaction`. FWIW, the code leaves more open questions, like what is `questionairAddress` and how do we know that it's the same address as the account that's signing the transaction? I think the code is much clearer if you revert the full 3rd edit: https://stackoverflow.com/posts/52375369/revisions – carver Sep 22 '18 at 19:16

1 Answers1

1

Ah, as @yasaman.h discovered, there is an off-by-one error in the nonce:

# original:
nonce = w3.eth.getTransactionCount(w3.toChecksumAddress(questionairAddress)) + 1

# should be:
nonce = w3.eth.getTransactionCount(w3.toChecksumAddress(questionairAddress))

The nonce of a transaction must be equal to the count of previously-sent transactions. So the first transaction sent by a new account would have a nonce of zero.

carver
  • 2,229
  • 12
  • 28