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

#[sv::messages] does not need generics to be forwarded to interfaces with associated types #350

Closed
kulikthebird opened this issue Apr 11, 2024 · 0 comments · Fixed by #361
Assignees
Labels

Comments

@kulikthebird
Copy link
Contributor

kulikthebird commented Apr 11, 2024

Let's take Sudo message generation as example:

pub enum ContractSudoMsg<SomeGeneric>
    where
        SomeGeneric: sylvia::types::CustomMsg,
    {
        InterfaceAssoc(
            <interface_assoc::sv::Api<
                SomeGeneric,
            > as sylvia::types::InterfaceApi>::Sudo,
        ),
        ContractGeneric(SudoMsg),
    }

In the code above the generics from sv::messages are passed to the Api structure in the contract main impl block by #[contract] macro.

Proposal

A new trait should be implemented by the #[inteface] macro:

mod some_interface {
    mod sv {
        // #[sylvia::interface] generates the new trait:
        pub trait InterfaceApi {
            type Sudo;
            type Exec;
            type Query;
        }

        // #[sylvia::interface] implements the above trait for every contract
        // that impelments given interface, in this example `SomeInterface`
        //
        impl<Contract: SomeInterface> InterfaceApi for Contract {
            type Sudo = SudoMsg<SomeInterface::SomeGeneric>
            // type Exec = ExecMsg<SomeInterface::SomeGenericForExec>
            // type Query = QueryMsg<SomeInterface::SomeGenericForQuery>
        }
    }
}

Now, the above example Sudo message could be transformed to something like the following:

pub enum ContractSudoMsg<Contract> 
where
    Contract: interface_assoc::sv::InterfaceApi,
    // etc. for all other sv::messages
{
    SomeInterface(
        <Contract as some_interface::sv::InterfaceApi>::Sudo,
    ),
    ContractGeneric(SudoMsg),
}

Summary:

  1. sylvia::types::InterfaceApi and some_interface::sv::Api should be removed.
  2. A trait InterfaceApi should be generated per interface in sv module.
  3. A new InterfaceApi is implemented for every contract that implements the given interface SomeInterface.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants