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

Commit 8b22d75

Browse files
author
Joe C
authored
token-js: add COption<Pubkey> support to InitializeTransferFeeConfig instruction (#6164)
1 parent 3d03765 commit 8b22d75

File tree

4 files changed

+476
-296
lines changed

4 files changed

+476
-296
lines changed

token/js/src/extensions/transferFee/instructions.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { struct, u16, u8 } from '@solana/buffer-layout';
2-
import { publicKey, u64 } from '@solana/buffer-layout-utils';
3-
import type { AccountMeta, Signer } from '@solana/web3.js';
4-
import { PublicKey, TransactionInstruction } from '@solana/web3.js';
2+
import { u64 } from '@solana/buffer-layout-utils';
3+
import type { AccountMeta, Signer, PublicKey } from '@solana/web3.js';
4+
import { TransactionInstruction } from '@solana/web3.js';
55
import { programSupportsExtensions, TOKEN_2022_PROGRAM_ID } from '../../constants.js';
66
import {
77
TokenInvalidInstructionDataError,
@@ -12,6 +12,7 @@ import {
1212
} from '../../errors.js';
1313
import { addSigners } from '../../instructions/internal.js';
1414
import { TokenInstruction } from '../../instructions/types.js';
15+
import { COptionPublicKeyLayout } from '../../serialization.js';
1516

1617
export enum TransferFeeInstruction {
1718
InitializeTransferFeeConfig = 0,
@@ -28,10 +29,8 @@ export enum TransferFeeInstruction {
2829
export interface InitializeTransferFeeConfigInstructionData {
2930
instruction: TokenInstruction.TransferFeeExtension;
3031
transferFeeInstruction: TransferFeeInstruction.InitializeTransferFeeConfig;
31-
transferFeeConfigAuthorityOption: 1 | 0;
32-
transferFeeConfigAuthority: PublicKey;
33-
withdrawWithheldAuthorityOption: 1 | 0;
34-
withdrawWithheldAuthority: PublicKey;
32+
transferFeeConfigAuthority: PublicKey | null;
33+
withdrawWithheldAuthority: PublicKey | null;
3534
transferFeeBasisPoints: number;
3635
maximumFee: bigint;
3736
}
@@ -40,10 +39,8 @@ export interface InitializeTransferFeeConfigInstructionData {
4039
export const initializeTransferFeeConfigInstructionData = struct<InitializeTransferFeeConfigInstructionData>([
4140
u8('instruction'),
4241
u8('transferFeeInstruction'),
43-
u8('transferFeeConfigAuthorityOption'),
44-
publicKey('transferFeeConfigAuthority'),
45-
u8('withdrawWithheldAuthorityOption'),
46-
publicKey('withdrawWithheldAuthority'),
42+
new COptionPublicKeyLayout('transferFeeConfigAuthority'),
43+
new COptionPublicKeyLayout('withdrawWithheldAuthority'),
4744
u16('transferFeeBasisPoints'),
4845
u64('maximumFee'),
4946
]);
@@ -78,10 +75,8 @@ export function createInitializeTransferFeeConfigInstruction(
7875
{
7976
instruction: TokenInstruction.TransferFeeExtension,
8077
transferFeeInstruction: TransferFeeInstruction.InitializeTransferFeeConfig,
81-
transferFeeConfigAuthorityOption: transferFeeConfigAuthority ? 1 : 0,
82-
transferFeeConfigAuthority: transferFeeConfigAuthority || new PublicKey(0),
83-
withdrawWithheldAuthorityOption: withdrawWithheldAuthority ? 1 : 0,
84-
withdrawWithheldAuthority: withdrawWithheldAuthority || new PublicKey(0),
78+
transferFeeConfigAuthority: transferFeeConfigAuthority,
79+
withdrawWithheldAuthority: withdrawWithheldAuthority,
8580
transferFeeBasisPoints: transferFeeBasisPoints,
8681
maximumFee: maximumFee,
8782
},
@@ -174,9 +169,7 @@ export function decodeInitializeTransferFeeConfigInstructionUnchecked({
174169
const {
175170
instruction,
176171
transferFeeInstruction,
177-
transferFeeConfigAuthorityOption,
178172
transferFeeConfigAuthority,
179-
withdrawWithheldAuthorityOption,
180173
withdrawWithheldAuthority,
181174
transferFeeBasisPoints,
182175
maximumFee,
@@ -190,8 +183,8 @@ export function decodeInitializeTransferFeeConfigInstructionUnchecked({
190183
data: {
191184
instruction,
192185
transferFeeInstruction,
193-
transferFeeConfigAuthority: transferFeeConfigAuthorityOption ? transferFeeConfigAuthority : null,
194-
withdrawWithheldAuthority: withdrawWithheldAuthorityOption ? withdrawWithheldAuthority : null,
186+
transferFeeConfigAuthority,
187+
withdrawWithheldAuthority,
195188
transferFeeBasisPoints,
196189
maximumFee,
197190
},

token/js/src/serialization.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Layout } from '@solana/buffer-layout';
2+
import { publicKey } from '@solana/buffer-layout-utils';
3+
import type { PublicKey } from '@solana/web3.js';
4+
5+
export class COptionPublicKeyLayout extends Layout<PublicKey | null> {
6+
private publicKeyLayout: Layout<PublicKey>;
7+
8+
constructor(property?: string | undefined) {
9+
super(-1, property);
10+
this.publicKeyLayout = publicKey();
11+
}
12+
13+
decode(buffer: Uint8Array, offset: number = 0): PublicKey | null {
14+
const option = buffer[offset];
15+
if (option === 0) {
16+
return null;
17+
}
18+
return this.publicKeyLayout.decode(buffer, offset + 1);
19+
}
20+
21+
encode(src: PublicKey | null, buffer: Uint8Array, offset: number = 0): number {
22+
if (src === null) {
23+
buffer[offset] = 0;
24+
return 1;
25+
} else {
26+
buffer[offset] = 1;
27+
this.publicKeyLayout.encode(src, buffer, offset + 1);
28+
return 33;
29+
}
30+
}
31+
32+
getSpan(buffer?: Uint8Array, offset: number = 0): number {
33+
if (buffer) {
34+
const option = buffer[offset];
35+
return option === 0 ? 1 : 1 + this.publicKeyLayout.span;
36+
}
37+
return 1 + this.publicKeyLayout.span;
38+
}
39+
}

0 commit comments

Comments
 (0)