alloy-rs / alloy

Transports, Middleware, and Networks for the Alloy project
https://alloy.rs
Apache License 2.0
449 stars 148 forks source link

[Feature] Get abi error data from an error response on call / send transaction #787

Open rplusq opened 1 month ago

rplusq commented 1 month ago

Component

contract

Describe the feature you would like

Right now, assuming I have a contract Foo with a function bar, if it has an error, I have to do a lot of parsing / handling to get the abi error. I would like to have a more convenient function to handle this.

Example of how I'm doing it right now:

sol!(
    #[allow(missing_docs)]
    #[sol(rpc)]
    Foo,
    "abis/Foo.json"
);

let foo_contract = Foo::new(FOO_ADDRESS, &provider);

let tx_receipt = match foo_contract
        .bar()
        .from(signer)
        .send()
        .await
    {
        Ok(result) => result.get_receipt().await?,
        Err(error) => match error {
            Error::TransportError(TransportError::ErrorResp(err)) => {
                let data = err.data.unwrap_or_default();
                let data = data.get().trim_matches('"');
                let data = Bytes::from_str(data)?;
                let decoded_error = Foo::FooErrors::abi_decode(&data, true)?;

            }
            _ => {
                return Err(eyre::eyre!(
                    "Received TransportError with data: {:?}",
                    eyre::eyre!(error)
                ))
            }
        },
    };

Additional context

No response

prestwich commented 1 month ago

related logic from ethers-rs here

we essentially need to

rplusq commented 4 days ago

Starting to work on this