Skip to content

Commit

Permalink
Merge pull request #517 from rabbitholegg/sam/boost-4470-expand-filte…
Browse files Browse the repository at this point in the history
…r-support

[BOOST-4470] Further expand filter interfaces, schemas
  • Loading branch information
sammccord authored Aug 21, 2024
2 parents 939fd62 + 0e4eb4c commit 0c550ce
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-ducks-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rabbitholegg/questdk-plugin-utils": minor
---

allow any combination of filters in mint params
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export type {
PremintValidationParams,
PremintActionDetail,
PremintActionForm,
Primitive,
} from './types'

export {
Expand Down
17 changes: 8 additions & 9 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,
} 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
41 changes: 24 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,21 @@ 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()])
export 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 +176,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 +195,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
1 change: 1 addition & 0 deletions packages/utils/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export {
export { ActionType, OrderType } from './actions'

export type {
Primitive,
FilterObject,
BitmaskFilter,
NthFilter,
Expand Down

0 comments on commit 0c550ce

Please sign in to comment.