Skip to content

Commit 35de0ce

Browse files
authored
js-legacy: Add more examples (#310)
#### Problem The group, pausable, and scaled-ui-amount extensions have been in the code for some time, but there aren't any JS examples about how to use them. #### Summary of changes Similar to most other examples, add some lines about how to use them with the JS legacy client.
1 parent af35bb1 commit 35de0ce

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {
2+
clusterApiUrl,
3+
Connection,
4+
Keypair,
5+
LAMPORTS_PER_SOL,
6+
sendAndConfirmTransaction,
7+
SystemProgram,
8+
Transaction,
9+
} from '@solana/web3.js';
10+
import {
11+
createInitializeGroupPointerInstruction,
12+
createInitializeGroupMemberPointerInstruction,
13+
createInitializeMintInstruction,
14+
ExtensionType,
15+
getMintLen,
16+
TOKEN_2022_PROGRAM_ID,
17+
} from '@solana/spl-token';
18+
19+
(async () => {
20+
const payer = Keypair.generate();
21+
const mint = Keypair.generate();
22+
const decimals = 9;
23+
const mintLen = getMintLen([ExtensionType.GroupPointer, ExtensionType.GroupMemberPointer]);
24+
25+
const connection = new Connection(clusterApiUrl('devnet'), 'confirmed');
26+
27+
const airdropSignature = await connection.requestAirdrop(payer.publicKey, 2 * LAMPORTS_PER_SOL);
28+
await connection.confirmTransaction({
29+
signature: airdropSignature,
30+
...(await connection.getLatestBlockhash()),
31+
});
32+
33+
const mintLamports = await connection.getMinimumBalanceForRentExemption(mintLen);
34+
const mintTransaction = new Transaction().add(
35+
SystemProgram.createAccount({
36+
fromPubkey: payer.publicKey,
37+
newAccountPubkey: mint.publicKey,
38+
space: mintLen,
39+
lamports: mintLamports,
40+
programId: TOKEN_2022_PROGRAM_ID,
41+
}),
42+
createInitializeGroupPointerInstruction(mint.publicKey, payer.publicKey, mint.publicKey, TOKEN_2022_PROGRAM_ID),
43+
createInitializeGroupMemberPointerInstruction(
44+
mint.publicKey,
45+
payer.publicKey,
46+
mint.publicKey,
47+
TOKEN_2022_PROGRAM_ID,
48+
),
49+
createInitializeMintInstruction(mint.publicKey, decimals, payer.publicKey, null, TOKEN_2022_PROGRAM_ID),
50+
);
51+
const sig = await sendAndConfirmTransaction(connection, mintTransaction, [payer, mint]);
52+
console.log('Signature:', sig);
53+
})();
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {
2+
clusterApiUrl,
3+
Connection,
4+
Keypair,
5+
sendAndConfirmTransaction,
6+
SystemProgram,
7+
Transaction,
8+
LAMPORTS_PER_SOL,
9+
} from '@solana/web3.js';
10+
import {
11+
createInitializeMintInstruction,
12+
createInitializePausableConfigInstruction,
13+
getMintLen,
14+
pause,
15+
resume,
16+
ExtensionType,
17+
TOKEN_2022_PROGRAM_ID,
18+
} from '../src';
19+
20+
(async () => {
21+
const connection = new Connection(clusterApiUrl('devnet'), 'confirmed');
22+
23+
const payer = Keypair.generate();
24+
const airdropSignature = await connection.requestAirdrop(payer.publicKey, 2 * LAMPORTS_PER_SOL);
25+
await connection.confirmTransaction({ signature: airdropSignature, ...(await connection.getLatestBlockhash()) });
26+
27+
const mintKeypair = Keypair.generate();
28+
const decimals = 9;
29+
const mintLen = getMintLen([ExtensionType.PausableConfig]);
30+
const lamports = await connection.getMinimumBalanceForRentExemption(mintLen);
31+
const transaction = new Transaction().add(
32+
SystemProgram.createAccount({
33+
fromPubkey: payer.publicKey,
34+
newAccountPubkey: mintKeypair.publicKey,
35+
space: mintLen,
36+
lamports,
37+
programId: TOKEN_2022_PROGRAM_ID,
38+
}),
39+
createInitializePausableConfigInstruction(mintKeypair.publicKey, payer.publicKey, TOKEN_2022_PROGRAM_ID),
40+
createInitializeMintInstruction(
41+
mintKeypair.publicKey,
42+
decimals,
43+
payer.publicKey,
44+
payer.publicKey,
45+
TOKEN_2022_PROGRAM_ID,
46+
),
47+
);
48+
await sendAndConfirmTransaction(connection, transaction, [payer, mintKeypair]);
49+
50+
await pause(connection, payer, mintKeypair.publicKey, payer);
51+
await resume(connection, payer, mintKeypair.publicKey, payer);
52+
})();
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import {
2+
clusterApiUrl,
3+
Connection,
4+
Keypair,
5+
sendAndConfirmTransaction,
6+
SystemProgram,
7+
Transaction,
8+
LAMPORTS_PER_SOL,
9+
} from '@solana/web3.js';
10+
import {
11+
createInitializeMintInstruction,
12+
createInitializeScaledUiAmountConfigInstruction,
13+
getMintLen,
14+
updateMultiplier,
15+
ExtensionType,
16+
TOKEN_2022_PROGRAM_ID,
17+
} from '../src';
18+
19+
(async () => {
20+
const connection = new Connection(clusterApiUrl('devnet'), 'confirmed');
21+
22+
const payer = Keypair.generate();
23+
const airdropSignature = await connection.requestAirdrop(payer.publicKey, 2 * LAMPORTS_PER_SOL);
24+
await connection.confirmTransaction({ signature: airdropSignature, ...(await connection.getLatestBlockhash()) });
25+
26+
const mintKeypair = Keypair.generate();
27+
const decimals = 9;
28+
const multiplier = 10.1;
29+
const mintLen = getMintLen([ExtensionType.ScaledUiAmountConfig]);
30+
const lamports = await connection.getMinimumBalanceForRentExemption(mintLen);
31+
const transaction = new Transaction().add(
32+
SystemProgram.createAccount({
33+
fromPubkey: payer.publicKey,
34+
newAccountPubkey: mintKeypair.publicKey,
35+
space: mintLen,
36+
lamports,
37+
programId: TOKEN_2022_PROGRAM_ID,
38+
}),
39+
createInitializeScaledUiAmountConfigInstruction(
40+
mintKeypair.publicKey,
41+
payer.publicKey,
42+
multiplier,
43+
TOKEN_2022_PROGRAM_ID,
44+
),
45+
createInitializeMintInstruction(
46+
mintKeypair.publicKey,
47+
decimals,
48+
payer.publicKey,
49+
payer.publicKey,
50+
TOKEN_2022_PROGRAM_ID,
51+
),
52+
);
53+
await sendAndConfirmTransaction(connection, transaction, [payer, mintKeypair]);
54+
55+
const newMultiplier = 50;
56+
await updateMultiplier(
57+
connection,
58+
payer,
59+
mintKeypair.publicKey,
60+
payer.publicKey,
61+
newMultiplier,
62+
BigInt(0),
63+
[],
64+
undefined,
65+
TOKEN_2022_PROGRAM_ID,
66+
);
67+
})();

0 commit comments

Comments
 (0)