-
Notifications
You must be signed in to change notification settings - Fork 2
/
all.ts
202 lines (185 loc) · 5.32 KB
/
all.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import {
clusterApiUrl,
sendAndConfirmTransaction,
Connection,
Keypair,
SystemProgram,
Transaction,
LAMPORTS_PER_SOL,
} from '@solana/web3.js';
import {
ExtensionType,
createInitializeMintInstruction,
mintTo,
createAccount,
getMintLen,
TOKEN_2022_PROGRAM_ID,
unpackAccount,
getTransferFeeAmount,
} from '@solana/spl-token';
import {
createInitializeTransferFeeConfigInstruction,
harvestWithheldTokensToMint,
transferCheckedWithFee,
withdrawWithheldTokensFromAccounts,
withdrawWithheldTokensFromMint,
} from '@solana/spl-token';
import { readFile, writeFile } from 'fs/promises';
import { decode } from 'bs58';
import * as dotenv from 'dotenv';
dotenv.config();
async function loadSecretKey(filename: string): Promise<Keypair | undefined> {
try {
const data = (await readFile('secrets/' + filename)).toString();
let secretKey;
try {
const keyArray = JSON.parse(data);
secretKey = new Uint8Array(keyArray);
} catch (e) {
secretKey = decode(data);
}
return Keypair.fromSecretKey(secretKey);
}
catch (e) {
console.log(e);
console.log(`Please setup ${filename} in secrets folder`);
}
}
async function saveSecretKey(keypair: Keypair, filename: string) {
try {
await writeFile('secrets/' + filename, `[${keypair.secretKey.toString()}]`);
} catch (e) {
console.log(e);
}
}
(async () => {
const signer = await loadSecretKey('signer.key');
const dev2 = await loadSecretKey('dev2.key');
if (!signer || !dev2) {
process.exit(1);
}
// const payer = Keypair.generate();
const payer = signer;
const mintAuthority = signer;
const mintKeypair = Keypair.generate();
const mint = mintKeypair.publicKey;
const transferFeeConfigAuthority = signer;
const withdrawWithheldAuthority = signer;
saveSecretKey(mintKeypair, 'mint.key');
// saveKeypair(payer, 'payer.txt');
// saveKeypair(mintAuthority, 'mintAuthority.txt');
// saveKeypair(mintKeypair, 'mintKeypair.txt');
// saveKeypair(transferFeeConfigAuthority, 'transferFeeConfigAuthority.txt');
// saveKeypair(withdrawWithheldAuthority, 'withdrawWithheldAuthority.txt');
const extensions = [ExtensionType.TransferFeeConfig];
const mintLen = getMintLen(extensions);
const decimals = 9;
const feeBasisPoints = 300;
const maxSupply = BigInt(1_000_000_000_000_000_000);
const maxFee = maxSupply;
const connection = new Connection(clusterApiUrl(process.env.NODE_ENV == 'production' ? 'mainnet-beta' : 'devnet'), 'confirmed');
// const airdropSignature = await connection.requestAirdrop(payer.publicKey, 2 * LAMPORTS_PER_SOL);
// await connection.confirmTransaction({ signature: airdropSignature, ...(await connection.getLatestBlockhash()) });
const mintLamports = await connection.getMinimumBalanceForRentExemption(mintLen);
const mintTransaction = new Transaction().add(
SystemProgram.createAccount({
fromPubkey: payer.publicKey,
newAccountPubkey: mint,
space: mintLen,
lamports: mintLamports,
programId: TOKEN_2022_PROGRAM_ID,
}),
createInitializeTransferFeeConfigInstruction(
mint,
transferFeeConfigAuthority.publicKey,
withdrawWithheldAuthority.publicKey,
feeBasisPoints,
maxFee,
TOKEN_2022_PROGRAM_ID
),
createInitializeMintInstruction(mint, decimals, mintAuthority.publicKey, null, TOKEN_2022_PROGRAM_ID)
);
await sendAndConfirmTransaction(connection, mintTransaction, [payer, mintKeypair], undefined);
// mint token
const mintAmount = maxSupply;
const signerTokenAccount = await createAccount(
connection,
payer,
mint,
payer.publicKey,
undefined,
undefined,
TOKEN_2022_PROGRAM_ID
);
console.log('signerTokenAccount :>> ', signerTokenAccount);
await mintTo(
connection,
payer,
mint,
signerTokenAccount,
mintAuthority,
mintAmount,
[],
undefined,
TOKEN_2022_PROGRAM_ID
);
// transfer
const destinationAccount = await createAccount(
connection,
payer,
mint,
dev2.publicKey,
undefined,
undefined,
TOKEN_2022_PROGRAM_ID
);
console.log('destinationAccount :>> ', destinationAccount);
const transferAmount = BigInt(1_000_000_000_000);
const fee = (transferAmount * BigInt(feeBasisPoints)) / BigInt(10_000);
await transferCheckedWithFee(
connection,
payer,
signerTokenAccount,
mint,
destinationAccount,
payer,
transferAmount,
decimals,
fee,
[],
undefined,
TOKEN_2022_PROGRAM_ID
);
// find withheld accounts
const allAccounts = await connection.getProgramAccounts(TOKEN_2022_PROGRAM_ID, {
commitment: 'confirmed',
filters: [
{
memcmp: {
offset: 0,
bytes: mint.toString(),
},
},
],
});
const accountsToWithdrawFrom = [];
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);
}
}
// withdraw
await withdrawWithheldTokensFromAccounts(
connection,
payer,
mint,
signerTokenAccount,
withdrawWithheldAuthority,
[],
accountsToWithdrawFrom,
undefined,
TOKEN_2022_PROGRAM_ID
);
})();