-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
initializeMintCloseAuthority.ts
139 lines (127 loc) · 4.24 KB
/
initializeMintCloseAuthority.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
import { struct, u8 } from '@solana/buffer-layout';
import type { AccountMeta, PublicKey } from '@solana/web3.js';
import { TransactionInstruction } from '@solana/web3.js';
import { programSupportsExtensions } from '../constants.js';
import {
TokenInvalidInstructionDataError,
TokenInvalidInstructionKeysError,
TokenInvalidInstructionProgramError,
TokenInvalidInstructionTypeError,
TokenUnsupportedInstructionError,
} from '../errors.js';
import { TokenInstruction } from './types.js';
import { COptionPublicKeyLayout } from '../serialization.js';
/** TODO: docs */
export interface InitializeMintCloseAuthorityInstructionData {
instruction: TokenInstruction.InitializeMintCloseAuthority;
closeAuthority: PublicKey | null;
}
/** TODO: docs */
export const initializeMintCloseAuthorityInstructionData = struct<InitializeMintCloseAuthorityInstructionData>([
u8('instruction'),
new COptionPublicKeyLayout('closeAuthority'),
]);
/**
* Construct an InitializeMintCloseAuthority instruction
*
* @param mint Token mint account
* @param closeAuthority Optional authority that can close the mint
* @param programId SPL Token program account
*
* @return Instruction to add to a transaction
*/
export function createInitializeMintCloseAuthorityInstruction(
mint: PublicKey,
closeAuthority: PublicKey | null,
programId: PublicKey
): TransactionInstruction {
if (!programSupportsExtensions(programId)) {
throw new TokenUnsupportedInstructionError();
}
const keys = [{ pubkey: mint, isSigner: false, isWritable: true }];
const data = Buffer.alloc(initializeMintCloseAuthorityInstructionData.span);
initializeMintCloseAuthorityInstructionData.encode(
{
instruction: TokenInstruction.InitializeMintCloseAuthority,
closeAuthority,
},
data
);
return new TransactionInstruction({ keys, programId, data });
}
/** A decoded, valid InitializeMintCloseAuthority instruction */
export interface DecodedInitializeMintCloseAuthorityInstruction {
programId: PublicKey;
keys: {
mint: AccountMeta;
};
data: {
instruction: TokenInstruction.InitializeMintCloseAuthority;
closeAuthority: PublicKey | null;
};
}
/**
* Decode an InitializeMintCloseAuthority instruction and validate it
*
* @param instruction Transaction instruction to decode
* @param programId SPL Token program account
*
* @return Decoded, valid instruction
*/
export function decodeInitializeMintCloseAuthorityInstruction(
instruction: TransactionInstruction,
programId: PublicKey
): DecodedInitializeMintCloseAuthorityInstruction {
if (!instruction.programId.equals(programId)) throw new TokenInvalidInstructionProgramError();
if (instruction.data.length !== initializeMintCloseAuthorityInstructionData.span)
throw new TokenInvalidInstructionDataError();
const {
keys: { mint },
data,
} = decodeInitializeMintCloseAuthorityInstructionUnchecked(instruction);
if (data.instruction !== TokenInstruction.InitializeMintCloseAuthority)
throw new TokenInvalidInstructionTypeError();
if (!mint) throw new TokenInvalidInstructionKeysError();
return {
programId,
keys: {
mint,
},
data,
};
}
/** A decoded, non-validated InitializeMintCloseAuthority instruction */
export interface DecodedInitializeMintCloseAuthorityInstructionUnchecked {
programId: PublicKey;
keys: {
mint: AccountMeta | undefined;
};
data: {
instruction: number;
closeAuthority: PublicKey | null;
};
}
/**
* Decode an InitializeMintCloseAuthority instruction without validating it
*
* @param instruction Transaction instruction to decode
*
* @return Decoded, non-validated instruction
*/
export function decodeInitializeMintCloseAuthorityInstructionUnchecked({
programId,
keys: [mint],
data,
}: TransactionInstruction): DecodedInitializeMintCloseAuthorityInstructionUnchecked {
const { instruction, closeAuthority } = initializeMintCloseAuthorityInstructionData.decode(data);
return {
programId,
keys: {
mint,
},
data: {
instruction,
closeAuthority,
},
};
}