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

Auto impl display for Abigen Event Structs #507

Closed
gakonst opened this issue Oct 15, 2021 · 5 comments · Fixed by #513 or #517
Closed

Auto impl display for Abigen Event Structs #507

gakonst opened this issue Oct 15, 2021 · 5 comments · Fixed by #513 or #517
Labels
enhancement New feature or request

Comments

@gakonst
Copy link
Owner

gakonst commented Oct 15, 2021

Is your feature request related to a problem? Please describe.

Right now, printing events in a nice human readable way requires matching on each event variant and then formatting the output. This is suboptimal because if there's a large amount of events, it means there's a lot of manual work to be done.

Relatedly, I can see the same issue arising on printing function args related to #506, so it might be helpful to tackle these 2 together.

Describe the solution you'd like

Define some encoding rules (e.g. all Bytes should be hex::encode'd before printing them) and implement Display for all event and function structs emitted by abigen.

@gakonst gakonst added the enhancement New feature or request label Oct 15, 2021
@mattsse
Copy link
Collaborator

mattsse commented Oct 15, 2021

it really boils down to rules for some Token https://docs.rs/ethabi/15.0.0/ethabi/token/enum.Token.html

so essefntially

  • bytes | address => 0x + hex
  • fixedbytes => as rust array [....]? or something like [fixed length; .......]

@gakonst
Copy link
Owner Author

gakonst commented Oct 15, 2021

Maybe erring on the side of simplicity / consistency:

  1. Numbers => decimal representations
  2. Anything bytes related -> 0x encoding
  3. Arrays (independently of fixed/variable length) -> [ <..>, <..> ] (where <..> is the inner type's Display impl)

@mattsse
Copy link
Collaborator

mattsse commented Oct 15, 2021

I suppose we should limit this to abigen! only and not generate Display when using EthAbiType and EthEvent directly, which could be unexpected and not intuitive.
Also, should we only print the events inputs or EventName(val1, val2, val3) style?

@gakonst
Copy link
Owner Author

gakonst commented Oct 16, 2021

My thinking was that theDisplay impl could be a comma separated list of val1,...val_n. We could make the Debug impl include the name perhaps if we wanted?

I think that doing it for EthAbiType/EthEvent also makes sense, so i'd be supportive of doing that too - unless you think that it's a footgun in any way

@mattsse
Copy link
Collaborator

mattsse commented Oct 16, 2021

I think we could go for an EthDisplay derive proc macro that would implement Display for the type.
By default abigen! would add this to all events, and for types a user could do

#[derive(Debug, Clone, PartialEq, EthAbiType, EthDisplay)]
struct ValueChanged {
    old_author: Address,
    new_author: Address,
}

assert_eq!("0x.....,0x....", format!("{}", ValueChanged{...}));

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
2 participants