Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit eb32c5e

Browse files
qiweiiibuffalojoec
andauthored
fix: Use COptionPubkeyLayout for all COption fields (#6173)
* fix: Use COptionPubkeyLayout for all COption fields * add test for null close authority * token js: init mint tests --------- Co-authored-by: Joe <[email protected]>
1 parent 259c6c2 commit eb32c5e

File tree

5 files changed

+176
-44
lines changed

5 files changed

+176
-44
lines changed

token/js/src/instructions/initializeMint.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { struct, u8 } from '@solana/buffer-layout';
22
import { publicKey } from '@solana/buffer-layout-utils';
3-
import type { AccountMeta } from '@solana/web3.js';
4-
import { PublicKey, SYSVAR_RENT_PUBKEY, TransactionInstruction } from '@solana/web3.js';
3+
import type { AccountMeta, PublicKey } from '@solana/web3.js';
4+
import { SYSVAR_RENT_PUBKEY, TransactionInstruction } from '@solana/web3.js';
55
import { TOKEN_PROGRAM_ID } from '../constants.js';
66
import {
77
TokenInvalidInstructionDataError,
@@ -10,23 +10,22 @@ import {
1010
TokenInvalidInstructionTypeError,
1111
} from '../errors.js';
1212
import { TokenInstruction } from './types.js';
13+
import { COptionPublicKeyLayout } from '../serialization.js';
1314

1415
/** TODO: docs */
1516
export interface InitializeMintInstructionData {
1617
instruction: TokenInstruction.InitializeMint;
1718
decimals: number;
1819
mintAuthority: PublicKey;
19-
freezeAuthorityOption: 1 | 0;
20-
freezeAuthority: PublicKey;
20+
freezeAuthority: PublicKey | null;
2121
}
2222

2323
/** TODO: docs */
2424
export const initializeMintInstructionData = struct<InitializeMintInstructionData>([
2525
u8('instruction'),
2626
u8('decimals'),
2727
publicKey('mintAuthority'),
28-
u8('freezeAuthorityOption'),
29-
publicKey('freezeAuthority'),
28+
new COptionPublicKeyLayout('freezeAuthority'),
3029
]);
3130

3231
/**
@@ -58,8 +57,7 @@ export function createInitializeMintInstruction(
5857
instruction: TokenInstruction.InitializeMint,
5958
decimals,
6059
mintAuthority,
61-
freezeAuthorityOption: freezeAuthority ? 1 : 0,
62-
freezeAuthority: freezeAuthority || new PublicKey(0),
60+
freezeAuthority,
6361
},
6462
data
6563
);
@@ -143,8 +141,7 @@ export function decodeInitializeMintInstructionUnchecked({
143141
keys: [mint, rent],
144142
data,
145143
}: TransactionInstruction): DecodedInitializeMintInstructionUnchecked {
146-
const { instruction, decimals, mintAuthority, freezeAuthorityOption, freezeAuthority } =
147-
initializeMintInstructionData.decode(data);
144+
const { instruction, decimals, mintAuthority, freezeAuthority } = initializeMintInstructionData.decode(data);
148145

149146
return {
150147
programId,
@@ -156,7 +153,7 @@ export function decodeInitializeMintInstructionUnchecked({
156153
instruction,
157154
decimals,
158155
mintAuthority,
159-
freezeAuthority: freezeAuthorityOption ? freezeAuthority : null,
156+
freezeAuthority,
160157
},
161158
};
162159
}

token/js/src/instructions/initializeMint2.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { struct, u8 } from '@solana/buffer-layout';
22
import { publicKey } from '@solana/buffer-layout-utils';
3-
import type { AccountMeta } from '@solana/web3.js';
4-
import { PublicKey, TransactionInstruction } from '@solana/web3.js';
3+
import type { AccountMeta, PublicKey } from '@solana/web3.js';
4+
import { TransactionInstruction } from '@solana/web3.js';
55
import { TOKEN_PROGRAM_ID } from '../constants.js';
66
import {
77
TokenInvalidInstructionDataError,
@@ -10,23 +10,22 @@ import {
1010
TokenInvalidInstructionTypeError,
1111
} from '../errors.js';
1212
import { TokenInstruction } from './types.js';
13+
import { COptionPublicKeyLayout } from '../serialization.js';
1314

1415
/** TODO: docs */
1516
export interface InitializeMint2InstructionData {
1617
instruction: TokenInstruction.InitializeMint2;
1718
decimals: number;
1819
mintAuthority: PublicKey;
19-
freezeAuthorityOption: 1 | 0;
20-
freezeAuthority: PublicKey;
20+
freezeAuthority: PublicKey | null;
2121
}
2222

2323
/** TODO: docs */
2424
export const initializeMint2InstructionData = struct<InitializeMint2InstructionData>([
2525
u8('instruction'),
2626
u8('decimals'),
2727
publicKey('mintAuthority'),
28-
u8('freezeAuthorityOption'),
29-
publicKey('freezeAuthority'),
28+
new COptionPublicKeyLayout('freezeAuthority'),
3029
]);
3130

3231
/**
@@ -55,8 +54,7 @@ export function createInitializeMint2Instruction(
5554
instruction: TokenInstruction.InitializeMint2,
5655
decimals,
5756
mintAuthority,
58-
freezeAuthorityOption: freezeAuthority ? 1 : 0,
59-
freezeAuthority: freezeAuthority || new PublicKey(0),
57+
freezeAuthority,
6058
},
6159
data
6260
);
@@ -135,8 +133,7 @@ export function decodeInitializeMint2InstructionUnchecked({
135133
keys: [mint],
136134
data,
137135
}: TransactionInstruction): DecodedInitializeMint2InstructionUnchecked {
138-
const { instruction, decimals, mintAuthority, freezeAuthorityOption, freezeAuthority } =
139-
initializeMint2InstructionData.decode(data);
136+
const { instruction, decimals, mintAuthority, freezeAuthority } = initializeMint2InstructionData.decode(data);
140137

141138
return {
142139
programId,
@@ -147,7 +144,7 @@ export function decodeInitializeMint2InstructionUnchecked({
147144
instruction,
148145
decimals,
149146
mintAuthority,
150-
freezeAuthority: freezeAuthorityOption ? freezeAuthority : null,
147+
freezeAuthority,
151148
},
152149
};
153150
}

token/js/src/instructions/initializeMintCloseAuthority.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { struct, u8 } from '@solana/buffer-layout';
2-
import { publicKey } from '@solana/buffer-layout-utils';
3-
import type { AccountMeta } from '@solana/web3.js';
4-
import { PublicKey, TransactionInstruction } from '@solana/web3.js';
2+
import type { AccountMeta, PublicKey } from '@solana/web3.js';
3+
import { TransactionInstruction } from '@solana/web3.js';
54
import { programSupportsExtensions } from '../constants.js';
65
import {
76
TokenInvalidInstructionDataError,
@@ -11,19 +10,18 @@ import {
1110
TokenUnsupportedInstructionError,
1211
} from '../errors.js';
1312
import { TokenInstruction } from './types.js';
13+
import { COptionPublicKeyLayout } from '../serialization.js';
1414

1515
/** TODO: docs */
1616
export interface InitializeMintCloseAuthorityInstructionData {
1717
instruction: TokenInstruction.InitializeMintCloseAuthority;
18-
closeAuthorityOption: 1 | 0;
19-
closeAuthority: PublicKey;
18+
closeAuthority: PublicKey | null;
2019
}
2120

2221
/** TODO: docs */
2322
export const initializeMintCloseAuthorityInstructionData = struct<InitializeMintCloseAuthorityInstructionData>([
2423
u8('instruction'),
25-
u8('closeAuthorityOption'),
26-
publicKey('closeAuthority'),
24+
new COptionPublicKeyLayout('closeAuthority'),
2725
]);
2826

2927
/**
@@ -49,8 +47,7 @@ export function createInitializeMintCloseAuthorityInstruction(
4947
initializeMintCloseAuthorityInstructionData.encode(
5048
{
5149
instruction: TokenInstruction.InitializeMintCloseAuthority,
52-
closeAuthorityOption: closeAuthority ? 1 : 0,
53-
closeAuthority: closeAuthority || new PublicKey(0),
50+
closeAuthority,
5451
},
5552
data
5653
);
@@ -127,8 +124,7 @@ export function decodeInitializeMintCloseAuthorityInstructionUnchecked({
127124
keys: [mint],
128125
data,
129126
}: TransactionInstruction): DecodedInitializeMintCloseAuthorityInstructionUnchecked {
130-
const { instruction, closeAuthorityOption, closeAuthority } =
131-
initializeMintCloseAuthorityInstructionData.decode(data);
127+
const { instruction, closeAuthority } = initializeMintCloseAuthorityInstructionData.decode(data);
132128

133129
return {
134130
programId,
@@ -137,7 +133,7 @@ export function decodeInitializeMintCloseAuthorityInstructionUnchecked({
137133
},
138134
data: {
139135
instruction,
140-
closeAuthority: closeAuthorityOption ? closeAuthority : null,
136+
closeAuthority,
141137
},
142138
};
143139
}

token/js/src/instructions/setAuthority.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { struct, u8 } from '@solana/buffer-layout';
22
import { publicKey } from '@solana/buffer-layout-utils';
3-
import type { AccountMeta, Signer } from '@solana/web3.js';
4-
import { PublicKey, TransactionInstruction } from '@solana/web3.js';
3+
import type { AccountMeta, Signer, PublicKey } from '@solana/web3.js';
4+
import { TransactionInstruction } from '@solana/web3.js';
55
import { TOKEN_PROGRAM_ID } from '../constants.js';
66
import {
77
TokenInvalidInstructionDataError,
@@ -11,6 +11,7 @@ import {
1111
} from '../errors.js';
1212
import { addSigners } from './internal.js';
1313
import { TokenInstruction } from './types.js';
14+
import { COptionPublicKeyLayout } from '../serialization.js';
1415

1516
/** Authority types defined by the program */
1617
export enum AuthorityType {
@@ -33,16 +34,14 @@ export enum AuthorityType {
3334
export interface SetAuthorityInstructionData {
3435
instruction: TokenInstruction.SetAuthority;
3536
authorityType: AuthorityType;
36-
newAuthorityOption: 1 | 0;
37-
newAuthority: PublicKey;
37+
newAuthority: PublicKey | null;
3838
}
3939

4040
/** TODO: docs */
4141
export const setAuthorityInstructionData = struct<SetAuthorityInstructionData>([
4242
u8('instruction'),
4343
u8('authorityType'),
44-
u8('newAuthorityOption'),
45-
publicKey('newAuthority'),
44+
new COptionPublicKeyLayout('newAuthority'),
4645
]);
4746

4847
/**
@@ -72,8 +71,7 @@ export function createSetAuthorityInstruction(
7271
{
7372
instruction: TokenInstruction.SetAuthority,
7473
authorityType,
75-
newAuthorityOption: newAuthority ? 1 : 0,
76-
newAuthority: newAuthority || new PublicKey(0),
74+
newAuthority,
7775
},
7876
data
7977
);
@@ -158,7 +156,7 @@ export function decodeSetAuthorityInstructionUnchecked({
158156
keys: [account, currentAuthority, ...multiSigners],
159157
data,
160158
}: TransactionInstruction): DecodedSetAuthorityInstructionUnchecked {
161-
const { instruction, authorityType, newAuthorityOption, newAuthority } = setAuthorityInstructionData.decode(data);
159+
const { instruction, authorityType, newAuthority } = setAuthorityInstructionData.decode(data);
162160

163161
return {
164162
programId,
@@ -170,7 +168,7 @@ export function decodeSetAuthorityInstructionUnchecked({
170168
data: {
171169
instruction,
172170
authorityType,
173-
newAuthority: newAuthorityOption ? newAuthority : null,
171+
newAuthority,
174172
},
175173
};
176174
}

0 commit comments

Comments
 (0)