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 Sep 4, 2023
1 parent 04622a3 commit f0b0b56
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 81 deletions.
3 changes: 2 additions & 1 deletion src/agents/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ChainId, ChecksumAddress } from '../types';
import { ChecksumAddress } from '../types';
import { ChainId } from '../web3';

type Contracts = {
readonly SUBSCRIPTION_MANAGER: ChecksumAddress | undefined;
Expand Down
9 changes: 3 additions & 6 deletions src/conditions/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ export class TimeCondition extends Condition {
}
}

export {
contractConditionSchema,
type ContractConditionProps,
} from './contract';
export { rpcConditionSchema, type RpcConditionProps } from './rpc';
export { timeConditionSchema, type TimeConditionProps } from './time';
export { type ContractConditionProps } from './contract';
export { type RpcConditionProps } from './rpc';
export { type TimeConditionProps } from './time';
4 changes: 2 additions & 2 deletions src/conditions/base/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from 'zod';

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

import { EthAddressOrUserAddressSchema, returnValueTestSchema } from './shared';

Expand Down
9 changes: 2 additions & 7 deletions src/conditions/condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,12 @@ export class Condition {

public static validate(
schema: ConditionSchema,
value: ConditionProps,
override: Partial<ConditionProps> = {}
value: ConditionProps
): {
data?: ConditionProps;
error?: z.ZodError;
} {
const newValue = {
...value,
...override,
};
const result = schema.safeParse(newValue);
const result = schema.safeParse(value);
if (result.success) {
return { data: result.data };
}
Expand Down
9 changes: 9 additions & 0 deletions src/conditions/const.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import { ChainId } from '../web3';

export const USER_ADDRESS_PARAM = ':userAddress';

export const ETH_ADDRESS_REGEXP = new RegExp('^0x[a-fA-F0-9]{40}$');

export const SUPPORTED_CHAIN_IDS = [
ChainId.POLYGON,
ChainId.MUMBAI,
ChainId.GOERLI,
ChainId.MAINNET,
];
5 changes: 1 addition & 4 deletions src/conditions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@ export {
} from './condition-expr';
export { ConditionContext, type CustomContextParam } from './context';
export { Condition, type ConditionProps } from './condition';
export {
compoundConditionSchema,
type CompoundConditionProps,
} from './compound-condition';
export { type CompoundConditionProps } from './compound-condition';
File renamed without changes.
14 changes: 0 additions & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
export type ChecksumAddress = string;
export type HexEncodedBytes = string;
export type Base64EncodedBytes = string;

export enum ChainId {
POLYGON = 137,
MUMBAI = 80001,
GOERLI = 5,
MAINNET = 1,
}

export const SUPPORTED_CHAIN_IDS = [
ChainId.POLYGON,
ChainId.MUMBAI,
ChainId.GOERLI,
ChainId.MAINNET,
];
8 changes: 8 additions & 0 deletions src/web3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { fromHexString } from './utils';

export enum ChainId {
POLYGON = 137,
MUMBAI = 80001,
GOERLI = 5,
MAINNET = 1,
}

export const toCanonicalAddress = (address: string): Uint8Array => {
const ETH_ADDRESS_STRING_PREFIX = '0x';
const nonPrefixed = address.startsWith(ETH_ADDRESS_STRING_PREFIX)
Expand Down
39 changes: 0 additions & 39 deletions test/unit/conditions/base/condition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
import {
TEST_CHAIN_ID,
TEST_CONTRACT_ADDR,
TEST_CONTRACT_ADDR_2,
testContractConditionObj,
} from '../../testVariables';

Expand All @@ -22,44 +21,6 @@ describe('validation', () => {
expect(result.error).toBeUndefined();
expect(result.data.contractAddress).toEqual(TEST_CONTRACT_ADDR);
});

it('accepts on a valid value override', async () => {
const validOverride = {
chain: TEST_CHAIN_ID,
contractAddress: TEST_CONTRACT_ADDR_2,
};
const result = Condition.validate(
condition.schema,
condition.value,
validOverride
);
expect(result.error).toBeUndefined();
expect(result.data).toMatchObject(validOverride);
});

it('rejects on an invalid value override', async () => {
const invalidOverride = {
chain: -1,
contractAddress: TEST_CONTRACT_ADDR,
};
const result = Condition.validate(
condition.schema,
condition.value,
invalidOverride
);
expect(result.error).toBeDefined();
expect(result.data).toBeUndefined();
expect(result.error?.format()).toMatchObject({
chain: {
_errors: [
'Invalid literal value, expected 137',
'Invalid literal value, expected 80001',
'Invalid literal value, expected 5',
'Invalid literal value, expected 1',
],
},
});
});
});

describe('serialization', () => {
Expand Down
6 changes: 4 additions & 2 deletions test/unit/conditions/base/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {
import {
ContractCondition,
ContractConditionProps,
contractConditionSchema,
} from '../../../../src/conditions/base';
import { FunctionAbiProps } from '../../../../src/conditions/base/contract';
import {
contractConditionSchema,
FunctionAbiProps,
} from '../../../../src/conditions/base/contract';
import { USER_ADDRESS_PARAM } from '../../../../src/conditions/const';
import { fakeProvider, fakeSigner } from '../../../utils';
import { testContractConditionObj, testFunctionAbi } from '../../testVariables';
Expand Down
6 changes: 2 additions & 4 deletions test/unit/conditions/base/rpc.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
RpcCondition,
rpcConditionSchema,
} from '../../../../src/conditions/base';
import { RpcCondition } from '../../../../src/conditions/base';
import { rpcConditionSchema } from '../../../../src/conditions/base/rpc';
import { testRpcConditionObj } from '../../testVariables';

describe('validation', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/conditions/base/time.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
TimeCondition,
TimeConditionProps,
timeConditionSchema,
} from '../../../../src/conditions/base';
import { ReturnValueTestProps } from '../../../../src/conditions/base/shared';
import { timeConditionSchema } from '../../../../src/conditions/base/time';

describe('validation', () => {
const returnValueTest: ReturnValueTestProps = {
Expand Down
3 changes: 2 additions & 1 deletion test/unit/conditions/compound-condition.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { compoundConditionSchema, Condition } from '../../../src/conditions';
import { Condition } from '../../../src/conditions';
import { CompoundCondition } from '../../../src/conditions/base';
import { compoundConditionSchema } from '../../../src/conditions/compound-condition';
import {
testContractConditionObj,
testRpcConditionObj,
Expand Down
13 changes: 13 additions & 0 deletions test/unit/conditions/condition-expr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,19 @@ describe('condition set', () => {
}
);

it('rejects a mismatched condition type', () => {
const conditionObj = {
...testTimeConditionObj,
conditionType: 'rpc',
} as unknown as TimeConditionProps;
expect(() => {
ConditionExpression.fromObj({
version: ConditionExpression.VERSION,
condition: conditionObj,
});
}).toThrow(/^Invalid condition/);
});

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

Expand Down

0 comments on commit f0b0b56

Please sign in to comment.