import {z} from 'zod' export type MethodCallExpression = {line_number: number, call_position: 'first' | 'last' | 'any'}; export type WhitelistedCallerExpression = {addresses: Array}; export type BlacklistedCallerExpression = {addresses: Array}; export type ContractAddressExpression = {address: string, transaction_type?: ('direct' | 'source' | 'internal') | undefined}; export type NetworkExpression = {network_id: string}; export type TagExpression = {tag: string, transaction_type?: ('direct' | 'source' | 'internal') | undefined}; export type TxStatusExpression = {transaction_success?: boolean | undefined}; export type ParameterCondition = {parameter_name: string, operator: '>' | '>=' | '<' | '<=' | '==' | '!=' | 'contains' | 'notContains', parameter_type: 'uint' | 'int' | 'bool' | 'address' | 'string' | 'byte', nested_parameter_type?: string | undefined, comparison_value: string}; export type FunctionParamsExpression = {address: string, function_line_number: number, parameter_conditions: Array}; export type TxValueExpression = {transaction_value: string, operator: '>' | '>=' | '<' | '<=' | '==' | '!='}; export type EmittedLogExpression = {address: string, event_name: string, event_id: string, match_any?: boolean | undefined, decode_events?: boolean | undefined, match_non_project_contracts?: boolean | undefined, parameter_conditions?: Array | undefined}; export type StateChangeExpression = {address: string, parameter_conditions: Array<{parameter_name: string, parameter_type: string, compare_change?: boolean | undefined, compare_threshold?: boolean | undefined, compare_percentage?: boolean | undefined, comparison_value?: string | undefined, operator?: ('>' | '<' | '==' | '>=' | '<=') | undefined}>}; export type ViewFunctionExpression = {address: string, input: string, network_id: string, parameter_condition?: ({compare_threshold?: boolean | undefined, comparison_value?: string | undefined, operator?: ('>' | '<' | '==' | '>=' | '<=') | undefined, parameter_type?: string | undefined}) | undefined}; export type EthBalanceExpression = {address: string, threshold: string}; export type ERC20TransferMatcherExpression = {address: string, log_name: string, balances: string}; export type TxErrorExpression = {addresses_to_ignore?: Array | undefined}; export type TxInternalErrorExpression = {addresses_to_match?: Array | undefined}; export type NoActionExpression = {no_log?: ({address?: string | undefined, network_id?: string | undefined, event_id?: string | undefined, event_name?: string | undefined}) | undefined, no_transaction?: ({address?: string | undefined, is_from_address?: boolean | undefined, network_id?: string | undefined, transaction_success?: boolean | undefined}) | undefined, check_after_seconds: number}; export type SandwichTransactionExpression = {address: string, transaction_type?: ('direct' | 'source' | 'internal') | undefined}; export type AlertExpression = {type: 'method_call' | 'whitelisted_caller_addresses' | 'blacklisted_caller_addresses' | 'contract_address' | 'network' | 'tag' | 'tx_status' | 'function_params' | 'tx_value' | 'emitted_log' | 'state_change' | 'view_function' | 'eth_balance' | 'erc20_transfer_matcher' | 'tx_error' | 'tx_internal_error' | 'no_action' | 'sandwich_transaction', expression: MethodCallExpression | WhitelistedCallerExpression | BlacklistedCallerExpression | ContractAddressExpression | NetworkExpression | TagExpression | TxStatusExpression | FunctionParamsExpression | TxValueExpression | EmittedLogExpression | StateChangeExpression | ViewFunctionExpression | EthBalanceExpression | ERC20TransferMatcherExpression | TxErrorExpression | TxInternalErrorExpression | NoActionExpression | SandwichTransactionExpression}; export type CreateAlertPayload = {name: string, description?: string | undefined, color?: ('#eeeeee' | '#34ace0' | '#ffda79' | '#ff5252' | '#33d9b2') | undefined, simple_type: 'successful_tx' | 'failed_tx' | 'allowlisted_callers' | 'blocklisted_callers' | 'log_emitted' | 'erc20_transfer_log_emitted' | 'emitted_log_parameter' | 'function_called' | 'called_function_parameter' | 'state_change' | 'eth_balance' | 'tx_value' | 'view_function', enabled?: boolean | undefined, expressions: Array, delivery_channels: Array<{enabled?: boolean | undefined, id?: string | undefined}>}; export type CreateApiV1AccountAccountSlugProjectProjectSlugAlertArgs = {accountSlug: string, projectSlug: string, body: CreateAlertPayload}; export type CreateApiV1AccountAccountSlugProjectProjectSlugAlertOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const methodCallExpression = z.object({line_number: z.number().int(), call_position: z.enum(["first", "last", "any"])}); export const whitelistedCallerExpression = z.object({addresses: z.array(z.string())}); export const blacklistedCallerExpression = z.object({addresses: z.array(z.string())}); export const contractAddressExpression = z.object({address: z.string(), transaction_type: z.enum(["direct", "source", "internal"]).optional()}); export const networkExpression = z.object({network_id: z.string()}); export const tagExpression = z.object({tag: z.string(), transaction_type: z.enum(["direct", "source", "internal"]).optional()}); export const txStatusExpression = z.object({transaction_success: z.boolean().optional()}); export const parameterCondition = z.object({parameter_name: z.string(), operator: z.enum([">", ">=", "<", "<=", "==", "!=", "contains", "notContains"]), parameter_type: z.enum(["uint", "int", "bool", "address", "string", "byte"]), nested_parameter_type: z.string().optional(), comparison_value: z.string()}); export const functionParamsExpression = z.object({address: z.string(), function_line_number: z.number().int(), parameter_conditions: z.array(parameterCondition)}); export const txValueExpression = z.object({transaction_value: z.string(), operator: z.enum([">", ">=", "<", "<=", "==", "!="])}); export const emittedLogExpression = z.object({address: z.string(), event_name: z.string(), event_id: z.string(), match_any: z.boolean().optional(), decode_events: z.boolean().optional(), match_non_project_contracts: z.boolean().optional(), parameter_conditions: z.array(parameterCondition).optional()}); export const stateChangeExpression = z.object({address: z.string(), parameter_conditions: z.array(z.object({parameter_name: z.string(), parameter_type: z.string(), compare_change: z.boolean().optional(), compare_threshold: z.boolean().optional(), compare_percentage: z.boolean().optional(), comparison_value: z.string().optional(), operator: z.enum([">", "<", "==", ">=", "<="]).optional()}))}); export const viewFunctionExpression = z.object({address: z.string(), input: z.string(), network_id: z.string(), parameter_condition: z.object({compare_threshold: z.boolean().optional(), comparison_value: z.string().optional(), operator: z.enum([">", "<", "==", ">=", "<="]).optional(), parameter_type: z.string().optional()}).optional()}); export const ethBalanceExpression = z.object({address: z.string(), threshold: z.string()}); export const eRC20TransferMatcherExpression = z.object({address: z.string(), log_name: z.string(), balances: z.string()}); export const txErrorExpression = z.object({addresses_to_ignore: z.array(z.string()).optional()}); export const txInternalErrorExpression = z.object({addresses_to_match: z.array(z.string()).optional()}); export const noActionExpression = z.object({no_log: z.object({address: z.string().optional(), network_id: z.string().optional(), event_id: z.string().optional(), event_name: z.string().optional()}).optional(), no_transaction: z.object({address: z.string().optional(), is_from_address: z.boolean().optional(), network_id: z.string().optional(), transaction_success: z.boolean().optional()}).optional(), check_after_seconds: z.number().int()}); export const sandwichTransactionExpression = z.object({address: z.string(), transaction_type: z.enum(["direct", "source", "internal"]).optional()}); export const alertExpression = z.object({type: z.enum(["method_call", "whitelisted_caller_addresses", "blacklisted_caller_addresses", "contract_address", "network", "tag", "tx_status", "function_params", "tx_value", "emitted_log", "state_change", "view_function", "eth_balance", "erc20_transfer_matcher", "tx_error", "tx_internal_error", "no_action", "sandwich_transaction"]), expression: z.union([methodCallExpression, whitelistedCallerExpression, blacklistedCallerExpression, contractAddressExpression, networkExpression, tagExpression, txStatusExpression, functionParamsExpression, txValueExpression, emittedLogExpression, stateChangeExpression, viewFunctionExpression, ethBalanceExpression, eRC20TransferMatcherExpression, txErrorExpression, txInternalErrorExpression, noActionExpression, sandwichTransactionExpression])}); export const createAlertResponse = z.object({alert: z.object({id: z.string(), project_id: z.string(), name: z.string(), description: z.string().optional(), enabled: z.boolean(), color: z.enum(["#eeeeee", "#34ace0", "#ffda79", "#ff5252", "#33d9b2"]), severity: z.enum(["default", "info", "warning", "danger", "success"]), created_at: z.string(), updated_at: z.string(), is_editable: z.boolean(), delivery_channels: z.array(z.object({delivery_channel_id: z.string().optional(), enabled: z.boolean().optional(), created_at: z.string().optional()})).optional(), expressions: z.array(alertExpression)})}); 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 Alert * * POST /v1/account/{accountSlug}/project/{projectSlug}/alert * * Also accepts auth: BearerAuth */ export const createApiV1AccountAccountSlugProjectProjectSlugAlert = async (args: CreateApiV1AccountAccountSlugProjectProjectSlugAlertArgs, options?: CreateApiV1AccountAccountSlugProjectProjectSlugAlertOptions) => { 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(/\/+$/, '')}/v1/account/${encodeURIComponent(String(args.accountSlug))}/project/${encodeURIComponent(String(args.projectSlug))}/alert${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(createAlertResponse, await res.json()) };