forked from solana-labs/solana-program-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimmutableOwner.ts
61 lines (55 loc) · 2 KB
/
immutableOwner.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
import {
clusterApiUrl,
sendAndConfirmTransaction,
Connection,
Keypair,
SystemProgram,
Transaction,
LAMPORTS_PER_SOL,
} from '@solana/web3.js';
import {
createAccount,
createMint,
createInitializeImmutableOwnerInstruction,
createInitializeAccountInstruction,
getAccountLen,
ExtensionType,
TOKEN_2022_PROGRAM_ID,
} from '../src';
(async () => {
const connection = new Connection(clusterApiUrl('devnet'), 'confirmed');
const payer = Keypair.generate();
const airdropSignature = await connection.requestAirdrop(payer.publicKey, 2 * LAMPORTS_PER_SOL);
await connection.confirmTransaction({ signature: airdropSignature, ...(await connection.getLatestBlockhash()) });
const mintAuthority = Keypair.generate();
const decimals = 9;
const mint = await createMint(
connection,
payer,
mintAuthority.publicKey,
mintAuthority.publicKey,
decimals,
undefined,
undefined,
TOKEN_2022_PROGRAM_ID,
);
const accountLen = getAccountLen([ExtensionType.ImmutableOwner]);
const lamports = await connection.getMinimumBalanceForRentExemption(accountLen);
const owner = Keypair.generate();
const accountKeypair = Keypair.generate();
const account = accountKeypair.publicKey;
const transaction = new Transaction().add(
SystemProgram.createAccount({
fromPubkey: payer.publicKey,
newAccountPubkey: account,
space: accountLen,
lamports,
programId: TOKEN_2022_PROGRAM_ID,
}),
createInitializeImmutableOwnerInstruction(account, TOKEN_2022_PROGRAM_ID),
createInitializeAccountInstruction(account, mint, owner.publicKey, TOKEN_2022_PROGRAM_ID),
);
await sendAndConfirmTransaction(connection, transaction, [payer, accountKeypair], undefined);
// create associated token account
await createAccount(connection, payer, mint, owner.publicKey, undefined, undefined, TOKEN_2022_PROGRAM_ID);
})();