Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

token-js: add COption<Pubkey> support to InitializeTransferFeeConfig instruction #6164

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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