/
actions.ts
67 lines (62 loc) · 2.62 KB
/
actions.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
import type { ConfirmOptions, Connection, PublicKey, Signer, TransactionSignature } from '@solana/web3.js';
import { sendAndConfirmTransaction, Transaction } from '@solana/web3.js';
import { getSigners } from '../../actions/internal.js';
import { TOKEN_2022_PROGRAM_ID } from '../../constants.js';
import type { AccountState } from '../../state/account.js';
import {
createInitializeDefaultAccountStateInstruction,
createUpdateDefaultAccountStateInstruction,
} from './instructions.js';
/**
* Initialize a default account state on a mint
*
* @param connection Connection to use
* @param payer Payer of the transaction fees
* @param mint Mint to initialize with extension
* @param state Account state with which to initialize new accounts
* @param confirmOptions Options for confirming the transaction
* @param programId SPL Token program account
*
* @return Signature of the confirmed transaction
*/
export async function initializeDefaultAccountState(
connection: Connection,
payer: Signer,
mint: PublicKey,
state: AccountState,
confirmOptions?: ConfirmOptions,
programId = TOKEN_2022_PROGRAM_ID
): Promise<TransactionSignature> {
const transaction = new Transaction().add(createInitializeDefaultAccountStateInstruction(mint, state, programId));
return await sendAndConfirmTransaction(connection, transaction, [payer], confirmOptions);
}
/**
* Update the default account state on a mint
*
* @param connection Connection to use
* @param payer Payer of the transaction fees
* @param mint Mint to modify
* @param state New account state to set on created accounts
* @param freezeAuthority Freeze authority of the mint
* @param multiSigners Signing accounts if `freezeAuthority` is a multisig
* @param confirmOptions Options for confirming the transaction
* @param programId SPL Token program account
*
* @return Signature of the confirmed transaction
*/
export async function updateDefaultAccountState(
connection: Connection,
payer: Signer,
mint: PublicKey,
state: AccountState,
freezeAuthority: Signer | PublicKey,
multiSigners: Signer[] = [],
confirmOptions?: ConfirmOptions,
programId = TOKEN_2022_PROGRAM_ID
): Promise<TransactionSignature> {
const [freezeAuthorityPublicKey, signers] = getSigners(freezeAuthority, multiSigners);
const transaction = new Transaction().add(
createUpdateDefaultAccountStateInstruction(mint, state, freezeAuthorityPublicKey, signers, programId)
);
return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions);
}