Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions clients/js/src/generated/instructions/batch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* 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 {
AccountRole,
addDecoderSizePrefix,
addEncoderSizePrefix,
combineCodec,
getArrayDecoder,
getArrayEncoder,
getBytesDecoder,
getBytesEncoder,
getStructDecoder,
getStructEncoder,
getU8Decoder,
getU8Encoder,
transformEncoder,
type AccountMeta,
type Address,
type Codec,
type Decoder,
type Encoder,
type Instruction,
type InstructionWithAccounts,
type InstructionWithData,
type ReadonlyUint8Array,
type TransactionSigner,
} from '@solana/kit';
import { TOKEN_PROGRAM_ADDRESS } from '../programs';

export const BATCH_DISCRIMINATOR = 255;

export function getBatchDiscriminatorBytes() {
return getU8Encoder().encode(BATCH_DISCRIMINATOR);
}

export type BatchInstruction<
TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<TRemainingAccounts>;

export type BatchInstructionData = {
discriminator: number;
/** Instruction data for batch instructions. */
data: Array<{ numberOfAccounts: number; instructionData: ReadonlyUint8Array }>;
};

export type BatchInstructionDataArgs = {
/** Instruction data for batch instructions. */
data: Array<{ numberOfAccounts: number; instructionData: ReadonlyUint8Array }>;
};

export function getBatchInstructionDataEncoder(): Encoder<BatchInstructionDataArgs> {
return transformEncoder(
getStructEncoder([
['discriminator', getU8Encoder()],
[
'data',
getArrayEncoder(
getStructEncoder([
['numberOfAccounts', getU8Encoder()],
['instructionData', addEncoderSizePrefix(getBytesEncoder(), getU8Encoder())],
]),
{ size: 'remainder' },
),
],
]),
value => ({ ...value, discriminator: BATCH_DISCRIMINATOR }),
);
}

export function getBatchInstructionDataDecoder(): Decoder<BatchInstructionData> {
return getStructDecoder([
['discriminator', getU8Decoder()],
[
'data',
getArrayDecoder(
getStructDecoder([
['numberOfAccounts', getU8Decoder()],
['instructionData', addDecoderSizePrefix(getBytesDecoder(), getU8Decoder())],
]),
{ size: 'remainder' },
),
],
]);
}

export function getBatchInstructionDataCodec(): Codec<BatchInstructionDataArgs, BatchInstructionData> {
return combineCodec(getBatchInstructionDataEncoder(), getBatchInstructionDataDecoder());
}

export type BatchInput = {
data: BatchInstructionDataArgs['data'];
accounts?: Array<TransactionSigner>;
};

export function getBatchInstruction<TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS>(
input: BatchInput,
config?: { programAddress?: TProgramAddress },
): BatchInstruction<TProgramAddress> {
// Program address.
const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;

// Original args.
const args = { ...input };

// Remaining accounts.
const remainingAccounts: AccountMeta[] = (args.accounts ?? []).map(signer => ({
address: signer.address,
role: AccountRole.READONLY_SIGNER,
signer,
}));

return Object.freeze({
accounts: remainingAccounts,
data: getBatchInstructionDataEncoder().encode(args as BatchInstructionDataArgs),
programAddress,
} as BatchInstruction<TProgramAddress>);
}

export type ParsedBatchInstruction<TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS> = {
programAddress: Address<TProgram>;
data: BatchInstructionData;
};

