import {z} from 'zod' export type GetApiEvmosEvmV1EstimateGasArgs = {args?: string | undefined, gasCap?: unknown | undefined, proposerAddress?: string | undefined, chainId?: unknown | undefined}; export type GetApiEvmosEvmV1EstimateGasOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const ethermintEvmV1EstimateGasResponse = z.object({gas: z.string().optional(), ret: z.string().optional(), vmError: z.string().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * EstimateGas implements the `eth_estimateGas` rpc api * * GET /evmos/evm/v1/estimate_gas */ export const getApiEvmosEvmV1EstimateGas = async (args: GetApiEvmosEvmV1EstimateGasArgs, options: GetApiEvmosEvmV1EstimateGasOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.args != null) query.append("args", String(args.args)) if (args.gasCap != null) query.append("gasCap", String(args.gasCap)) if (args.proposerAddress != null) query.append("proposerAddress", String(args.proposerAddress)) if (args.chainId != null) query.append("chainId", String(args.chainId)) const url = `${baseUrl.replace(/\/+$/, '')}/evmos/evm/v1/estimate_gas${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(ethermintEvmV1EstimateGasResponse, await res.json()) };