CosmWasm / sylvia

CosmWasm smart contract framework
Apache License 2.0
96 stars 16 forks source link

Sudo support #252

Closed jawoznia closed 8 months ago

jawoznia commented 12 months ago

Motivation

Since Sylvia supports CustomMsg and CustomQuery we should add support for sudo.

Generate SudoMsg

To allow sudo communication with a contract, first we have to generate the SudoMsg. It should be represented as and enum and concatenate messages as variants. Sudo should be supported by both interface and contract and generated as

pub enum SudoMsg {
    MoveFunds {
        recipient: String,
        amount: Vec<Coin>,
    }
}

Sudo in Interface

First step to provide support for the sudo would be to add support for it in interfaces. We should support below example and generate SudoMsg with two variants.

#[interface]
pub trait MyInterface {
    type Error: From<StdError>;

    #[msg(sudo)]
    fn move_funds(&self, ctx: SudoCtx, recipient: String, funds: Vec<Coin>) -> Result<Response, Self::Error>;

    #[msg(sudo)]
    fn send_funds(&self, ctx: SudoCtx, recipient: String, funds: Vec<Coin>) -> Result<Response, Self::Error>;
}

This should look the same as ExecMsg and QueyMsg so not much to be covered here.

Sudo in Contract

Same as in case of the Interface SudoMsg should be generated as ExecMsg and QueryMsg. There is however one additional thing to be generated which is ContractSudoMsg. It is needed as we have to be able to implement Interfaces on the Contracts and we want to be able to dispatch to proper handler.

Next to the generated messages we have to generate utils for handling the sudo in the MultiTest helpers.

fn move_funds(&self, recipient: String, funds: Vec<Coin>) -> AnyResult<AppResponse> {
    let msg = SudoMsg::MoveFunds(recipient, funds);
    (*self.app)
        .app_mut()
        .sudo(msg)
        .map_err(Into::into)
}

Sudo in Interface Implemenation

Interface implementation handles two things:

We don't have to update Querier generation, however sudo messages should be implemented on the interface::Proxy. At this point we should be able to have a Contract handling sudo messages from both itself and Interface and be able to test the behavior using MultiTest.

Coexistence with other features

We should make sure that sudo works with custom and generics. Adding variants to respective examples should confirm proper support for these features.

Override entry points example should also be updated with sudo variant. This way we can remove manually implemented message. This example should generate the SudoMsg and use the user defined entry point in the MultiTest helpers, specifically in cw_multi_test::Contract implementation on Contract.

Roadmap

Update docs

README and sylvia-book has to be updated with changes from this feature.

jawoznia commented 12 months ago

close #139

jawoznia commented 8 months ago

closed with #317