-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.ts
84 lines (76 loc) · 2.18 KB
/
bot.ts
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
import { clusterApiUrl, Connection, Keypair, PublicKey } from "@solana/web3.js";
import {
TOKEN_2022_PROGRAM_ID,
unpackAccount,
getTransferFeeAmount,
withdrawWithheldTokensFromAccounts,
} from "@solana/spl-token";
import fs from "fs";
import { CronJob } from "cron";
// Local wallet from keypair json
const loadWalletKey = (keypair: string): Keypair => {
if (!keypair || keypair == "") {
throw new Error("Keypair is required!");
}
const loaded = Keypair.fromSecretKey(
new Uint8Array(JSON.parse(fs.readFileSync(keypair).toString()))
);
return loaded;
};
const MINT = new PublicKey("3reRsoxaj5Cn2mMYtQ3NBnq14o4mfaszHY9aYb1BvjVa");
const TREASURY_WALLET = new PublicKey(
"EoGmEccBukEUU21AgwekcK9TNhR2gozi1qWQkpekrxXn"
);
const WITHDRAW_WITHHELD_AUTHORITY = new PublicKey(
"51QHr8aS4En232fPCWUYLxWYw4crwxeap56n4jF1283Y"
);
const payer = loadWalletKey("./keys/cli-wallet.json");
const connection = new Connection(clusterApiUrl("mainnet-beta"), "confirmed");
// Withheld cron job will be triggere every 30s
const cronJob = new CronJob("*/30 * * * * *", async () => {
// Find all token fee existed ATAs
const allAccounts = await connection.getProgramAccounts(
TOKEN_2022_PROGRAM_ID,
{
commitment: "confirmed",
filters: [
{
memcmp: {
offset: 0,
bytes: MINT.toString(),
},
},
],
}
);
// Collect fee to TREASURY_WALLET simply by loop
const accountsToWithdrawFrom: PublicKey[] = [];
for (const accountInfo of allAccounts) {
const account = unpackAccount(
accountInfo.pubkey,
accountInfo.account,
TOKEN_2022_PROGRAM_ID
);
const transferFeeAmount = getTransferFeeAmount(account);
if (
transferFeeAmount !== null &&
transferFeeAmount.withheldAmount > BigInt(0)
) {
accountsToWithdrawFrom.push(accountInfo.pubkey);
const result = await withdrawWithheldTokensFromAccounts(
connection,
payer,
MINT,
TREASURY_WALLET,
WITHDRAW_WITHHELD_AUTHORITY,
[],
[accountInfo.pubkey],
undefined,
TOKEN_2022_PROGRAM_ID
);
}
}
});
if (!cronJob.running) {
cronJob.start();
}