-
Notifications
You must be signed in to change notification settings - Fork 23
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
Conversation
b0e00fe
to
a0bd371
Compare
a0bd371
to
32e6a94
Compare
Codecov Report
@@ Coverage Diff @@
## tdec-epic #228 +/- ##
=============================================
- Coverage 83.47% 83.24% -0.23%
=============================================
Files 37 37
Lines 968 979 +11
Branches 121 123 +2
=============================================
+ Hits 808 815 +7
- Misses 154 158 +4
Partials 6 6
|
Bundled size for the package is listed below: build/module/types/ethers-contracts/factories: 82.03 KB |
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.
Great change
src/conditions/base/contract.ts
Outdated
}); | ||
} | ||
|
||
if (!asInterface.fragments) { |
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.
Should we be using .functions
instead of .fragments
? Just reading from here - https://docs.ethers.org/v5/api/utils/abi/interface/#Interface--properties.
Same for various logic below.
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.
✔️
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 initially avoided that since the keys in .functions
are function signatures, not function names.
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.
Where are the key values used? L44?
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.
Yes, we use them to fetch the FunctionFragments
src/conditions/base/contract.ts
Outdated
return helper.message({ | ||
custom: '"method" must be the same as "functionAbi.name"', | ||
custom: '"functionAbi" does not contain the method specified as "method"', |
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.
Can we output the actual method name. So something like (?):
custom: '"functionAbi" does not contain the method specified as "method"', | |
custom: `"functionAbi" not valid for method, "${method}"`, |
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.
✔️
}); | ||
} | ||
|
||
// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Nice
@@ -7,7 +7,7 @@ import { | |||
import { ContractCondition } from '../../../../src/conditions/base'; |
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'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.
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.
See comment about testing.
6b16a1b
to
37a2b72
Compare
Bundled size for the package is listed below: build/module/types/ethers-contracts/factories: 82.03 KB |
1 similar comment
Bundled size for the package is listed below: build/module/types/ethers-contracts/factories: 82.03 KB |
])( | ||
'rejects malformed functionAbi', | ||
({ method, expectedError, functionAbi }) => { | ||
expect(() => |
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.
Love this! 😍
src/conditions/base/contract.ts
Outdated
const abiMethodName = Object.keys(asInterface.functions).find((name) => | ||
name.startsWith(`${method}(`) | ||
); | ||
const functionFragment = abiMethodName | ||
? asInterface.functions[abiMethodName] | ||
: null; |
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)?
const abiMethodName = Object.keys(asInterface.functions).find((name) => | |
name.startsWith(`${method}(`) | |
); | |
const functionFragment = abiMethodName | |
? asInterface.functions[abiMethodName] | |
: null; | |
const functionFragment = asInterface.getFunction(method); |
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.
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.
🎸 - good stuff. Minor nitpick suggestion.
Co-authored-by: Derek Pierre <derek.pierre@gmail.com>
Bundled size for the package is listed below: build/module/types/ethers-contracts/factories: 82.03 KB |
Type of PR:
Required reviews:
What this does:
ethers
to validatefunctionABI
Issues fixed/closed:
Why it's needed:
functionABI
, but it's still possible to pass mangled/illegal JSON ABI into your conditionsNotes for reviewers:
Joi
andethers
, and we have to implement some checks and balances ourselves on top of that.