-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlistenForFrontrun.js
32 lines (26 loc) · 947 Bytes
/
listenForFrontrun.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
const { ethers } = require("ethers");
require("dotenv").config();
const url = `https://eth-mainnet.alchemyapi.io/v2/${process.env.KEY}`;
const privateKey = process.env.PRIVATE_KEY;
const provider = new ethers.providers.JsonRpcProvider(url);
const signer = new ethers.Wallet(privateKey, provider);
const ABI = [
"function _transfer() external",
"event Transfered(address, uint256)",
];
const CONTRACT_ADDRESS = "0xD4D6112c07420f413bdDE19b80E350CA013379c5";
const contract = new ethers.Contract(CONTRACT_ADDRESS, ABI, signer);
// First call listene()
async function listen() {
contract.on("Transfered", async (recipient, amount) => {
if (recipient.toLowerCase() != signer.address.toLocaleLowerCase()) {
console.log(`We got front run by: ${recipient}`);
} else {
console.log("We didn't get front run");
}
});
}
// Then call insecureTransfer()
async function insecureTransfer() {
await contract._transfer();
}