Skip to content

Commit

Permalink
Resolved comments PR and updated unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
andresceballosm committed Jul 29, 2024
1 parent 9cc37e6 commit b731bf2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
50 changes: 22 additions & 28 deletions packages/taco/src/conditions/base/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ const functionAbiVariableSchema = z
})
.strict();

export const humanReadableAbiSchema = z
.string()
.refine(
(abi) => {
try {
toJsonAbiFormat(abi);
return true;
} catch (e) {
return false;
}
},
{
message: 'Invalid Human-Readable ABI format',
},
)
.transform(toJsonAbiFormat);

const functionAbiSchema = z
.object({
name: z.string(),
Expand Down Expand Up @@ -78,10 +95,9 @@ function toJsonAbiFormat(humanReadableAbi: string): any {
const fragment = ethers.utils.FunctionFragment.from(
abiWithoutFunctionKeyword,
);
const jsonAbi = JSON.parse(fragment.format(ethers.utils.FormatTypes.json));

delete jsonAbi.constant;
delete jsonAbi.payable;
const { constant, payable, ...jsonAbi } = JSON.parse(
fragment.format(ethers.utils.FormatTypes.json),
);

jsonAbi.inputs = jsonAbi.inputs.map((input: any) => ({
...input,
Expand All @@ -108,25 +124,7 @@ export const contractConditionSchema = rpcConditionSchema
standardContractType: z.enum(['ERC20', 'ERC721']).optional(),
method: z.string(),
functionAbi: z
.union([
functionAbiSchema,
z
.string()
.refine(
(abi) => {
try {
toJsonAbiFormat(abi);
return true;
} catch (e) {
return false;
}
},
{
message: 'Invalid Human-Readable ABI format',
},
)
.transform(toJsonAbiFormat),
])
.union([functionAbiSchema, humanReadableAbiSchema])
.optional(),
parameters: z.array(paramOrContextParamSchema),
})
Expand All @@ -143,12 +141,8 @@ export const contractConditionSchema = rpcConditionSchema
);

export type ContractConditionProps = z.infer<typeof contractConditionSchema>;
interface ContractConditionHumanReadableAbi extends ContractConditionProps {
functionAbi: string;
}

export class ContractCondition extends Condition {
constructor(value: OmitConditionType<ContractConditionHumanReadableAbi | ContractConditionProps>) {
constructor(value: OmitConditionType<ContractConditionProps>) {
if (typeof value.functionAbi === 'string') {
value.functionAbi = toJsonAbiFormat(value.functionAbi);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/taco/test/conditions/base/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
contractConditionSchema,
ContractConditionType,
FunctionAbiProps,
humanReadableAbiSchema,
} from '../../../src/conditions/base/contract';
import { ConditionExpression } from '../../../src/conditions/condition-expr';
import { USER_ADDRESS_PARAM } from '../../../src/conditions/const';
Expand Down Expand Up @@ -344,7 +345,14 @@ describe('supports custom function abi', () => {
functionAbi: humanReadableAbi,
method: 'balanceOf',
});
const invalidHumanReadableAbi = 'function invalidAbi';

expect(() =>
humanReadableAbiSchema.parse(humanReadableAbi),
).not.toThrow();
expect(() => humanReadableAbiSchema.parse(invalidHumanReadableAbi)).toThrow(
'Invalid Human-Readable ABI format',
);
expect(condition.value.functionAbi).toEqual({
name: 'balanceOf',
type: 'function',
Expand Down

0 comments on commit b731bf2

Please sign in to comment.