|
| 1 | +import type { Transport } from 'viem'; |
| 2 | +import { z } from 'zod'; |
| 3 | +import { evmAddressSchema, hexData32Schema, hexDataSchema } from '../../../types/index.js'; |
| 4 | + |
| 5 | +export enum GelatoStatusCode { |
| 6 | + Pending = 100, |
| 7 | + Submitted = 110, |
| 8 | + Included = 200, |
| 9 | + Finalized = 210, |
| 10 | + Rejected = 400, |
| 11 | + Reverted = 500 |
| 12 | +} |
| 13 | + |
| 14 | +const logSchema = z.object({ |
| 15 | + address: evmAddressSchema, |
| 16 | + data: hexDataSchema, |
| 17 | + topics: z.array(hexData32Schema) |
| 18 | +}); |
| 19 | + |
| 20 | +const receiptSchema = z.object({ |
| 21 | + blockHash: hexData32Schema, |
| 22 | + blockNumber: z.coerce.bigint(), |
| 23 | + gasUsed: z.coerce.bigint(), |
| 24 | + logs: z.array(logSchema).optional(), |
| 25 | + transactionHash: hexData32Schema |
| 26 | +}); |
| 27 | + |
| 28 | +const baseStatusSchema = z.object({ |
| 29 | + chainId: z.coerce.number(), |
| 30 | + createdAt: z.number() |
| 31 | +}); |
| 32 | + |
| 33 | +const pendingStatusSchema = baseStatusSchema.extend({ |
| 34 | + status: z.literal(GelatoStatusCode.Pending) |
| 35 | +}); |
| 36 | + |
| 37 | +const submittedStatusSchema = baseStatusSchema.extend({ |
| 38 | + hash: hexData32Schema, |
| 39 | + status: z.literal(GelatoStatusCode.Submitted) |
| 40 | +}); |
| 41 | + |
| 42 | +const includedStatusSchema = baseStatusSchema.extend({ |
| 43 | + receipt: receiptSchema, |
| 44 | + status: z.literal(GelatoStatusCode.Included) |
| 45 | +}); |
| 46 | + |
| 47 | +const finalizedStatusSchema = baseStatusSchema.extend({ |
| 48 | + receipt: receiptSchema, |
| 49 | + status: z.literal(GelatoStatusCode.Finalized) |
| 50 | +}); |
| 51 | + |
| 52 | +const rejectedStatusSchema = baseStatusSchema.extend({ |
| 53 | + data: z.unknown().optional(), |
| 54 | + message: z.string(), |
| 55 | + status: z.literal(GelatoStatusCode.Rejected) |
| 56 | +}); |
| 57 | + |
| 58 | +const revertedStatusSchema = baseStatusSchema.extend({ |
| 59 | + data: z.string(), |
| 60 | + message: z.string().optional(), |
| 61 | + status: z.literal(GelatoStatusCode.Reverted) |
| 62 | +}); |
| 63 | + |
| 64 | +export const gelatoTerminalStatusSchema = z.discriminatedUnion('status', [ |
| 65 | + finalizedStatusSchema, |
| 66 | + rejectedStatusSchema, |
| 67 | + revertedStatusSchema |
| 68 | +]); |
| 69 | + |
| 70 | +export const gelatoStatusSchema = z.discriminatedUnion('status', [ |
| 71 | + pendingStatusSchema, |
| 72 | + submittedStatusSchema, |
| 73 | + includedStatusSchema, |
| 74 | + finalizedStatusSchema, |
| 75 | + rejectedStatusSchema, |
| 76 | + revertedStatusSchema |
| 77 | +]); |
| 78 | + |
| 79 | +export type GelatoTerminalStatus = z.infer<typeof gelatoTerminalStatusSchema>; |
| 80 | + |
| 81 | +export type GelatoStatus = z.infer<typeof gelatoStatusSchema>; |
| 82 | + |
| 83 | +export type GetGelatoStatusParameters = { |
| 84 | + id: string; |
| 85 | +}; |
| 86 | + |
| 87 | +export const getGelatoStatus = async ( |
| 88 | + client: ReturnType<Transport>, |
| 89 | + parameters: GetGelatoStatusParameters |
| 90 | +): Promise<GelatoStatus> => { |
| 91 | + const { id } = parameters; |
| 92 | + |
| 93 | + const result = await client.request({ |
| 94 | + method: 'gelato_getStatus', |
| 95 | + params: { id } |
| 96 | + }); |
| 97 | + |
| 98 | + return gelatoStatusSchema.parse(result); |
| 99 | +}; |
0 commit comments