UNPKG

@robertprp/intents-sdk

Version:

Shogun Network Intent-based cross-chain swaps SDK

212 lines (187 loc) 7.65 kB
/** * 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 { getAddressDecoder, getAddressEncoder, getProgramDerivedAddress, type Address } from '@solana/kit'; import { combineCodec, fixDecoderSize, fixEncoderSize, getBytesDecoder, getBytesEncoder, getStructDecoder, getStructEncoder, transformEncoder, type Codec, type Decoder, type Encoder, type ReadonlyUint8Array, } from '@solana/kit'; import { type IAccountMeta, type IInstruction, type IInstructionWithAccounts, type IInstructionWithData, type WritableAccount, type WritableSignerAccount, } from '@solana/kit'; import { type IAccountSignerMeta, type TransactionSigner } from '@solana/kit'; import { SINGLE_CHAIN_GUARD_PROGRAM_ADDRESS } from '../programs/index.js'; import { getAccountMetaFactory, type ResolvedAccount } from '../shared/index.js'; export const SET_ADMIN_DISCRIMINATOR = new Uint8Array([251, 163, 0, 52, 91, 194, 187, 92]); export function getSetAdminDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(SET_ADMIN_DISCRIMINATOR); } export type SetAdminInstruction< TProgram extends string = typeof SINGLE_CHAIN_GUARD_PROGRAM_ADDRESS, TAccountAdmin extends string | IAccountMeta<string> = string, TAccountAdminSingleton extends string | IAccountMeta<string> = string, TRemainingAccounts extends readonly IAccountMeta<string>[] = [], > = IInstruction<TProgram> & IInstructionWithData<Uint8Array> & IInstructionWithAccounts< [ TAccountAdmin extends string ? WritableSignerAccount<TAccountAdmin> & IAccountSignerMeta<TAccountAdmin> : TAccountAdmin, TAccountAdminSingleton extends string ? WritableAccount<TAccountAdminSingleton> : TAccountAdminSingleton, ...TRemainingAccounts, ] >; export type SetAdminInstructionData = { discriminator: ReadonlyUint8Array; newAdmin: Address; }; export type SetAdminInstructionDataArgs = { newAdmin: Address }; export function getSetAdminInstructionDataEncoder(): Encoder<SetAdminInstructionDataArgs> { return transformEncoder( getStructEncoder([ ['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['newAdmin', getAddressEncoder()], ]), (value) => ({ ...value, discriminator: SET_ADMIN_DISCRIMINATOR }), ); } export function getSetAdminInstructionDataDecoder(): Decoder<SetAdminInstructionData> { return getStructDecoder([ ['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['newAdmin', getAddressDecoder()], ]); } export function getSetAdminInstructionDataCodec(): Codec<SetAdminInstructionDataArgs, SetAdminInstructionData> { return combineCodec(getSetAdminInstructionDataEncoder(), getSetAdminInstructionDataDecoder()); } export type SetAdminAsyncInput< TAccountAdmin extends string = string, TAccountAdminSingleton extends string = string, > = { /** Admin signer */ admin: TransactionSigner<TAccountAdmin>; /** Admin singleton account that grants access to admin functions */ adminSingleton?: Address<TAccountAdminSingleton>; newAdmin: SetAdminInstructionDataArgs['newAdmin']; }; export async function getSetAdminInstructionAsync< TAccountAdmin extends string, TAccountAdminSingleton extends string, TProgramAddress extends Address = typeof SINGLE_CHAIN_GUARD_PROGRAM_ADDRESS, >( input: SetAdminAsyncInput<TAccountAdmin, TAccountAdminSingleton>, config?: { programAddress?: TProgramAddress }, ): Promise<SetAdminInstruction<TProgramAddress, TAccountAdmin, TAccountAdminSingleton>> { // Program address. const programAddress = config?.programAddress ?? SINGLE_CHAIN_GUARD_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { admin: { value: input.admin ?? null, isWritable: true }, adminSingleton: { value: input.adminSingleton ?? null, isWritable: true }, }; const accounts = originalAccounts as Record<keyof typeof originalAccounts, ResolvedAccount>; // Original args. const args = { ...input }; // Resolve default values. if (!accounts.adminSingleton.value) { accounts.adminSingleton.value = await getProgramDerivedAddress({ programAddress, seeds: [getBytesEncoder().encode(new Uint8Array([97, 100, 109, 105, 110]))], }); } const getAccountMeta = getAccountMetaFactory(programAddress, 'programId'); const instruction = { accounts: [getAccountMeta(accounts.admin), getAccountMeta(accounts.adminSingleton)], programAddress, data: getSetAdminInstructionDataEncoder().encode(args as SetAdminInstructionDataArgs), } as SetAdminInstruction<TProgramAddress, TAccountAdmin, TAccountAdminSingleton>; return instruction; } export type SetAdminInput<TAccountAdmin extends string = string, TAccountAdminSingleton extends string = string> = { /** Admin signer */ admin: TransactionSigner<TAccountAdmin>; /** Admin singleton account that grants access to admin functions */ adminSingleton: Address<TAccountAdminSingleton>; newAdmin: SetAdminInstructionDataArgs['newAdmin']; }; export function getSetAdminInstruction< TAccountAdmin extends string, TAccountAdminSingleton extends string, TProgramAddress extends Address = typeof SINGLE_CHAIN_GUARD_PROGRAM_ADDRESS, >( input: SetAdminInput<TAccountAdmin, TAccountAdminSingleton>, config?: { programAddress?: TProgramAddress }, ): SetAdminInstruction<TProgramAddress, TAccountAdmin, TAccountAdminSingleton> { // Program address. const programAddress = config?.programAddress ?? SINGLE_CHAIN_GUARD_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { admin: { value: input.admin ?? null, isWritable: true }, adminSingleton: { value: input.adminSingleton ?? null, isWritable: true }, }; const accounts = originalAccounts as Record<keyof typeof originalAccounts, ResolvedAccount>; // Original args. const args = { ...input }; const getAccountMeta = getAccountMetaFactory(programAddress, 'programId'); const instruction = { accounts: [getAccountMeta(accounts.admin), getAccountMeta(accounts.adminSingleton)], programAddress, data: getSetAdminInstructionDataEncoder().encode(args as SetAdminInstructionDataArgs), } as SetAdminInstruction<TProgramAddress, TAccountAdmin, TAccountAdminSingleton>; return instruction; } export type ParsedSetAdminInstruction< TProgram extends string = typeof SINGLE_CHAIN_GUARD_PROGRAM_ADDRESS, TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[], > = { programAddress: Address<TProgram>; accounts: { /** Admin signer */ admin: TAccountMetas[0]; /** Admin singleton account that grants access to admin functions */ adminSingleton: TAccountMetas[1]; }; data: SetAdminInstructionData; }; export function parseSetAdminInstruction<TProgram extends string, TAccountMetas extends readonly IAccountMeta[]>( instruction: IInstruction<TProgram> & IInstructionWithAccounts<TAccountMetas> & IInstructionWithData<Uint8Array>, ): ParsedSetAdminInstruction<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: { admin: getNextAccount(), adminSingleton: getNextAccount(), }, data: getSetAdminInstructionDataDecoder().decode(instruction.data), }; }