|
| 1 | +import { struct, u8 } from '@solana/buffer-layout'; |
| 2 | +import { publicKey } from '@solana/buffer-layout-utils'; |
| 3 | +import type { Signer } from '@solana/web3.js'; |
| 4 | +import { PublicKey, TransactionInstruction } from '@solana/web3.js'; |
| 5 | +import { TOKEN_2022_PROGRAM_ID, programSupportsExtensions } from '../../constants.js'; |
| 6 | +import { TokenUnsupportedInstructionError } from '../../errors.js'; |
| 7 | +import { TokenInstruction } from '../../instructions/types.js'; |
| 8 | +import { addSigners } from '../../instructions/internal.js'; |
| 9 | + |
| 10 | +export enum GroupMemberPointerInstruction { |
| 11 | + Initialize = 0, |
| 12 | + Update = 1, |
| 13 | +} |
| 14 | + |
| 15 | +export const initializeGroupMemberPointerData = struct<{ |
| 16 | + instruction: TokenInstruction.GroupMemberPointerExtension; |
| 17 | + groupMemberPointerInstruction: number; |
| 18 | + authority: PublicKey; |
| 19 | + memberAddress: PublicKey; |
| 20 | +}>([ |
| 21 | + // prettier-ignore |
| 22 | + u8('instruction'), |
| 23 | + u8('groupMemberPointerInstruction'), |
| 24 | + publicKey('authority'), |
| 25 | + publicKey('memberAddress'), |
| 26 | +]); |
| 27 | + |
| 28 | +/** |
| 29 | + * Construct an Initialize GroupMemberPointer instruction |
| 30 | + * |
| 31 | + * @param mint Token mint account |
| 32 | + * @param authority Optional Authority that can set the member address |
| 33 | + * @param memberAddress Optional Account address that holds the member |
| 34 | + * @param programId SPL Token program account |
| 35 | + * |
| 36 | + * @return Instruction to add to a transaction |
| 37 | + */ |
| 38 | +export function createInitializeGroupMemberPointerInstruction( |
| 39 | + mint: PublicKey, |
| 40 | + authority: PublicKey | null, |
| 41 | + memberAddress: PublicKey | null, |
| 42 | + programId: PublicKey = TOKEN_2022_PROGRAM_ID |
| 43 | +): TransactionInstruction { |
| 44 | + if (!programSupportsExtensions(programId)) { |
| 45 | + throw new TokenUnsupportedInstructionError(); |
| 46 | + } |
| 47 | + const keys = [{ pubkey: mint, isSigner: false, isWritable: true }]; |
| 48 | + |
| 49 | + const data = Buffer.alloc(initializeGroupMemberPointerData.span); |
| 50 | + initializeGroupMemberPointerData.encode( |
| 51 | + { |
| 52 | + instruction: TokenInstruction.GroupMemberPointerExtension, |
| 53 | + groupMemberPointerInstruction: GroupMemberPointerInstruction.Initialize, |
| 54 | + authority: authority ?? PublicKey.default, |
| 55 | + memberAddress: memberAddress ?? PublicKey.default, |
| 56 | + }, |
| 57 | + data |
| 58 | + ); |
| 59 | + |
| 60 | + return new TransactionInstruction({ keys, programId, data: data }); |
| 61 | +} |
| 62 | + |
| 63 | +export const updateGroupMemberPointerData = struct<{ |
| 64 | + instruction: TokenInstruction.GroupMemberPointerExtension; |
| 65 | + groupMemberPointerInstruction: number; |
| 66 | + memberAddress: PublicKey; |
| 67 | +}>([ |
| 68 | + // prettier-ignore |
| 69 | + u8('instruction'), |
| 70 | + u8('groupMemberPointerInstruction'), |
| 71 | + publicKey('memberAddress'), |
| 72 | +]); |
| 73 | + |
| 74 | +export function createUpdateGroupMemberPointerInstruction( |
| 75 | + mint: PublicKey, |
| 76 | + authority: PublicKey, |
| 77 | + memberAddress: PublicKey | null, |
| 78 | + multiSigners: (Signer | PublicKey)[] = [], |
| 79 | + programId: PublicKey = TOKEN_2022_PROGRAM_ID |
| 80 | +): TransactionInstruction { |
| 81 | + if (!programSupportsExtensions(programId)) { |
| 82 | + throw new TokenUnsupportedInstructionError(); |
| 83 | + } |
| 84 | + |
| 85 | + const keys = addSigners([{ pubkey: mint, isSigner: false, isWritable: true }], authority, multiSigners); |
| 86 | + |
| 87 | + const data = Buffer.alloc(updateGroupMemberPointerData.span); |
| 88 | + updateGroupMemberPointerData.encode( |
| 89 | + { |
| 90 | + instruction: TokenInstruction.GroupMemberPointerExtension, |
| 91 | + groupMemberPointerInstruction: GroupMemberPointerInstruction.Update, |
| 92 | + memberAddress: memberAddress ?? PublicKey.default, |
| 93 | + }, |
| 94 | + data |
| 95 | + ); |
| 96 | + |
| 97 | + return new TransactionInstruction({ keys, programId, data: data }); |
| 98 | +} |
0 commit comments