-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlashBotsTest.js
90 lines (67 loc) · 2.98 KB
/
FlashBotsTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { providers, Wallet } from "ethers";
import { FlashbotsBundleProvider, FlashbotsBundleResolution} from "@flashbots/ethers-provider-bundle";
// BigInt
const GWEI = 10n ** 9n;
const ETHER = 10n ** 18n;
// goerli testnet
const CHAIN_ID = 5;
const FLASHBOTS_ENDPOINT = "https://relay-goerli.flashbots.net";
// Alchemy Goerli API
const provider = new providers.JsonRpcProvider(process.env.ALCHEMY_GOERLI_API);
// Create new wallet with private key and the rpc provider
const wallet = new Wallet(process.env.PRIVATE_KEY, provider);
async function main() {
// `signer` 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 signer = new Wallet.createRandom();
// Flashbots provider requires passing in a standard provider and a signer
const flashbot = await FlashbotsBundleProvider.create(provider, signer, FLASHBOTS_ENDPOINT);
// Listening for events
provider.on("block", async (blockNumber) => {
console.log(`
---------------------------
|--> Block number: ${blockNumber} |
---------------------------
`);
const signedTx = await flashbot.signBundle([
{
signer: wallet,
transaction: {
chainId: CHAIN_ID,
type: 2,
value: 0, // Need Goerli network test ether
data: "0x68656c6c6f", //hello
maxFeePerGas: GWEI * 3n,
maxPriorityFeePerGas: GWEI * 2n,
gasLimit: 43000, // must always be over 42,000
to: "0x26C4ca34f722BD8fD23D58f34576d8718c883A80", //waste gas contract: https://goerli.etherscan.io/address/0x26C4ca34f722BD8fD23D58f34576d8718c883A80#code
},
},
]);
console.log('--> date: ', new Date());
const targetBlock = blockNumber + 1
const simulation = await flashbot.simulate(signedTx, targetBlock);
if ("error" in simulation) {
console.log(`--> Simulation error: ${simulation.error.message}`);
} else {
console.log(`--> Simulation success`);
};
// Submit Bundle
const bundleSubmission = await flashbot.sendRawBundle(signedTx, targetBlock);
console.log('Bundle submitted. Waiting...\n');
if ("error" in bundleSubmission) {
throw new Error(bundleSubmission.error.message);
};
// Wait for response. If Bundle is not included try again targeting next block. Else print tx and exit(0)
const response = await bundleSubmission.wait();
console.log(`--> Received response: ${FlashbotsBundleResolution[response]}`)
if (response === FlashbotsBundleResolution.BundleIncluded) {
console.log(`> Bundle included in ${targetBlock}`);
console.log(JSON.stringify(simulation, null, 2));
process.exit(0);
} else if (response === FlashbotsBundleResolution.BlockPassedWithoutInclusion || response === FlashbotsBundleResolution.AccountNonceTooHigh) {
console.log(`> Bundle not included in ${targetBlock}`);
};
});
}
main();