Result<Response, ContractError> is the typical return type of entry point functions, with Response serving as a wrapper for Events in the Cosmos SDK.
The Response type should be returned as the successful result of a contract entry point, such as instantiate or execute. You can declare it as mutable and add to it in the function body, but a more common pattern is to construct it at the end and return it if all computations have been successful. In the examples that follow, Response is wrapped by Ok since it is being returned as part of a function that returns the Result type, with Response representing the Right or success branch.
The Query entry point is an exception, as it will return StdResult
You can review the source code for the Response struct here.
The most basic use of Response is as follows:
This is a typical scenario in instantiate functions when no message is returned to the client.
However, in most cases of handling execute, a Response should be returned:
let res = Response::new().add_attribute("action", "transfer").add_attribute("from", info.sender).add_attribute("to", recipient).add_attribute("amount", amount);Ok(res)
This code snippet involves several additional steps, so let's analyze it further. You can access the source code for this example by clicking here.
- A new Response is created
- Several key/value pairs are added to it
- The Response is returned wrapped in a Result type using Ok
When invoking your contract through the command-line interface (CLI), you will observe them logged as a part of the "raw_log" response, along with other SDK events.
Rather than solely appending attributes, add_event can be employed to add an unencapsulated event that can be interacted with by other clients or contracts.