Aditya Dev
Aditya Dev

Reputation: 100

Web3-Py : Nonce Transaction Overwriting Problem

hello I am using python web3 to send transactions but nonce is the problem my code is

w3 = Web3(Web3.HTTPProvider("https://rpc.tomochain.com/"))
mainWallet = '0xAad6a88877E6AB7FbC33fdAce672780A85Fc88a8'
nonce = w3.eth.getTransactionCount(mainWallet)
amount_to_send = 0.01
private_key = 'xxxxxxxx'

tx = {
        'nonce': nonce,
        'to': to_address,
        'value': w3.toWei(amount_to_send, 'ether'),
        'gas': 21000,
        'gasPrice': w3.toWei('0.3', 'gwei')
    }

sign_tx = w3.eth.account.signTransaction(tx, private_key)
tran_hash = w3.eth.sendRawTransaction(sign_tx.rawTransaction)
txn = w3.toHex(tran_hash)
print(txn)

The Above code handle transactions well but the problem is when more than 1 user tries to withdraw nonce problem come suppose user 1 request withdraw of 0.001 & user 2 request withdraw of 0.002 and both of them request at the same time then the 0.001 transactions got canceled and only 0.002 passed

What can I do so the both of them pass

Upvotes: 1

Views: 1894

Answers (1)

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83758

Maintain a local ACID-compliant of transactions and used nonces before broadcasting them out. An example here.

Upvotes: 1

Related Questions