Skip to main content

Create Raw Transactions

We can create raw transactions on brownie and predict their transaction hash without sending them.

contract = SomeContract.at('<contract address here>')
transaction = {
"from": attacker.address,
"to": contract.address,
"value": 0,
"gas": 0,
"gasPrice": web3.toWei('15', 'gwei'),
"nonce": web3.eth.getTransactionCount(attacker.address),
"chainId": web3.eth.chainId,
"data": contract.lockTokens.encode_input(1e19),
}

# Estimate the gas and save it
estimated_gas = web3.eth.estimateGas(transaction)
transaction['gas'] = estimated_gas

# Sign the transaction with a private key
signed_tx = web3.eth.account.sign_transaction(transaction, ATTACKER_PRIVATE_KEY)
transaction_hash_predict = web3.sha3(signed_tx.rawTransaction)

For the 'data' we can calculate the input with calling functions encode_input function. With the help of this we can create a transaction and sign it & get the transaction hash. If we want to send it to chain we just need to call sendRawTransaction method.

txh = web3.eth.sendRawTransaction(signed_tx.rawTransaction)