/** * This code was AUTOGENERATED using the Codama library. * Please DO NOT EDIT THIS FILE, instead use visitors * to add features, then rerun Codama to update it. * * @see https://github.com/codama-idl/codama */ import { combineCodec, getAddressEncoder, getProgramDerivedAddress, getStructDecoder, getStructEncoder, getU8Decoder, getU8Encoder, getUtf8Encoder, transformEncoder, type AccountMeta, type AccountSignerMeta, type Address, type FixedSizeCodec, type FixedSizeDecoder, type FixedSizeEncoder, type Instruction, type InstructionWithAccounts, type InstructionWithData, type ReadonlyAccount, type ReadonlySignerAccount, type ReadonlyUint8Array, type TransactionSigner, type WritableAccount, type WritableSignerAccount, } from '@solana/kit'; import { COMMERCE_PROGRAM_PROGRAM_ADDRESS } from '../programs'; import { expectAddress, getAccountMetaFactory, type ResolvedAccount, } from '../shared'; export const CREATE_OPERATOR_DISCRIMINATOR = 1; export function getCreateOperatorDiscriminatorBytes() { return getU8Encoder().encode(CREATE_OPERATOR_DISCRIMINATOR); } export type CreateOperatorInstruction< TProgram extends string = typeof COMMERCE_PROGRAM_PROGRAM_ADDRESS, TAccountPayer extends string | AccountMeta = string, TAccountOperator extends string | AccountMeta = string, TAccountAuthority extends string | AccountMeta = string, TAccountSystemProgram extends | string | AccountMeta = '11111111111111111111111111111111', TRemainingAccounts extends readonly AccountMeta[] = [], > = Instruction & InstructionWithData & InstructionWithAccounts< [ TAccountPayer extends string ? WritableSignerAccount & AccountSignerMeta : TAccountPayer, TAccountOperator extends string ? WritableAccount : TAccountOperator, TAccountAuthority extends string ? ReadonlySignerAccount & AccountSignerMeta : TAccountAuthority, TAccountSystemProgram extends string ? ReadonlyAccount : TAccountSystemProgram, ...TRemainingAccounts, ] >; export type CreateOperatorInstructionData = { discriminator: number; bump: number; }; export type CreateOperatorInstructionDataArgs = { bump: number }; export function getCreateOperatorInstructionDataEncoder(): FixedSizeEncoder { return transformEncoder( getStructEncoder([ ['discriminator', getU8Encoder()], ['bump', getU8Encoder()], ]), (value) => ({ ...value, discriminator: CREATE_OPERATOR_DISCRIMINATOR }) ); } export function getCreateOperatorInstructionDataDecoder(): FixedSizeDecoder { return getStructDecoder([ ['discriminator', getU8Decoder()], ['bump', getU8Decoder()], ]); } export function getCreateOperatorInstructionDataCodec(): FixedSizeCodec< CreateOperatorInstructionDataArgs, CreateOperatorInstructionData > { return combineCodec( getCreateOperatorInstructionDataEncoder(), getCreateOperatorInstructionDataDecoder() ); } export type CreateOperatorAsyncInput< TAccountPayer extends string = string, TAccountOperator extends string = string, TAccountAuthority extends string = string, TAccountSystemProgram extends string = string, > = { payer: TransactionSigner; operator?: Address; authority: TransactionSigner; systemProgram?: Address; bump: CreateOperatorInstructionDataArgs['bump']; }; export async function getCreateOperatorInstructionAsync< TAccountPayer extends string, TAccountOperator extends string, TAccountAuthority extends string, TAccountSystemProgram extends string, TProgramAddress extends Address = typeof COMMERCE_PROGRAM_PROGRAM_ADDRESS, >( input: CreateOperatorAsyncInput< TAccountPayer, TAccountOperator, TAccountAuthority, TAccountSystemProgram >, config?: { programAddress?: TProgramAddress } ): Promise< CreateOperatorInstruction< TProgramAddress, TAccountPayer, TAccountOperator, TAccountAuthority, TAccountSystemProgram > > { // Program address. const programAddress = config?.programAddress ?? COMMERCE_PROGRAM_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { payer: { value: input.payer ?? null, isWritable: true }, operator: { value: input.operator ?? null, isWritable: true }, authority: { value: input.authority ?? null, isWritable: false }, systemProgram: { value: input.systemProgram ?? null, isWritable: false }, }; const accounts = originalAccounts as Record< keyof typeof originalAccounts, ResolvedAccount >; // Original args. const args = { ...input }; // Resolve default values. if (!accounts.operator.value) { accounts.operator.value = await getProgramDerivedAddress({ programAddress: 'commkU28d52cwo2Ma3Marxz4Qr9REtfJtuUfqnDnbhT' as Address<'commkU28d52cwo2Ma3Marxz4Qr9REtfJtuUfqnDnbhT'>, seeds: [ getUtf8Encoder().encode('operator'), getAddressEncoder().encode(expectAddress(accounts.authority.value)), ], }); } if (!accounts.systemProgram.value) { accounts.systemProgram.value = '11111111111111111111111111111111' as Address<'11111111111111111111111111111111'>; } const getAccountMeta = getAccountMetaFactory(programAddress, 'programId'); return Object.freeze({ accounts: [ getAccountMeta(accounts.payer), getAccountMeta(accounts.operator), getAccountMeta(accounts.authority), getAccountMeta(accounts.systemProgram), ], data: getCreateOperatorInstructionDataEncoder().encode( args as CreateOperatorInstructionDataArgs ), programAddress, } as CreateOperatorInstruction< TProgramAddress, TAccountPayer, TAccountOperator, TAccountAuthority, TAccountSystemProgram >); } export type CreateOperatorInput< TAccountPayer extends string = string, TAccountOperator extends string = string, TAccountAuthority extends string = string, TAccountSystemProgram extends string = string, > = { payer: TransactionSigner; operator: Address; authority: TransactionSigner; systemProgram?: Address; bump: CreateOperatorInstructionDataArgs['bump']; }; export function getCreateOperatorInstruction< TAccountPayer extends string, TAccountOperator extends string, TAccountAuthority extends string, TAccountSystemProgram extends string, TProgramAddress extends Address = typeof COMMERCE_PROGRAM_PROGRAM_ADDRESS, >( input: CreateOperatorInput< TAccountPayer, TAccountOperator, TAccountAuthority, TAccountSystemProgram >, config?: { programAddress?: TProgramAddress } ): CreateOperatorInstruction< TProgramAddress, TAccountPayer, TAccountOperator, TAccountAuthority, TAccountSystemProgram > { // Program address. const programAddress = config?.programAddress ?? COMMERCE_PROGRAM_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { payer: { value: input.payer ?? null, isWritable: true }, operator: { value: input.operator ?? null, isWritable: true }, authority: { value: input.authority ?? null, isWritable: false }, systemProgram: { value: input.systemProgram ?? null, isWritable: false }, }; const accounts = originalAccounts as Record< keyof typeof originalAccounts, ResolvedAccount >; // Original args. const args = { ...input }; // Resolve default values. if (!accounts.systemProgram.value) { accounts.systemProgram.value = '11111111111111111111111111111111' as Address<'11111111111111111111111111111111'>; } const getAccountMeta = getAccountMetaFactory(programAddress, 'programId'); return Object.freeze({ accounts: [ getAccountMeta(accounts.payer), getAccountMeta(accounts.operator), getAccountMeta(accounts.authority), getAccountMeta(accounts.systemProgram), ], data: getCreateOperatorInstructionDataEncoder().encode( args as CreateOperatorInstructionDataArgs ), programAddress, } as CreateOperatorInstruction< TProgramAddress, TAccountPayer, TAccountOperator, TAccountAuthority, TAccountSystemProgram >); } export type ParsedCreateOperatorInstruction< TProgram extends string = typeof COMMERCE_PROGRAM_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[], > = { programAddress: Address; accounts: { payer: TAccountMetas[0]; operator: TAccountMetas[1]; authority: TAccountMetas[2]; systemProgram: TAccountMetas[3]; }; data: CreateOperatorInstructionData; }; export function parseCreateOperatorInstruction< TProgram extends string, TAccountMetas extends readonly AccountMeta[], >( instruction: Instruction & InstructionWithAccounts & InstructionWithData ): ParsedCreateOperatorInstruction { if (instruction.accounts.length < 4) { // TODO: Coded error. throw new Error('Not enough accounts'); } let accountIndex = 0; const getNextAccount = () => { const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!; accountIndex += 1; return accountMeta; }; return { programAddress: instruction.programAddress, accounts: { payer: getNextAccount(), operator: getNextAccount(), authority: getNextAccount(), systemProgram: getNextAccount(), }, data: getCreateOperatorInstructionDataDecoder().decode(instruction.data), }; }