import { PublicKey } from "@solana/web3.js"; import BN from "bn.js"; import { Idl, IdlInstructionAccounts as IdlInstructionAccounts, IdlInstructionAccountItem, IdlField, IdlInstruction, IdlType, IdlTypeDef, IdlTypeDefTyEnum, IdlTypeDefTyStruct, IdlTypeDefTyType, IdlDefinedFields, IdlDefinedFieldsNamed, IdlDefinedFieldsTuple, IdlArrayLen } from "../../idl"; import { Accounts, Context } from "../context"; import { MethodsBuilder } from "./methods"; /** * All instructions for an IDL. */ export type AllInstructions = I["instructions"][number]; /** * Returns a type of instruction name to the IdlInstruction. */ type InstructionMap = { [K in I["name"]]: I & { name: K; }; }; /** * Returns a type of instruction name to the IdlInstruction. */ export type AllInstructionsMap = InstructionMap>; /** * All accounts for an IDL. */ export type AllAccounts = ResolveIdlTypePointer; /** * Returns a type of instruction name to the IdlInstruction. */ type AccountMap = { [K in I[number]["name"]]: I & { name: K; }; }; /** * Returns a type of instruction name to the IdlInstruction. */ export type AllAccountsMap = AccountMap>; /** * All events for an IDL. */ export type AllEvents = ResolveIdlTypePointer; export type MakeInstructionsNamespace]: unknown; } = { [M in keyof InstructionMap]: unknown; }> = { [M in keyof InstructionMap]: InstructionContextFn[M], Ret> & Mk[M]; }; export type MakeMethodsNamespace = { [M in keyof InstructionMap]: MethodsFn[M], MethodsBuilder[M]>>; }; export type InstructionContextFn, Ret> = (...args: InstructionContextFnArgs) => Ret; export type InstructionContextFnArgs = [ ...ArgsTuple>, Context> ]; export type InstructionAccountAddresses> = InstructionAccountsAddresses; type InstructionAccountsAddresses = { [N in A["name"]]: InstructionAccountsAddress; }; type InstructionAccountsAddress = A extends IdlInstructionAccounts ? InstructionAccountsAddresses : PublicKey; export type MethodsFn = (...args: ArgsTuple>) => Ret; type TypeMap = { pubkey: PublicKey; bool: boolean; string: string; bytes: Buffer; } & { [K in "u8" | "i8" | "u16" | "i16" | "u32" | "i32" | "f32" | "f64"]: number; } & { [K in "u64" | "i64" | "u128" | "i128" | "u256" | "i256"]: BN; }; export type DecodeType = IdlType extends T ? unknown : T extends keyof TypeMap ? TypeMap[T] : T extends { defined: { name: keyof Defined; }; } ? Defined[T["defined"]["name"]] : T extends { option: IdlType; } ? DecodeType | null : T extends { coption: IdlType; } ? DecodeType | null : T extends { vec: IdlType; } ? DecodeType[] : T extends { array: [defined: IdlType, size: IdlArrayLen]; } ? DecodeType[] : unknown; /** * Tuple of arguments. */ type ArgsTuple = { [K in keyof A]: A[K] extends IdlField ? DecodeType : unknown; } & unknown[]; /** * flat {a: number, b: {c: string}} into number | string */ type UnboxToUnion = T extends (infer U)[] ? UnboxToUnion : T extends Record ? "__empty_object__" : T extends Record ? UnboxToUnion : T; type DecodeDefinedField = F extends IdlType ? DecodeType : never; /** * decode enum variant: named or tuple */ type DecodeDefinedFields = F extends IdlDefinedFieldsNamed ? { [F2 in F[number] as F2["name"]]: DecodeDefinedField; } : F extends IdlDefinedFieldsTuple ? { [F3 in keyof F as Exclude]: DecodeDefinedField; } : Record; type DecodeEnumVariants = { [V in I["variants"][number] as V["name"]]: DecodeDefinedFields, Defined>; }; type ValueOf = T[keyof T]; type XorEnumVariants> = ValueOf<{ [K1 in keyof T]: { [K2 in Exclude]?: never; } & { [K2 in K1]: T[K2]; }; }>; type DecodeEnum = XorEnumVariants>; type DecodeStruct = DecodeDefinedFields, Defined>; type DecodeAlias = DecodeType; export type TypeDef = I["type"] extends IdlTypeDefTyEnum ? DecodeEnum : I["type"] extends IdlTypeDefTyStruct ? DecodeStruct : I["type"] extends IdlTypeDefTyType ? DecodeAlias : never; type TypeDefDictionary = { [K in T[number] as K["name"]]: TypeDef; }; type DecodedHelper = { [D in T[number] as D["name"]]: TypeDef; }; type UnknownType = "__unknown_defined_type__"; /** * Empty "defined" object to produce `UnknownType` instead of never/unknown * during IDL types decoding. */ type EmptyDefined = Record; type RecursiveDepth2> = UnknownType extends UnboxToUnion ? RecursiveDepth3> : Decoded; type RecursiveDepth3> = UnknownType extends UnboxToUnion ? RecursiveDepth4> : Decoded; type RecursiveDepth4 = DecodedHelper; /** * TypeScript can't handle truly recursive type (RecursiveTypes instead of RecursiveDepth2). * Hence we're doing recursion of depth=4 manually * */ type RecursiveTypes> = UnknownType extends UnboxToUnion ? RecursiveDepth2> : Decoded; export type IdlTypes = RecursiveTypes>; export type IdlErrors = NonNullable[number]; export type IdlAccounts = ResolveIdlPointerSection; export type IdlEvents = ResolveIdlPointerSection; type IdlPointerSection = keyof Pick; type ResolveIdlPointerSection = ResolveIdlTypePointer> = TypeDefDictionary>; type ResolveIdlTypePointer = FilterTuple, { name: NonNullable[number]["name"]; }>; type FilterTuple = T extends [infer Head, ...infer Tail] ? [Head] extends [F] ? [Head, ...FilterTuple] : FilterTuple : []; export {}; //# sourceMappingURL=types.d.ts.map