This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 795
ContractError: added ability to retrieve revert message from bytes #2515
Open
TheBigBlase
wants to merge
5
commits into
gakonst:master
Choose a base branch
from
TheBigBlase:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DaniPopes
suggested changes
Jul 25, 2023
ethers-contract/src/call.rs
Outdated
Comment on lines
119
to
126
let bytes = data.to_vec(); | ||
let mut bytes = bytes.as_slice(); | ||
let data_string: &[u8]; | ||
|
||
(_, bytes) = bytes.split_at(4); // first 4 bytes for function selector | ||
(_, bytes) = bytes.split_at(32); // next 32 for data offset | ||
(_, bytes) = bytes.split_at(32); // next 32 bytes for str len | ||
(data_string, _) = bytes.split_at(32); // remaining 32 for string data |
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.
You can just slice the Bytes
with data[100..]
, and you also have to add a length check to avoid panicking
ethers-contract/src/call.rs
Outdated
let mut bytes = bytes.as_slice(); | ||
let data_string: &[u8]; | ||
|
||
(_, bytes) = bytes.split_at(4); // first 4 bytes for function selector |
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.
Might be worth returning None
if the selector is not 08c379a0
(keccak256("Error(string)")
). You can achieve this by doing bytes[..4] != [0x08, 0xc3, 0x79, 0xa0]
I'm not sure that i did it in the most idiomatic way, i mean i used |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
This tries to retrieve revert data message from a ContractError from a JsonRpcError, if the RPC sends it.
this somewhat solves #2186 and completes #2392
Solution
We use the solidity revert model: https://docs.soliditylang.org/en/v0.8.12/control-structures.html#revert and parse the returned bytes accordingly.
I don't know if the rust compiler will make the bin call .split_at multiple times, so i left the code in the most readable state possible
PR Checklist