export function parseBatchInstruction<TProgram extends string>(
instruction: Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array>,
): ParsedBatchInstruction<TProgram> {
return {
programAddress: instruction.programAddress,
data: getBatchInstructionDataDecoder().decode(instruction.data),
};
}
3 changes: 3 additions & 0 deletions clients/js/src/generated/instructions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
export * from './amountToUiAmount';
export * from './approve';
export * from './approveChecked';
export * from './batch';
export * from './burn';
export * from './burnChecked';
export * from './closeAccount';
Expand All @@ -34,3 +35,5 @@ export * from './thawAccount';
export * from './transfer';
export * from './transferChecked';
export * from './uiAmountToAmount';
export * from './unwrapLamports';
export * from './withdrawExcessLamports';
48 changes: 39 additions & 9 deletions clients/js/src/generated/instructions/syncNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
type Instruction,
type InstructionWithAccounts,
type InstructionWithData,
type ReadonlyAccount,
type ReadonlyUint8Array,
type WritableAccount,
} from '@solana/kit';
Expand All @@ -38,11 +39,18 @@ export function getSyncNativeDiscriminatorBytes() {
export type SyncNativeInstruction<
TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
TAccountAccount extends string | AccountMeta<string> = string,
TAccountRent extends string | AccountMeta<string> | undefined = undefined,
TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
InstructionWithData<ReadonlyUint8Array> &
InstructionWithAccounts<
[TAccountAccount extends string ? WritableAccount<TAccountAccount> : TAccountAccount, ...TRemainingAccounts]
[
TAccountAccount extends string ? WritableAccount<TAccountAccount> : TAccountAccount,
...(TAccountRent extends undefined
? []
: [TAccountRent extends string ? ReadonlyAccount<TAccountRent> : TAccountRent]),
...TRemainingAccounts,
]
>;

export type SyncNativeInstructionData = { discriminator: number };
Expand All @@ -67,31 +75,45 @@ export function getSyncNativeInstructionDataCodec(): FixedSizeCodec<
return combineCodec(getSyncNativeInstructionDataEncoder(), getSyncNativeInstructionDataDecoder());
}

export type SyncNativeInput<TAccountAccount extends string = string> = {
export type SyncNativeInput<TAccountAccount extends string = string, TAccountRent extends string = string> = {
/** The native token account to sync with its underlying lamports. */
account: Address<TAccountAccount>;
/** Rent sysvar. */
rent?: Address<TAccountRent>;
};

export function getSyncNativeInstruction<
TAccountAccount extends string,
TAccountRent extends string,
TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS,
>(
input: SyncNativeInput<TAccountAccount>,
input: SyncNativeInput<TAccountAccount, TAccountRent>,
config?: { programAddress?: TProgramAddress },
): SyncNativeInstruction<TProgramAddress, TAccountAccount> {
): SyncNativeInstruction<TProgramAddress, TAccountAccount, TAccountRent> {
// Program address.
const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;

// Original accounts.
const originalAccounts = { account: { value: input.account ?? null, isWritable: true } };
const originalAccounts = {
account: { value: input.account ?? null, isWritable: true },
rent: { value: input.rent ?? null, isWritable: false },
};
const accounts = originalAccounts as Record<keyof typeof originalAccounts, ResolvedInstructionAccount>;

const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
// Resolve default values.
if (!accounts.rent.value) {
accounts.rent.value =
'SysvarRent111111111111111111111111111111111' as Address<'SysvarRent111111111111111111111111111111111'>;
}

const getAccountMeta = getAccountMetaFactory(programAddress, 'omitted');
return Object.freeze({
accounts: [getAccountMeta('account', accounts.account)],
accounts: [getAccountMeta('account', accounts.account), getAccountMeta('rent', accounts.rent)].filter(
<T>(x: T | undefined): x is T => x !== undefined,
),
data: getSyncNativeInstructionDataEncoder().encode({}),
programAddress,
} as SyncNativeInstruction<TProgramAddress, TAccountAccount>);
} as SyncNativeInstruction<TProgramAddress, TAccountAccount, TAccountRent>);
}

export type ParsedSyncNativeInstruction<
Expand All @@ -102,6 +124,8 @@ export type ParsedSyncNativeInstruction<
accounts: {
/** The native token account to sync with its underlying lamports. */
account: TAccountMetas[0];
/** Rent sysvar. */
rent?: TAccountMetas[1] | undefined;
};
data: SyncNativeInstructionData;
};
Expand All @@ -123,9 +147,15 @@ export function parseSyncNativeInstruction<TProgram extends string, TAccountMeta
accountIndex += 1;
return accountMeta;
};
let optionalAccountsRemaining = instruction.accounts.length - 1;
const getNextOptionalAccount = () => {
if (optionalAccountsRemaining === 0) return undefined;
optionalAccountsRemaining -= 1;
return getNextAccount();
};
return {
programAddress: instruction.programAddress,
accounts: { account: getNextAccount() },
accounts: { account: getNextAccount(), rent: getNextOptionalAccount() },
data: getSyncNativeInstructionDataDecoder().decode(instruction.data),
};
}
Loading
Loading