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

Reply dispatch generation #429

Closed
Tracked by #418
jawoznia opened this issue Aug 30, 2024 · 0 comments
Closed
Tracked by #418

Reply dispatch generation #429

jawoznia opened this issue Aug 30, 2024 · 0 comments
Labels
part Part of a bigger issue

Comments

@jawoznia
Copy link
Collaborator

jawoznia commented Aug 30, 2024

We keep the dispatching logic outside of the entry point and instead move it to a method like dispatch_reply() to provide reusability.

The method should match over every generated ID and provide additional checks for result of the submessage.
In case the handler for success or failure is not defined we will simply forward the result.

pub fn dispatch(
    deps: sylvia::cw_std::DepsMut<<Contract as sylvia::types::ContractApi>::CustomQuery>,
    env: sylvia::cw_std::Env,
    msg: sylvia::cw_std::Reply,
) -> std::result::Result<
    sylvia::cw_std::Response<<Contract as sylvia::types::ContractApi>::CustomMsg>,
    sylvia::cw_std::StdError,
> {
    let Reply {
        id,
        payload,
        gas_used,
        result,
    } = msg;

    match id {
        ADD_ADMIN_REPLY_ID => {
            match result {
                Ok(response) => {
                    let SubMsgResponse { events, data } = response;
                    let AddAdminReplyMsg { arg1, arg2 } = from_json(payload)?;
                    reply_success((deps, env, gas_used, events, data).into(), data.from_data(), arg1, arg2)
                },
                Err(error) => {
                    let AddAdminReplyMsg { arg1, arg2 } = from_json(payload)?;
                    reply_add_failure((deps, env, gas_used, vec![], vec![]).into(), error, arg1, arg2)
                }
        },
        UPDATE_ADMIN_REPLY_ID => {
            match result {
                Ok(response) => {
                    let SubMsgResponse { events, data } = response;
                    let UpdateAdminReplyMsg { arg1, arg2 } = from_json(payload)?;
                    reply_success((deps, env, gas_used, events, data).into(), data.from_data(), arg1, arg2)
                },
                Err(error) => {
                    Err(error)
                }
            }
        },
        REMOVE_ADMIN_REPLY_ID() => {
            let AddAdminReplyMsg { arg1, arg2 } = from_json(payload)?;
            reply_success((deps, env, gas_used, vec![], vec![]).into(), result, arg1, arg2)
        },
        _ => result.map(|_| Err(StdError::generic(format!("Unknown reply id: {}", id)))).map_err(Into::into),
    }
}

Note that above approach requires methods for a single identifier to have the same fields received from the Payload.
You can also define a single handler for an identifier-result pair.

For above to work we have to provide some new functionality.

@jawoznia jawoznia mentioned this issue Aug 30, 2024
12 tasks
@jawoznia jawoznia added the part Part of a bigger issue label Aug 30, 2024
jawoznia added a commit that referenced this issue Sep 10, 2024
Since this change would be breaking it's hidden behind `sv_replies`
feature flag.
jawoznia added a commit that referenced this issue Sep 10, 2024
Since this change would be breaking it's hidden behind `sv_replies`
feature flag.
jawoznia added a commit that referenced this issue Sep 12, 2024
Since this change would be breaking it's hidden behind `sv_replies`
feature flag.
jawoznia added a commit that referenced this issue Sep 13, 2024
Since this change would be breaking it's hidden behind `sv_replies`
feature flag.
jawoznia added a commit that referenced this issue Sep 18, 2024
Since this change would be breaking it's hidden behind `sv_replies`
feature flag.
jawoznia added a commit that referenced this issue Sep 18, 2024
Since this change would be breaking it's hidden behind `sv_replies`
feature flag.
jawoznia added a commit that referenced this issue Sep 19, 2024
Since this change would be breaking it's hidden behind `sv_replies`
feature flag.
@jawoznia jawoznia closed this as completed Oct 2, 2024
jawoznia added a commit that referenced this issue Nov 20, 2024
Since this change would be breaking it's hidden behind `sv_replies`
feature flag.
This was referenced Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
part Part of a bigger issue
Projects
None yet
Development

No branches or pull requests

1 participant