-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate functionAbi
using ethers
ABI parser
#228
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { ethers } from 'ethers'; | ||
import Joi from 'joi'; | ||
|
||
import { ETH_ADDRESS_REGEXP } from '../const'; | ||
|
@@ -6,31 +7,54 @@ import { RpcCondition, rpcConditionRecord } from './rpc'; | |
|
||
export const STANDARD_CONTRACT_TYPES = ['ERC20', 'ERC721']; | ||
|
||
const functionAbiVariable = Joi.object({ | ||
internalType: Joi.string(), // TODO is this needed? | ||
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(), | ||
inputs: Joi.array(), | ||
outputs: Joi.array(), | ||
stateMutability: Joi.string().valid('view', 'pure').required(), | ||
}).custom((functionAbi, helper) => { | ||
// Is `functionABI` a valid function fragment? | ||
let asInterface; | ||
try { | ||
asInterface = new ethers.utils.Interface([functionAbi]); | ||
} catch (e: unknown) { | ||
const { message } = e as Error; | ||
return helper.message({ | ||
custom: message, | ||
}); | ||
} | ||
|
||
if (!asInterface.functions) { | ||
return helper.message({ | ||
custom: '"functionAbi" is missing a function fragment', | ||
}); | ||
} | ||
|
||
if (Object.values(asInterface.functions).length !== 1) { | ||
return helper.message({ | ||
custom: '"functionAbi" must contain exactly one function fragment', | ||
}); | ||
} | ||
|
||
// Now we just need to validate against the parent schema | ||
// Validate method name | ||
const method = helper.state.ancestors[0].method; | ||
if (functionAbi.name !== method) { | ||
const abiMethodName = Object.keys(asInterface.functions).find((name) => | ||
name.startsWith(`${method}(`) | ||
); | ||
const functionFragment = abiMethodName | ||
? asInterface.functions[abiMethodName] | ||
: null; | ||
if (!functionFragment) { | ||
return helper.message({ | ||
custom: '"method" must be the same as "functionAbi.name"', | ||
custom: `"functionAbi" not valid for method: "${method}"`, | ||
}); | ||
} | ||
|
||
// Validate nr of parameters | ||
const parameters = helper.state.ancestors[0].parameters; | ||
if (functionAbi.inputs?.length !== parameters.length) { | ||
if (functionFragment.inputs.length !== parameters.length) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Nice |
||
return helper.message({ | ||
custom: '"parameters" must have the same length as "functionAbi.inputs"', | ||
}); | ||
|
@@ -39,7 +63,7 @@ const functionAbiSchema = Joi.object({ | |
return functionAbi; | ||
}); | ||
|
||
export const contractConditionRecord: Record<string, Joi.Schema> = { | ||
export const contractConditionRecord = { | ||
...rpcConditionRecord, | ||
contractAddress: Joi.string().pattern(ETH_ADDRESS_REGEXP).required(), | ||
standardContractType: Joi.string() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ import { | |
import { ContractCondition } from '../../../../src/conditions/base'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd love to see some comprehensive testing around validation of many different functionAbis, both valid and invalid, and correct processing/failure as part of this PR. |
||
import { USER_ADDRESS_PARAM } from '../../../../src/conditions/const'; | ||
import { fakeWeb3Provider } from '../../../utils'; | ||
import { testContractConditionObj } from '../../testVariables'; | ||
import { testContractConditionObj, testFunctionAbi } from '../../testVariables'; | ||
|
||
describe('validation', () => { | ||
it('accepts on a valid schema', () => { | ||
|
@@ -103,30 +103,10 @@ describe('accepts either standardContractType or functionAbi but not both or non | |
}); | ||
|
||
describe('supports custom function abi', () => { | ||
const fakeFunctionAbi = { | ||
name: 'myFunction', | ||
type: 'function', | ||
inputs: [ | ||
{ | ||
name: 'account', | ||
type: 'address', | ||
}, | ||
{ | ||
name: 'myCustomParam', | ||
type: 'uint256', | ||
}, | ||
], | ||
outputs: [ | ||
{ | ||
name: 'someValue', | ||
type: 'uint256', | ||
}, | ||
], | ||
}; | ||
const contractConditionObj = { | ||
...testContractConditionObj, | ||
standardContractType: undefined, | ||
functionAbi: fakeFunctionAbi, | ||
functionAbi: testFunctionAbi, | ||
method: 'myFunction', | ||
parameters: [USER_ADDRESS_PARAM, ':customParam'], | ||
returnValueTest: { | ||
|
@@ -143,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(() => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love this! 😍 |
||
new ContractCondition({ | ||
...contractConditionObj, | ||
functionAbi, | ||
method, | ||
}).toObj() | ||
).toThrow(expectedError); | ||
} | ||
); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be reduced to the following (https://docs.ethers.org/v5/api/utils/abi/interface/#Interface--fragments)?
It seems to possibly throw an
ArgumentError
, https://github.com/ethers-io/ethers.js/blob/master/packages/abi/src.ts/interface.ts#L193, but do we care?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes sense given our conversation about the ambiguity of functions. Fixed.