import type { Abi } from '../abi.js' import type { Error, Filter } from '../types.js' import { isStructSignature } from './runtime/signatures.js' import { parseStructs } from './runtime/structs.js' import { parseSignature } from './runtime/utils.js' import type { Signatures } from './types/signatures.js' import type { ParseStructs } from './types/structs.js' import type { ParseSignature } from './types/utils.js' /** * Parses human-readable ABI into JSON {@link Abi} * * @param signatures - Human-readable ABI * @returns Parsed {@link Abi} * * @deprecated Human-readable ABI utilities are moving to Ox. * Install [`ox`](https://oxlib.sh) and use [`Abi.from.ReturnType`](https://oxlib.sh/api/Abi/from#return-type) instead: * `import { Abi } from 'ox'`. * * @example * type Result = ParseAbi< * // ^? type Result = readonly [{ name: "balanceOf"; type: "function"; stateMutability:... * [ * 'function balanceOf(address owner) view returns (uint256)', * 'event Transfer(address indexed from, address indexed to, uint256 amount)', * ] * > */ export type ParseAbi = string[] extends signatures ? Abi // If `T` was not able to be inferred (e.g. just `string[]`), return `Abi` : signatures extends readonly string[] ? signatures extends Signatures // Validate signatures ? ParseStructs extends infer structs ? { [key in keyof signatures]: signatures[key] extends string ? ParseSignature : never } extends infer mapped extends readonly unknown[] ? Filter extends infer result ? result extends readonly [] ? never : result : never : never : never : never : never /** * Parses human-readable ABI into JSON {@link Abi} * * @param signatures - Human-Readable ABI * @returns Parsed {@link Abi} * * @deprecated Human-readable ABI utilities are moving to Ox. * Install [`ox`](https://oxlib.sh) and use [`Abi.from`](https://oxlib.sh/api/Abi/from) instead: * `import { Abi } from 'ox'`. * * @example * const abi = parseAbi([ * // ^? const abi: readonly [{ name: "balanceOf"; type: "function"; stateMutability:... * 'function balanceOf(address owner) view returns (uint256)', * 'event Transfer(address indexed from, address indexed to, uint256 amount)', * ]) */ export function parseAbi( signatures: signatures['length'] extends 0 ? Error<'At least one signature required'> : Signatures extends signatures ? signatures : Signatures, ): ParseAbi { const structs = parseStructs(signatures as readonly string[]) const abi = [] const length = signatures.length as number for (let i = 0; i < length; i++) { const signature = (signatures as readonly string[])[i]! if (isStructSignature(signature)) continue abi.push(parseSignature(signature, structs)) } return abi as unknown as ParseAbi }