Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

ContractError: added ability to retrieve revert message from bytes #2515

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

TheBigBlase
Copy link

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

  • Added Tests
  • Added Documentation (inline rustdoc)
  • Breaking changes

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
Copy link
Collaborator

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

let mut bytes = bytes.as_slice();
let data_string: &[u8];

(_, bytes) = bytes.split_at(4); // first 4 bytes for function selector
Copy link
Collaborator

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]

@TheBigBlase
Copy link
Author

I'm not sure that i did it in the most idiomatic way, i mean i used return in the middle of a function.
Please let me know ; and thank you for taking some time looking into this

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants