Skip to content

Commit

Permalink
feat(v4): use filter at top level for operators instead of filteroper…
Browse files Browse the repository at this point in the history
…ator
  • Loading branch information
sammccord committed Aug 21, 2024
1 parent 939fd62 commit 20390ad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
19 changes: 9 additions & 10 deletions packages/utils/src/types/actions.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -216,15 +215,15 @@ 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(),
})

export const MintActionDetailSchema = z.object({
chainId: z.number(),
contractAddress: EthAddressSchema,
tokenId: z.number().optional(),
amount: z.union([NumericSchema, FilterOperatorSchema]),
amount: FilterSchema,
amountOperator: QuestInputActionParamsAmountOperatorEnum.optional(),
})

Expand Down
36 changes: 19 additions & 17 deletions packages/utils/src/types/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ export type FilterOperator =

export type ArrayOperator =
| {
$some?: FilterOperator[]
$some?: Filter[]
}
| {
$first?: FilterOperator
$first?: Filter
}
| {
$last?: FilterOperator
$last?: Filter
}
| {
$nth?: NthFilter
Expand All @@ -81,33 +81,33 @@ export type ArrayOperator =
export const ArrayOperatorSchema: z.ZodType<ArrayOperator> = 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<LogicalOperator> = z.union([
z.object({
$and: z
.lazy(() => FilterOperatorSchema)
.lazy(() => FilterSchema)
.array()
.optional(),
}),
z.object({
$or: z
.lazy(() => FilterOperatorSchema)
.lazy(() => FilterSchema)
.array()
.optional(),
}),
Expand All @@ -121,16 +121,16 @@ export const FilterOperatorSchema = z.union([
])

export type TransactionFilter = {
[K in keyof Transaction]: FilterOperator
[K in keyof Partial<Transaction>]: Filter
}

export const TransactionFilterSchema = z.record(
export const TransactionFilterSchema: z.ZodType<TransactionFilter> = 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
Expand Down Expand Up @@ -171,12 +171,14 @@ export const AbiParamFilterSchema = z

export type Filter =
| Primitive
| Array<Primitive>
| FilterObject
| FilterArray
| FilterOperator
| Abi
export const FilterSchema = z.union([
PrimitiveSchema,
PrimitiveSchema.array(),
FilterObjectSchema,
FilterOperatorSchema,
z.lazy(() => FilterSchema.array()),
Expand All @@ -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()]),
Expand Down

0 comments on commit 20390ad

Please sign in to comment.