/** * 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 { getAddressEncoder, getProgramDerivedAddress, type Address, } from '@solana/addresses'; import { addDecoderSizePrefix, addEncoderSizePrefix, combineCodec, fixDecoderSize, fixEncoderSize, getBytesDecoder, getBytesEncoder, getStructDecoder, getStructEncoder, getU32Decoder, getU32Encoder, getU64Decoder, getU64Encoder, getU8Decoder, getU8Encoder, getUtf8Decoder, getUtf8Encoder, transformEncoder, type Codec, type Decoder, type Encoder, type ReadonlyUint8Array, } from '@solana/codecs'; import { IAccountMeta, IInstruction, IInstructionWithAccounts, IInstructionWithData, } from '../../utils/instruction-compat'; import { type IAccountSignerMeta, type TransactionSigner, } from '@solana/signers'; import { POD_COM_PROGRAM_ADDRESS } from '../programs'; import { expectAddress, expectSome, getAccountMetaFactory, type ResolvedAccount, } from '../shared'; // Define missing types for compatibility type ReadonlyAccount = T; type WritableAccount = T; type WritableSignerAccount = T; export const CREATE_CHANNEL_DISCRIMINATOR = new Uint8Array([ 142, 179, 25, 199, 84, 243, 69, 80, ]); export function getCreateChannelDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode( CREATE_CHANNEL_DISCRIMINATOR ); } export type CreateChannelInstruction< TProgram extends string = typeof POD_COM_PROGRAM_ADDRESS, TAccountChannelAccount extends string | IAccountMeta = string, TAccountCreator extends string | IAccountMeta = string, TAccountSystemProgram extends | string | IAccountMeta = '11111111111111111111111111111111', TRemainingAccounts extends readonly IAccountMeta[] = [], > = IInstruction & IInstructionWithData & IInstructionWithAccounts< [ TAccountChannelAccount extends string ? WritableAccount : TAccountChannelAccount, TAccountCreator extends string ? WritableSignerAccount & IAccountSignerMeta : TAccountCreator, TAccountSystemProgram extends string ? ReadonlyAccount : TAccountSystemProgram, ...TRemainingAccounts, ] >; export type CreateChannelInstructionData = { discriminator: ReadonlyUint8Array; channelId: string; name: string; description: string; visibility: number; maxParticipants: number; feePerMessage: bigint; }; export type CreateChannelInstructionDataArgs = { channelId: string; name: string; description: string; visibility: number; maxParticipants: number; feePerMessage: number | bigint; }; export function getCreateChannelInstructionDataEncoder(): Encoder { return transformEncoder( getStructEncoder([ ['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['channelId', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())], ['name', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())], ['description', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())], ['visibility', getU8Encoder()], ['maxParticipants', getU32Encoder()], ['feePerMessage', getU64Encoder()], ]), (value) => ({ ...value, discriminator: CREATE_CHANNEL_DISCRIMINATOR }) ); } export function getCreateChannelInstructionDataDecoder(): Decoder { return getStructDecoder([ ['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['channelId', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())], ['name', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())], ['description', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())], ['visibility', getU8Decoder()], ['maxParticipants', getU32Decoder()], ['feePerMessage', getU64Decoder()], ]); } export function getCreateChannelInstructionDataCodec(): Codec< CreateChannelInstructionDataArgs, CreateChannelInstructionData > { return combineCodec( getCreateChannelInstructionDataEncoder(), getCreateChannelInstructionDataDecoder() ); } export type CreateChannelAsyncInput< TAccountChannelAccount extends string = string, TAccountCreator extends string = string, TAccountSystemProgram extends string = string, > = { channelAccount?: Address; creator: TransactionSigner; systemProgram?: Address; channelId: CreateChannelInstructionDataArgs['channelId']; name: CreateChannelInstructionDataArgs['name']; description: CreateChannelInstructionDataArgs['description']; visibility: CreateChannelInstructionDataArgs['visibility']; maxParticipants: CreateChannelInstructionDataArgs['maxParticipants']; feePerMessage: CreateChannelInstructionDataArgs['feePerMessage']; }; export async function getCreateChannelInstructionAsync< TAccountChannelAccount extends string, TAccountCreator extends string, TAccountSystemProgram extends string, TProgramAddress extends Address = typeof POD_COM_PROGRAM_ADDRESS, >( input: CreateChannelAsyncInput< TAccountChannelAccount, TAccountCreator, TAccountSystemProgram >, config?: { programAddress?: TProgramAddress } ): Promise< CreateChannelInstruction< TProgramAddress, TAccountChannelAccount, TAccountCreator, TAccountSystemProgram > > { // Program address. const programAddress = config?.programAddress ?? POD_COM_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { channelAccount: { value: input.channelAccount ?? null, isWritable: true }, creator: { value: input.creator ?? null, isWritable: true }, 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.channelAccount.value) { accounts.channelAccount.value = await getProgramDerivedAddress({ programAddress, seeds: [ getBytesEncoder().encode( new Uint8Array([99, 104, 97, 110, 110, 101, 108]) ), getAddressEncoder().encode(expectAddress(accounts.creator.value)), addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder()).encode( expectSome(args.channelId) ), ], }); } if (!accounts.systemProgram.value) { accounts.systemProgram.value = '11111111111111111111111111111111' as Address<'11111111111111111111111111111111'>; } const getAccountMeta = getAccountMetaFactory(programAddress, 'programId'); const instruction = { accounts: [ getAccountMeta(accounts.channelAccount), getAccountMeta(accounts.creator), getAccountMeta(accounts.systemProgram), ].filter(Boolean), programAddress, data: new Uint8Array(getCreateChannelInstructionDataEncoder().encode( args as CreateChannelInstructionDataArgs )), } as any as CreateChannelInstruction< TProgramAddress, TAccountChannelAccount, TAccountCreator, TAccountSystemProgram >; return instruction; } export type CreateChannelInput< TAccountChannelAccount extends string = string, TAccountCreator extends string = string, TAccountSystemProgram extends string = string, > = { channelAccount: Address; creator: TransactionSigner; systemProgram?: Address; channelId: CreateChannelInstructionDataArgs['channelId']; name: CreateChannelInstructionDataArgs['name']; description: CreateChannelInstructionDataArgs['description']; visibility: CreateChannelInstructionDataArgs['visibility']; maxParticipants: CreateChannelInstructionDataArgs['maxParticipants']; feePerMessage: CreateChannelInstructionDataArgs['feePerMessage']; }; export function getCreateChannelInstruction< TAccountChannelAccount extends string, TAccountCreator extends string, TAccountSystemProgram extends string, TProgramAddress extends Address = typeof POD_COM_PROGRAM_ADDRESS, >( input: CreateChannelInput< TAccountChannelAccount, TAccountCreator, TAccountSystemProgram >, config?: { programAddress?: TProgramAddress } ): CreateChannelInstruction< TProgramAddress, TAccountChannelAccount, TAccountCreator, TAccountSystemProgram > { // Program address. const programAddress = config?.programAddress ?? POD_COM_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { channelAccount: { value: input.channelAccount ?? null, isWritable: true }, creator: { value: input.creator ?? null, isWritable: true }, 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'); const instruction = { accounts: [ getAccountMeta(accounts.channelAccount), getAccountMeta(accounts.creator), getAccountMeta(accounts.systemProgram), ].filter(Boolean), programAddress, data: new Uint8Array(getCreateChannelInstructionDataEncoder().encode( args as CreateChannelInstructionDataArgs )), } as any as CreateChannelInstruction< TProgramAddress, TAccountChannelAccount, TAccountCreator, TAccountSystemProgram >; return instruction; } export type ParsedCreateChannelInstruction< TProgram extends string = typeof POD_COM_PROGRAM_ADDRESS, TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[], > = { programAddress: Address; accounts: { channelAccount: TAccountMetas[0]; creator: TAccountMetas[1]; systemProgram: TAccountMetas[2]; }; data: CreateChannelInstructionData; }; export function parseCreateChannelInstruction< TProgram extends string, TAccountMetas extends readonly IAccountMeta[], >( instruction: IInstruction & IInstructionWithAccounts & IInstructionWithData ): ParsedCreateChannelInstruction { if (instruction.accounts.length < 3) { throw new Error('CreateChannel instruction requires at least 3 accounts: channelAccount, creator, systemProgram'); } let accountIndex = 0; const getNextAccount = () => { const accountMeta = instruction.accounts![accountIndex]!; accountIndex += 1; return accountMeta; }; return { programAddress: instruction.programAddress, accounts: { channelAccount: getNextAccount(), creator: getNextAccount(), systemProgram: getNextAccount(), }, data: getCreateChannelInstructionDataDecoder().decode(instruction.data), }; }