Skip to content

Commit

Permalink
apply pr suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Aug 30, 2023
1 parent e5b0976 commit fcc83eb
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 35 deletions.
24 changes: 5 additions & 19 deletions src/conditions/base/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
import { z } from 'zod';

import { ETH_ADDRESS_REGEXP, USER_ADDRESS_PARAM } from '../const';
import { SUPPORTED_CHAIN_IDS } from '../../types';
import createUnionSchema from '../../zod';

export const returnValueTestSchema = z.object({
index: z.number().optional(),
comparator: z.enum(['==', '>', '<', '>=', '<=', '!=']),
value: z.union([z.string(), z.number(), z.boolean()]),
});

export type ReturnValueTestProps = z.infer<typeof returnValueTestSchema>;

const EthAddressOrUserAddressSchema = z.array(
z.union([z.string().regex(ETH_ADDRESS_REGEXP), z.literal(USER_ADDRESS_PARAM)])
);
import { EthAddressOrUserAddressSchema, returnValueTestSchema } from './shared';

export const rpcConditionSchema = z.object({
conditionType: z.literal('rpc').default('rpc'),
chain: z.union([
z.literal(137),
z.literal(80001),
z.literal(5),
z.literal(1),
]),
chain: createUnionSchema(SUPPORTED_CHAIN_IDS),
method: z.enum(['eth_getBalance', 'balanceOf']),
parameters: EthAddressOrUserAddressSchema,
parameters: z.array(EthAddressOrUserAddressSchema),
returnValueTest: returnValueTestSchema,
});

Expand Down
18 changes: 18 additions & 0 deletions src/conditions/base/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { z } from 'zod';

import { ETH_ADDRESS_REGEXP, USER_ADDRESS_PARAM } from '../const';

export const returnValueTestSchema = z.object({
index: z.number().optional(),
comparator: z.enum(['==', '>', '<', '>=', '<=', '!=']),
value: z.unknown(),
});

export type ReturnValueTestProps = z.infer<typeof returnValueTestSchema>;

const EthAddressSchema = z.string().regex(ETH_ADDRESS_REGEXP);
const UserAddressSchema = z.literal(USER_ADDRESS_PARAM);
export const EthAddressOrUserAddressSchema = z.union([
EthAddressSchema,
UserAddressSchema,
]);
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ export enum ChainId {
GOERLI = 5,
MAINNET = 1,
}

export const SUPPORTED_CHAIN_IDS = [
ChainId.POLYGON,
ChainId.MUMBAI,
ChainId.GOERLI,
ChainId.MAINNET,
];
31 changes: 31 additions & 0 deletions src/zod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Primitive, z, ZodLiteral } from 'zod';

// Source: https://github.com/colinhacks/zod/issues/831#issuecomment-1063481764
const createUnion = <
T extends Readonly<[Primitive, Primitive, ...Primitive[]]>
>(
values: T
) => {
const zodLiterals = values.map((value) => z.literal(value)) as unknown as [
ZodLiteral<Primitive>,
ZodLiteral<Primitive>,
...ZodLiteral<Primitive>[]
];
return z.union(zodLiterals);
};

function createUnionSchema<T extends readonly Primitive[]>(values: T) {
if (values.length === 0) {
return z.never();
}

if (values.length === 1) {
return z.literal(values[0]);
}

return createUnion(
values as unknown as Readonly<[Primitive, Primitive, ...Primitive[]]>
);
}

export default createUnionSchema;
2 changes: 1 addition & 1 deletion test/unit/conditions/base/time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
TimeCondition,
TimeConditionProps,
} from '../../../../src/conditions/base';
import { ReturnValueTestProps } from '../../../../src/conditions/base/rpc';
import { ReturnValueTestProps } from '../../../../src/conditions/base/shared';

