Private Transactions
How to send a single transaction to Flashbots
If you want to send a single transaction to Flashbots without sending it as a bundle, you can use the eth_sendPrivateTransaction
method.
This method sends your transaction to the Flashbots builder on every block for a maximum of 25 blocks. No need to listen for the next block and re-send yourself.
Private transactions can be cancelled with the eth_cancelPrivateTransaction
method. Once a transaction is included in a block and received by proposers, we cannot "recall" it. However, we can stop including transactions in future blocks.
See RPC endpoint for JSON-RPC definitions of the methods.
These methods are currently implemented in ethers-provider-flashbots-bundle.js and web3-flashbots.py.
- ethers.js
- web3-flashbots.py
const signer = Wallet.createRandom()
const provider = new providers.JsonRpcProvider("http://localhost:8545")
const flashbotsProvider = await FlashbotsBundleProvider.create(provider, signer)
const transaction = {
from: signer.address,
to: signer.address,
value: "0x42",
gasPrice: BigNumber.from(99).mul(1e9),
gasLimit: BigNumber.from(21000),
}
const res = await flashbotsProvider.sendPrivateTransaction({
transaction,
signer,
}, {
maxBlockNumber: (await provider.getBlockNumber()) + 5, // only allow tx to be included for the next 5 blocks
});
const waitRes = await res.wait();
if (waitRes === FlashbotsTransactionResolution.TransactionIncluded) {
console.log("Private transaction successfully included on-chain.")
} else if (waitRes === FlashbotsTransactionResolution.TransactionDropped) {
console.log("Private transaction was not included in a block and has been removed from the system.")
}
web3 = Web3(HTTPProvider("http://localhost:8545"))
flashbot(w3, signer)
signer: LocalAccount = Account.from_key("0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
nonce = web3.eth.get_transaction_count(signer.address)
tx1: TxParams = {
"to": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"value": Web3.toWei(1, "ether"),
"data": "0xd0e30db0",
"gas": 21000,
"maxFeePerGas": Web3.toWei(100, "gwei"),
"maxPriorityFeePerGas": Web3.toWei(10, "gwei"),
"nonce": nonce,
"chainId": 1,
"type": 2,
}
web3.flashbots.send_private_transaction({
"signer": signer,
"transaction": tx1,
})