From 6206359426414f40f4924355f8e3aa9a725ec596 Mon Sep 17 00:00:00 2001 From: Piotr Roslaniec Date: Tue, 7 Nov 2023 14:25:56 +0100 Subject: [PATCH 1/7] refactor(taco): remove redundant exports from taco package --- demos/taco-demo/src/ConditionBuilder.tsx | 2 +- demos/taco-nft-demo/src/NFTConditionBuilder.tsx | 4 ++-- examples/taco/nextjs/src/app/page.tsx | 2 +- examples/taco/nodejs/src/index.ts | 2 +- examples/taco/react/src/App.tsx | 2 +- examples/taco/webpack-5/src/index.ts | 2 +- packages/taco/examples/conditions.ts | 2 +- packages/taco/src/conditions/index.ts | 3 --- .../taco/test/conditions/base/condition.test.ts | 4 ++-- .../taco/test/conditions/base/contract.test.ts | 11 ++++++----- packages/taco/test/conditions/base/rpc.test.ts | 4 ++-- packages/taco/test/conditions/base/time.test.ts | 10 ++-------- .../test/conditions/compound-condition.test.ts | 4 +--- .../taco/test/conditions/condition-expr.test.ts | 14 +++++++------- packages/taco/test/conditions/conditions.test.ts | 3 ++- packages/taco/test/conditions/context.test.ts | 13 ++++--------- packages/taco/test/taco.test.ts | 2 +- packages/taco/test/test-utils.ts | 15 +++++++-------- 18 files changed, 42 insertions(+), 57 deletions(-) diff --git a/demos/taco-demo/src/ConditionBuilder.tsx b/demos/taco-demo/src/ConditionBuilder.tsx index 15b0e5312..717ee6ebe 100644 --- a/demos/taco-demo/src/ConditionBuilder.tsx +++ b/demos/taco-demo/src/ConditionBuilder.tsx @@ -8,7 +8,7 @@ interface Props { enabled: boolean; } -const rpcCondition = new conditions.RpcCondition({ +const rpcCondition = new conditions.base.RpcCondition({ chain: Mumbai.chainId, method: 'eth_getBalance', parameters: [':userAddress'], diff --git a/demos/taco-nft-demo/src/NFTConditionBuilder.tsx b/demos/taco-nft-demo/src/NFTConditionBuilder.tsx index 7cb63e2a1..5125d539a 100644 --- a/demos/taco-nft-demo/src/NFTConditionBuilder.tsx +++ b/demos/taco-nft-demo/src/NFTConditionBuilder.tsx @@ -51,7 +51,7 @@ export const NFTConditionBuilder = ({ const makeCondition = (): conditions.Condition => { if (tokenId) { - return new conditions.ContractCondition({ + return new conditions.base.ContractCondition({ contractAddress, chain, standardContractType: 'ERC721', @@ -63,7 +63,7 @@ export const NFTConditionBuilder = ({ }, }); } - return new conditions.ContractCondition({ + return new conditions.base.ContractCondition({ contractAddress, chain, standardContractType: 'ERC721', diff --git a/examples/taco/nextjs/src/app/page.tsx b/examples/taco/nextjs/src/app/page.tsx index a9530033a..b8555a762 100644 --- a/examples/taco/nextjs/src/app/page.tsx +++ b/examples/taco/nextjs/src/app/page.tsx @@ -68,7 +68,7 @@ function App() { setEncrypting(true) try { const signer = provider.getSigner(); - const hasPositiveBalance = new conditions.RpcCondition({ + const hasPositiveBalance = new conditions.base.RpcCondition({ chain: 80001, method: 'eth_getBalance', parameters: [':userAddress', 'latest'], diff --git a/examples/taco/nodejs/src/index.ts b/examples/taco/nodejs/src/index.ts index e9595823d..d7a472dc0 100644 --- a/examples/taco/nodejs/src/index.ts +++ b/examples/taco/nodejs/src/index.ts @@ -46,7 +46,7 @@ const encryptToBytes = async (messageString: string) => { const message = toBytes(messageString); console.log(format('Encrypting message ("%s") ...', messageString)); - const hasPositiveBalance = new conditions.RpcCondition({ + const hasPositiveBalance = new conditions.base.RpcCondition({ chain: 80001, method: 'eth_getBalance', parameters: [':userAddress', 'latest'], diff --git a/examples/taco/react/src/App.tsx b/examples/taco/react/src/App.tsx index 9820d7d65..fbb16b578 100644 --- a/examples/taco/react/src/App.tsx +++ b/examples/taco/react/src/App.tsx @@ -65,7 +65,7 @@ function App() { setEncrypting(true) try { const signer = provider.getSigner(); - const hasPositiveBalance = new conditions.RpcCondition({ + const hasPositiveBalance = new conditions.base.RpcCondition({ chain: 80001, method: 'eth_getBalance', parameters: [':userAddress', 'latest'], diff --git a/examples/taco/webpack-5/src/index.ts b/examples/taco/webpack-5/src/index.ts index ebf81dd5c..2e20132c5 100644 --- a/examples/taco/webpack-5/src/index.ts +++ b/examples/taco/webpack-5/src/index.ts @@ -37,7 +37,7 @@ const runExample = async () => { console.log('Encrypting message...'); const message = toBytes('this is a secret'); - const hasPositiveBalance = new conditions.RpcCondition({ + const hasPositiveBalance = new conditions.base.RpcCondition({ chain: 80001, method: 'eth_getBalance', parameters: [':userAddress', 'latest'], diff --git a/packages/taco/examples/conditions.ts b/packages/taco/examples/conditions.ts index c459b7cf2..0238e3889 100644 --- a/packages/taco/examples/conditions.ts +++ b/packages/taco/examples/conditions.ts @@ -78,7 +78,7 @@ console.assert( 'CompoundCondition requires signer', ); -const myFunctionAbi: conditions.FunctionAbiProps = { +const myFunctionAbi: conditions.base.FunctionAbiProps = { name: 'myFunction', type: 'function', stateMutability: 'view', diff --git a/packages/taco/src/conditions/index.ts b/packages/taco/src/conditions/index.ts index fad3849b1..38a337396 100644 --- a/packages/taco/src/conditions/index.ts +++ b/packages/taco/src/conditions/index.ts @@ -1,9 +1,6 @@ import * as base from './base'; import * as predefined from './predefined'; -export * from './base'; -export * from './predefined'; - export { CompoundConditionProps, CompoundConditionType, diff --git a/packages/taco/test/conditions/base/condition.test.ts b/packages/taco/test/conditions/base/condition.test.ts index 895373c4f..25fc267d5 100644 --- a/packages/taco/test/conditions/base/condition.test.ts +++ b/packages/taco/test/conditions/base/condition.test.ts @@ -3,9 +3,9 @@ import { describe, expect, it } from 'vitest'; import { Condition, - ContractCondition, - ERC721Ownership, } from '../../../src/conditions'; +import {ContractCondition} from "../../../src/conditions/base"; +import {ERC721Ownership} from "../../../src/conditions/predefined"; import { fakeCondition, testContractConditionObj } from '../../test-utils'; describe('validation', () => { diff --git a/packages/taco/test/conditions/base/contract.test.ts b/packages/taco/test/conditions/base/contract.test.ts index be469ab32..5aff54a68 100644 --- a/packages/taco/test/conditions/base/contract.test.ts +++ b/packages/taco/test/conditions/base/contract.test.ts @@ -4,13 +4,14 @@ import { beforeAll, describe, expect, it } from 'vitest'; import { ConditionExpression, - ContractCondition, - ContractConditionProps, - ContractConditionType, CustomContextParam, - FunctionAbiProps, } from '../../../src/conditions'; -import { contractConditionSchema } from '../../../src/conditions/base/contract'; +import {ContractCondition} from "../../../src/conditions/base"; +import { + ContractConditionProps, + contractConditionSchema, + ContractConditionType, FunctionAbiProps +} from '../../../src/conditions/base/contract'; import { USER_ADDRESS_PARAM } from '../../../src/conditions/const'; import { testContractConditionObj, testFunctionAbi } from '../../test-utils'; diff --git a/packages/taco/test/conditions/base/rpc.test.ts b/packages/taco/test/conditions/base/rpc.test.ts index 95450eefd..2fb0ff24b 100644 --- a/packages/taco/test/conditions/base/rpc.test.ts +++ b/packages/taco/test/conditions/base/rpc.test.ts @@ -1,8 +1,8 @@ import { TEST_CONTRACT_ADDR } from '@nucypher/test-utils'; import { describe, expect, it } from 'vitest'; -import { RpcCondition, RpcConditionType } from '../../../src/conditions'; -import { rpcConditionSchema } from '../../../src/conditions/base/rpc'; +import {RpcCondition} from "../../../src/conditions/base"; +import {rpcConditionSchema, RpcConditionType} from '../../../src/conditions/base/rpc'; import { testRpcConditionObj } from '../../test-utils'; describe('validation', () => { diff --git a/packages/taco/test/conditions/base/time.test.ts b/packages/taco/test/conditions/base/time.test.ts index 7e931860f..429258d47 100644 --- a/packages/taco/test/conditions/base/time.test.ts +++ b/packages/taco/test/conditions/base/time.test.ts @@ -1,13 +1,7 @@ import { describe, expect, it } from 'vitest'; -import { - ReturnValueTestProps, - TimeCondition, - TimeConditionMethod, - TimeConditionProps, - TimeConditionType, -} from '../../../src/conditions'; -import { timeConditionSchema } from '../../../src/conditions/base/time'; +import {ReturnValueTestProps, TimeCondition, TimeConditionProps} from "../../../src/conditions/base"; +import {TimeConditionMethod, timeConditionSchema, TimeConditionType} from '../../../src/conditions/base/time'; describe('validation', () => { const returnValueTest: ReturnValueTestProps = { diff --git a/packages/taco/test/conditions/compound-condition.test.ts b/packages/taco/test/conditions/compound-condition.test.ts index 143c518dc..974e4d58f 100644 --- a/packages/taco/test/conditions/compound-condition.test.ts +++ b/packages/taco/test/conditions/compound-condition.test.ts @@ -1,11 +1,9 @@ import { describe, expect, it } from 'vitest'; import { - CompoundCondition, CompoundConditionProps, - ContractCondition, - TimeCondition, } from '../../src/conditions'; +import {CompoundCondition, ContractCondition, TimeCondition} from "../../src/conditions/base"; import { compoundConditionSchema, CompoundConditionType, diff --git a/packages/taco/test/conditions/condition-expr.test.ts b/packages/taco/test/conditions/condition-expr.test.ts index d465718bb..2bb20e752 100644 --- a/packages/taco/test/conditions/condition-expr.test.ts +++ b/packages/taco/test/conditions/condition-expr.test.ts @@ -5,17 +5,17 @@ import { SemVer } from 'semver'; import { beforeAll, describe, expect, it } from 'vitest'; import { - CompoundCondition, ConditionExpression, +} from '../../src/conditions'; +import { + CompoundCondition, ContractCondition, ContractConditionProps, - ERC721Balance, - RpcCondition, - RpcConditionType, - TimeCondition, - TimeConditionProps, -} from '../../src/conditions'; + RpcCondition, RpcConditionType, + TimeCondition, TimeConditionProps +} from "../../src/conditions/base"; import { USER_ADDRESS_PARAM } from '../../src/conditions/const'; +import {ERC721Balance} from "../../src/conditions/predefined"; import { testContractConditionObj, testFunctionAbi, diff --git a/packages/taco/test/conditions/conditions.test.ts b/packages/taco/test/conditions/conditions.test.ts index 6ad391470..0f4582a00 100644 --- a/packages/taco/test/conditions/conditions.test.ts +++ b/packages/taco/test/conditions/conditions.test.ts @@ -3,8 +3,9 @@ import { fakeProvider, fakeSigner } from '@nucypher/test-utils'; import { beforeAll, describe, expect, it } from 'vitest'; import { initialize } from '../../src'; -import { CompoundCondition, ConditionContext } from '../../src/conditions'; +import {CompoundCondition} from "../../src/conditions/base"; import { SUPPORTED_CHAIN_IDS } from '../../src/conditions/const'; +import {ConditionContext} from "../../src/conditions/context"; describe('conditions', () => { beforeAll(async () => { diff --git a/packages/taco/test/conditions/context.test.ts b/packages/taco/test/conditions/context.test.ts index 7704212e8..9037bdc44 100644 --- a/packages/taco/test/conditions/context.test.ts +++ b/packages/taco/test/conditions/context.test.ts @@ -4,23 +4,18 @@ import { ethers } from 'ethers'; import { beforeAll, describe, expect, it } from 'vitest'; import { toBytes, toHexString } from '../../src'; -import { - ConditionExpression, - ContractCondition, - ContractConditionProps, - CustomContextParam, - ReturnValueTestProps, - RpcCondition, -} from '../../src/conditions'; +import {ContractCondition, + ContractConditionProps, ReturnValueTestProps, RpcCondition} from "../../src/conditions/base"; import { paramOrContextParamSchema } from '../../src/conditions/base/shared'; import { USER_ADDRESS_PARAM } from '../../src/conditions/const'; -import { RESERVED_CONTEXT_PARAMS } from '../../src/conditions/context/context'; +import {CustomContextParam, RESERVED_CONTEXT_PARAMS} from '../../src/conditions/context/context'; import { testContractConditionObj, testFunctionAbi, testReturnValueTest, testRpcConditionObj, } from '../test-utils'; +import {ConditionExpression} from "../../src/conditions/condition-expr"; describe('context', () => { let provider: ethers.providers.Provider; diff --git a/packages/taco/test/taco.test.ts b/packages/taco/test/taco.test.ts index 398398c7a..f16740bed 100644 --- a/packages/taco/test/taco.test.ts +++ b/packages/taco/test/taco.test.ts @@ -28,7 +28,7 @@ import { // Shared test variables const message = 'this is a secret'; -const ownsNFT = new conditions.ERC721Ownership({ +const ownsNFT = new conditions.predefined.ERC721Ownership({ contractAddress: '0x1e988ba4692e52Bc50b375bcC8585b95c48AaD77', parameters: [3591], chain: 5, diff --git a/packages/taco/test/test-utils.ts b/packages/taco/test/test-utils.ts index 5a0083503..0395c3f05 100644 --- a/packages/taco/test/test-utils.ts +++ b/packages/taco/test/test-utils.ts @@ -36,17 +36,16 @@ import { SpyInstance, vi } from 'vitest'; import { ConditionExpression, - ContractConditionProps, - ContractConditionType, - ERC721Balance, - FunctionAbiProps, +} from '../src/conditions'; +import { + ContractConditionProps, ContractConditionType, FunctionAbiProps, ReturnValueTestProps, - RpcConditionProps, - RpcConditionType, + RpcConditionProps, RpcConditionType, TimeConditionMethod, TimeConditionProps, - TimeConditionType, -} from '../src/conditions'; + TimeConditionType +} from "../src/conditions/base"; +import {ERC721Balance} from "../src/conditions/predefined"; import { DkgClient, DkgRitual } from '../src/dkg'; import { encryptMessage } from '../src/tdec'; From 4caebce943c5d46407893fb0014d25a325cb0385 Mon Sep 17 00:00:00 2001 From: Piotr Roslaniec Date: Tue, 7 Nov 2023 14:26:44 +0100 Subject: [PATCH 2/7] chore(linter): fix prettier linting --- .../taco/test/conditions/base/condition.test.ts | 8 +++----- .../taco/test/conditions/base/contract.test.ts | 5 +++-- packages/taco/test/conditions/base/rpc.test.ts | 7 +++++-- packages/taco/test/conditions/base/time.test.ts | 12 ++++++++++-- .../test/conditions/compound-condition.test.ts | 8 +++++--- .../taco/test/conditions/condition-expr.test.ts | 14 +++++++------- .../taco/test/conditions/conditions.test.ts | 4 ++-- packages/taco/test/conditions/context.test.ts | 12 ++++++++---- packages/taco/test/test-utils.ts | 17 +++++++++-------- 9 files changed, 52 insertions(+), 35 deletions(-) diff --git a/packages/taco/test/conditions/base/condition.test.ts b/packages/taco/test/conditions/base/condition.test.ts index 25fc267d5..ee1aaf5a8 100644 --- a/packages/taco/test/conditions/base/condition.test.ts +++ b/packages/taco/test/conditions/base/condition.test.ts @@ -1,11 +1,9 @@ import { TEST_CONTRACT_ADDR } from '@nucypher/test-utils'; import { describe, expect, it } from 'vitest'; -import { - Condition, -} from '../../../src/conditions'; -import {ContractCondition} from "../../../src/conditions/base"; -import {ERC721Ownership} from "../../../src/conditions/predefined"; +import { Condition } from '../../../src/conditions'; +import { ContractCondition } from '../../../src/conditions/base'; +import { ERC721Ownership } from '../../../src/conditions/predefined'; import { fakeCondition, testContractConditionObj } from '../../test-utils'; describe('validation', () => { diff --git a/packages/taco/test/conditions/base/contract.test.ts b/packages/taco/test/conditions/base/contract.test.ts index 5aff54a68..afcb2a95b 100644 --- a/packages/taco/test/conditions/base/contract.test.ts +++ b/packages/taco/test/conditions/base/contract.test.ts @@ -6,11 +6,12 @@ import { ConditionExpression, CustomContextParam, } from '../../../src/conditions'; -import {ContractCondition} from "../../../src/conditions/base"; +import { ContractCondition } from '../../../src/conditions/base'; import { ContractConditionProps, contractConditionSchema, - ContractConditionType, FunctionAbiProps + ContractConditionType, + FunctionAbiProps, } from '../../../src/conditions/base/contract'; import { USER_ADDRESS_PARAM } from '../../../src/conditions/const'; import { testContractConditionObj, testFunctionAbi } from '../../test-utils'; diff --git a/packages/taco/test/conditions/base/rpc.test.ts b/packages/taco/test/conditions/base/rpc.test.ts index 2fb0ff24b..449782275 100644 --- a/packages/taco/test/conditions/base/rpc.test.ts +++ b/packages/taco/test/conditions/base/rpc.test.ts @@ -1,8 +1,11 @@ import { TEST_CONTRACT_ADDR } from '@nucypher/test-utils'; import { describe, expect, it } from 'vitest'; -import {RpcCondition} from "../../../src/conditions/base"; -import {rpcConditionSchema, RpcConditionType} from '../../../src/conditions/base/rpc'; +import { RpcCondition } from '../../../src/conditions/base'; +import { + rpcConditionSchema, + RpcConditionType, +} from '../../../src/conditions/base/rpc'; import { testRpcConditionObj } from '../../test-utils'; describe('validation', () => { diff --git a/packages/taco/test/conditions/base/time.test.ts b/packages/taco/test/conditions/base/time.test.ts index 429258d47..0797cd138 100644 --- a/packages/taco/test/conditions/base/time.test.ts +++ b/packages/taco/test/conditions/base/time.test.ts @@ -1,7 +1,15 @@ import { describe, expect, it } from 'vitest'; -import {ReturnValueTestProps, TimeCondition, TimeConditionProps} from "../../../src/conditions/base"; -import {TimeConditionMethod, timeConditionSchema, TimeConditionType} from '../../../src/conditions/base/time'; +import { + ReturnValueTestProps, + TimeCondition, + TimeConditionProps, +} from '../../../src/conditions/base'; +import { + TimeConditionMethod, + timeConditionSchema, + TimeConditionType, +} from '../../../src/conditions/base/time'; describe('validation', () => { const returnValueTest: ReturnValueTestProps = { diff --git a/packages/taco/test/conditions/compound-condition.test.ts b/packages/taco/test/conditions/compound-condition.test.ts index 974e4d58f..ee1ec09d0 100644 --- a/packages/taco/test/conditions/compound-condition.test.ts +++ b/packages/taco/test/conditions/compound-condition.test.ts @@ -1,9 +1,11 @@ import { describe, expect, it } from 'vitest'; +import { CompoundConditionProps } from '../../src/conditions'; import { - CompoundConditionProps, -} from '../../src/conditions'; -import {CompoundCondition, ContractCondition, TimeCondition} from "../../src/conditions/base"; + CompoundCondition, + ContractCondition, + TimeCondition, +} from '../../src/conditions/base'; import { compoundConditionSchema, CompoundConditionType, diff --git a/packages/taco/test/conditions/condition-expr.test.ts b/packages/taco/test/conditions/condition-expr.test.ts index 2bb20e752..706dd8ee0 100644 --- a/packages/taco/test/conditions/condition-expr.test.ts +++ b/packages/taco/test/conditions/condition-expr.test.ts @@ -4,18 +4,18 @@ import { TEST_CHAIN_ID, TEST_CONTRACT_ADDR } from '@nucypher/test-utils'; import { SemVer } from 'semver'; import { beforeAll, describe, expect, it } from 'vitest'; -import { - ConditionExpression, -} from '../../src/conditions'; +import { ConditionExpression } from '../../src/conditions'; import { CompoundCondition, ContractCondition, ContractConditionProps, - RpcCondition, RpcConditionType, - TimeCondition, TimeConditionProps -} from "../../src/conditions/base"; + RpcCondition, + RpcConditionType, + TimeCondition, + TimeConditionProps, +} from '../../src/conditions/base'; import { USER_ADDRESS_PARAM } from '../../src/conditions/const'; -import {ERC721Balance} from "../../src/conditions/predefined"; +import { ERC721Balance } from '../../src/conditions/predefined'; import { testContractConditionObj, testFunctionAbi, diff --git a/packages/taco/test/conditions/conditions.test.ts b/packages/taco/test/conditions/conditions.test.ts index 0f4582a00..f3499a69e 100644 --- a/packages/taco/test/conditions/conditions.test.ts +++ b/packages/taco/test/conditions/conditions.test.ts @@ -3,9 +3,9 @@ import { fakeProvider, fakeSigner } from '@nucypher/test-utils'; import { beforeAll, describe, expect, it } from 'vitest'; import { initialize } from '../../src'; -import {CompoundCondition} from "../../src/conditions/base"; +import { ConditionContext } from '../../src/conditions'; +import { CompoundCondition } from '../../src/conditions/base'; import { SUPPORTED_CHAIN_IDS } from '../../src/conditions/const'; -import {ConditionContext} from "../../src/conditions/context"; describe('conditions', () => { beforeAll(async () => { diff --git a/packages/taco/test/conditions/context.test.ts b/packages/taco/test/conditions/context.test.ts index 9037bdc44..d7e62f7f1 100644 --- a/packages/taco/test/conditions/context.test.ts +++ b/packages/taco/test/conditions/context.test.ts @@ -4,18 +4,22 @@ import { ethers } from 'ethers'; import { beforeAll, describe, expect, it } from 'vitest'; import { toBytes, toHexString } from '../../src'; -import {ContractCondition, - ContractConditionProps, ReturnValueTestProps, RpcCondition} from "../../src/conditions/base"; +import { ConditionExpression, CustomContextParam } from '../../src/conditions'; +import { + ContractCondition, + ContractConditionProps, + ReturnValueTestProps, + RpcCondition, +} from '../../src/conditions/base'; import { paramOrContextParamSchema } from '../../src/conditions/base/shared'; import { USER_ADDRESS_PARAM } from '../../src/conditions/const'; -import {CustomContextParam, RESERVED_CONTEXT_PARAMS} from '../../src/conditions/context/context'; +import { RESERVED_CONTEXT_PARAMS } from '../../src/conditions/context/context'; import { testContractConditionObj, testFunctionAbi, testReturnValueTest, testRpcConditionObj, } from '../test-utils'; -import {ConditionExpression} from "../../src/conditions/condition-expr"; describe('context', () => { let provider: ethers.providers.Provider; diff --git a/packages/taco/test/test-utils.ts b/packages/taco/test/test-utils.ts index 0395c3f05..076c7c4a0 100644 --- a/packages/taco/test/test-utils.ts +++ b/packages/taco/test/test-utils.ts @@ -34,18 +34,19 @@ import { } from '@nucypher/test-utils'; import { SpyInstance, vi } from 'vitest'; +import { ConditionExpression } from '../src/conditions'; import { - ConditionExpression, -} from '../src/conditions'; -import { - ContractConditionProps, ContractConditionType, FunctionAbiProps, + ContractConditionProps, + ContractConditionType, + FunctionAbiProps, ReturnValueTestProps, - RpcConditionProps, RpcConditionType, + RpcConditionProps, + RpcConditionType, TimeConditionMethod, TimeConditionProps, - TimeConditionType -} from "../src/conditions/base"; -import {ERC721Balance} from "../src/conditions/predefined"; + TimeConditionType, +} from '../src/conditions/base'; +import { ERC721Balance } from '../src/conditions/predefined'; import { DkgClient, DkgRitual } from '../src/dkg'; import { encryptMessage } from '../src/tdec'; From 6158484a3dc51578aff425cbc25a284655846ae3 Mon Sep 17 00:00:00 2001 From: Piotr Roslaniec Date: Wed, 8 Nov 2023 11:39:09 +0100 Subject: [PATCH 3/7] feature(conditions): move compound condition to base namespace --- .../taco/src/conditions/{ => base}/compound-condition.ts | 6 +++--- packages/taco/src/conditions/base/index.ts | 6 +++--- packages/taco/src/conditions/condition.ts | 2 +- packages/taco/src/conditions/context/context.ts | 2 +- packages/taco/src/conditions/index.ts | 2 +- packages/taco/test/conditions/compound-condition.test.ts | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) rename packages/taco/src/conditions/{ => base}/compound-condition.ts (88%) diff --git a/packages/taco/src/conditions/compound-condition.ts b/packages/taco/src/conditions/base/compound-condition.ts similarity index 88% rename from packages/taco/src/conditions/compound-condition.ts rename to packages/taco/src/conditions/base/compound-condition.ts index 772fa55e2..57d5b2f33 100644 --- a/packages/taco/src/conditions/compound-condition.ts +++ b/packages/taco/src/conditions/base/compound-condition.ts @@ -1,8 +1,8 @@ import { z } from 'zod'; -import { contractConditionSchema } from './base/contract'; -import { rpcConditionSchema } from './base/rpc'; -import { timeConditionSchema } from './base/time'; +import { contractConditionSchema } from './contract'; +import { rpcConditionSchema } from './rpc'; +import { timeConditionSchema } from './time'; export const CompoundConditionType = 'compound'; diff --git a/packages/taco/src/conditions/base/index.ts b/packages/taco/src/conditions/base/index.ts index 00f50a14d..e5eab4b78 100644 --- a/packages/taco/src/conditions/base/index.ts +++ b/packages/taco/src/conditions/base/index.ts @@ -1,10 +1,10 @@ +import { Condition, ConditionProps } from '../condition'; + import { CompoundConditionProps, compoundConditionSchema, CompoundConditionType, -} from '../compound-condition'; -import { Condition, ConditionProps } from '../condition'; - +} from './compound-condition'; import { ContractConditionProps, contractConditionSchema, diff --git a/packages/taco/src/conditions/condition.ts b/packages/taco/src/conditions/condition.ts index 8bb673dc9..31bd6b2ef 100644 --- a/packages/taco/src/conditions/condition.ts +++ b/packages/taco/src/conditions/condition.ts @@ -16,7 +16,7 @@ import { import { CompoundConditionProps, CompoundConditionType, -} from './compound-condition'; +} from './base/compound-condition'; import { USER_ADDRESS_PARAM } from './const'; type ConditionSchema = z.ZodSchema; diff --git a/packages/taco/src/conditions/context/context.ts b/packages/taco/src/conditions/context/context.ts index 7b528f70e..f531f7389 100644 --- a/packages/taco/src/conditions/context/context.ts +++ b/packages/taco/src/conditions/context/context.ts @@ -2,7 +2,7 @@ import { Context, Conditions as WASMConditions } from '@nucypher/nucypher-core'; import { fromJSON, toJSON } from '@nucypher/shared'; import { ethers } from 'ethers'; -import { CompoundConditionType } from '../compound-condition'; +import { CompoundConditionType } from '../base/compound-condition'; import { Condition, ConditionProps } from '../condition'; import { ConditionExpression } from '../condition-expr'; import { diff --git a/packages/taco/src/conditions/index.ts b/packages/taco/src/conditions/index.ts index 38a337396..f17081bd9 100644 --- a/packages/taco/src/conditions/index.ts +++ b/packages/taco/src/conditions/index.ts @@ -4,7 +4,7 @@ import * as predefined from './predefined'; export { CompoundConditionProps, CompoundConditionType, -} from './compound-condition'; +} from './base/compound-condition'; export { Condition, ConditionProps } from './condition'; export { ConditionExpression, ConditionExpressionJSON } from './condition-expr'; export { ConditionContext, CustomContextParam } from './context'; diff --git a/packages/taco/test/conditions/compound-condition.test.ts b/packages/taco/test/conditions/compound-condition.test.ts index ee1ec09d0..362ced81e 100644 --- a/packages/taco/test/conditions/compound-condition.test.ts +++ b/packages/taco/test/conditions/compound-condition.test.ts @@ -9,7 +9,7 @@ import { import { compoundConditionSchema, CompoundConditionType, -} from '../../src/conditions/compound-condition'; +} from '../../src/conditions/base/compound-condition'; import { testContractConditionObj, testRpcConditionObj, From 4bb61ccc6a630a7d65b643bc1c09cdb8314c6c0b Mon Sep 17 00:00:00 2001 From: Piotr Roslaniec Date: Thu, 9 Nov 2023 10:27:08 +0100 Subject: [PATCH 4/7] feature(conditions): dont re-export time condition method --- packages/taco/src/conditions/base/index.ts | 1 - packages/taco/test/test-utils.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/taco/src/conditions/base/index.ts b/packages/taco/src/conditions/base/index.ts index e5eab4b78..356912325 100644 --- a/packages/taco/src/conditions/base/index.ts +++ b/packages/taco/src/conditions/base/index.ts @@ -96,7 +96,6 @@ export { export { RpcConditionType, type RpcConditionProps } from './rpc'; export { ReturnValueTestProps } from './shared'; export { - TimeConditionMethod, TimeConditionType, type TimeConditionProps, } from './time'; diff --git a/packages/taco/test/test-utils.ts b/packages/taco/test/test-utils.ts index 076c7c4a0..55c4592ed 100644 --- a/packages/taco/test/test-utils.ts +++ b/packages/taco/test/test-utils.ts @@ -42,10 +42,10 @@ import { ReturnValueTestProps, RpcConditionProps, RpcConditionType, - TimeConditionMethod, TimeConditionProps, TimeConditionType, } from '../src/conditions/base'; +import { TimeConditionMethod } from "../src/conditions/base/time"; import { ERC721Balance } from '../src/conditions/predefined'; import { DkgClient, DkgRitual } from '../src/dkg'; import { encryptMessage } from '../src/tdec'; From 42f2c39548449c8523b19ac69014803dccc4b8bb Mon Sep 17 00:00:00 2001 From: Piotr Roslaniec Date: Thu, 16 Nov 2023 12:36:23 +0100 Subject: [PATCH 5/7] refactor(conditions): refactor conditon related objects into modules --- examples/taco/react/src/hooks/useTaco.ts | 4 ++-- packages/taco/examples/conditions.ts | 18 +++++++++--------- packages/taco/src/conditions/base/index.ts | 11 ++++------- .../{base => }/compound-condition.ts | 6 +++--- packages/taco/src/conditions/condition.ts | 4 ++-- .../taco/src/conditions/context/context.ts | 6 ++---- packages/taco/src/conditions/index.ts | 11 ++++------- .../taco/src/conditions/predefined/index.ts | 2 +- packages/taco/src/taco.ts | 8 +++----- packages/taco/src/tdec.ts | 7 ++----- .../test/conditions/base/condition.test.ts | 4 ++-- .../taco/test/conditions/base/contract.test.ts | 6 ++---- .../test/conditions/compound-condition.test.ts | 2 +- .../test/conditions/condition-expr.test.ts | 4 ++-- .../taco/test/conditions/conditions.test.ts | 2 +- packages/taco/test/conditions/context.test.ts | 13 +++++++++---- packages/taco/test/taco.test.ts | 2 +- packages/taco/test/test-utils.ts | 6 +++--- 18 files changed, 53 insertions(+), 63 deletions(-) rename packages/taco/src/conditions/{base => }/compound-condition.ts (88%) diff --git a/examples/taco/react/src/hooks/useTaco.ts b/examples/taco/react/src/hooks/useTaco.ts index ed7732eb6..376a80a62 100644 --- a/examples/taco/react/src/hooks/useTaco.ts +++ b/examples/taco/react/src/hooks/useTaco.ts @@ -1,4 +1,5 @@ import { + conditions, decrypt, Domain, encrypt, @@ -6,7 +7,6 @@ import { initialize, ThresholdMessageKit, } from '@nucypher/taco'; -import { Condition } from '@nucypher/taco/src/conditions'; import { ethers } from 'ethers'; import { useCallback, useEffect, useState } from 'react'; @@ -43,7 +43,7 @@ export default function useTaco({ const encryptDataToBytes = useCallback( async ( message: string, - condition: Condition, + condition: conditions.condition.Condition, encryptorSigner: ethers.Signer, ) => { if (!isInit || !provider) return; diff --git a/packages/taco/examples/conditions.ts b/packages/taco/examples/conditions.ts index 0238e3889..f6eea7038 100644 --- a/packages/taco/examples/conditions.ts +++ b/packages/taco/examples/conditions.ts @@ -1,13 +1,13 @@ import { conditions } from '../src'; -const ownsNFT = new conditions.predefined.ERC721Ownership({ +const ownsNFT = new conditions.predefined.erc721.ERC721Ownership({ contractAddress: '0x1e988ba4692e52Bc50b375bcC8585b95c48AaD77', parameters: [3591], chain: 5, }); console.assert(ownsNFT.requiresSigner(), 'ERC721Ownership requires signer'); -const hasAtLeastTwoNFTs = new conditions.predefined.ERC721Balance({ +const hasAtLeastTwoNFTs = new conditions.predefined.erc721.ERC721Balance({ contractAddress: '0x1e988ba4692e52Bc50b375bcC8585b95c48AaD77', chain: 5, returnValueTest: { @@ -20,7 +20,7 @@ console.assert( 'ERC721Balance requires signer', ); -const ownsNFTRaw = new conditions.base.ContractCondition({ +const ownsNFTRaw = new conditions.base.contract.ContractCondition({ // Provided by the `predefined.ERC721Balance` method: 'balanceOf', parameters: [':userAddress'], @@ -38,7 +38,7 @@ console.assert( 'ContractCondition requires a signer', ); -const hasAnyNativeAsset = new conditions.base.RpcCondition({ +const hasAnyNativeAsset = new conditions.base.rpc.RpcCondition({ chain: 5, method: 'eth_getBalance', parameters: [':userAddress'], @@ -52,13 +52,13 @@ console.assert( 'RpcCondition requires signer', ); -const ownsNFTOnChain5 = new conditions.predefined.ERC721Ownership({ +const ownsNFTOnChain5 = new conditions.predefined.erc721.ERC721Ownership({ contractAddress: '0x1e988ba4692e52Bc50b375bcC8585b95c48AaD77', parameters: [3591], chain: 5, // The first network we target }); -const hasAnyNativeAssetOnChain1 = new conditions.base.RpcCondition({ +const hasAnyNativeAssetOnChain1 = new conditions.base.rpc.RpcCondition({ chain: 1, // The second network we target method: 'eth_getBalance', parameters: [':userAddress'], @@ -68,7 +68,7 @@ const hasAnyNativeAssetOnChain1 = new conditions.base.RpcCondition({ }, }); -const multichainCondition = conditions.base.CompoundCondition.and([ +const multichainCondition = conditions.compound.CompoundCondition.and([ ownsNFTOnChain5, hasAnyNativeAssetOnChain1, ]); @@ -78,7 +78,7 @@ console.assert( 'CompoundCondition requires signer', ); -const myFunctionAbi: conditions.base.FunctionAbiProps = { +const myFunctionAbi: conditions.base.contract.FunctionAbiProps = { name: 'myFunction', type: 'function', stateMutability: 'view', @@ -103,7 +103,7 @@ const myFunctionAbi: conditions.base.FunctionAbiProps = { ], }; -const myContractCallCondition = new conditions.base.ContractCondition({ +const myContractCallCondition = new conditions.base.contract.ContractCondition({ method: 'myFunctionAbi', // `myMethodAbi.name` parameters: [':userAddress', ':myCustomParam'], // `myMethodAbi.inputs` functionAbi: myFunctionAbi, diff --git a/packages/taco/src/conditions/base/index.ts b/packages/taco/src/conditions/base/index.ts index 356912325..3f50e5960 100644 --- a/packages/taco/src/conditions/base/index.ts +++ b/packages/taco/src/conditions/base/index.ts @@ -1,10 +1,10 @@ -import { Condition, ConditionProps } from '../condition'; - import { CompoundConditionProps, compoundConditionSchema, CompoundConditionType, -} from './compound-condition'; +} from '../compound-condition'; +import { Condition, ConditionProps } from '../condition'; + import { ContractConditionProps, contractConditionSchema, @@ -95,7 +95,4 @@ export { } from './contract'; export { RpcConditionType, type RpcConditionProps } from './rpc'; export { ReturnValueTestProps } from './shared'; -export { - TimeConditionType, - type TimeConditionProps, -} from './time'; +export { TimeConditionType, type TimeConditionProps } from './time'; diff --git a/packages/taco/src/conditions/base/compound-condition.ts b/packages/taco/src/conditions/compound-condition.ts similarity index 88% rename from packages/taco/src/conditions/base/compound-condition.ts rename to packages/taco/src/conditions/compound-condition.ts index 57d5b2f33..772fa55e2 100644 --- a/packages/taco/src/conditions/base/compound-condition.ts +++ b/packages/taco/src/conditions/compound-condition.ts @@ -1,8 +1,8 @@ import { z } from 'zod'; -import { contractConditionSchema } from './contract'; -import { rpcConditionSchema } from './rpc'; -import { timeConditionSchema } from './time'; +import { contractConditionSchema } from './base/contract'; +import { rpcConditionSchema } from './base/rpc'; +import { timeConditionSchema } from './base/time'; export const CompoundConditionType = 'compound'; diff --git a/packages/taco/src/conditions/condition.ts b/packages/taco/src/conditions/condition.ts index 31bd6b2ef..0a9ae972a 100644 --- a/packages/taco/src/conditions/condition.ts +++ b/packages/taco/src/conditions/condition.ts @@ -16,7 +16,7 @@ import { import { CompoundConditionProps, CompoundConditionType, -} from './base/compound-condition'; +} from './compound-condition'; import { USER_ADDRESS_PARAM } from './const'; type ConditionSchema = z.ZodSchema; @@ -27,7 +27,7 @@ const ERR_INVALID_CONDITION = (error: z.ZodError) => const ERR_INVALID_CONDITION_TYPE = (type: string) => `Invalid condition type: ${type}`; -class ConditionFactory { +export class ConditionFactory { public static conditionFromProps(obj: ConditionProps): Condition { switch (obj.conditionType) { case RpcConditionType: diff --git a/packages/taco/src/conditions/context/context.ts b/packages/taco/src/conditions/context/context.ts index f531f7389..bd2f1cb7c 100644 --- a/packages/taco/src/conditions/context/context.ts +++ b/packages/taco/src/conditions/context/context.ts @@ -2,7 +2,7 @@ import { Context, Conditions as WASMConditions } from '@nucypher/nucypher-core'; import { fromJSON, toJSON } from '@nucypher/shared'; import { ethers } from 'ethers'; -import { CompoundConditionType } from '../base/compound-condition'; +import { CompoundConditionType } from '../compound-condition'; import { Condition, ConditionProps } from '../condition'; import { ConditionExpression } from '../condition-expr'; import { @@ -180,11 +180,9 @@ export class ConditionContext { signer?: ethers.Signer, customParameters?: Record, ): ConditionContext { - const innerCondition = - ConditionExpression.fromWASMConditions(conditions).condition; return new ConditionContext( provider, - innerCondition, + ConditionExpression.fromWASMConditions(conditions).condition, customParameters, signer, ); diff --git a/packages/taco/src/conditions/index.ts b/packages/taco/src/conditions/index.ts index f17081bd9..b80c1ef39 100644 --- a/packages/taco/src/conditions/index.ts +++ b/packages/taco/src/conditions/index.ts @@ -1,11 +1,8 @@ import * as base from './base'; import * as predefined from './predefined'; -export { - CompoundConditionProps, - CompoundConditionType, -} from './base/compound-condition'; -export { Condition, ConditionProps } from './condition'; -export { ConditionExpression, ConditionExpressionJSON } from './condition-expr'; -export { ConditionContext, CustomContextParam } from './context'; +export * as compound from './compound-condition'; +export * as condition from './condition'; +export * as conditionExpr from './condition-expr'; +export * as context from './context'; export { base, predefined }; diff --git a/packages/taco/src/conditions/predefined/index.ts b/packages/taco/src/conditions/predefined/index.ts index 2c08b2da4..14e93c604 100644 --- a/packages/taco/src/conditions/predefined/index.ts +++ b/packages/taco/src/conditions/predefined/index.ts @@ -1 +1 @@ -export { ERC721Balance, ERC721Ownership } from './erc721'; +export * as erc721 from './erc721'; diff --git a/packages/taco/src/taco.ts b/packages/taco/src/taco.ts index a9d9054b2..011e289d2 100644 --- a/packages/taco/src/taco.ts +++ b/packages/taco/src/taco.ts @@ -16,11 +16,9 @@ import { import { ethers } from 'ethers'; import { keccak256 } from 'ethers/lib/utils'; -import { - Condition, - ConditionExpression, - CustomContextParam, -} from './conditions'; +import { Condition } from './conditions/condition'; +import { ConditionExpression } from './conditions/condition-expr'; +import { CustomContextParam } from './conditions/context'; import { DkgClient } from './dkg'; import { retrieveAndDecrypt } from './tdec'; diff --git a/packages/taco/src/tdec.ts b/packages/taco/src/tdec.ts index 642506c67..3f55e7c99 100644 --- a/packages/taco/src/tdec.ts +++ b/packages/taco/src/tdec.ts @@ -23,11 +23,8 @@ import { import { ethers } from 'ethers'; import { arrayify, keccak256 } from 'ethers/lib/utils'; -import { - ConditionContext, - ConditionExpression, - CustomContextParam, -} from './conditions'; +import { ConditionExpression } from './conditions/condition-expr'; +import { ConditionContext, CustomContextParam } from './conditions/context'; const ERR_DECRYPTION_FAILED = (errors: unknown) => `Threshold of responses not met; TACo decryption failed with errors: ${JSON.stringify( diff --git a/packages/taco/test/conditions/base/condition.test.ts b/packages/taco/test/conditions/base/condition.test.ts index ee1aaf5a8..b2052d791 100644 --- a/packages/taco/test/conditions/base/condition.test.ts +++ b/packages/taco/test/conditions/base/condition.test.ts @@ -1,9 +1,9 @@ import { TEST_CONTRACT_ADDR } from '@nucypher/test-utils'; import { describe, expect, it } from 'vitest'; -import { Condition } from '../../../src/conditions'; import { ContractCondition } from '../../../src/conditions/base'; -import { ERC721Ownership } from '../../../src/conditions/predefined'; +import { Condition } from '../../../src/conditions/condition'; +import { ERC721Ownership } from '../../../src/conditions/predefined/erc721'; import { fakeCondition, testContractConditionObj } from '../../test-utils'; describe('validation', () => { diff --git a/packages/taco/test/conditions/base/contract.test.ts b/packages/taco/test/conditions/base/contract.test.ts index afcb2a95b..4f9236f75 100644 --- a/packages/taco/test/conditions/base/contract.test.ts +++ b/packages/taco/test/conditions/base/contract.test.ts @@ -2,10 +2,6 @@ import { initialize } from '@nucypher/nucypher-core'; import { fakeProvider, fakeSigner } from '@nucypher/test-utils'; import { beforeAll, describe, expect, it } from 'vitest'; -import { - ConditionExpression, - CustomContextParam, -} from '../../../src/conditions'; import { ContractCondition } from '../../../src/conditions/base'; import { ContractConditionProps, @@ -13,7 +9,9 @@ import { ContractConditionType, FunctionAbiProps, } from '../../../src/conditions/base/contract'; +import { ConditionExpression } from '../../../src/conditions/condition-expr'; import { USER_ADDRESS_PARAM } from '../../../src/conditions/const'; +import { CustomContextParam } from '../../../src/conditions/context'; import { testContractConditionObj, testFunctionAbi } from '../../test-utils'; describe('validation', () => { diff --git a/packages/taco/test/conditions/compound-condition.test.ts b/packages/taco/test/conditions/compound-condition.test.ts index 362ced81e..ee1ec09d0 100644 --- a/packages/taco/test/conditions/compound-condition.test.ts +++ b/packages/taco/test/conditions/compound-condition.test.ts @@ -9,7 +9,7 @@ import { import { compoundConditionSchema, CompoundConditionType, -} from '../../src/conditions/base/compound-condition'; +} from '../../src/conditions/compound-condition'; import { testContractConditionObj, testRpcConditionObj, diff --git a/packages/taco/test/conditions/condition-expr.test.ts b/packages/taco/test/conditions/condition-expr.test.ts index 706dd8ee0..b64767eea 100644 --- a/packages/taco/test/conditions/condition-expr.test.ts +++ b/packages/taco/test/conditions/condition-expr.test.ts @@ -4,7 +4,6 @@ import { TEST_CHAIN_ID, TEST_CONTRACT_ADDR } from '@nucypher/test-utils'; import { SemVer } from 'semver'; import { beforeAll, describe, expect, it } from 'vitest'; -import { ConditionExpression } from '../../src/conditions'; import { CompoundCondition, ContractCondition, @@ -14,8 +13,9 @@ import { TimeCondition, TimeConditionProps, } from '../../src/conditions/base'; +import { ConditionExpression } from '../../src/conditions/condition-expr'; import { USER_ADDRESS_PARAM } from '../../src/conditions/const'; -import { ERC721Balance } from '../../src/conditions/predefined'; +import { ERC721Balance } from '../../src/conditions/predefined/erc721'; import { testContractConditionObj, testFunctionAbi, diff --git a/packages/taco/test/conditions/conditions.test.ts b/packages/taco/test/conditions/conditions.test.ts index f3499a69e..696fb413d 100644 --- a/packages/taco/test/conditions/conditions.test.ts +++ b/packages/taco/test/conditions/conditions.test.ts @@ -3,9 +3,9 @@ import { fakeProvider, fakeSigner } from '@nucypher/test-utils'; import { beforeAll, describe, expect, it } from 'vitest'; import { initialize } from '../../src'; -import { ConditionContext } from '../../src/conditions'; import { CompoundCondition } from '../../src/conditions/base'; import { SUPPORTED_CHAIN_IDS } from '../../src/conditions/const'; +import { ConditionContext } from '../../src/conditions/context'; describe('conditions', () => { beforeAll(async () => { diff --git a/packages/taco/test/conditions/context.test.ts b/packages/taco/test/conditions/context.test.ts index d7e62f7f1..187656ba2 100644 --- a/packages/taco/test/conditions/context.test.ts +++ b/packages/taco/test/conditions/context.test.ts @@ -4,16 +4,21 @@ import { ethers } from 'ethers'; import { beforeAll, describe, expect, it } from 'vitest'; import { toBytes, toHexString } from '../../src'; -import { ConditionExpression, CustomContextParam } from '../../src/conditions'; import { ContractCondition, ContractConditionProps, - ReturnValueTestProps, RpcCondition, } from '../../src/conditions/base'; -import { paramOrContextParamSchema } from '../../src/conditions/base/shared'; +import { + paramOrContextParamSchema, + ReturnValueTestProps, +} from '../../src/conditions/base/shared'; +import { ConditionExpression } from '../../src/conditions/condition-expr'; import { USER_ADDRESS_PARAM } from '../../src/conditions/const'; -import { RESERVED_CONTEXT_PARAMS } from '../../src/conditions/context/context'; +import { + CustomContextParam, + RESERVED_CONTEXT_PARAMS, +} from '../../src/conditions/context/context'; import { testContractConditionObj, testFunctionAbi, diff --git a/packages/taco/test/taco.test.ts b/packages/taco/test/taco.test.ts index f16740bed..2a289daed 100644 --- a/packages/taco/test/taco.test.ts +++ b/packages/taco/test/taco.test.ts @@ -28,7 +28,7 @@ import { // Shared test variables const message = 'this is a secret'; -const ownsNFT = new conditions.predefined.ERC721Ownership({ +const ownsNFT = new conditions.predefined.erc721.ERC721Ownership({ contractAddress: '0x1e988ba4692e52Bc50b375bcC8585b95c48AaD77', parameters: [3591], chain: 5, diff --git a/packages/taco/test/test-utils.ts b/packages/taco/test/test-utils.ts index 55c4592ed..f56ef6e56 100644 --- a/packages/taco/test/test-utils.ts +++ b/packages/taco/test/test-utils.ts @@ -34,7 +34,6 @@ import { } from '@nucypher/test-utils'; import { SpyInstance, vi } from 'vitest'; -import { ConditionExpression } from '../src/conditions'; import { ContractConditionProps, ContractConditionType, @@ -45,8 +44,9 @@ import { TimeConditionProps, TimeConditionType, } from '../src/conditions/base'; -import { TimeConditionMethod } from "../src/conditions/base/time"; -import { ERC721Balance } from '../src/conditions/predefined'; +import { TimeConditionMethod } from '../src/conditions/base/time'; +import { ConditionExpression } from '../src/conditions/condition-expr'; +import { ERC721Balance } from '../src/conditions/predefined/erc721'; import { DkgClient, DkgRitual } from '../src/dkg'; import { encryptMessage } from '../src/tdec'; From bf44f127b65ce56949c8140af6dd8034a2a55845 Mon Sep 17 00:00:00 2001 From: Piotr Roslaniec Date: Wed, 22 Nov 2023 11:51:52 +0100 Subject: [PATCH 6/7] fix after rebase --- demos/taco-demo/src/App.tsx | 2 +- demos/taco-demo/src/ConditionBuilder.tsx | 6 +++--- demos/taco-nft-demo/src/App.tsx | 2 +- demos/taco-nft-demo/src/NFTConditionBuilder.tsx | 6 +++--- packages/taco/examples/conditions.ts | 12 ++++++------ packages/taco/examples/context.ts | 2 +- packages/taco/examples/encrypt-decrypt.ts | 2 +- .../taco/test/conditions/compound-condition.test.ts | 2 +- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/demos/taco-demo/src/App.tsx b/demos/taco-demo/src/App.tsx index 0329df38b..c2bf54e9b 100644 --- a/demos/taco-demo/src/App.tsx +++ b/demos/taco-demo/src/App.tsx @@ -22,7 +22,7 @@ export default function App() { useEthers(); const [loading, setLoading] = useState(false); - const [condition, setCondition] = useState(); + const [condition, setCondition] = useState(); const [encryptedMessage, setEncryptedMessage] = useState(); const [decryptedMessage, setDecryptedMessage] = useState(); diff --git a/demos/taco-demo/src/ConditionBuilder.tsx b/demos/taco-demo/src/ConditionBuilder.tsx index 717ee6ebe..7ec498b20 100644 --- a/demos/taco-demo/src/ConditionBuilder.tsx +++ b/demos/taco-demo/src/ConditionBuilder.tsx @@ -3,8 +3,8 @@ import { Mumbai, useEthers } from '@usedapp/core'; import React, { useState } from 'react'; interface Props { - condition?: conditions.Condition | undefined; - setConditions: (value: conditions.Condition) => void; + condition?: conditions.condition.Condition | undefined; + setConditions: (value: conditions.condition.Condition) => void; enabled: boolean; } @@ -59,7 +59,7 @@ export const ConditionBuilder = ({ const onCreateCondition = (e: any) => { e.preventDefault(); - setConditions(conditions.Condition.fromObj(JSON.parse(conditionString))); + setConditions(conditions.condition.Condition.fromObj(JSON.parse(conditionString))); }; return ( diff --git a/demos/taco-nft-demo/src/App.tsx b/demos/taco-nft-demo/src/App.tsx index c4928f8e5..2fb5d26e4 100644 --- a/demos/taco-nft-demo/src/App.tsx +++ b/demos/taco-nft-demo/src/App.tsx @@ -22,7 +22,7 @@ export default function App() { useEthers(); const [loading, setLoading] = useState(false); - const [condition, setCondition] = useState(); + const [condition, setCondition] = useState(); const [encryptedMessage, setEncryptedMessage] = useState(); const [decryptedMessage, setDecryptedMessage] = useState(); diff --git a/demos/taco-nft-demo/src/NFTConditionBuilder.tsx b/demos/taco-nft-demo/src/NFTConditionBuilder.tsx index 5125d539a..d3c1eeeed 100644 --- a/demos/taco-nft-demo/src/NFTConditionBuilder.tsx +++ b/demos/taco-nft-demo/src/NFTConditionBuilder.tsx @@ -3,8 +3,8 @@ import { useEthers } from '@usedapp/core'; import React, { useState } from 'react'; interface Props { - condition?: conditions.Condition | undefined; - setConditions: (value: conditions.Condition) => void; + condition?: conditions.condition.Condition | undefined; + setConditions: (value: conditions.condition.Condition) => void; enabled: boolean; } @@ -49,7 +49,7 @@ export const NFTConditionBuilder = ({ const tokenIdInput = makeInput(setTokenId); const chainInput = makeChainInput(setChain, 5); - const makeCondition = (): conditions.Condition => { + const makeCondition = (): conditions.condition.Condition => { if (tokenId) { return new conditions.base.ContractCondition({ contractAddress, diff --git a/packages/taco/examples/conditions.ts b/packages/taco/examples/conditions.ts index f6eea7038..70734dae0 100644 --- a/packages/taco/examples/conditions.ts +++ b/packages/taco/examples/conditions.ts @@ -20,7 +20,7 @@ console.assert( 'ERC721Balance requires signer', ); -const ownsNFTRaw = new conditions.base.contract.ContractCondition({ +const ownsNFTRaw = new conditions.base.ContractCondition({ // Provided by the `predefined.ERC721Balance` method: 'balanceOf', parameters: [':userAddress'], @@ -38,7 +38,7 @@ console.assert( 'ContractCondition requires a signer', ); -const hasAnyNativeAsset = new conditions.base.rpc.RpcCondition({ +const hasAnyNativeAsset = new conditions.base.RpcCondition({ chain: 5, method: 'eth_getBalance', parameters: [':userAddress'], @@ -58,7 +58,7 @@ const ownsNFTOnChain5 = new conditions.predefined.erc721.ERC721Ownership({ chain: 5, // The first network we target }); -const hasAnyNativeAssetOnChain1 = new conditions.base.rpc.RpcCondition({ +const hasAnyNativeAssetOnChain1 = new conditions.base.RpcCondition({ chain: 1, // The second network we target method: 'eth_getBalance', parameters: [':userAddress'], @@ -68,7 +68,7 @@ const hasAnyNativeAssetOnChain1 = new conditions.base.rpc.RpcCondition({ }, }); -const multichainCondition = conditions.compound.CompoundCondition.and([ +const multichainCondition = conditions.base.CompoundCondition.and([ ownsNFTOnChain5, hasAnyNativeAssetOnChain1, ]); @@ -78,7 +78,7 @@ console.assert( 'CompoundCondition requires signer', ); -const myFunctionAbi: conditions.base.contract.FunctionAbiProps = { +const myFunctionAbi: conditions.base.FunctionAbiProps = { name: 'myFunction', type: 'function', stateMutability: 'view', @@ -103,7 +103,7 @@ const myFunctionAbi: conditions.base.contract.FunctionAbiProps = { ], }; -const myContractCallCondition = new conditions.base.contract.ContractCondition({ +const myContractCallCondition = new conditions.base.ContractCondition({ method: 'myFunctionAbi', // `myMethodAbi.name` parameters: [':userAddress', ':myCustomParam'], // `myMethodAbi.inputs` functionAbi: myFunctionAbi, diff --git a/packages/taco/examples/context.ts b/packages/taco/examples/context.ts index 136934de6..bbdf687e3 100644 --- a/packages/taco/examples/context.ts +++ b/packages/taco/examples/context.ts @@ -1,5 +1,5 @@ import { conditions } from '../src'; -import { CustomContextParam } from '../src/conditions'; +import { CustomContextParam } from '../src/conditions/context'; const ownsNFTRaw = new conditions.base.ContractCondition({ method: 'balanceOf', diff --git a/packages/taco/examples/encrypt-decrypt.ts b/packages/taco/examples/encrypt-decrypt.ts index 9f1154cd5..651697ea3 100644 --- a/packages/taco/examples/encrypt-decrypt.ts +++ b/packages/taco/examples/encrypt-decrypt.ts @@ -21,7 +21,7 @@ const run = async () => { // @ts-ignore const web3Provider = new ethers.providers.Web3Provider(window.ethereum); - const ownsNFT = new conditions.predefined.ERC721Ownership({ + const ownsNFT = new conditions.predefined.erc721.ERC721Ownership({ contractAddress: '0x1e988ba4692e52Bc50b375bcC8585b95c48AaD77', parameters: [3591], chain: 5, diff --git a/packages/taco/test/conditions/compound-condition.test.ts b/packages/taco/test/conditions/compound-condition.test.ts index ee1ec09d0..ed6fca7f6 100644 --- a/packages/taco/test/conditions/compound-condition.test.ts +++ b/packages/taco/test/conditions/compound-condition.test.ts @@ -1,12 +1,12 @@ import { describe, expect, it } from 'vitest'; -import { CompoundConditionProps } from '../../src/conditions'; import { CompoundCondition, ContractCondition, TimeCondition, } from '../../src/conditions/base'; import { + CompoundConditionProps, compoundConditionSchema, CompoundConditionType, } from '../../src/conditions/compound-condition'; From 447be5bee3249dcea001703df9eabccc948475d5 Mon Sep 17 00:00:00 2001 From: Piotr Roslaniec Date: Wed, 22 Nov 2023 15:05:13 +0100 Subject: [PATCH 7/7] refactor(conditions): move conditions to their respective modules --- demos/taco-demo/src/ConditionBuilder.tsx | 4 +- .../taco-nft-demo/src/NFTConditionBuilder.tsx | 4 +- examples/taco/nextjs/src/app/page.tsx | 2 +- examples/taco/nextjs/src/hooks/useTaco.ts | 2 +- examples/taco/nodejs/src/index.ts | 2 +- examples/taco/react/src/App.tsx | 2 +- examples/taco/webpack-5/src/index.ts | 2 +- packages/taco/examples/conditions.ts | 12 +-- packages/taco/examples/context.ts | 2 +- packages/taco/src/conditions/base/contract.ts | 12 ++- packages/taco/src/conditions/base/index.ts | 98 +------------------ packages/taco/src/conditions/base/rpc.ts | 16 ++- packages/taco/src/conditions/base/time.ts | 12 +++ .../taco/src/conditions/compound-condition.ts | 41 ++++++++ .../taco/src/conditions/condition-expr.ts | 3 +- .../taco/src/conditions/condition-factory.ts | 37 +++++++ packages/taco/src/conditions/condition.ts | 39 -------- packages/taco/src/conditions/const.ts | 2 + .../taco/src/conditions/context/context.ts | 3 +- packages/taco/src/conditions/index.ts | 1 + .../taco/src/conditions/predefined/erc721.ts | 2 +- .../taco/src/conditions/{base => }/shared.ts | 4 +- .../test/conditions/base/condition.test.ts | 2 +- .../test/conditions/base/contract.test.ts | 2 +- .../taco/test/conditions/base/rpc.test.ts | 2 +- .../taco/test/conditions/base/time.test.ts | 6 +- .../conditions/compound-condition.test.ts | 6 +- .../test/conditions/condition-expr.test.ts | 9 +- .../taco/test/conditions/conditions.test.ts | 2 +- packages/taco/test/conditions/context.test.ts | 18 ++-- packages/taco/test/test-utils.ts | 10 +- 31 files changed, 172 insertions(+), 187 deletions(-) create mode 100644 packages/taco/src/conditions/condition-factory.ts rename packages/taco/src/conditions/{base => }/shared.ts (94%) diff --git a/demos/taco-demo/src/ConditionBuilder.tsx b/demos/taco-demo/src/ConditionBuilder.tsx index 7ec498b20..e2c400545 100644 --- a/demos/taco-demo/src/ConditionBuilder.tsx +++ b/demos/taco-demo/src/ConditionBuilder.tsx @@ -8,7 +8,7 @@ interface Props { enabled: boolean; } -const rpcCondition = new conditions.base.RpcCondition({ +const rpcCondition = new conditions.base.rpc.RpcCondition({ chain: Mumbai.chainId, method: 'eth_getBalance', parameters: [':userAddress'], @@ -59,7 +59,7 @@ export const ConditionBuilder = ({ const onCreateCondition = (e: any) => { e.preventDefault(); - setConditions(conditions.condition.Condition.fromObj(JSON.parse(conditionString))); + setConditions(conditions.ConditionFactory.conditionFromProps(JSON.parse(conditionString))); }; return ( diff --git a/demos/taco-nft-demo/src/NFTConditionBuilder.tsx b/demos/taco-nft-demo/src/NFTConditionBuilder.tsx index d3c1eeeed..1c8ac1d0e 100644 --- a/demos/taco-nft-demo/src/NFTConditionBuilder.tsx +++ b/demos/taco-nft-demo/src/NFTConditionBuilder.tsx @@ -51,7 +51,7 @@ export const NFTConditionBuilder = ({ const makeCondition = (): conditions.condition.Condition => { if (tokenId) { - return new conditions.base.ContractCondition({ + return new conditions.base.contract.ContractCondition({ contractAddress, chain, standardContractType: 'ERC721', @@ -63,7 +63,7 @@ export const NFTConditionBuilder = ({ }, }); } - return new conditions.base.ContractCondition({ + return new conditions.base.contract.ContractCondition({ contractAddress, chain, standardContractType: 'ERC721', diff --git a/examples/taco/nextjs/src/app/page.tsx b/examples/taco/nextjs/src/app/page.tsx index b8555a762..a73fb6b9b 100644 --- a/examples/taco/nextjs/src/app/page.tsx +++ b/examples/taco/nextjs/src/app/page.tsx @@ -68,7 +68,7 @@ function App() { setEncrypting(true) try { const signer = provider.getSigner(); - const hasPositiveBalance = new conditions.base.RpcCondition({ + const hasPositiveBalance = new conditions.base.rpc.RpcCondition({ chain: 80001, method: 'eth_getBalance', parameters: [':userAddress', 'latest'], diff --git a/examples/taco/nextjs/src/hooks/useTaco.ts b/examples/taco/nextjs/src/hooks/useTaco.ts index be78a7bb5..376a80a62 100644 --- a/examples/taco/nextjs/src/hooks/useTaco.ts +++ b/examples/taco/nextjs/src/hooks/useTaco.ts @@ -43,7 +43,7 @@ export default function useTaco({ const encryptDataToBytes = useCallback( async ( message: string, - condition: conditions.Condition, + condition: conditions.condition.Condition, encryptorSigner: ethers.Signer, ) => { if (!isInit || !provider) return; diff --git a/examples/taco/nodejs/src/index.ts b/examples/taco/nodejs/src/index.ts index d7a472dc0..ce273075c 100644 --- a/examples/taco/nodejs/src/index.ts +++ b/examples/taco/nodejs/src/index.ts @@ -46,7 +46,7 @@ const encryptToBytes = async (messageString: string) => { const message = toBytes(messageString); console.log(format('Encrypting message ("%s") ...', messageString)); - const hasPositiveBalance = new conditions.base.RpcCondition({ + const hasPositiveBalance = new conditions.base.rpc.RpcCondition({ chain: 80001, method: 'eth_getBalance', parameters: [':userAddress', 'latest'], diff --git a/examples/taco/react/src/App.tsx b/examples/taco/react/src/App.tsx index fbb16b578..f5418eeb9 100644 --- a/examples/taco/react/src/App.tsx +++ b/examples/taco/react/src/App.tsx @@ -65,7 +65,7 @@ function App() { setEncrypting(true) try { const signer = provider.getSigner(); - const hasPositiveBalance = new conditions.base.RpcCondition({ + const hasPositiveBalance = new conditions.base.rpc.RpcCondition({ chain: 80001, method: 'eth_getBalance', parameters: [':userAddress', 'latest'], diff --git a/examples/taco/webpack-5/src/index.ts b/examples/taco/webpack-5/src/index.ts index 2e20132c5..1a05cae55 100644 --- a/examples/taco/webpack-5/src/index.ts +++ b/examples/taco/webpack-5/src/index.ts @@ -37,7 +37,7 @@ const runExample = async () => { console.log('Encrypting message...'); const message = toBytes('this is a secret'); - const hasPositiveBalance = new conditions.base.RpcCondition({ + const hasPositiveBalance = new conditions.base.rpc.RpcCondition({ chain: 80001, method: 'eth_getBalance', parameters: [':userAddress', 'latest'], diff --git a/packages/taco/examples/conditions.ts b/packages/taco/examples/conditions.ts index 70734dae0..f6eea7038 100644 --- a/packages/taco/examples/conditions.ts +++ b/packages/taco/examples/conditions.ts @@ -20,7 +20,7 @@ console.assert( 'ERC721Balance requires signer', ); -const ownsNFTRaw = new conditions.base.ContractCondition({ +const ownsNFTRaw = new conditions.base.contract.ContractCondition({ // Provided by the `predefined.ERC721Balance` method: 'balanceOf', parameters: [':userAddress'], @@ -38,7 +38,7 @@ console.assert( 'ContractCondition requires a signer', ); -const hasAnyNativeAsset = new conditions.base.RpcCondition({ +const hasAnyNativeAsset = new conditions.base.rpc.RpcCondition({ chain: 5, method: 'eth_getBalance', parameters: [':userAddress'], @@ -58,7 +58,7 @@ const ownsNFTOnChain5 = new conditions.predefined.erc721.ERC721Ownership({ chain: 5, // The first network we target }); -const hasAnyNativeAssetOnChain1 = new conditions.base.RpcCondition({ +const hasAnyNativeAssetOnChain1 = new conditions.base.rpc.RpcCondition({ chain: 1, // The second network we target method: 'eth_getBalance', parameters: [':userAddress'], @@ -68,7 +68,7 @@ const hasAnyNativeAssetOnChain1 = new conditions.base.RpcCondition({ }, }); -const multichainCondition = conditions.base.CompoundCondition.and([ +const multichainCondition = conditions.compound.CompoundCondition.and([ ownsNFTOnChain5, hasAnyNativeAssetOnChain1, ]); @@ -78,7 +78,7 @@ console.assert( 'CompoundCondition requires signer', ); -const myFunctionAbi: conditions.base.FunctionAbiProps = { +const myFunctionAbi: conditions.base.contract.FunctionAbiProps = { name: 'myFunction', type: 'function', stateMutability: 'view', @@ -103,7 +103,7 @@ const myFunctionAbi: conditions.base.FunctionAbiProps = { ], }; -const myContractCallCondition = new conditions.base.ContractCondition({ +const myContractCallCondition = new conditions.base.contract.ContractCondition({ method: 'myFunctionAbi', // `myMethodAbi.name` parameters: [':userAddress', ':myCustomParam'], // `myMethodAbi.inputs` functionAbi: myFunctionAbi, diff --git a/packages/taco/examples/context.ts b/packages/taco/examples/context.ts index bbdf687e3..73b684673 100644 --- a/packages/taco/examples/context.ts +++ b/packages/taco/examples/context.ts @@ -1,7 +1,7 @@ import { conditions } from '../src'; import { CustomContextParam } from '../src/conditions/context'; -const ownsNFTRaw = new conditions.base.ContractCondition({ +const ownsNFTRaw = new conditions.base.contract.ContractCondition({ method: 'balanceOf', parameters: [':userAddress'], standardContractType: 'ERC721', diff --git a/packages/taco/src/conditions/base/contract.ts b/packages/taco/src/conditions/base/contract.ts index 65ec9ee4f..d811d8f2c 100644 --- a/packages/taco/src/conditions/base/contract.ts +++ b/packages/taco/src/conditions/base/contract.ts @@ -1,10 +1,11 @@ import { ethers } from 'ethers'; import { z } from 'zod'; +import { Condition } from '../condition'; import { ETH_ADDRESS_REGEXP } from '../const'; +import { OmitConditionType, paramOrContextParamSchema } from '../shared'; import { rpcConditionSchema } from './rpc'; -import { paramOrContextParamSchema } from './shared'; // TODO: Consider replacing with `z.unknown`: // Since Solidity types are tied to Solidity version, we may not be able to accurately represent them in Zod. @@ -96,3 +97,12 @@ export const contractConditionSchema = rpcConditionSchema ); export type ContractConditionProps = z.infer; + +export class ContractCondition extends Condition { + constructor(value: OmitConditionType) { + super(contractConditionSchema, { + conditionType: ContractConditionType, + ...value, + }); + } +} diff --git a/packages/taco/src/conditions/base/index.ts b/packages/taco/src/conditions/base/index.ts index 3f50e5960..0364f1c1d 100644 --- a/packages/taco/src/conditions/base/index.ts +++ b/packages/taco/src/conditions/base/index.ts @@ -1,98 +1,6 @@ -import { - CompoundConditionProps, - compoundConditionSchema, - CompoundConditionType, -} from '../compound-condition'; -import { Condition, ConditionProps } from '../condition'; - -import { - ContractConditionProps, - contractConditionSchema, - ContractConditionType, -} from './contract'; -import { RpcConditionProps, rpcConditionSchema, RpcConditionType } from './rpc'; -import { - TimeConditionProps, - timeConditionSchema, - TimeConditionType, -} from './time'; - -type OmitConditionType = Omit; - // Exporting classes here instead of their respective schema files to // avoid circular dependency on Condition class. -type ConditionOrProps = Condition | ConditionProps; - -export class CompoundCondition extends Condition { - constructor(value: OmitConditionType) { - super(compoundConditionSchema, { - conditionType: CompoundConditionType, - ...value, - }); - } - - private static withOperator( - operands: ConditionOrProps[], - operator: 'or' | 'and' | 'not', - ): CompoundCondition { - const asObjects = operands.map((operand) => { - if (operand instanceof Condition) { - return operand.toObj(); - } - return operand; - }); - return new CompoundCondition({ - operator, - operands: asObjects, - }); - } - - public static or(conditions: ConditionOrProps[]): CompoundCondition { - return CompoundCondition.withOperator(conditions, 'or'); - } - - public static and(conditions: ConditionOrProps[]): CompoundCondition { - return CompoundCondition.withOperator(conditions, 'and'); - } - - public static not(condition: ConditionOrProps): CompoundCondition { - return CompoundCondition.withOperator([condition], 'not'); - } -} - -export class ContractCondition extends Condition { - constructor(value: OmitConditionType) { - super(contractConditionSchema, { - conditionType: ContractConditionType, - ...value, - }); - } -} - -export class RpcCondition extends Condition { - constructor(value: OmitConditionType) { - super(rpcConditionSchema, { - conditionType: RpcConditionType, - ...value, - }); - } -} - -export class TimeCondition extends Condition { - constructor(value: OmitConditionType) { - super(timeConditionSchema, { - conditionType: TimeConditionType, - ...value, - }); - } -} - -export { - ContractConditionType, - FunctionAbiProps, - type ContractConditionProps, -} from './contract'; -export { RpcConditionType, type RpcConditionProps } from './rpc'; -export { ReturnValueTestProps } from './shared'; -export { TimeConditionType, type TimeConditionProps } from './time'; +export * as contract from './contract'; +export * as rpc from './rpc'; +export * as time from './time'; diff --git a/packages/taco/src/conditions/base/rpc.ts b/packages/taco/src/conditions/base/rpc.ts index d7ca7a113..f84821743 100644 --- a/packages/taco/src/conditions/base/rpc.ts +++ b/packages/taco/src/conditions/base/rpc.ts @@ -1,13 +1,14 @@ import { z } from 'zod'; +import { Condition } from '../condition'; import { SUPPORTED_CHAIN_IDS } from '../const'; -import createUnionSchema from '../zod'; - import { EthAddressOrUserAddressSchema, + OmitConditionType, paramOrContextParamSchema, returnValueTestSchema, -} from './shared'; +} from '../shared'; +import createUnionSchema from '../zod'; export const RpcConditionType = 'rpc'; @@ -24,3 +25,12 @@ export const rpcConditionSchema = z.object({ }); export type RpcConditionProps = z.infer; + +export class RpcCondition extends Condition { + constructor(value: OmitConditionType) { + super(rpcConditionSchema, { + conditionType: RpcConditionType, + ...value, + }); + } +} diff --git a/packages/taco/src/conditions/base/time.ts b/packages/taco/src/conditions/base/time.ts index 7e43ae9a1..2e4739e19 100644 --- a/packages/taco/src/conditions/base/time.ts +++ b/packages/taco/src/conditions/base/time.ts @@ -1,5 +1,8 @@ import { z } from 'zod'; +import { Condition } from '../condition'; +import { OmitConditionType } from '../shared'; + import { rpcConditionSchema } from './rpc'; // TimeCondition is an RpcCondition with the method set to 'blocktime' and no parameters @@ -16,3 +19,12 @@ export const timeConditionSchema = z.object({ }); export type TimeConditionProps = z.infer; + +export class TimeCondition extends Condition { + constructor(value: OmitConditionType) { + super(timeConditionSchema, { + conditionType: TimeConditionType, + ...value, + }); + } +} diff --git a/packages/taco/src/conditions/compound-condition.ts b/packages/taco/src/conditions/compound-condition.ts index 772fa55e2..09c9186fd 100644 --- a/packages/taco/src/conditions/compound-condition.ts +++ b/packages/taco/src/conditions/compound-condition.ts @@ -3,6 +3,8 @@ import { z } from 'zod'; import { contractConditionSchema } from './base/contract'; import { rpcConditionSchema } from './base/rpc'; import { timeConditionSchema } from './base/time'; +import { Condition, ConditionProps } from './condition'; +import { OmitConditionType } from './shared'; export const CompoundConditionType = 'compound'; @@ -47,3 +49,42 @@ export const compoundConditionSchema: z.ZodSchema = z ); export type CompoundConditionProps = z.infer; + +export type ConditionOrProps = Condition | ConditionProps; + +export class CompoundCondition extends Condition { + constructor(value: OmitConditionType) { + super(compoundConditionSchema, { + conditionType: CompoundConditionType, + ...value, + }); + } + + private static withOperator( + operands: ConditionOrProps[], + operator: 'or' | 'and' | 'not', + ): CompoundCondition { + const asObjects = operands.map((operand) => { + if (operand instanceof Condition) { + return operand.toObj(); + } + return operand; + }); + return new CompoundCondition({ + operator, + operands: asObjects, + }); + } + + public static or(conditions: ConditionOrProps[]): CompoundCondition { + return CompoundCondition.withOperator(conditions, 'or'); + } + + public static and(conditions: ConditionOrProps[]): CompoundCondition { + return CompoundCondition.withOperator(conditions, 'and'); + } + + public static not(condition: ConditionOrProps): CompoundCondition { + return CompoundCondition.withOperator([condition], 'not'); + } +} diff --git a/packages/taco/src/conditions/condition-expr.ts b/packages/taco/src/conditions/condition-expr.ts index da2fa3c95..7b290008c 100644 --- a/packages/taco/src/conditions/condition-expr.ts +++ b/packages/taco/src/conditions/condition-expr.ts @@ -4,6 +4,7 @@ import { ethers } from 'ethers'; import { SemVer } from 'semver'; import { Condition } from './condition'; +import { ConditionFactory } from './condition-factory'; import { ConditionContext, CustomContextParam } from './context'; const ERR_VERSION = (provided: string, current: string) => @@ -43,7 +44,7 @@ export class ConditionExpression { throw new Error(ERR_CONDITION(obj.condition)); } - const condition = Condition.fromObj(obj.condition); + const condition = ConditionFactory.conditionFromProps(obj.condition); return new ConditionExpression(condition, obj.version); } diff --git a/packages/taco/src/conditions/condition-factory.ts b/packages/taco/src/conditions/condition-factory.ts new file mode 100644 index 000000000..93e34452f --- /dev/null +++ b/packages/taco/src/conditions/condition-factory.ts @@ -0,0 +1,37 @@ +import { + ContractCondition, + ContractConditionProps, + ContractConditionType, +} from './base/contract'; +import { RpcCondition, RpcConditionProps, RpcConditionType } from './base/rpc'; +import { + TimeCondition, + TimeConditionProps, + TimeConditionType, +} from './base/time'; +import { + CompoundCondition, + CompoundConditionProps, + CompoundConditionType, +} from './compound-condition'; +import { Condition, ConditionProps } from './condition'; + +const ERR_INVALID_CONDITION_TYPE = (type: string) => + `Invalid condition type: ${type}`; + +export class ConditionFactory { + public static conditionFromProps(props: ConditionProps): Condition { + switch (props.conditionType) { + case RpcConditionType: + return new RpcCondition(props as RpcConditionProps); + case TimeConditionType: + return new TimeCondition(props as TimeConditionProps); + case ContractConditionType: + return new ContractCondition(props as ContractConditionProps); + case CompoundConditionType: + return new CompoundCondition(props as CompoundConditionProps); + default: + throw new Error(ERR_INVALID_CONDITION_TYPE(props.conditionType)); + } + } +} diff --git a/packages/taco/src/conditions/condition.ts b/packages/taco/src/conditions/condition.ts index 0a9ae972a..88d19b9cb 100644 --- a/packages/taco/src/conditions/condition.ts +++ b/packages/taco/src/conditions/condition.ts @@ -1,22 +1,6 @@ import { objectEquals } from '@nucypher/shared'; import { z } from 'zod'; -import { - CompoundCondition, - ContractCondition, - ContractConditionProps, - ContractConditionType, - RpcCondition, - RpcConditionProps, - RpcConditionType, - TimeCondition, - TimeConditionProps, - TimeConditionType, -} from './base'; -import { - CompoundConditionProps, - CompoundConditionType, -} from './compound-condition'; import { USER_ADDRESS_PARAM } from './const'; type ConditionSchema = z.ZodSchema; @@ -24,25 +8,6 @@ export type ConditionProps = z.infer; const ERR_INVALID_CONDITION = (error: z.ZodError) => `Invalid condition: ${JSON.stringify(error.issues)}`; -const ERR_INVALID_CONDITION_TYPE = (type: string) => - `Invalid condition type: ${type}`; - -export class ConditionFactory { - public static conditionFromProps(obj: ConditionProps): Condition { - switch (obj.conditionType) { - case RpcConditionType: - return new RpcCondition(obj as RpcConditionProps); - case TimeConditionType: - return new TimeCondition(obj as TimeConditionProps); - case ContractConditionType: - return new ContractCondition(obj as ContractConditionProps); - case CompoundConditionType: - return new CompoundCondition(obj as CompoundConditionProps); - default: - throw new Error(ERR_INVALID_CONDITION_TYPE(obj.conditionType)); - } - } -} export class Condition { constructor( @@ -82,10 +47,6 @@ export class Condition { return data; } - public static fromObj(obj: ConditionProps): Condition { - return ConditionFactory.conditionFromProps(obj); - } - public equals(other: Condition) { return objectEquals(this.toObj(), other.toObj()); } diff --git a/packages/taco/src/conditions/const.ts b/packages/taco/src/conditions/const.ts index c7f80d5f0..e2d7e3dd9 100644 --- a/packages/taco/src/conditions/const.ts +++ b/packages/taco/src/conditions/const.ts @@ -16,3 +16,5 @@ export const SUPPORTED_CHAIN_IDS = [ ChainId.SEPOLIA, ChainId.ETHEREUM_MAINNET, ]; + +export const RESERVED_CONTEXT_PARAMS = [USER_ADDRESS_PARAM]; diff --git a/packages/taco/src/conditions/context/context.ts b/packages/taco/src/conditions/context/context.ts index bd2f1cb7c..bbd86872f 100644 --- a/packages/taco/src/conditions/context/context.ts +++ b/packages/taco/src/conditions/context/context.ts @@ -8,6 +8,7 @@ import { ConditionExpression } from '../condition-expr'; import { CONTEXT_PARAM_PREFIX, CONTEXT_PARAM_REGEXP, + RESERVED_CONTEXT_PARAMS, USER_ADDRESS_PARAM, } from '../const'; @@ -16,8 +17,6 @@ import { TypedSignature, WalletAuthenticationProvider } from './providers'; export type CustomContextParam = string | number | boolean; export type ContextParam = CustomContextParam | TypedSignature; -export const RESERVED_CONTEXT_PARAMS = [USER_ADDRESS_PARAM]; - const ERR_RESERVED_PARAM = (key: string) => `Cannot use reserved parameter name ${key} as custom parameter`; const ERR_INVALID_CUSTOM_PARAM = (key: string) => diff --git a/packages/taco/src/conditions/index.ts b/packages/taco/src/conditions/index.ts index b80c1ef39..320b52a68 100644 --- a/packages/taco/src/conditions/index.ts +++ b/packages/taco/src/conditions/index.ts @@ -4,5 +4,6 @@ import * as predefined from './predefined'; export * as compound from './compound-condition'; export * as condition from './condition'; export * as conditionExpr from './condition-expr'; +export { ConditionFactory } from './condition-factory'; export * as context from './context'; export { base, predefined }; diff --git a/packages/taco/src/conditions/predefined/erc721.ts b/packages/taco/src/conditions/predefined/erc721.ts index 940264f71..5e0e7f434 100644 --- a/packages/taco/src/conditions/predefined/erc721.ts +++ b/packages/taco/src/conditions/predefined/erc721.ts @@ -2,7 +2,7 @@ import { ContractCondition, ContractConditionProps, ContractConditionType, -} from '../base'; +} from '../base/contract'; import { USER_ADDRESS_PARAM } from '../const'; type ERC721OwnershipFields = 'contractAddress' | 'chain' | 'parameters'; diff --git a/packages/taco/src/conditions/base/shared.ts b/packages/taco/src/conditions/shared.ts similarity index 94% rename from packages/taco/src/conditions/base/shared.ts rename to packages/taco/src/conditions/shared.ts index 325abda27..da452cd1e 100644 --- a/packages/taco/src/conditions/base/shared.ts +++ b/packages/taco/src/conditions/shared.ts @@ -5,7 +5,7 @@ import { CONTEXT_PARAM_REGEXP, ETH_ADDRESS_REGEXP, USER_ADDRESS_PARAM, -} from '../const'; +} from './const'; export const contextParamSchema = z.string().regex(CONTEXT_PARAM_REGEXP); // We want to discriminate between ContextParams and plain strings @@ -41,3 +41,5 @@ export const EthAddressOrUserAddressSchema = z.union([ EthAddressSchema, UserAddressSchema, ]); + +export type OmitConditionType = Omit; diff --git a/packages/taco/test/conditions/base/condition.test.ts b/packages/taco/test/conditions/base/condition.test.ts index b2052d791..514d5b733 100644 --- a/packages/taco/test/conditions/base/condition.test.ts +++ b/packages/taco/test/conditions/base/condition.test.ts @@ -1,7 +1,7 @@ import { TEST_CONTRACT_ADDR } from '@nucypher/test-utils'; import { describe, expect, it } from 'vitest'; -import { ContractCondition } from '../../../src/conditions/base'; +import { ContractCondition } from '../../../src/conditions/base/contract'; import { Condition } from '../../../src/conditions/condition'; import { ERC721Ownership } from '../../../src/conditions/predefined/erc721'; import { fakeCondition, testContractConditionObj } from '../../test-utils'; diff --git a/packages/taco/test/conditions/base/contract.test.ts b/packages/taco/test/conditions/base/contract.test.ts index 4f9236f75..daedd95b1 100644 --- a/packages/taco/test/conditions/base/contract.test.ts +++ b/packages/taco/test/conditions/base/contract.test.ts @@ -2,8 +2,8 @@ import { initialize } from '@nucypher/nucypher-core'; import { fakeProvider, fakeSigner } from '@nucypher/test-utils'; import { beforeAll, describe, expect, it } from 'vitest'; -import { ContractCondition } from '../../../src/conditions/base'; import { + ContractCondition, ContractConditionProps, contractConditionSchema, ContractConditionType, diff --git a/packages/taco/test/conditions/base/rpc.test.ts b/packages/taco/test/conditions/base/rpc.test.ts index 449782275..58a8f8cb5 100644 --- a/packages/taco/test/conditions/base/rpc.test.ts +++ b/packages/taco/test/conditions/base/rpc.test.ts @@ -1,8 +1,8 @@ import { TEST_CONTRACT_ADDR } from '@nucypher/test-utils'; import { describe, expect, it } from 'vitest'; -import { RpcCondition } from '../../../src/conditions/base'; import { + RpcCondition, rpcConditionSchema, RpcConditionType, } from '../../../src/conditions/base/rpc'; diff --git a/packages/taco/test/conditions/base/time.test.ts b/packages/taco/test/conditions/base/time.test.ts index 0797cd138..7394346d9 100644 --- a/packages/taco/test/conditions/base/time.test.ts +++ b/packages/taco/test/conditions/base/time.test.ts @@ -1,15 +1,13 @@ import { describe, expect, it } from 'vitest'; import { - ReturnValueTestProps, TimeCondition, - TimeConditionProps, -} from '../../../src/conditions/base'; -import { TimeConditionMethod, + TimeConditionProps, timeConditionSchema, TimeConditionType, } from '../../../src/conditions/base/time'; +import { ReturnValueTestProps } from '../../../src/conditions/shared'; describe('validation', () => { const returnValueTest: ReturnValueTestProps = { diff --git a/packages/taco/test/conditions/compound-condition.test.ts b/packages/taco/test/conditions/compound-condition.test.ts index ed6fca7f6..7ef2f1c3d 100644 --- a/packages/taco/test/conditions/compound-condition.test.ts +++ b/packages/taco/test/conditions/compound-condition.test.ts @@ -1,11 +1,9 @@ import { describe, expect, it } from 'vitest'; +import { ContractCondition } from '../../src/conditions/base/contract'; +import { TimeCondition } from '../../src/conditions/base/time'; import { CompoundCondition, - ContractCondition, - TimeCondition, -} from '../../src/conditions/base'; -import { CompoundConditionProps, compoundConditionSchema, CompoundConditionType, diff --git a/packages/taco/test/conditions/condition-expr.test.ts b/packages/taco/test/conditions/condition-expr.test.ts index b64767eea..c68385cde 100644 --- a/packages/taco/test/conditions/condition-expr.test.ts +++ b/packages/taco/test/conditions/condition-expr.test.ts @@ -5,14 +5,15 @@ import { SemVer } from 'semver'; import { beforeAll, describe, expect, it } from 'vitest'; import { - CompoundCondition, ContractCondition, ContractConditionProps, - RpcCondition, - RpcConditionType, +} from '../../src/conditions/base/contract'; +import { RpcCondition, RpcConditionType } from '../../src/conditions/base/rpc'; +import { TimeCondition, TimeConditionProps, -} from '../../src/conditions/base'; +} from '../../src/conditions/base/time'; +import { CompoundCondition } from '../../src/conditions/compound-condition'; import { ConditionExpression } from '../../src/conditions/condition-expr'; import { USER_ADDRESS_PARAM } from '../../src/conditions/const'; import { ERC721Balance } from '../../src/conditions/predefined/erc721'; diff --git a/packages/taco/test/conditions/conditions.test.ts b/packages/taco/test/conditions/conditions.test.ts index 696fb413d..c27dd82f7 100644 --- a/packages/taco/test/conditions/conditions.test.ts +++ b/packages/taco/test/conditions/conditions.test.ts @@ -3,7 +3,7 @@ import { fakeProvider, fakeSigner } from '@nucypher/test-utils'; import { beforeAll, describe, expect, it } from 'vitest'; import { initialize } from '../../src'; -import { CompoundCondition } from '../../src/conditions/base'; +import { CompoundCondition } from '../../src/conditions/compound-condition'; import { SUPPORTED_CHAIN_IDS } from '../../src/conditions/const'; import { ConditionContext } from '../../src/conditions/context'; diff --git a/packages/taco/test/conditions/context.test.ts b/packages/taco/test/conditions/context.test.ts index 187656ba2..9a4b28229 100644 --- a/packages/taco/test/conditions/context.test.ts +++ b/packages/taco/test/conditions/context.test.ts @@ -7,18 +7,18 @@ import { toBytes, toHexString } from '../../src'; import { ContractCondition, ContractConditionProps, - RpcCondition, -} from '../../src/conditions/base'; -import { - paramOrContextParamSchema, - ReturnValueTestProps, -} from '../../src/conditions/base/shared'; +} from '../../src/conditions/base/contract'; +import { RpcCondition } from '../../src/conditions/base/rpc'; import { ConditionExpression } from '../../src/conditions/condition-expr'; -import { USER_ADDRESS_PARAM } from '../../src/conditions/const'; import { - CustomContextParam, RESERVED_CONTEXT_PARAMS, -} from '../../src/conditions/context/context'; + USER_ADDRESS_PARAM, +} from '../../src/conditions/const'; +import { CustomContextParam } from '../../src/conditions/context'; +import { + paramOrContextParamSchema, + ReturnValueTestProps, +} from '../../src/conditions/shared'; import { testContractConditionObj, testFunctionAbi, diff --git a/packages/taco/test/test-utils.ts b/packages/taco/test/test-utils.ts index f56ef6e56..1d711fe86 100644 --- a/packages/taco/test/test-utils.ts +++ b/packages/taco/test/test-utils.ts @@ -38,15 +38,19 @@ import { ContractConditionProps, ContractConditionType, FunctionAbiProps, - ReturnValueTestProps, +} from '../src/conditions/base/contract'; +import { RpcConditionProps, RpcConditionType, +} from '../src/conditions/base/rpc'; +import { + TimeConditionMethod, TimeConditionProps, TimeConditionType, -} from '../src/conditions/base'; -import { TimeConditionMethod } from '../src/conditions/base/time'; +} from '../src/conditions/base/time'; import { ConditionExpression } from '../src/conditions/condition-expr'; import { ERC721Balance } from '../src/conditions/predefined/erc721'; +import { ReturnValueTestProps } from '../src/conditions/shared'; import { DkgClient, DkgRitual } from '../src/dkg'; import { encryptMessage } from '../src/tdec';