| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- /**
- * This code was AUTOGENERATED using the kinobi library.
- * Please DO NOT EDIT THIS FILE, instead use visitors
- * to add features, then rerun kinobi to update it.
- *
- * @see https://github.com/metaplex-foundation/kinobi
- */
- import { BASE_ACCOUNT_SIZE } from '@solana/accounts';
- import {
- Address,
- getAddressDecoder,
- getAddressEncoder,
- } from '@solana/addresses';
- import {
- Codec,
- Decoder,
- Encoder,
- combineCodec,
- mapEncoder,
- } from '@solana/codecs-core';
- import {
- getStructDecoder,
- getStructEncoder,
- } from '@solana/codecs-data-structures';
- import {
- getU32Decoder,
- getU32Encoder,
- getU64Decoder,
- getU64Encoder,
- } from '@solana/codecs-numbers';
- import {
- AccountRole,
- IAccountMeta,
- IInstruction,
- IInstructionWithAccounts,
- IInstructionWithData,
- WritableSignerAccount,
- } from '@solana/instructions';
- import { IAccountSignerMeta, TransactionSigner } from '@solana/signers';
- import {
- IInstructionWithByteDelta,
- ResolvedAccount,
- accountMetaWithDefault,
- getAccountMetasWithSigners,
- } from '../shared';
- export type CreateAccountInstruction<
- TProgram extends string = '11111111111111111111111111111111',
- TAccountPayer extends string | IAccountMeta<string> = string,
- TAccountNewAccount extends string | IAccountMeta<string> = string,
- TRemainingAccounts extends Array<IAccountMeta<string>> = []
- > = IInstruction<TProgram> &
- IInstructionWithData<Uint8Array> &
- IInstructionWithAccounts<
- [
- TAccountPayer extends string
- ? WritableSignerAccount<TAccountPayer>
- : TAccountPayer,
- TAccountNewAccount extends string
- ? WritableSignerAccount<TAccountNewAccount>
- : TAccountNewAccount,
- ...TRemainingAccounts
- ]
- >;
- export type CreateAccountInstructionWithSigners<
- TProgram extends string = '11111111111111111111111111111111',
- TAccountPayer extends string | IAccountMeta<string> = string,
- TAccountNewAccount extends string | IAccountMeta<string> = string,
- TRemainingAccounts extends Array<IAccountMeta<string>> = []
- > = IInstruction<TProgram> &
- IInstructionWithData<Uint8Array> &
- IInstructionWithAccounts<
- [
- TAccountPayer extends string
- ? WritableSignerAccount<TAccountPayer> &
- IAccountSignerMeta<TAccountPayer>
- : TAccountPayer,
- TAccountNewAccount extends string
- ? WritableSignerAccount<TAccountNewAccount> &
- IAccountSignerMeta<TAccountNewAccount>
- : TAccountNewAccount,
- ...TRemainingAccounts
- ]
- >;
- export type CreateAccountInstructionData = {
- discriminator: number;
- lamports: bigint;
- space: bigint;
- programAddress: Address;
- };
- export type CreateAccountInstructionDataArgs = {
- lamports: number | bigint;
- space: number | bigint;
- programAddress: Address;
- };
- export function getCreateAccountInstructionDataEncoder(): Encoder<CreateAccountInstructionDataArgs> {
- return mapEncoder(
- getStructEncoder([
- ['discriminator', getU32Encoder()],
- ['lamports', getU64Encoder()],
- ['space', getU64Encoder()],
- ['programAddress', getAddressEncoder()],
- ]),
- (value) => ({ ...value, discriminator: 0 })
- );
- }
- export function getCreateAccountInstructionDataDecoder(): Decoder<CreateAccountInstructionData> {
- return getStructDecoder([
- ['discriminator', getU32Decoder()],
- ['lamports', getU64Decoder()],
- ['space', getU64Decoder()],
- ['programAddress', getAddressDecoder()],
- ]);
- }
- export function getCreateAccountInstructionDataCodec(): Codec<
- CreateAccountInstructionDataArgs,
- CreateAccountInstructionData
- > {
- return combineCodec(
- getCreateAccountInstructionDataEncoder(),
- getCreateAccountInstructionDataDecoder()
- );
- }
- export type CreateAccountInput<
- TAccountPayer extends string,
- TAccountNewAccount extends string
- > = {
- payer: Address<TAccountPayer>;
- newAccount: Address<TAccountNewAccount>;
- lamports: CreateAccountInstructionDataArgs['lamports'];
- space: CreateAccountInstructionDataArgs['space'];
- programAddress: CreateAccountInstructionDataArgs['programAddress'];
- };
- export type CreateAccountInputWithSigners<
- TAccountPayer extends string,
- TAccountNewAccount extends string
- > = {
- payer: TransactionSigner<TAccountPayer>;
- newAccount: TransactionSigner<TAccountNewAccount>;
- lamports: CreateAccountInstructionDataArgs['lamports'];
- space: CreateAccountInstructionDataArgs['space'];
- programAddress: CreateAccountInstructionDataArgs['programAddress'];
- };
- export function getCreateAccountInstruction<
- TAccountPayer extends string,
- TAccountNewAccount extends string,
- TProgram extends string = '11111111111111111111111111111111'
- >(
- input: CreateAccountInputWithSigners<TAccountPayer, TAccountNewAccount>
- ): CreateAccountInstructionWithSigners<
- TProgram,
- TAccountPayer,
- TAccountNewAccount
- > &
- IInstructionWithByteDelta;
- export function getCreateAccountInstruction<
- TAccountPayer extends string,
- TAccountNewAccount extends string,
- TProgram extends string = '11111111111111111111111111111111'
- >(
- input: CreateAccountInput<TAccountPayer, TAccountNewAccount>
- ): CreateAccountInstruction<TProgram, TAccountPayer, TAccountNewAccount> &
- IInstructionWithByteDelta;
- export function getCreateAccountInstruction<
- TAccountPayer extends string,
- TAccountNewAccount extends string,
- TProgram extends string = '11111111111111111111111111111111'
- >(
- input: CreateAccountInput<TAccountPayer, TAccountNewAccount>
- ): IInstruction & IInstructionWithByteDelta {
- // Program address.
- const programAddress =
- '11111111111111111111111111111111' as Address<'11111111111111111111111111111111'>;
- // Original accounts.
- type AccountMetas = Parameters<
- typeof getCreateAccountInstructionRaw<
- TProgram,
- TAccountPayer,
- TAccountNewAccount
- >
- >[0];
- const accounts: Record<keyof AccountMetas, ResolvedAccount> = {
- payer: { value: input.payer ?? null, isWritable: true },
- newAccount: { value: input.newAccount ?? null, isWritable: true },
- };
- // Original args.
- const args = { ...input };
- // Bytes created or reallocated by the instruction.
- const byteDelta: number = [Number(args.space) + BASE_ACCOUNT_SIZE].reduce(
- (a, b) => a + b,
- 0
- );
- // Get account metas and signers.
- const accountMetas = getAccountMetasWithSigners(
- accounts,
- 'programId',
- programAddress
- );
- const instruction = getCreateAccountInstructionRaw(
- accountMetas as Record<keyof AccountMetas, IAccountMeta>,
- args as CreateAccountInstructionDataArgs,
- programAddress
- );
- return Object.freeze({ ...instruction, byteDelta });
- }
- export function getCreateAccountInstructionRaw<
- TProgram extends string = '11111111111111111111111111111111',
- TAccountPayer extends string | IAccountMeta<string> = string,
- TAccountNewAccount extends string | IAccountMeta<string> = string,
- TRemainingAccounts extends Array<IAccountMeta<string>> = []
- >(
- accounts: {
- payer: TAccountPayer extends string
- ? Address<TAccountPayer>
- : TAccountPayer;
- newAccount: TAccountNewAccount extends string
- ? Address<TAccountNewAccount>
- : TAccountNewAccount;
- },
- args: CreateAccountInstructionDataArgs,
- programAddress: Address<TProgram> = '11111111111111111111111111111111' as Address<TProgram>,
- remainingAccounts?: TRemainingAccounts
- ) {
- return {
- accounts: [
- accountMetaWithDefault(accounts.payer, AccountRole.WRITABLE_SIGNER),
- accountMetaWithDefault(accounts.newAccount, AccountRole.WRITABLE_SIGNER),
- ...(remainingAccounts ?? []),
- ],
- data: getCreateAccountInstructionDataEncoder().encode(args),
- programAddress,
- } as CreateAccountInstruction<
- TProgram,
- TAccountPayer,
- TAccountNewAccount,
- TRemainingAccounts
- >;
- }
- export type ParsedCreateAccountInstruction<
- TProgram extends string = '11111111111111111111111111111111',
- TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[]
- > = {
- programAddress: Address<TProgram>;
- accounts: {
- payer: TAccountMetas[0];
- newAccount: TAccountMetas[1];
- };
- data: CreateAccountInstructionData;
- };
- export function parseCreateAccountInstruction<
- TProgram extends string,
- TAccountMetas extends readonly IAccountMeta[]
- >(
- instruction: IInstruction<TProgram> &
- IInstructionWithAccounts<TAccountMetas> &
- IInstructionWithData<Uint8Array>
- ): ParsedCreateAccountInstruction<TProgram, TAccountMetas> {
- if (instruction.accounts.length < 2) {
- // TODO: Coded error.
- 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: {
- payer: getNextAccount(),
- newAccount: getNextAccount(),
- },
- data: getCreateAccountInstructionDataDecoder().decode(instruction.data),
- };
- }
|