forked from solana-labs/solana-program-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransferFee.ts
167 lines (151 loc) · 4.63 KB
/
transferFee.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
import {
clusterApiUrl,
sendAndConfirmTransaction,
Connection,
Keypair,
SystemProgram,
Transaction,
LAMPORTS_PER_SOL,
} from '@solana/web3.js';
import {
ExtensionType,
createInitializeMintInstruction,
mintTo,
createAccount,
getMintLen,
getTransferFeeAmount,
unpackAccount,
TOKEN_2022_PROGRAM_ID,
} from '../src';
import {
createInitializeTransferFeeConfigInstruction,
harvestWithheldTokensToMint,
transferCheckedWithFee,
withdrawWithheldTokensFromAccounts,
withdrawWithheldTokensFromMint,
} from '../src/extensions/transferFee/index';
(async () => {
const payer = Keypair.generate();
const mintAuthority = Keypair.generate();
const mintKeypair = Keypair.generate();
const mint = mintKeypair.publicKey;
const transferFeeConfigAuthority = Keypair.generate();
const withdrawWithheldAuthority = Keypair.generate();
const extensions = [ExtensionType.TransferFeeConfig];
const mintLen = getMintLen(extensions);
const decimals = 9;
const feeBasisPoints = 50;
const maxFee = BigInt(5_000);
const connection = new Connection(clusterApiUrl('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);
const mintAmount = BigInt(1_000_000_000);
const owner = Keypair.generate();
const sourceAccount = await createAccount(
connection,
payer,
mint,
owner.publicKey,
undefined,
undefined,
TOKEN_2022_PROGRAM_ID,
);
await mintTo(
connection,
payer,
mint,
sourceAccount,
mintAuthority,
mintAmount,
[],
undefined,
TOKEN_2022_PROGRAM_ID,
);
const accountKeypair = Keypair.generate();
const destinationAccount = await createAccount(
connection,
payer,
mint,
owner.publicKey,
accountKeypair,
undefined,
TOKEN_2022_PROGRAM_ID,
);
const transferAmount = BigInt(1_000_000);
const fee = (transferAmount * BigInt(feeBasisPoints)) / BigInt(10_000);
await transferCheckedWithFee(
connection,
payer,
sourceAccount,
mint,
destinationAccount,
owner,
transferAmount,
decimals,
fee,
[],
undefined,
TOKEN_2022_PROGRAM_ID,
);
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);
}
}
await withdrawWithheldTokensFromAccounts(
connection,
payer,
mint,
destinationAccount,
withdrawWithheldAuthority,
[],
accountsToWithdrawFrom,
undefined,
TOKEN_2022_PROGRAM_ID,
);
await harvestWithheldTokensToMint(connection, payer, mint, [destinationAccount], undefined, TOKEN_2022_PROGRAM_ID);
await withdrawWithheldTokensFromMint(
connection,
payer,
mint,
destinationAccount,
withdrawWithheldAuthority,
[],
undefined,
TOKEN_2022_PROGRAM_ID,
);
})();