Skip to content

Commit

Permalink
Use abitypes for defining schema for functionAbi on ContractCondition.
Browse files Browse the repository at this point in the history
  • Loading branch information
derekpierre committed Oct 11, 2024
1 parent 5bac8b7 commit 152d37c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 50 deletions.
1 change: 1 addition & 0 deletions packages/taco/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@nucypher/nucypher-core": "*",
"@nucypher/shared": "workspace:*",
"@nucypher/taco-auth": "workspace:*",
"abitype": "^1.0.6",
"ethers": "*",
"semver": "^7.6.3",
"zod": "*"
Expand Down
27 changes: 3 additions & 24 deletions packages/taco/src/conditions/schemas/contract.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,17 @@
import { ETH_ADDRESS_REGEXP } from '@nucypher/shared';
import { ethers } from 'ethers';
import { z } from 'zod';
import { AbiParameter } from 'abitype/zod';

import { paramOrContextParamSchema } from './context';
import { rpcConditionSchema } from './rpc';

// 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.
// Alternatively, find a TS Solidity type lib.
const EthBaseTypes: [string, ...string[]] = [
'bool',
'string',
'address',
'address payable',
...Array.from({ length: 32 }, (_v, i) => `bytes${i + 1}`), // bytes1 through bytes32
'bytes',
...Array.from({ length: 32 }, (_v, i) => `uint${8 * (i + 1)}`), // uint8 through uint256
...Array.from({ length: 32 }, (_v, i) => `int${8 * (i + 1)}`), // int8 through int256
];

const functionAbiVariableSchema = z
.object({
name: z.string(),
type: z.enum(EthBaseTypes),
internalType: z.enum(EthBaseTypes), // TODO: Do we need to validate this?
})
.strict();

const functionAbiSchema = z
.object({
name: z.string(),
type: z.literal('function'),
inputs: z.array(functionAbiVariableSchema).min(0),
outputs: z.array(functionAbiVariableSchema).nonempty(),
inputs: z.array(AbiParameter).min(0),
outputs: z.array(AbiParameter).nonempty(),
stateMutability: z.union([z.literal('view'), z.literal('pure')]),
})
.strict()
Expand Down
102 changes: 76 additions & 26 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 152d37c

Please sign in to comment.