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 Jun 28, 2023
1 parent 32e6a94 commit 37a2b72
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/conditions/base/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ const functionAbiSchema = Joi.object({
});
}

if (!asInterface.fragments) {
if (!asInterface.functions) {
return helper.message({
custom: '"functionAbi" is missing a function fragment',
});
}

if (asInterface.fragments.length > 1) {
if (Object.values(asInterface.functions).length !== 1) {
return helper.message({
custom: '"functionAbi" must contain exactly one function fragment',
});
Expand All @@ -40,12 +40,15 @@ const functionAbiSchema = Joi.object({
// Now we just need to validate against the parent schema
// Validate method name
const method = helper.state.ancestors[0].method;
const functionFragment = asInterface.fragments.filter(
(f) => f.name === method
)[0];
const abiMethodName = Object.keys(asInterface.functions).find((name) =>
name.startsWith(`${method}(`)
);
const functionFragment = abiMethodName
? asInterface.functions[abiMethodName]
: null;
if (!functionFragment) {
return helper.message({
custom: '"functionAbi" does not contain the method specified as "method"',
custom: `"functionAbi" not valid for method: "${method}"`,
});
}

Expand Down
107 changes: 106 additions & 1 deletion test/unit/conditions/base/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,117 @@ describe('supports custom function abi', () => {
const customParams: Record<string, CustomContextParam> = {};
customParams[myCustomParam] = 1234;

it('accepts custom function abi', async () => {
it('accepts custom function abi with a custom parameter', async () => {
const asJson = await conditionContext
.withCustomParams(customParams)
.toJson();
expect(asJson).toBeDefined();
expect(asJson).toContain(USER_ADDRESS_PARAM);
expect(asJson).toContain(myCustomParam);
});

it.each([
{
method: 'balanceOf',
functionAbi: {
name: 'balanceOf',
type: 'function',
inputs: [{ name: '_owner', type: 'address' }],
outputs: [{ name: 'balance', type: 'uint256' }],
stateMutability: 'view',
},
},
{
method: 'get',
functionAbi: {
name: 'get',
type: 'function',
inputs: [],
outputs: [],
stateMutability: 'pure',
},
},
])('accepts well-formed functionAbi', ({ method, functionAbi }) => {
expect(() =>
new ContractCondition({
...contractConditionObj,
parameters: functionAbi.inputs.map(
(input) => `fake_parameter_${input}`
), //
functionAbi,
method,
}).toObj()
).not.toThrow();
});

it.each([
{
method: '1234',
expectedError: '"functionAbi.name" must be a string',
functionAbi: {
name: 1234, // invalid value
type: 'function',
inputs: [{ name: '_owner', type: 'address' }],
outputs: [{ name: 'balance', type: 'uint256' }],
stateMutability: 'view',
},
},
{
method: 'transfer',
expectedError: '"functionAbi.inputs" must be an array',
functionAbi: {
name: 'transfer',
type: 'function',
inputs: 'invalid value', // invalid value
outputs: [{ name: '_status', type: 'bool' }],
stateMutability: 'pure',
},
},
{
method: 'get',
expectedError:
'"functionAbi.stateMutability" must be one of [view, pure]',
functionAbi: {
name: 'get',
type: 'function',
inputs: [],
outputs: [],
stateMutability: 'invalid', // invalid value
},
},
{
method: 'test',
expectedError: '"functionAbi.outputs" must be an array',
functionAbi: {
name: 'test',
type: 'function',
inputs: [],
outputs: 'invalid value', // Invalid value
stateMutability: 'pure',
},
},
{
method: 'calculatePow',
expectedError:
'Invalid condition: "parameters" must have the same length as "functionAbi.inputs"',
functionAbi: {
name: 'calculatePow',
type: 'function',
// 'inputs': [] // Missing inputs array
outputs: [{ name: 'result', type: 'uint256' }],
stateMutability: 'view',
},
},
])(
'rejects malformed functionAbi',
({ method, expectedError, functionAbi }) => {
expect(() =>
new ContractCondition({
...contractConditionObj,
functionAbi,
method,
}).toObj()
).toThrow(expectedError);
}
);
});

1 comment on commit 37a2b72

@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: 152.34 KB
build/module/types: 156.25 KB
build/module/test: 42.97 KB
build/module/src/sdk/strategy: 31.25 KB
build/module/src/sdk: 46.88 KB
build/module/src/characters: 89.84 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: 156.25 KB
build/module/src/agents: 35.16 KB
build/module/src/policies: 19.53 KB
build/module/src/kits: 19.53 KB
build/module/src: 433.59 KB
build/module: 687.50 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/test: 46.88 KB
build/main/src/sdk/strategy: 31.25 KB
build/main/src/sdk: 46.88 KB
build/main/src/characters: 89.84 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: 156.25 KB
build/main/src/agents: 35.16 KB
build/main/src/policies: 19.53 KB
build/main/src/kits: 19.53 KB
build/main/src: 437.50 KB
build/main: 695.31 KB
build: 1.35 MB

Please sign in to comment.