Skip to content

Commit

Permalink
Validate functionAbi-related fields (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec authored Jun 14, 2023
2 parents aad7ebe + 4f32411 commit 7138a04
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 142 deletions.
35 changes: 34 additions & 1 deletion src/conditions/base/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,47 @@ import { RpcCondition, rpcConditionSchema } from './rpc';

export const STANDARD_CONTRACT_TYPES = ['ERC20', 'ERC721'];

const functionAbiVariable = Joi.object({
internalType: Joi.string().required(),
name: Joi.string().required(),
type: Joi.string().required(),
});

const functionAbiSchema = Joi.object({
name: Joi.string().required(),
type: Joi.string().valid('function').required(),
inputs: Joi.array().items(functionAbiVariable),
outputs: Joi.array().items(functionAbiVariable),
// TODO: Should we restrict this to 'view'?
// stateMutability: Joi.string().valid('view').required(),
}).custom((functionAbi, helper) => {
// Validate method name
const method = helper.state.ancestors[0].method;
if (functionAbi.name !== method) {
return helper.message({
custom: '"method" must be the same as "functionAbi.name"',
});
}

// Validate nr of parameters
const parameters = helper.state.ancestors[0].parameters;
if (functionAbi.inputs?.length !== parameters.length) {
return helper.message({
custom: '"parameters" must have the same length as "functionAbi.inputs"',
});
}

return functionAbi;
});

const contractMethodSchemas: Record<string, Joi.Schema> = {
...rpcConditionSchema,
contractAddress: Joi.string().pattern(ETH_ADDRESS_REGEXP).required(),
standardContractType: Joi.string()
.valid(...STANDARD_CONTRACT_TYPES)
.optional(),
method: Joi.string().required(),
functionAbi: Joi.object().optional(),
functionAbi: functionAbiSchema.optional(),
parameters: Joi.array().required(),
};

Expand Down
141 changes: 0 additions & 141 deletions test/unit/conditions/base/evm.test.ts

This file was deleted.

1 change: 1 addition & 0 deletions test/unit/conditions/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ describe('context parameters', () => {
...testContractConditionObj,
standardContractType: undefined, // We're going to use a custom function ABI
functionAbi: testFunctionAbi,
method: testFunctionAbi.name,
parameters: [USER_ADDRESS_PARAM, customParamKey], // We're going to use a custom parameter
returnValueTest: {
...testReturnValueTest,
Expand Down
3 changes: 3 additions & 0 deletions test/unit/testVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,19 @@ export const testFunctionAbi = {
type: 'function',
inputs: [
{
internalType: 'address',
name: 'account',
type: 'address',
},
{
internalType: 'uint256',
name: 'myCustomParam',
type: 'uint256',
},
],
outputs: [
{
internalType: 'uint256',
name: 'someValue',
type: 'uint256',
},
Expand Down

1 comment on commit 7138a04

@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/module/types/ethers-contracts/factories: 82.03 KB
build/module/types/ethers-contracts: 156.25 KB
build/module/types: 160.16 KB
build/module/src/agents: 31.25 KB
build/module/src/policies: 19.53 KB
build/module/src/conditions/base: 54.69 KB
build/module/src/conditions/predefined: 19.53 KB
build/module/src/conditions/context: 39.06 KB
build/module/src/conditions: 152.34 KB
build/module/src/sdk/strategy: 42.97 KB
build/module/src/sdk: 58.59 KB
build/module/src/characters: 89.84 KB
build/module/src/kits: 19.53 KB
build/module/src: 437.50 KB
build/module: 648.44 KB
build/main/types/ethers-contracts/factories: 82.03 KB
build/main/types/ethers-contracts: 156.25 KB
build/main/types: 160.16 KB
build/main/src/agents: 31.25 KB
build/main/src/policies: 19.53 KB
build/main/src/conditions/base: 54.69 KB
build/main/src/conditions/predefined: 19.53 KB
build/main/src/conditions/context: 39.06 KB
build/main/src/conditions: 152.34 KB
build/main/src/sdk/strategy: 42.97 KB
build/main/src/sdk: 58.59 KB
build/main/src/characters: 89.84 KB
build/main/src/kits: 19.53 KB
build/main/src: 441.41 KB
build/main: 652.34 KB
build: 1.27 MB

Please sign in to comment.