/** * 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, getU8Decoder, getU8Encoder, getUtf8Decoder, getUtf8Encoder, transformEncoder, type Codec, type Decoder, type Encoder, type ReadonlyUint8Array, } from '@solana/codecs'; import { type IAccountSignerMeta, type TransactionSigner, } from '@solana/signers'; import { POD_COM_PROGRAM_ADDRESS } from '../programs'; import { expectAddress, expectSome, getAccountMetaFactory, type ResolvedAccount, } from '../shared'; import type { IAccountMeta, IInstruction, IInstructionWithAccounts, IInstructionWithData, } from '../../utils/instruction-compat'; // Define missing types for compatibility type ReadonlyAccount = T; type WritableAccount = T; type WritableSignerAccount = T; export const BROADCAST_MESSAGE_DISCRIMINATOR = new Uint8Array([ 82, 156, 47, 199, 117, 203, 24, 91, ]); export function getBroadcastMessageDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode( BROADCAST_MESSAGE_DISCRIMINATOR ); } export type BroadcastMessageInstruction< TProgram extends string = typeof POD_COM_PROGRAM_ADDRESS, TAccountMessageAccount extends string | IAccountMeta = string, TAccountChannelAccount extends string | IAccountMeta = string, TAccountSender extends string | IAccountMeta = string, TAccountSystemProgram extends | string | IAccountMeta = '11111111111111111111111111111111', TRemainingAccounts extends ReadonlyArray> = [], > = IInstruction & IInstructionWithData & IInstructionWithAccounts[]>; export interface BroadcastMessageInstructionData { discriminator: ReadonlyUint8Array; messageId: string; content: string; messageType: number; } export interface BroadcastMessageInstructionDataArgs { messageId: string; content: string; messageType: number; } export function getBroadcastMessageInstructionDataEncoder(): Encoder { return transformEncoder( getStructEncoder([ ['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['messageId', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())], ['content', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())], ['messageType', getU8Encoder()], ]), value => ({ ...value, discriminator: BROADCAST_MESSAGE_DISCRIMINATOR }) ); } export function getBroadcastMessageInstructionDataDecoder(): Decoder { return getStructDecoder([ ['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['messageId', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())], ['content', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())], ['messageType', getU8Decoder()], ]); } export function getBroadcastMessageInstructionDataCodec(): Codec< BroadcastMessageInstructionDataArgs, BroadcastMessageInstructionData > { return combineCodec( getBroadcastMessageInstructionDataEncoder(), getBroadcastMessageInstructionDataDecoder() ); } export interface BroadcastMessageAsyncInput< TAccountMessageAccount extends string = string, TAccountChannelAccount extends string = string, TAccountSender extends string = string, TAccountSystemProgram extends string = string, > { messageAccount?: Address; channelAccount: Address; sender: TransactionSigner; systemProgram?: Address; messageId: BroadcastMessageInstructionDataArgs['messageId']; content: BroadcastMessageInstructionDataArgs['content']; messageType: BroadcastMessageInstructionDataArgs['messageType']; } export async function getBroadcastMessageInstructionAsync< TAccountMessageAccount extends string, TAccountChannelAccount extends string, TAccountSender extends string, TAccountSystemProgram extends string, TProgramAddress extends Address = typeof POD_COM_PROGRAM_ADDRESS, >( input: BroadcastMessageAsyncInput< TAccountMessageAccount, TAccountChannelAccount, TAccountSender, TAccountSystemProgram >, config?: { programAddress?: TProgramAddress } ): Promise< BroadcastMessageInstruction< TProgramAddress, TAccountMessageAccount, TAccountChannelAccount, TAccountSender, TAccountSystemProgram > > { // Program address. const programAddress = config?.programAddress ?? POD_COM_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { messageAccount: { value: input.messageAccount ?? null, isWritable: true }, channelAccount: { value: input.channelAccount ?? null, isWritable: true }, sender: { value: input.sender ?? 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.messageAccount.value) { accounts.messageAccount.value = await getProgramDerivedAddress({ programAddress, seeds: [ getBytesEncoder().encode( new Uint8Array([98, 114, 111, 97, 100, 99, 97, 115, 116]) ), getAddressEncoder().encode( expectAddress(accounts.channelAccount.value) ), getAddressEncoder().encode(expectAddress(accounts.sender.value)), addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder()).encode( expectSome(args.messageId) ), ], }); } if (!accounts.systemProgram.value) { accounts.systemProgram.value = '11111111111111111111111111111111' as Address<'11111111111111111111111111111111'>; } const getAccountMeta = getAccountMetaFactory(programAddress, 'programId'); const instruction = { accounts: [ getAccountMeta(accounts.messageAccount), getAccountMeta(accounts.channelAccount), getAccountMeta(accounts.sender), getAccountMeta(accounts.systemProgram), ] as any, programAddress, data: new Uint8Array(getBroadcastMessageInstructionDataEncoder().encode( args as BroadcastMessageInstructionDataArgs )), } as BroadcastMessageInstruction< TProgramAddress, TAccountMessageAccount, TAccountChannelAccount, TAccountSender, TAccountSystemProgram >; return instruction; } export interface BroadcastMessageInput< TAccountMessageAccount extends string = string, TAccountChannelAccount extends string = string, TAccountSender extends string = string, TAccountSystemProgram extends string = string, > { messageAccount: Address; channelAccount: Address; sender: TransactionSigner; systemProgram?: Address; messageId: BroadcastMessageInstructionDataArgs['messageId']; content: BroadcastMessageInstructionDataArgs['content']; messageType: BroadcastMessageInstructionDataArgs['messageType']; } export function getBroadcastMessageInstruction< TAccountMessageAccount extends string, TAccountChannelAccount extends string, TAccountSender extends string, TAccountSystemProgram extends string, TProgramAddress extends Address = typeof POD_COM_PROGRAM_ADDRESS, >( input: BroadcastMessageInput< TAccountMessageAccount, TAccountChannelAccount, TAccountSender, TAccountSystemProgram >, config?: { programAddress?: TProgramAddress } ): BroadcastMessageInstruction< TProgramAddress, TAccountMessageAccount, TAccountChannelAccount, TAccountSender, TAccountSystemProgram > { // Program address. const programAddress = config?.programAddress ?? POD_COM_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { messageAccount: { value: input.messageAccount ?? null, isWritable: true }, channelAccount: { value: input.channelAccount ?? null, isWritable: true }, sender: { value: input.sender ?? 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.messageAccount), getAccountMeta(accounts.channelAccount), getAccountMeta(accounts.sender), getAccountMeta(accounts.systemProgram), ] as any, programAddress, data: new Uint8Array(getBroadcastMessageInstructionDataEncoder().encode( args as BroadcastMessageInstructionDataArgs )), } as BroadcastMessageInstruction< TProgramAddress, TAccountMessageAccount, TAccountChannelAccount, TAccountSender, TAccountSystemProgram >; return instruction; } export interface ParsedBroadcastMessageInstruction< TProgram extends string = typeof POD_COM_PROGRAM_ADDRESS, TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[], > { programAddress: Address; accounts: { messageAccount: TAccountMetas[0]; channelAccount: TAccountMetas[1]; sender: TAccountMetas[2]; systemProgram: TAccountMetas[3]; }; data: BroadcastMessageInstructionData; } export function parseBroadcastMessageInstruction< TProgram extends string, TAccountMetas extends readonly IAccountMeta[], >( instruction: IInstruction & IInstructionWithAccounts & IInstructionWithData ): ParsedBroadcastMessageInstruction { if (instruction.accounts.length < 4) { // Specific error for instruction validation throw new Error('Not enough accounts'); } let accountIndex = 0; const getNextAccount = () => { const accountMeta = instruction.accounts[accountIndex]!; accountIndex += 1; return accountMeta; }; return { programAddress: instruction.programAddress, accounts: { messageAccount: getNextAccount(), channelAccount: getNextAccount(), sender: getNextAccount(), systemProgram: getNextAccount(), }, data: getBroadcastMessageInstructionDataDecoder().decode(instruction.data), }; }