describe('validation', () => {
const returnValueTest: ReturnValueTestProps = {
Expand Down
45 changes: 31 additions & 14 deletions test/unit/conditions/condition-expr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ContractConditionProps,
RpcCondition,
TimeCondition,
TimeConditionProps,
} from '../../../src/conditions/base';
import { USER_ADDRESS_PARAM } from '../../../src/conditions/const';
import { ERC721Balance } from '../../../src/conditions/predefined';
Expand Down Expand Up @@ -66,7 +67,7 @@ describe('condition set', () => {
describe('equality', () => {
const conditionExprCurrentVersion = new ConditionExpression(rpcCondition);

it('same version and condition', async () => {
it('same version and condition', () => {
const conditionExprSameCurrentVersion = new ConditionExpression(
rpcCondition,
ConditionExpression.VERSION
Expand All @@ -76,7 +77,7 @@ describe('condition set', () => {
).toBeTruthy();
});

it('different minor/patch version but same condition', async () => {
it('different minor/patch version but same condition', () => {
const conditionExprOlderMinorVersion = new ConditionExpression(
rpcCondition,
'0.1.0'
Expand All @@ -96,7 +97,7 @@ describe('condition set', () => {
).not.toBeTruthy();
});

it('minor/patch number greater than major; still older', async () => {
it('minor/patch number greater than major; still older', () => {
const conditionExprOlderMinorVersion = new ConditionExpression(
rpcCondition,
'0.9.0'
Expand Down Expand Up @@ -139,7 +140,7 @@ describe('condition set', () => {
contractConditionWithAbi,
timeCondition,
compoundCondition,
])('same version but different condition', async (condition) => {
])('same version but different condition', (condition) => {
const conditionExprSameVersionDifferentCondition =
new ConditionExpression(condition);
expect(
Expand All @@ -149,7 +150,7 @@ describe('condition set', () => {
).not.toBeTruthy();
});

it('same contract condition although using erc721 helper', async () => {
it('same contract condition although using erc721 helper', () => {
const erc721ConditionExpr = new ConditionExpression(
erc721BalanceCondition
);
Expand All @@ -172,7 +173,7 @@ describe('condition set', () => {
rpcCondition,
timeCondition,
compoundCondition,
])('serializes to and from json', async (condition) => {
])('serializes to and from json', (condition) => {
const conditionExpr = new ConditionExpression(condition);
const conditionExprJson = conditionExpr.toJson();
expect(conditionExprJson).toBeDefined();
Expand All @@ -187,7 +188,7 @@ describe('condition set', () => {
expect(conditionExprFromJson.equals(conditionExprFromJson)).toBeTruthy();
});

it('incompatible version', async () => {
it('incompatible version', () => {
const currentVersion = new SemVer(ConditionExpression.VERSION);
const invalidVersion = currentVersion.inc('major');
expect(() => {
Expand All @@ -202,7 +203,7 @@ describe('condition set', () => {

it.each(['version', 'x.y', 'x.y.z', '-1,0.0', '1.0.0.0.0.0.0'])(
'invalid versions',
async (invalidVersion) => {
(invalidVersion) => {
expect(() => {
ConditionExpression.fromObj({
version: invalidVersion,
Expand All @@ -212,7 +213,23 @@ describe('condition set', () => {
}
);

it('erc721 condition serialization', async () => {
it.each(['_invalid_condition_type_', undefined as unknown as string])(
'rejects an invalid condition type',
(invalidConditionType) => {
const conditionObj = {
...testTimeConditionObj,
conditionType: invalidConditionType,
} as unknown as TimeConditionProps;
expect(() => {
ConditionExpression.fromObj({
version: ConditionExpression.VERSION,
condition: conditionObj,
});
}).toThrow(`Invalid conditionType: ${invalidConditionType}`);
}
);

it('erc721 condition serialization', () => {
const conditionExpr = new ConditionExpression(erc721BalanceCondition);

const erc721BalanceConditionObj = erc721BalanceCondition.toObj();
Expand Down Expand Up @@ -240,7 +257,7 @@ describe('condition set', () => {
expect(conditionExprFromJson.condition).toBeInstanceOf(ContractCondition);
});

it('contract condition no abi serialization', async () => {
it('contract condition no abi serialization', () => {
const conditionExpr = new ConditionExpression(contractConditionNoAbi);

const conditionExprJson = conditionExpr.toJson();
Expand Down Expand Up @@ -272,7 +289,7 @@ describe('condition set', () => {
expect(conditionExprFromJson.condition).toBeInstanceOf(ContractCondition);
});

it('contract condition with abi serialization', async () => {
it('contract condition with abi serialization', () => {
const conditionExpr = new ConditionExpression(contractConditionWithAbi);

const conditionExprJson = conditionExpr.toJson();
Expand Down Expand Up @@ -305,7 +322,7 @@ describe('condition set', () => {
expect(conditionExprFromJson.condition).toBeInstanceOf(ContractCondition);
});

it('time condition serialization', async () => {
it('time condition serialization', () => {
const conditionExpr = new ConditionExpression(timeCondition);

const conditionExprJson = conditionExpr.toJson();
Expand All @@ -328,7 +345,7 @@ describe('condition set', () => {
expect(conditionExprFromJson.condition).toBeInstanceOf(TimeCondition);
});

it('rpc condition serialization', async () => {
it('rpc condition serialization', () => {
const conditionExpr = new ConditionExpression(rpcCondition);

const conditionExprJson = conditionExpr.toJson();
Expand All @@ -352,7 +369,7 @@ describe('condition set', () => {
expect(conditionExprFromJson.condition).toBeInstanceOf(RpcCondition);
});

it('compound condition serialization', async () => {
it('compound condition serialization', () => {
const conditionExpr = new ConditionExpression(compoundCondition);
const compoundConditionObj = compoundCondition.toObj();

Expand Down
2 changes: 1 addition & 1 deletion test/unit/testVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
TimeConditionProps,
} from '../../src/conditions/base';
import { FunctionAbiProps } from '../../src/conditions/base/contract';
import { ReturnValueTestProps } from '../../src/conditions/base/rpc';
import { ReturnValueTestProps } from '../../src/conditions/base/shared';

export const aliceSecretKeyBytes = new Uint8Array([
55, 82, 190, 189, 203, 164, 60, 148, 36, 86, 46, 123, 63, 152, 215, 113, 174,
Expand Down

1 comment on commit fcc83eb

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bundled size for the package is listed below:

build/main/src/characters: 74.22 KB
build/main/src/kits: 19.53 KB
build/main/src/conditions/context: 42.97 KB
build/main/src/conditions/predefined: 19.53 KB
build/main/src/conditions/base: 54.69 KB
build/main/src/conditions: 164.06 KB
build/main/src/agents: 39.06 KB
build/main/src/sdk/strategy: 35.16 KB
build/main/src/sdk: 46.88 KB
build/main/src/policies: 19.53 KB
build/main/src: 449.22 KB
build/main/types/ethers-contracts/factories: 82.03 KB
build/main/types/ethers-contracts: 152.34 KB
build/main/types: 156.25 KB
build/main: 664.06 KB
build/module/src/characters: 74.22 KB
build/module/src/kits: 19.53 KB
build/module/src/conditions/context: 42.97 KB
build/module/src/conditions/predefined: 19.53 KB
build/module/src/conditions/base: 54.69 KB
build/module/src/conditions: 164.06 KB
build/module/src/agents: 39.06 KB
build/module/src/sdk/strategy: 31.25 KB
build/module/src/sdk: 42.97 KB
build/module/src/policies: 19.53 KB
build/module/src: 441.41 KB
build/module/types/ethers-contracts/factories: 82.03 KB
build/module/types/ethers-contracts: 152.34 KB
build/module/types: 156.25 KB
build/module: 656.25 KB
build: 1.29 MB

Please sign in to comment.