import {z} from 'zod' export type SimulationDraftRow = {contractAddress: string, inputDataType?: ('decoded' | 'raw') | undefined, rawFunctionInput?: string | undefined, contractFunction?: ({name: string, selector?: string | undefined, signature?: string | undefined} | null) | undefined, functionInputs?: (Array | {[key: string]: unknown}) | undefined, contractAbiImport?: string | undefined, from?: string | undefined, gas?: (string | number) | undefined, gasPrice?: (string | number) | undefined, value?: (string | number) | undefined, block?: (string | number) | undefined, blockIndex?: (string | number | null) | undefined, endOfBlock?: boolean | undefined, usePendingBlock?: boolean | undefined, depositTx?: boolean | undefined, mint?: string | undefined, blockHeaderOverrides?: ({number?: (string | number | null) | undefined, timestamp?: (string | number | null) | undefined}) | undefined, stateOverrides?: (Array<{id?: string | undefined, contractAddress: string, balance: string, storage?: Array<{key: string, value: string}> | undefined, code?: string | undefined}>) | undefined, accessList?: Array<{address: string, storageKeys: Array}> | undefined, fundAddress?: ({targetAddress: string, tokens: Array<{tokenAddress: string, amount: string}>} | null) | undefined, customSource?: ({compilerInfo: {[key: string]: unknown}, customSourceData: Array<{name: string, source: string, path: string, contractName?: string | undefined, address?: string | undefined}>} | null) | undefined, contractSourceEdited?: boolean | undefined}; export type SimulationDraftPayload = {v: 2, network: {id: string} | null, rows: Array}; export type SimulationDraftCreateRequest = {payload: SimulationDraftPayload, shared?: boolean | undefined}; export type CreateApiV2AccountAccountSlugProjectProjectSlugSimulationDraftsArgs = {accountSlug: string, projectSlug: string, body: SimulationDraftCreateRequest}; export type CreateApiV2AccountAccountSlugProjectProjectSlugSimulationDraftsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const simulationDraftCreateResponse = z.object({resource_id: z.string()}); 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 }; /** * Create a simulation draft * * POST /v2/account/{accountSlug}/project/{projectSlug}/simulation-drafts * * Also accepts auth: BearerAuth */ export const createApiV2AccountAccountSlugProjectProjectSlugSimulationDrafts = async (args: CreateApiV2AccountAccountSlugProjectProjectSlugSimulationDraftsArgs, options?: CreateApiV2AccountAccountSlugProjectProjectSlugSimulationDraftsOptions) => { const {baseUrl = "https://api.tenderly.co/api", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-Access-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/account/${encodeURIComponent(String(args.accountSlug))}/project/${encodeURIComponent(String(args.projectSlug))}/simulation-drafts${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(simulationDraftCreateResponse, await res.json()) };