diff --git a/src/agents/contracts.ts b/src/agents/contracts.ts index 0ed03352f..a25d89115 100644 --- a/src/agents/contracts.ts +++ b/src/agents/contracts.ts @@ -1,4 +1,5 @@ -import { ChainId, ChecksumAddress } from '../types'; +import { ChecksumAddress } from '../types'; +import { ChainId } from '../web3'; type Contracts = { readonly SUBSCRIPTION_MANAGER: ChecksumAddress | undefined; diff --git a/src/conditions/base/index.ts b/src/conditions/base/index.ts index a6a0abe36..b17914f34 100644 --- a/src/conditions/base/index.ts +++ b/src/conditions/base/index.ts @@ -35,9 +35,6 @@ export class TimeCondition extends Condition { } } -export { - contractConditionSchema, - type ContractConditionProps, -} from './contract'; -export { rpcConditionSchema, type RpcConditionProps } from './rpc'; -export { timeConditionSchema, type TimeConditionProps } from './time'; +export { type ContractConditionProps } from './contract'; +export { type RpcConditionProps } from './rpc'; +export { type TimeConditionProps } from './time'; diff --git a/src/conditions/base/rpc.ts b/src/conditions/base/rpc.ts index 1b6820dce..ea232d50f 100644 --- a/src/conditions/base/rpc.ts +++ b/src/conditions/base/rpc.ts @@ -1,7 +1,7 @@ import { z } from 'zod'; -import { SUPPORTED_CHAIN_IDS } from '../../types'; -import createUnionSchema from '../../zod'; +import { SUPPORTED_CHAIN_IDS } from '../const'; +import createUnionSchema from '../zod'; import { EthAddressOrUserAddressSchema, returnValueTestSchema } from './shared'; diff --git a/src/conditions/condition.ts b/src/conditions/condition.ts index 1fee07429..b16444ef1 100644 --- a/src/conditions/condition.ts +++ b/src/conditions/condition.ts @@ -31,17 +31,12 @@ export class Condition { public static validate( schema: ConditionSchema, - value: ConditionProps, - override: Partial = {} + value: ConditionProps ): { data?: ConditionProps; error?: z.ZodError; } { - const newValue = { - ...value, - ...override, - }; - const result = schema.safeParse(newValue); + const result = schema.safeParse(value); if (result.success) { return { data: result.data }; } diff --git a/src/conditions/const.ts b/src/conditions/const.ts index 3a8b941d2..9854c6a13 100644 --- a/src/conditions/const.ts +++ b/src/conditions/const.ts @@ -1,3 +1,12 @@ +import { ChainId } from '../web3'; + export const USER_ADDRESS_PARAM = ':userAddress'; export const ETH_ADDRESS_REGEXP = new RegExp('^0x[a-fA-F0-9]{40}$'); + +export const SUPPORTED_CHAIN_IDS = [ + ChainId.POLYGON, + ChainId.MUMBAI, + ChainId.GOERLI, + ChainId.MAINNET, +]; diff --git a/src/conditions/index.ts b/src/conditions/index.ts index 365b87a63..ac71ad556 100644 --- a/src/conditions/index.ts +++ b/src/conditions/index.ts @@ -8,7 +8,4 @@ export { } from './condition-expr'; export { ConditionContext, type CustomContextParam } from './context'; export { Condition, type ConditionProps } from './condition'; -export { - compoundConditionSchema, - type CompoundConditionProps, -} from './compound-condition'; +export { type CompoundConditionProps } from './compound-condition'; diff --git a/src/zod.ts b/src/conditions/zod.ts similarity index 100% rename from src/zod.ts rename to src/conditions/zod.ts diff --git a/src/types.ts b/src/types.ts index 56262e1ea..9befc497e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,17 +1,3 @@ export type ChecksumAddress = string; export type HexEncodedBytes = string; export type Base64EncodedBytes = string; - -export enum ChainId { - POLYGON = 137, - MUMBAI = 80001, - GOERLI = 5, - MAINNET = 1, -} - -export const SUPPORTED_CHAIN_IDS = [ - ChainId.POLYGON, - ChainId.MUMBAI, - ChainId.GOERLI, - ChainId.MAINNET, -]; diff --git a/src/web3.ts b/src/web3.ts index 1b5dcd11a..a55508f8f 100644 --- a/src/web3.ts +++ b/src/web3.ts @@ -1,4 +1,12 @@ import { fromHexString } from './utils'; + +export enum ChainId { + POLYGON = 137, + MUMBAI = 80001, + GOERLI = 5, + MAINNET = 1, +} + export const toCanonicalAddress = (address: string): Uint8Array => { const ETH_ADDRESS_STRING_PREFIX = '0x'; const nonPrefixed = address.startsWith(ETH_ADDRESS_STRING_PREFIX) diff --git a/test/unit/conditions/base/condition.test.ts b/test/unit/conditions/base/condition.test.ts index 611393148..e91322af0 100644 --- a/test/unit/conditions/base/condition.test.ts +++ b/test/unit/conditions/base/condition.test.ts @@ -7,7 +7,6 @@ import { import { TEST_CHAIN_ID, TEST_CONTRACT_ADDR, - TEST_CONTRACT_ADDR_2, testContractConditionObj, } from '../../testVariables'; @@ -22,44 +21,6 @@ describe('validation', () => { expect(result.error).toBeUndefined(); expect(result.data.contractAddress).toEqual(TEST_CONTRACT_ADDR); }); - - it('accepts on a valid value override', async () => { - const validOverride = { - chain: TEST_CHAIN_ID, - contractAddress: TEST_CONTRACT_ADDR_2, - }; - const result = Condition.validate( - condition.schema, - condition.value, - validOverride - ); - expect(result.error).toBeUndefined(); - expect(result.data).toMatchObject(validOverride); - }); - - it('rejects on an invalid value override', async () => { - const invalidOverride = { - chain: -1, - contractAddress: TEST_CONTRACT_ADDR, - }; - const result = Condition.validate( - condition.schema, - condition.value, - invalidOverride - ); - expect(result.error).toBeDefined(); - expect(result.data).toBeUndefined(); - expect(result.error?.format()).toMatchObject({ - chain: { - _errors: [ - 'Invalid literal value, expected 137', - 'Invalid literal value, expected 80001', - 'Invalid literal value, expected 5', - 'Invalid literal value, expected 1', - ], - }, - }); - }); }); describe('serialization', () => { diff --git a/test/unit/conditions/base/contract.test.ts b/test/unit/conditions/base/contract.test.ts index b85e6b6e2..002467210 100644 --- a/test/unit/conditions/base/contract.test.ts +++ b/test/unit/conditions/base/contract.test.ts @@ -5,9 +5,11 @@ import { import { ContractCondition, ContractConditionProps, - contractConditionSchema, } from '../../../../src/conditions/base'; -import { FunctionAbiProps } from '../../../../src/conditions/base/contract'; +import { + contractConditionSchema, + FunctionAbiProps, +} from '../../../../src/conditions/base/contract'; import { USER_ADDRESS_PARAM } from '../../../../src/conditions/const'; import { fakeProvider, fakeSigner } from '../../../utils'; import { testContractConditionObj, testFunctionAbi } from '../../testVariables'; diff --git a/test/unit/conditions/base/rpc.test.ts b/test/unit/conditions/base/rpc.test.ts index be853e014..c4f658092 100644 --- a/test/unit/conditions/base/rpc.test.ts +++ b/test/unit/conditions/base/rpc.test.ts @@ -1,7 +1,5 @@ -import { - RpcCondition, - rpcConditionSchema, -} from '../../../../src/conditions/base'; +import { RpcCondition } from '../../../../src/conditions/base'; +import { rpcConditionSchema } from '../../../../src/conditions/base/rpc'; import { testRpcConditionObj } from '../../testVariables'; describe('validation', () => { diff --git a/test/unit/conditions/base/time.test.ts b/test/unit/conditions/base/time.test.ts index 7429bc408..0ace6466f 100644 --- a/test/unit/conditions/base/time.test.ts +++ b/test/unit/conditions/base/time.test.ts @@ -1,9 +1,9 @@ import { TimeCondition, TimeConditionProps, - timeConditionSchema, } from '../../../../src/conditions/base'; import { ReturnValueTestProps } from '../../../../src/conditions/base/shared'; +import { timeConditionSchema } from '../../../../src/conditions/base/time'; describe('validation', () => { const returnValueTest: ReturnValueTestProps = { diff --git a/test/unit/conditions/compound-condition.test.ts b/test/unit/conditions/compound-condition.test.ts index bfad989c4..2f81a8bc8 100644 --- a/test/unit/conditions/compound-condition.test.ts +++ b/test/unit/conditions/compound-condition.test.ts @@ -1,5 +1,6 @@ -import { compoundConditionSchema, Condition } from '../../../src/conditions'; +import { Condition } from '../../../src/conditions'; import { CompoundCondition } from '../../../src/conditions/base'; +import { compoundConditionSchema } from '../../../src/conditions/compound-condition'; import { testContractConditionObj, testRpcConditionObj, diff --git a/test/unit/conditions/condition-expr.test.ts b/test/unit/conditions/condition-expr.test.ts index faaa57949..ff53730e1 100644 --- a/test/unit/conditions/condition-expr.test.ts +++ b/test/unit/conditions/condition-expr.test.ts @@ -229,6 +229,19 @@ describe('condition set', () => { } ); + it('rejects a mismatched condition type', () => { + const conditionObj = { + ...testTimeConditionObj, + conditionType: 'rpc', + } as unknown as TimeConditionProps; + expect(() => { + ConditionExpression.fromObj({ + version: ConditionExpression.VERSION, + condition: conditionObj, + }); + }).toThrow(/^Invalid condition/); + }); + it('erc721 condition serialization', () => { const conditionExpr = new ConditionExpression(erc721BalanceCondition);