-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_erc20_redirect.js
62 lines (57 loc) · 1.84 KB
/
fast_erc20_redirect.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
import { ethers } from "ethers";
import erc20Abi from "@openzeppelin/contracts/build/contracts/IERC20.json" assert { type: "json" };
const privateKey = "";
const monitoredAddress = "".toLowerCase(); //All lower, no 0x
const newWallet = "".toLowerCase(); //All lower, no 0x
const providerURL = "wss://";
if (privateKey.length == 0) {
console.log("Specify privateKey!");
process.exit(0);
}
if (monitoredAddress.length == 0) {
console.log("Specify monitoredAddress!");
process.exit(0);
}
if (newWallet.length == 0) {
console.log("Specify newWallet!");
process.exit(0);
}
if (providerURL.length == 0) {
console.log("Specify providerURL!");
process.exit(0);
}
const provider = new ethers.providers.WebSocketProvider(providerURL);
const wallet = new ethers.Wallet(privateKey, provider);
const erc20Interface = new ethers.utils.Interface(erc20Abi.abi);
const abiCoder = new ethers.utils.AbiCoder();
const filter = {
topics: [
ethers.utils.id("Transfer(address,address,uint256)"),
null,
abiCoder.encode(["address"], [monitoredAddress]),
],
};
provider.on(filter, (params) => {
redirectTransferAsync(params)
.then(() => console.log("Successfully redirected"))
.catch(() => console.log("Redirection failed"));
});
async function redirectTransferAsync(params) {
if (params.data == "0x") {
return;
} //Ignore NFTs
console.log("Incoming transfer detected: " + params.transactionHash);
const contractAddress = params.address;
const contract = new ethers.Contract(contractAddress, erc20Interface, wallet);
const amount = abiCoder.decode(["uint256"], params.data)[0];
contract.functions
.transfer(newWallet, amount)
.then(() => {
console.log("Successfully transferred out");
})
.catch((ex) => {
console.log("Transferring failed!");
console.error(ex);
});
}
await new Promise((resolve) => {});