Skip to content
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

[Bug] Type paths lost when generating code with sol! macro via json abi #660

Closed
benluelo opened this issue Jun 13, 2024 · 1 comment · Fixed by #694
Closed

[Bug] Type paths lost when generating code with sol! macro via json abi #660

benluelo opened this issue Jun 13, 2024 · 1 comment · Fixed by #694
Labels
bug Something isn't working

Comments

@benluelo
Copy link

benluelo commented Jun 13, 2024

Component

json-abi, sol! macro

What version of Alloy are you on?

v0.7.6

Operating System

Linux

Describe the bug

If a struct is in a library, the library path is lost when generating code from the json abi.

Take the following solidity code:

library LibA {
    struct Struct {
        uint64 field64;
    }
}

library LibB {
    struct Struct {
        uint128 field128;
    }
}

contract Contract {
    LibA.Struct internal aValue;
    LibB.Struct internal bValue;

    constructor(
        LibA.Struct memory aValue_,
        LibB.Struct memory bValue_
    )
    {
        aValue = aValue_;
        bValue = bValue_;
    }

    function fn(
        LibA.Struct memory aValue_,
        LibB.Struct memory bValue_
    ) public
    {
        aValue = aValue_;
        bValue = bValue_;
    }
}

this generates the following (abridged) rust code:

/**
Generated by the following Solidity interface...
```solidity
interface Contract {
    struct Struct {
        uint64 field64;
    }

    constructor(Struct aValue_, Struct bValue_);

    function fn(Struct memory aValue_, Struct memory bValue_) external;
}
```
*/
pub mod Contract {
    use ::alloy_sol_types as alloy_sol_types;
    /**```solidity
    struct Struct { uint64 field64; }
    ```*/
    pub struct Struct {
        pub field64: u64,
    }
    /**Constructor`.
    ```solidity
    constructor(Struct aValue_, Struct bValue_);
    ```*/
    pub struct constructorCall {
        pub aValue_: <Struct as ::alloy_sol_types::SolType>::RustType,
        pub bValue_: <Struct as ::alloy_sol_types::SolType>::RustType,
    }
    /**Function with signature `fn((uint64),(uint64))` and selector `0xd7d61df0`.
    ```solidity
    function r#fn(Struct memory aValue_, Struct memory bValue_) external;
    ```*/
    pub struct fnCall {
        pub aValue_: <Struct as ::alloy_sol_types::SolType>::RustType,
        pub bValue_: <Struct as ::alloy_sol_types::SolType>::RustType,
    }
    pub struct fnReturn {}
    ///Container for all the [`Contract`](self) function calls.
    pub enum ContractCalls {
        r#fn(fnCall),
    }
}

LibA.Struct has "overwritten" LibB.Struct, and as such both constructor and fn have invalid signatures.

The contract qualifier is dropped here

Some(InternalType::Struct { contract: _, ty }) => {
self.0.insert(It::new(ty, ItKind::Struct(components)));
}
, this information should be passed through somehow.

@benluelo benluelo added the bug Something isn't working label Jun 13, 2024
@mattsse
Copy link
Member

mattsse commented Jun 25, 2024

@DaniPopes I struggled with the same issue on abigen as well...

I think we can solve this by

  • including the contract information in It to make them truly unique
  • find a way to name duplicated structs, in abigen I simply started numerating them, but we can also append the parent projection, although this can again lead to naming conflicts like A.B.C D.B.C, so this only works if we concat all parents...

however this does not solve naming entirely, because when we print params we now also now need to know the name of the type we just renamed

we also don't want to do this if there aren't any conflicts

so I guess we need some filtering when we do:

its.visit_abi(self);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants