|
| 1 | +/** |
| 2 | + * This code was AUTOGENERATED using the Codama library. |
| 3 | + * Please DO NOT EDIT THIS FILE, instead use visitors |
| 4 | + * to add features, then rerun Codama to update it. |
| 5 | + * |
| 6 | + * @see https://github.com/codama-idl/codama |
| 7 | + */ |
| 8 | + |
| 9 | +import { |
| 10 | + BASE_ACCOUNT_SIZE, |
| 11 | + combineCodec, |
| 12 | + getAddressDecoder, |
| 13 | + getAddressEncoder, |
| 14 | + getStructDecoder, |
| 15 | + getStructEncoder, |
| 16 | + getU32Decoder, |
| 17 | + getU32Encoder, |
| 18 | + getU64Decoder, |
| 19 | + getU64Encoder, |
| 20 | + SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS, |
| 21 | + SolanaError, |
| 22 | + transformEncoder, |
| 23 | + type AccountMeta, |
| 24 | + type AccountSignerMeta, |
| 25 | + type Address, |
| 26 | + type FixedSizeCodec, |
| 27 | + type FixedSizeDecoder, |
| 28 | + type FixedSizeEncoder, |
| 29 | + type Instruction, |
| 30 | + type InstructionWithAccounts, |
| 31 | + type InstructionWithData, |
| 32 | + type ReadonlyUint8Array, |
| 33 | + type TransactionSigner, |
| 34 | + type WritableSignerAccount, |
| 35 | +} from '@solana/kit'; |
| 36 | +import { |
| 37 | + getAccountMetaFactory, |
| 38 | + type InstructionWithByteDelta, |
| 39 | + type ResolvedInstructionAccount, |
| 40 | +} from '@solana/kit/program-client-core'; |
| 41 | +import { SYSTEM_PROGRAM_ADDRESS } from '../programs'; |
| 42 | + |
| 43 | +export const CREATE_ACCOUNT_ALLOW_PREFUND_DISCRIMINATOR = 13; |
| 44 | + |
| 45 | +export function getCreateAccountAllowPrefundDiscriminatorBytes(): ReadonlyUint8Array { |
| 46 | + return getU32Encoder().encode(CREATE_ACCOUNT_ALLOW_PREFUND_DISCRIMINATOR); |
| 47 | +} |
| 48 | + |
| 49 | +export type CreateAccountAllowPrefundInstruction< |
| 50 | + TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS, |
| 51 | + TAccountNewAccount extends string | AccountMeta<string> = string, |
| 52 | + TAccountPayer extends string | AccountMeta<string> | undefined = undefined, |
| 53 | + TRemainingAccounts extends readonly AccountMeta<string>[] = [], |
| 54 | +> = Instruction<TProgram> & |
| 55 | + InstructionWithData<ReadonlyUint8Array> & |
| 56 | + InstructionWithAccounts< |
| 57 | + [ |
| 58 | + TAccountNewAccount extends string |
| 59 | + ? WritableSignerAccount<TAccountNewAccount> & AccountSignerMeta<TAccountNewAccount> |
| 60 | + : TAccountNewAccount, |
| 61 | + ...(TAccountPayer extends undefined |
| 62 | + ? [] |
| 63 | + : [ |
| 64 | + TAccountPayer extends string |
| 65 | + ? WritableSignerAccount<TAccountPayer> & AccountSignerMeta<TAccountPayer> |
| 66 | + : TAccountPayer, |
| 67 | + ]), |
| 68 | + ...TRemainingAccounts, |
| 69 | + ] |
| 70 | + >; |
| 71 | + |
| 72 | +export type CreateAccountAllowPrefundInstructionData = { |
| 73 | + discriminator: number; |
| 74 | + lamports: bigint; |
| 75 | + space: bigint; |
| 76 | + programAddress: Address; |
| 77 | +}; |
| 78 | + |
| 79 | +export type CreateAccountAllowPrefundInstructionDataArgs = { |
| 80 | + lamports?: number | bigint; |
| 81 | + space: number | bigint; |
| 82 | + programAddress: Address; |
| 83 | +}; |
| 84 | + |
| 85 | +export function getCreateAccountAllowPrefundInstructionDataEncoder(): FixedSizeEncoder<CreateAccountAllowPrefundInstructionDataArgs> { |
| 86 | + return transformEncoder( |
| 87 | + getStructEncoder([ |
| 88 | + ['discriminator', getU32Encoder()], |
| 89 | + ['lamports', getU64Encoder()], |
| 90 | + ['space', getU64Encoder()], |
| 91 | + ['programAddress', getAddressEncoder()], |
| 92 | + ]), |
| 93 | + value => ({ |
| 94 | + ...value, |
| 95 | + discriminator: CREATE_ACCOUNT_ALLOW_PREFUND_DISCRIMINATOR, |
| 96 | + lamports: value.lamports ?? 0, |
| 97 | + }), |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +export function getCreateAccountAllowPrefundInstructionDataDecoder(): FixedSizeDecoder<CreateAccountAllowPrefundInstructionData> { |
| 102 | + return getStructDecoder([ |
| 103 | + ['discriminator', getU32Decoder()], |
| 104 | + ['lamports', getU64Decoder()], |
| 105 | + ['space', getU64Decoder()], |
| 106 | + ['programAddress', getAddressDecoder()], |
| 107 | + ]); |
| 108 | +} |
| 109 | + |
| 110 | +export function getCreateAccountAllowPrefundInstructionDataCodec(): FixedSizeCodec< |
| 111 | + CreateAccountAllowPrefundInstructionDataArgs, |
| 112 | + CreateAccountAllowPrefundInstructionData |
| 113 | +> { |
| 114 | + return combineCodec( |
| 115 | + getCreateAccountAllowPrefundInstructionDataEncoder(), |
| 116 | + getCreateAccountAllowPrefundInstructionDataDecoder(), |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +export type CreateAccountAllowPrefundInput< |
| 121 | + TAccountNewAccount extends string = string, |
| 122 | + TAccountPayer extends string = string, |
| 123 | +> = { |
| 124 | + newAccount: TransactionSigner<TAccountNewAccount>; |
| 125 | + payer?: TransactionSigner<TAccountPayer>; |
| 126 | + lamports?: CreateAccountAllowPrefundInstructionDataArgs['lamports']; |
| 127 | + space: CreateAccountAllowPrefundInstructionDataArgs['space']; |
| 128 | + programAddress: CreateAccountAllowPrefundInstructionDataArgs['programAddress']; |
| 129 | +}; |
| 130 | + |
| 131 | +export function getCreateAccountAllowPrefundInstruction< |
| 132 | + TAccountNewAccount extends string, |
| 133 | + TAccountPayer extends string, |
| 134 | + TProgramAddress extends Address = typeof SYSTEM_PROGRAM_ADDRESS, |
| 135 | +>( |
| 136 | + input: CreateAccountAllowPrefundInput<TAccountNewAccount, TAccountPayer>, |
| 137 | + config?: { programAddress?: TProgramAddress }, |
| 138 | +): CreateAccountAllowPrefundInstruction<TProgramAddress, TAccountNewAccount, TAccountPayer> & InstructionWithByteDelta { |
| 139 | + // Program address. |
| 140 | + const programAddress = config?.programAddress ?? SYSTEM_PROGRAM_ADDRESS; |
| 141 | + |
| 142 | + // Original accounts. |
| 143 | + const originalAccounts = { |
| 144 | + newAccount: { value: input.newAccount ?? null, isWritable: true }, |
| 145 | + payer: { value: input.payer ?? null, isWritable: true }, |
| 146 | + }; |
| 147 | + const accounts = originalAccounts as Record<keyof typeof originalAccounts, ResolvedInstructionAccount>; |
| 148 | + |
| 149 | + // Original args. |
| 150 | + const args = { ...input }; |
| 151 | + |
| 152 | + // Bytes created or reallocated by the instruction. |
| 153 | + const byteDelta: number = [Number(args.space) + BASE_ACCOUNT_SIZE].reduce((a, b) => a + b, 0); |
| 154 | + |
| 155 | + const getAccountMeta = getAccountMetaFactory(programAddress, 'omitted'); |
| 156 | + return Object.freeze({ |
| 157 | + accounts: [getAccountMeta('newAccount', accounts.newAccount), getAccountMeta('payer', accounts.payer)].filter( |
| 158 | + <T>(x: T | undefined): x is T => x !== undefined, |
| 159 | + ), |
| 160 | + byteDelta, |
| 161 | + data: getCreateAccountAllowPrefundInstructionDataEncoder().encode( |
| 162 | + args as CreateAccountAllowPrefundInstructionDataArgs, |
| 163 | + ), |
| 164 | + programAddress, |
| 165 | + } as CreateAccountAllowPrefundInstruction<TProgramAddress, TAccountNewAccount, TAccountPayer> & |
| 166 | + InstructionWithByteDelta); |
| 167 | +} |
| 168 | + |
| 169 | +export type ParsedCreateAccountAllowPrefundInstruction< |
| 170 | + TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS, |
| 171 | + TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[], |
| 172 | +> = { |
| 173 | + programAddress: Address<TProgram>; |
| 174 | + accounts: { |
| 175 | + newAccount: TAccountMetas[0]; |
| 176 | + payer?: TAccountMetas[1] | undefined; |
| 177 | + }; |
| 178 | + data: CreateAccountAllowPrefundInstructionData; |
| 179 | +}; |
| 180 | + |
| 181 | +export function parseCreateAccountAllowPrefundInstruction< |
| 182 | + TProgram extends string, |
| 183 | + TAccountMetas extends readonly AccountMeta[], |
| 184 | +>( |
| 185 | + instruction: Instruction<TProgram> & |
| 186 | + InstructionWithAccounts<TAccountMetas> & |
| 187 | + InstructionWithData<ReadonlyUint8Array>, |
| 188 | +): ParsedCreateAccountAllowPrefundInstruction<TProgram, TAccountMetas> { |
| 189 | + if (instruction.accounts.length < 1) { |
| 190 | + throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS, { |
| 191 | + actualAccountMetas: instruction.accounts.length, |
| 192 | + expectedAccountMetas: 1, |
| 193 | + }); |
| 194 | + } |
| 195 | + let accountIndex = 0; |
| 196 | + const getNextAccount = () => { |
| 197 | + const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!; |
| 198 | + accountIndex += 1; |
| 199 | + return accountMeta; |
| 200 | + }; |
| 201 | + let optionalAccountsRemaining = instruction.accounts.length - 1; |
| 202 | + const getNextOptionalAccount = () => { |
| 203 | + if (optionalAccountsRemaining === 0) return undefined; |
| 204 | + optionalAccountsRemaining -= 1; |
| 205 | + return getNextAccount(); |
| 206 | + }; |
| 207 | + return { |
| 208 | + programAddress: instruction.programAddress, |
| 209 | + accounts: { newAccount: getNextAccount(), payer: getNextOptionalAccount() }, |
| 210 | + data: getCreateAccountAllowPrefundInstructionDataDecoder().decode(instruction.data), |
| 211 | + }; |
| 212 | +} |
0 commit comments