Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Created May 27, 2021 01:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuyasugano/fc02dd50728fef1e07fd0010a28f89d2 to your computer and use it in GitHub Desktop.
Save yuyasugano/fc02dd50728fef1e07fd0010a28f89d2 to your computer and use it in GitHub Desktop.
flashbots sample
const ethers = require('ethers');
const { FlashbotsBundleProvider} = require('@flashbots/ethers-provider-bundle')
// Standard json rpc provider directly from ethers.js. For example you can use Infura, Alchemy, or your own node.
// This sample uses one of the default provider goerli
const provider = new ethers.getDefaultProvider("goerli");
// `authSigner` is an Ethereum private key that does NOT store funds and is NOT your bot's primary key.
// This is an identifying key for signing payloads to establish reputation and whitelisting
const authSigner = new ethers.Wallet(
'0x2000000000000000000000000000000000000000000000000000000000000000',
provider
);
const init = async () => {
// Flashbots provider requires passing in a standard provider and an auth signer
const flashbotsProvider = await FlashbotsBundleProvider.create(
provider,
authSigner,
"https://relay-goerli.flashbots.net",
"goerli"
);
// sign a transaction with the defined authSigner
const signedTransactions = await flashbotsProvider.signBundle([
{
signer: authSigner,
transaction: {
to: "0xf1a54b075fb71768ac31b33fd7c61ad8f9f7dd18",
gasPrice: 10,
gasLimit: 33000,
chainId: 5,
value: 0,
},
},
]);
const blockNumber = await provider.getBlockNumber();
console.log(new Date());
const simulation = await flashbotsProvider.simulate(
signedTransactions,
blockNumber + 1
);
console.log(new Date());
// Using TypeScript discrimination
if ("error" in simulation) {
console.log(`Simulation Error: ${simulation.error.message}`);
} else {
console.log(
`Simulation Success: ${blockNumber} ${JSON.stringify(
simulation,
null,
2
)}`
);
}
console.log(signedTransactions);
// submit bundles for the next 10 blocks in this loop
// Flashbots runs a small portion of the validators on Goerli
for (var i = 1; i <= 10; i++) {
const bundleSubmission = flashbotsProvider.sendRawBundle(
signedTransactions,
blockNumber + i
);
console.log("submitted for block # ", blockNumber + i);
}
console.log("bundles submitted");
}
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment