Skip to content

Instantly share code, notes, and snippets.

@claytantor
Created October 24, 2024 16:35
Show Gist options
  • Select an option

  • Save claytantor/f69ca88b46ad90d70b6c6b7b16083ea8 to your computer and use it in GitHub Desktop.

Select an option

Save claytantor/f69ca88b46ad90d70b6c6b7b16083ea8 to your computer and use it in GitHub Desktop.
Basic Fashbots Provider Simulation Example
const ethers = require('ethers');
const {
FlashbotsBundleProvider,
} = require("@flashbots/ethers-provider-bundle");
const dotenv = require("dotenv");
dotenv.config({ path: './config/.env.fbbundle.sepolia' });
const {
ETH_NETWORK,
FROM_ADDRESS_PRIVATE_KEY,
TO_ADDRESS,
RPC_FLASHBOTS_RELAY,
RPC_ENDPOINT_ETHERS,
PRIORITY_FEE_GWEI,
} = process.env;
async function main() {
console.log("ethers version:", ethers.version);
console.log("ETH_NETWORK:", ETH_NETWORK);
console.log("RPC_ENDPOINT_ETHERS:", RPC_ENDPOINT_ETHERS);
console.log("RPC_FLASHBOTS_RELAY:", RPC_FLASHBOTS_RELAY);
const provider = new ethers.JsonRpcProvider(RPC_ENDPOINT_ETHERS);
const latestBlockNumber = await provider.getBlockNumber();
console.log("Latest Block number:", latestBlockNumber);
console.log(`getting block ${latestBlockNumber}...`);
const block = await provider.getBlock(latestBlockNumber);
console.log(block);
if (block.baseFeePerGas == null) {
console.error('This chain is not EIP-1559 enabled')
return false;
}
let chainId = (await provider.getNetwork()).chainId;
console.log("chainId:", chainId);
const wallet = new ethers.Wallet(
FROM_ADDRESS_PRIVATE_KEY
);
// Flashbots provider requires passing in a standard provider and an auth signer
const flashbotsProvider = await FlashbotsBundleProvider.create(
provider,
wallet,
RPC_FLASHBOTS_RELAY,
ETH_NETWORK
);
const maxBaseFeeInFutureBlock = FlashbotsBundleProvider.getMaxBaseFeeInFutureBlock(block.baseFeePerGas, 1)
// convert the priority fee from gwei to wei
const priorityFee = ethers.parseUnits(PRIORITY_FEE_GWEI, "gwei");
const valueToSend = ethers.parseEther("0.001");
let tx = {
to: TO_ADDRESS,
value: valueToSend,
maxFeePerGas: priorityFee + maxBaseFeeInFutureBlock,
maxPriorityFeePerGas: priorityFee,
gasLimit: 21000,
type: 2,
chainId: chainId,
};
const signedBundle = await flashbotsProvider.signBundle([
{
signer: wallet,
transaction: tx,
},
]);
console.log("simulating...")
const targetBlock = latestBlockNumber + 1
const simulation = await flashbotsProvider.simulate(signedBundle, targetBlock)
console.log(simulation)
if ('error' in simulation) {
console.warn(`Simulation Error: ${simulation.error.message}`)
return false
}
console.log("simulation completed bundle...")
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment