Skip to content

Commit

Permalink
token-js: add COption<Pubkey> support to `InitializeTransferFeeConf…
Browse files Browse the repository at this point in the history
…ig` instruction (#6164)
  • Loading branch information
Joe C authored Jan 23, 2024
1 parent 3d03765 commit 8b22d75
Show file tree
Hide file tree
Showing 4 changed files with 476 additions and 296 deletions.
31 changes: 12 additions & 19 deletions token/js/src/extensions/transferFee/instructions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { struct, u16, u8 } from '@solana/buffer-layout';
import { publicKey, u64 } from '@solana/buffer-layout-utils';
import type { AccountMeta, Signer } from '@solana/web3.js';
import { PublicKey, TransactionInstruction } from '@solana/web3.js';
import { u64 } from '@solana/buffer-layout-utils';
import type { AccountMeta, Signer, PublicKey } from '@solana/web3.js';
import { TransactionInstruction } from '@solana/web3.js';
import { programSupportsExtensions, TOKEN_2022_PROGRAM_ID } from '../../constants.js';
import {
TokenInvalidInstructionDataError,
Expand All @@ -12,6 +12,7 @@ import {
} from '../../errors.js';
import { addSigners } from '../../instructions/internal.js';
import { TokenInstruction } from '../../instructions/types.js';
import { COptionPublicKeyLayout } from '../../serialization.js';

export enum TransferFeeInstruction {
InitializeTransferFeeConfig = 0,
Expand All @@ -28,10 +29,8 @@ export enum TransferFeeInstruction {
export interface InitializeTransferFeeConfigInstructionData {
instruction: TokenInstruction.TransferFeeExtension;
transferFeeInstruction: TransferFeeInstruction.InitializeTransferFeeConfig;
transferFeeConfigAuthorityOption: 1 | 0;
transferFeeConfigAuthority: PublicKey;
withdrawWithheldAuthorityOption: 1 | 0;
withdrawWithheldAuthority: PublicKey;
transferFeeConfigAuthority: PublicKey | null;
withdrawWithheldAuthority: PublicKey | null;
transferFeeBasisPoints: number;
maximumFee: bigint;
}
Expand All @@ -40,10 +39,8 @@ export interface InitializeTransferFeeConfigInstructionData {
export const initializeTransferFeeConfigInstructionData = struct<InitializeTransferFeeConfigInstructionData>([
u8('instruction'),
u8('transferFeeInstruction'),
u8('transferFeeConfigAuthorityOption'),
publicKey('transferFeeConfigAuthority'),
u8('withdrawWithheldAuthorityOption'),
publicKey('withdrawWithheldAuthority'),
new COptionPublicKeyLayout('transferFeeConfigAuthority'),
new COptionPublicKeyLayout('withdrawWithheldAuthority'),
u16('transferFeeBasisPoints'),
u64('maximumFee'),
]);
Expand Down Expand Up @@ -78,10 +75,8 @@ export function createInitializeTransferFeeConfigInstruction(
{
instruction: TokenInstruction.TransferFeeExtension,
transferFeeInstruction: TransferFeeInstruction.InitializeTransferFeeConfig,
transferFeeConfigAuthorityOption: transferFeeConfigAuthority ? 1 : 0,
transferFeeConfigAuthority: transferFeeConfigAuthority || new PublicKey(0),
withdrawWithheldAuthorityOption: withdrawWithheldAuthority ? 1 : 0,
withdrawWithheldAuthority: withdrawWithheldAuthority || new PublicKey(0),
transferFeeConfigAuthority: transferFeeConfigAuthority,
withdrawWithheldAuthority: withdrawWithheldAuthority,
transferFeeBasisPoints: transferFeeBasisPoints,
maximumFee: maximumFee,
},
Expand Down Expand Up @@ -174,9 +169,7 @@ export function decodeInitializeTransferFeeConfigInstructionUnchecked({
const {
instruction,
transferFeeInstruction,
transferFeeConfigAuthorityOption,
transferFeeConfigAuthority,
withdrawWithheldAuthorityOption,
withdrawWithheldAuthority,
transferFeeBasisPoints,
maximumFee,
Expand All @@ -190,8 +183,8 @@ export function decodeInitializeTransferFeeConfigInstructionUnchecked({
data: {
instruction,
transferFeeInstruction,
transferFeeConfigAuthority: transferFeeConfigAuthorityOption ? transferFeeConfigAuthority : null,
withdrawWithheldAuthority: withdrawWithheldAuthorityOption ? withdrawWithheldAuthority : null,
transferFeeConfigAuthority,
withdrawWithheldAuthority,
transferFeeBasisPoints,
maximumFee,
},
Expand Down
39 changes: 39 additions & 0 deletions token/js/src/serialization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Layout } from '@solana/buffer-layout';
import { publicKey } from '@solana/buffer-layout-utils';
import type { PublicKey } from '@solana/web3.js';

export class COptionPublicKeyLayout extends Layout<PublicKey | null> {
private publicKeyLayout: Layout<PublicKey>;

constructor(property?: string | undefined) {
super(-1, property);
this.publicKeyLayout = publicKey();
}

decode(buffer: Uint8Array, offset: number = 0): PublicKey | null {
const option = buffer[offset];
if (option === 0) {
return null;
}
return this.publicKeyLayout.decode(buffer, offset + 1);
}

encode(src: PublicKey | null, buffer: Uint8Array, offset: number = 0): number {
if (src === null) {
buffer[offset] = 0;
return 1;
} else {
buffer[offset] = 1;
this.publicKeyLayout.encode(src, buffer, offset + 1);
return 33;
}
}

getSpan(buffer?: Uint8Array, offset: number = 0): number {
if (buffer) {
const option = buffer[offset];
return option === 0 ? 1 : 1 + this.publicKeyLayout.span;
}
return 1 + this.publicKeyLayout.span;
}
}
Loading

0 comments on commit 8b22d75

Please sign in to comment.