CosmWasm / sylvia

CosmWasm smart contract framework
Apache License 2.0
88 stars 14 forks source link

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

Closed kulikthebird closed 2 months ago

kulikthebird commented 3 months ago

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.