/
mintToChecked.ts
163 lines (151 loc) · 4.67 KB
/
mintToChecked.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
import { struct, u8 } from '@solana/buffer-layout';
import { u64 } from '@solana/buffer-layout-utils';
import type { AccountMeta, PublicKey, Signer } from '@solana/web3.js';
import { TransactionInstruction } from '@solana/web3.js';
import { TOKEN_PROGRAM_ID } from '../constants.js';
import {
TokenInvalidInstructionDataError,
TokenInvalidInstructionKeysError,
TokenInvalidInstructionProgramError,
TokenInvalidInstructionTypeError,
} from '../errors.js';
import { addSigners } from './internal.js';
import { TokenInstruction } from './types.js';
/** TODO: docs */
export interface MintToCheckedInstructionData {
instruction: TokenInstruction.MintToChecked;
amount: bigint;
decimals: number;
}
/** TODO: docs */
export const mintToCheckedInstructionData = struct<MintToCheckedInstructionData>([
u8('instruction'),
u64('amount'),
u8('decimals'),
]);
/**
* Construct a MintToChecked instruction
*
* @param mint Public key of the mint
* @param destination Address of the token account to mint to
* @param authority The mint authority
* @param amount Amount to mint
* @param decimals Number of decimals in amount to mint
* @param multiSigners Signing accounts if `authority` is a multisig
* @param programId SPL Token program account
*
* @return Instruction to add to a transaction
*/
export function createMintToCheckedInstruction(
mint: PublicKey,
destination: PublicKey,
authority: PublicKey,
amount: number | bigint,
decimals: number,
multiSigners: (Signer | PublicKey)[] = [],
programId = TOKEN_PROGRAM_ID
): TransactionInstruction {
const keys = addSigners(
[
{ pubkey: mint, isSigner: false, isWritable: true },
{ pubkey: destination, isSigner: false, isWritable: true },
],
authority,
multiSigners
);
const data = Buffer.alloc(mintToCheckedInstructionData.span);
mintToCheckedInstructionData.encode(
{
instruction: TokenInstruction.MintToChecked,
amount: BigInt(amount),
decimals,
},
data
);
return new TransactionInstruction({ keys, programId, data });
}
/** A decoded, valid MintToChecked instruction */
export interface DecodedMintToCheckedInstruction {
programId: PublicKey;
keys: {
mint: AccountMeta;
destination: AccountMeta;
authority: AccountMeta;
multiSigners: AccountMeta[];
};
data: {
instruction: TokenInstruction.MintToChecked;
amount: bigint;
decimals: number;
};
}
/**
* Decode a MintToChecked instruction and validate it
*
* @param instruction Transaction instruction to decode
* @param programId SPL Token program account
*
* @return Decoded, valid instruction
*/
export function decodeMintToCheckedInstruction(
instruction: TransactionInstruction,
programId = TOKEN_PROGRAM_ID
): DecodedMintToCheckedInstruction {
if (!instruction.programId.equals(programId)) throw new TokenInvalidInstructionProgramError();
if (instruction.data.length !== mintToCheckedInstructionData.span) throw new TokenInvalidInstructionDataError();
const {
keys: { mint, destination, authority, multiSigners },
data,
} = decodeMintToCheckedInstructionUnchecked(instruction);
if (data.instruction !== TokenInstruction.MintToChecked) throw new TokenInvalidInstructionTypeError();
if (!mint || !destination || !authority) throw new TokenInvalidInstructionKeysError();
// TODO: key checks?
return {
programId,
keys: {
mint,
destination,
authority,
multiSigners,
},
data,
};
}
/** A decoded, non-validated MintToChecked instruction */
export interface DecodedMintToCheckedInstructionUnchecked {
programId: PublicKey;
keys: {
mint: AccountMeta | undefined;
destination: AccountMeta | undefined;
authority: AccountMeta | undefined;
multiSigners: AccountMeta[];
};
data: {
instruction: number;
amount: bigint;
decimals: number;
};
}
/**
* Decode a MintToChecked instruction without validating it
*
* @param instruction Transaction instruction to decode
*
* @return Decoded, non-validated instruction
*/
export function decodeMintToCheckedInstructionUnchecked({
programId,
keys: [mint, destination, authority, ...multiSigners],
data,
}: TransactionInstruction): DecodedMintToCheckedInstructionUnchecked {
return {
programId,
keys: {
mint,
destination,
authority,
multiSigners,
},
data: mintToCheckedInstructionData.decode(data),
};
}