123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- /**
- * 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,
- combineCodec,
- getStructDecoder,
- getStructEncoder,
- getU64Decoder,
- getU64Encoder,
- getU8Decoder,
- getU8Encoder,
- transformEncoder,
- type AccountMeta,
- type AccountSignerMeta,
- type Address,
- type FixedSizeCodec,
- type FixedSizeDecoder,
- type FixedSizeEncoder,
- type Instruction,
- type InstructionWithAccounts,
- type InstructionWithData,
- type ReadonlyAccount,
- type ReadonlySignerAccount,
- type ReadonlyUint8Array,
- type TransactionSigner,
- type WritableAccount,
- } from '@solana/kit';
- import { TOKEN_PROGRAM_ADDRESS } from '../programs';
- import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
- export const TRANSFER_DISCRIMINATOR = 3;
- export function getTransferDiscriminatorBytes() {
- return getU8Encoder().encode(TRANSFER_DISCRIMINATOR);
- }
- export type TransferInstruction<
- TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
- TAccountSource extends string | AccountMeta<string> = string,
- TAccountDestination extends string | AccountMeta<string> = string,
- TAccountAuthority extends string | AccountMeta<string> = string,
- TRemainingAccounts extends readonly AccountMeta<string>[] = [],
- > = Instruction<TProgram> &
- InstructionWithData<ReadonlyUint8Array> &
- InstructionWithAccounts<
- [
- TAccountSource extends string
- ? WritableAccount<TAccountSource>
- : TAccountSource,
- TAccountDestination extends string
- ? WritableAccount<TAccountDestination>
- : TAccountDestination,
- TAccountAuthority extends string
- ? ReadonlyAccount<TAccountAuthority>
- : TAccountAuthority,
- ...TRemainingAccounts,
- ]
- >;
- export type TransferInstructionData = {
- discriminator: number;
- /** The amount of tokens to transfer. */
- amount: bigint;
- };
- export type TransferInstructionDataArgs = {
- /** The amount of tokens to transfer. */
- amount: number | bigint;
- };
- export function getTransferInstructionDataEncoder(): FixedSizeEncoder<TransferInstructionDataArgs> {
- return transformEncoder(
- getStructEncoder([
- ['discriminator', getU8Encoder()],
- ['amount', getU64Encoder()],
- ]),
- (value) => ({ ...value, discriminator: TRANSFER_DISCRIMINATOR })
- );
- }
- export function getTransferInstructionDataDecoder(): FixedSizeDecoder<TransferInstructionData> {
- return getStructDecoder([
- ['discriminator', getU8Decoder()],
- ['amount', getU64Decoder()],
- ]);
- }
- export function getTransferInstructionDataCodec(): FixedSizeCodec<
- TransferInstructionDataArgs,
- TransferInstructionData
- > {
- return combineCodec(
- getTransferInstructionDataEncoder(),
- getTransferInstructionDataDecoder()
- );
- }
- export type TransferInput<
- TAccountSource extends string = string,
- TAccountDestination extends string = string,
- TAccountAuthority extends string = string,
- > = {
- /** The source account. */
- source: Address<TAccountSource>;
- /** The destination account. */
- destination: Address<TAccountDestination>;
- /** The source account's owner/delegate or its multisignature account. */
- authority: Address<TAccountAuthority> | TransactionSigner<TAccountAuthority>;
- amount: TransferInstructionDataArgs['amount'];
- multiSigners?: Array<TransactionSigner>;
- };
- export function getTransferInstruction<
- TAccountSource extends string,
- TAccountDestination extends string,
- TAccountAuthority extends string,
- TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS,
- >(
- input: TransferInput<TAccountSource, TAccountDestination, TAccountAuthority>,
- config?: { programAddress?: TProgramAddress }
- ): TransferInstruction<
- TProgramAddress,
- TAccountSource,
- TAccountDestination,
- (typeof input)['authority'] extends TransactionSigner<TAccountAuthority>
- ? ReadonlySignerAccount<TAccountAuthority> &
- AccountSignerMeta<TAccountAuthority>
- : TAccountAuthority
- > {
- // Program address.
- const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;
- // Original accounts.
- const originalAccounts = {
- source: { value: input.source ?? null, isWritable: true },
- destination: { value: input.destination ?? null, isWritable: true },
- authority: { value: input.authority ?? null, isWritable: false },
- };
- const accounts = originalAccounts as Record<
- keyof typeof originalAccounts,
- ResolvedAccount
- >;
- // Original args.
- const args = { ...input };
- // Remaining accounts.
- const remainingAccounts: AccountMeta[] = (args.multiSigners ?? []).map(
- (signer) => ({
- address: signer.address,
- role: AccountRole.READONLY_SIGNER,
- signer,
- })
- );
- const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
- const instruction = {
- accounts: [
- getAccountMeta(accounts.source),
- getAccountMeta(accounts.destination),
- getAccountMeta(accounts.authority),
- ...remainingAccounts,
- ],
- programAddress,
- data: getTransferInstructionDataEncoder().encode(
- args as TransferInstructionDataArgs
- ),
- } as TransferInstruction<
- TProgramAddress,
- TAccountSource,
- TAccountDestination,
- (typeof input)['authority'] extends TransactionSigner<TAccountAuthority>
- ? ReadonlySignerAccount<TAccountAuthority> &
- AccountSignerMeta<TAccountAuthority>
- : TAccountAuthority
- >;
- return instruction;
- }
- export type ParsedTransferInstruction<
- TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
- TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
- > = {
- programAddress: Address<TProgram>;
- accounts: {
- /** The source account. */
- source: TAccountMetas[0];
- /** The destination account. */
- destination: TAccountMetas[1];
- /** The source account's owner/delegate or its multisignature account. */
- authority: TAccountMetas[2];
- };
- data: TransferInstructionData;
- };
- export function parseTransferInstruction<
- TProgram extends string,
- TAccountMetas extends readonly AccountMeta[],
- >(
- instruction: Instruction<TProgram> &
- InstructionWithAccounts<TAccountMetas> &
- InstructionWithData<ReadonlyUint8Array>
- ): ParsedTransferInstruction<TProgram, TAccountMetas> {
- if (instruction.accounts.length < 3) {
- // TODO: Coded error.
- throw new Error('Not enough accounts');
- }
- let accountIndex = 0;
- const getNextAccount = () => {
- const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
- accountIndex += 1;
- return accountMeta;
- };
- return {
- programAddress: instruction.programAddress,
- accounts: {
- source: getNextAccount(),
- destination: getNextAccount(),
- authority: getNextAccount(),
- },
- data: getTransferInstructionDataDecoder().decode(instruction.data),
- };
- }
|