From 20390ad3fbf1700d905222ac0e01af2dd5ed21dc Mon Sep 17 00:00:00 2001 From: Sam McCord Date: Wed, 21 Aug 2024 09:22:02 -0600 Subject: [PATCH] feat(v4): use filter at top level for operators instead of filteroperator --- packages/utils/src/types/actions.ts | 19 ++++++++------- packages/utils/src/types/filters.ts | 36 +++++++++++++++-------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/packages/utils/src/types/actions.ts b/packages/utils/src/types/actions.ts index f0e1f9ac5..b170b39b4 100644 --- a/packages/utils/src/types/actions.ts +++ b/packages/utils/src/types/actions.ts @@ -1,20 +1,19 @@ +import { UUID } from 'crypto' import { - type PublicClient, type Address, + type PublicClient, type SimulateContractReturnType, type TransactionRequest, } from 'viem' +import { ZodSchema, z } from 'zod' +import { PluginActionNotImplementedError } from '../errors' +import { EthAddressSchema } from './common' import { - FilterOperatorSchema, - NumericSchema, + FilterSchema, type FilterOperator, - type TransactionFilter, + type TransactionFilter } from './filters' -import { PluginActionNotImplementedError } from '../errors' import type { MintIntentParams } from './intents' -import { ZodSchema, z } from 'zod' -import { EthAddressSchema } from './common' -import { UUID } from 'crypto' import { QuestCompletionPayload } from './quests' export type SwapActionParams = { @@ -216,7 +215,7 @@ export const StakeActionFormSchema = BaseStakeActionFormaSchema export const MintActionFormSchema = z.object({ contractAddress: EthAddressSchema, tokenId: z.number().optional(), - amount: z.union([NumericSchema, FilterOperatorSchema]), + amount: FilterSchema, amountOperator: QuestInputActionParamsAmountOperatorEnum.optional(), }) @@ -224,7 +223,7 @@ export const MintActionDetailSchema = z.object({ chainId: z.number(), contractAddress: EthAddressSchema, tokenId: z.number().optional(), - amount: z.union([NumericSchema, FilterOperatorSchema]), + amount: FilterSchema, amountOperator: QuestInputActionParamsAmountOperatorEnum.optional(), }) diff --git a/packages/utils/src/types/filters.ts b/packages/utils/src/types/filters.ts index 5c80ebb3f..5afc73c03 100644 --- a/packages/utils/src/types/filters.ts +++ b/packages/utils/src/types/filters.ts @@ -66,13 +66,13 @@ export type FilterOperator = export type ArrayOperator = | { - $some?: FilterOperator[] + $some?: Filter[] } | { - $first?: FilterOperator + $first?: Filter } | { - $last?: FilterOperator + $last?: Filter } | { $nth?: NthFilter @@ -81,33 +81,33 @@ export type ArrayOperator = export const ArrayOperatorSchema: z.ZodType = z.union([ z.object({ $some: z - .lazy(() => FilterOperatorSchema) + .lazy(() => FilterSchema) .array() .optional(), }), - z.object({ $first: z.lazy(() => FilterOperatorSchema).optional() }), - z.object({ $last: z.lazy(() => FilterOperatorSchema).optional() }), - z.object({ $nth: z.lazy(() => NthFilterSchema).optional() }), + z.object({ $first: z.lazy(() => FilterSchema).optional() }), + z.object({ $last: z.lazy(() => FilterSchema).optional() }), + z.object({ $nth: z.lazy(() => FilterSchema).optional() }), ]) export type LogicalOperator = | { - $and?: FilterOperator[] + $and?: Filter[] } | { - $or?: FilterOperator[] + $or?: Filter[] } export const LogicalOperatorSchema: z.ZodType = z.union([ z.object({ $and: z - .lazy(() => FilterOperatorSchema) + .lazy(() => FilterSchema) .array() .optional(), }), z.object({ $or: z - .lazy(() => FilterOperatorSchema) + .lazy(() => FilterSchema) .array() .optional(), }), @@ -121,16 +121,16 @@ export const FilterOperatorSchema = z.union([ ]) export type TransactionFilter = { - [K in keyof Transaction]: FilterOperator + [K in keyof Partial]: Filter } -export const TransactionFilterSchema = z.record( +export const TransactionFilterSchema: z.ZodType = z.record( z.string(), - FilterOperatorSchema, + z.lazy(() => FilterSchema), ) -type Primitive = string | number | boolean -export const PrimitiveSchema = z.union([z.string(), z.number(), z.boolean()]) +type Primitive = string | number | boolean | bigint +export const PrimitiveSchema = z.union([z.string(), z.number(), z.boolean(), z.bigint()]) export type FilterObject = { [key: string]: Filter @@ -171,12 +171,14 @@ export const AbiParamFilterSchema = z export type Filter = | Primitive + | Array | FilterObject | FilterArray | FilterOperator | Abi export const FilterSchema = z.union([ PrimitiveSchema, + PrimitiveSchema.array(), FilterObjectSchema, FilterOperatorSchema, z.lazy(() => FilterSchema.array()), @@ -188,7 +190,7 @@ export const FilterArraySchema = FilterSchema.array() export type NthFilter = { index: bigint | number | string - value: TransactionFilter | FilterObject + value: TransactionFilter | Filter } export const NthFilterSchema = z.object({ index: z.union([z.bigint(), z.number(), z.string()]),