import type { Abi, AbiParameter, AbiParameterKind, AbiStateMutability, AbiType, MBits, SolidityAddress, SolidityArray, SolidityBool, SolidityBytes, SolidityFixedArrayRange, SolidityFixedArraySizeLookup, SolidityFunction, SolidityInt, SolidityString, SolidityTuple, TypedData, TypedDataParameter, TypedDataType } from './abi.js'; import type { ResolvedConfig } from './config.js'; import type { Error, Merge, Tuple } from './types.js'; /** * Converts {@link AbiType} to corresponding TypeScript primitive type. * * Does not include full array or tuple conversion. Use {@link AbiParameterToPrimitiveType} to fully convert arrays and tuples. * * @param TAbiType - {@link AbiType} to convert to TypeScript representation * @param TAbiParameterKind - Optional {@link AbiParameterKind} to narrow by parameter type * @returns TypeScript primitive type */ export type AbiTypeToPrimitiveType = PrimitiveTypeLookup[TAbiType]; type PrimitiveTypeLookup = { [_ in SolidityAddress]: ResolvedConfig['AddressType']; } & { [_ in SolidityBool]: boolean; } & { [_ in SolidityBytes]: ResolvedConfig['BytesType'][TAbiParameterKind]; } & { [_ in SolidityFunction]: `${ResolvedConfig['AddressType']}${string}`; } & { [_ in SolidityInt]: TAbiType extends `${'u' | ''}int${infer TBits}` ? TBits extends keyof BitsTypeLookup ? BitsTypeLookup[TBits] : Error<'Unknown bits value.'> : Error<`Unknown 'SolidityInt' format.`>; } & { [_ in SolidityString]: string; } & { [_ in SolidityTuple]: Record; } & { [_ in SolidityArray]: readonly unknown[]; }; type GreaterThan48Bits = Exclude; type LessThanOrEqualTo48Bits = Exclude; type NoBits = Exclude; type BitsTypeLookup = { [_ in `${LessThanOrEqualTo48Bits}`]: ResolvedConfig['IntType']; } & { [_ in `${GreaterThan48Bits}`]: ResolvedConfig['BigIntType']; } & { [_ in NoBits]: ResolvedConfig['BigIntType']; }; /** * Converts {@link AbiParameter} to corresponding TypeScript primitive type. * * @param TAbiParameter - {@link AbiParameter} to convert to TypeScript representation * @param TAbiParameterKind - Optional {@link AbiParameterKind} to narrow by parameter type * @returns TypeScript primitive type */ export type AbiParameterToPrimitiveType = TAbiParameter['type'] extends Exclude ? AbiTypeToPrimitiveType : TAbiParameter extends { type: SolidityTuple; components: infer TComponents extends readonly AbiParameter[]; } ? TComponents extends readonly [] ? [] : _HasUnnamedAbiParameter extends true ? readonly [ ...{ [K in keyof TComponents]: AbiParameterToPrimitiveType; } ] : { [Component in TComponents[number] as Component extends { name: string; } ? Component['name'] : never]: AbiParameterToPrimitiveType; } : /** * First, infer `Head` against a known size type (either fixed-length array value or `""`). * * | Input | Head | * | --------------- | ------------ | * | `string[]` | `string` | * | `string[][][3]` | `string[][]` | */ TAbiParameter['type'] extends `${infer Head}[${'' | `${SolidityFixedArrayRange}`}]` ? TAbiParameter['type'] extends `${Head}[${infer Size}]` ? Size extends keyof SolidityFixedArraySizeLookup ? Tuple, TAbiParameterKind>, SolidityFixedArraySizeLookup[Size]> : readonly AbiParameterToPrimitiveType, TAbiParameterKind>[] : never : ResolvedConfig['StrictAbiType'] extends true ? TAbiParameter['type'] extends infer TAbiType extends string ? Error<`Unknown type '${TAbiType}'.`> : never : unknown; type _HasUnnamedAbiParameter = TAbiParameters extends readonly [ infer Head extends AbiParameter, ...infer Tail extends readonly AbiParameter[] ] ? Head extends { name: string; } ? Head['name'] extends '' ? true : _HasUnnamedAbiParameter : true : false; /** * Converts array of {@link AbiParameter} to corresponding TypeScript primitive types. * * @param TAbiParameters - Array of {@link AbiParameter} to convert to TypeScript representations * @param TAbiParameterKind - Optional {@link AbiParameterKind} to narrow by parameter type * @returns Array of TypeScript primitive types */ export type AbiParametersToPrimitiveTypes = { [K in keyof TAbiParameters]: AbiParameterToPrimitiveType; }; /** * Checks if type is {@link Abi}. * * @param TAbi - {@link Abi} to check * @returns Boolean for whether {@link TAbi} is {@link Abi} */ export type IsAbi = TAbi extends Abi ? true : false; /** * Extracts all {@link AbiFunction} types from {@link Abi}. * * @param TAbi - {@link Abi} to extract functions from * @param TAbiStateMutibility - {@link AbiStateMutability} to filter by * @returns All {@link AbiFunction} types from {@link Abi} */ export type ExtractAbiFunctions = Extract; /** * Extracts all {@link AbiFunction} names from {@link Abi}. * * @param TAbi - {@link Abi} to extract function names from * @param TAbiStateMutibility - {@link AbiStateMutability} to filter by * @returns Union of function names */ export type ExtractAbiFunctionNames = ExtractAbiFunctions['name']; /** * Extracts {@link AbiFunction} with name from {@link Abi}. * * @param TAbi - {@link Abi} to extract {@link AbiFunction} from * @param TFunctionName - String name of function to extract from {@link Abi} * @returns Matching {@link AbiFunction} */ export type ExtractAbiFunction> = Extract, { name: TFunctionName; }>; /** * Extracts all {@link AbiEvent} types from {@link Abi}. * * @param TAbi - {@link Abi} to extract events from * @returns All {@link AbiEvent} types from {@link Abi} */ export type ExtractAbiEvents = Extract; /** * Extracts all {@link AbiEvent} names from {@link Abi}. * * @param TAbi - {@link Abi} to extract event names from * @returns Union of event names */ export type ExtractAbiEventNames = ExtractAbiEvents['name']; /** * Extracts {@link AbiEvent} with name from {@link Abi}. * * @param TAbi - {@link Abi} to extract {@link AbiEvent} from * @param TEventName - String name of event to extract from {@link Abi} * @returns Matching {@link AbiEvent} */ export type ExtractAbiEvent> = Extract, { name: TEventName; }>; /** * Extracts all {@link AbiError} types from {@link Abi}. * * @param TAbi - {@link Abi} to extract errors from * @returns All {@link AbiError} types from {@link Abi} */ export type ExtractAbiErrors = Extract; /** * Extracts all {@link AbiError} names from {@link Abi}. * * @param TAbi - {@link Abi} to extract error names from * @returns Union of error names */ export type ExtractAbiErrorNames = ExtractAbiErrors['name']; /** * Extracts {@link AbiError} with name from {@link Abi}. * * @param TAbi - {@link Abi} to extract {@link AbiError} from * @param TErrorName - String name of error to extract from {@link Abi} * @returns Matching {@link AbiError} */ export type ExtractAbiError> = Extract, { name: TErrorName; }>; /** * Checks if type is {@link TypedData}. * * @param TTypedData - {@link TypedData} to check * @returns Boolean for whether {@link TTypedData} is {@link TypedData} */ export type IsTypedData = TTypedData extends TypedData ? { [K in keyof TTypedData]: { [K2 in TTypedData[K][number] as K2['type'] extends keyof TTypedData ? never : K2['type'] extends `${keyof TTypedData & string}[${string}]` ? never : K2['type'] extends TypedDataType ? never : K2['name']]: false; }; } extends { [K in keyof TTypedData]: Record; } ? true : false : false; /** * Converts {@link TTypedData} to corresponding TypeScript primitive types. * * @param TTypedData - {@link TypedData} to convert * @param TAbiParameterKind - Optional {@link AbiParameterKind} to narrow by parameter type * @returns Union of TypeScript primitive types */ export type TypedDataToPrimitiveTypes = { [K in keyof TTypedData]: { [K2 in TTypedData[K][number] as K2['name']]: K2['type'] extends K ? Error<`Cannot convert self-referencing struct '${K2['type']}' to primitive type.`> : K2['type'] extends keyof TTypedData ? K2['type'] extends keyof TKeyReferences ? Error<`Circular reference detected. '${K2['type']}' is a circular reference.`> : TypedDataToPrimitiveTypes, TAbiParameterKind, TKeyReferences & { [_ in K2['type']]: true; }>[K2['type']] : K2['type'] extends `${infer TType extends keyof TTypedData & string}[${infer Tail}]` ? AbiParameterToPrimitiveType; }>, TAbiParameterKind> : K2['type'] extends TypedDataType ? AbiParameterToPrimitiveType : Error<`Cannot convert unknown type '${K2['type']}' to primitive type.`>; }; }; type _TypedDataParametersToAbiParameters = { [K in keyof TTypedDataParameters]: TTypedDataParameters[K] extends infer TTypedDataParameter extends { name: string; type: unknown; } ? TTypedDataParameter['type'] extends keyof TTypedData ? Merge; }> : TTypedDataParameter['type'] extends `${infer TType extends keyof TTypedData & string}[${infer Tail}]` ? Merge; }> : TTypedDataParameter : never; }; export {}; //# sourceMappingURL=utils.d.ts.map