-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
token-js: add
COption<Pubkey>
support to `InitializeTransferFeeConf…
…ig` instruction (#6164)
- Loading branch information
Joe C
authored
Jan 23, 2024
1 parent
3d03765
commit 8b22d75
Showing
4 changed files
with
476 additions
and
296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.