jk_cosmwasm_std/entry_points.rs
1/// This macro generates the boilerplate required to call into the
2/// contract-specific logic from the entry-points to the Wasm module.
3///
4/// It should be invoked in a module scope(that is, not inside a function), and the argument to the macro
5/// should be the name of a second rust module that is imported in the invocation scope.
6/// The second module should export three functions with the following signatures:
7/// ```
8/// # use cosmwasm_std::{
9/// # Storage, Api, Querier, DepsMut, Deps, Env, StdError, MessageInfo,
10/// # Response, QueryResponse,
11/// # };
12/// #
13/// # type InstantiateMsg = ();
14/// pub fn instantiate(
15/// deps: DepsMut,
16/// env: Env,
17/// info: MessageInfo,
18/// msg: InstantiateMsg,
19/// ) -> Result<Response, StdError> {
20/// # Ok(Default::default())
21/// }
22///
23/// # type ExecuteMsg = ();
24/// pub fn execute(
25/// deps: DepsMut,
26/// env: Env,
27/// info: MessageInfo,
28/// msg: ExecuteMsg,
29/// ) -> Result<Response, StdError> {
30/// # Ok(Default::default())
31/// }
32///
33/// # type QueryMsg = ();
34/// pub fn query(
35/// deps: Deps,
36/// env: Env,
37/// msg: QueryMsg,
38/// ) -> Result<QueryResponse, StdError> {
39/// # Ok(Default::default())
40/// }
41/// ```
42/// where `InstantiateMsg`, `ExecuteMsg`, and `QueryMsg` are types that implement `DeserializeOwned + JsonSchema`.
43///
44/// # Example
45///
46/// ```ignore
47/// use contract; // The contract module
48///
49/// cosmwasm_std::create_entry_points!(contract);
50/// ```
51#[macro_export]
52macro_rules! create_entry_points {
53 (@migration; $contract:ident, true) => {
54 #[no_mangle]
55 extern "C" fn migrate(env_ptr: u32, msg_ptr: u32) -> u32 {
56 do_migrate(&$contract::migrate, env_ptr, msg_ptr)
57 }
58 };
59
60 (@migration; $contract:ident, false) => {};
61
62 (@inner; $contract:ident, migration = $migration:tt) => {
63 mod wasm {
64 use super::$contract;
65 use cosmwasm_std::{do_execute, do_instantiate, do_migrate, do_query};
66
67 #[no_mangle]
68 extern "C" fn instantiate(env_ptr: u32, info_ptr: u32, msg_ptr: u32) -> u32 {
69 do_instantiate(&$contract::instantiate, env_ptr, info_ptr, msg_ptr)
70 }
71
72 #[no_mangle]
73 extern "C" fn execute(env_ptr: u32, info_ptr: u32, msg_ptr: u32) -> u32 {
74 do_execute(&$contract::execute, env_ptr, info_ptr, msg_ptr)
75 }
76
77 #[no_mangle]
78 extern "C" fn query(env_ptr: u32, msg_ptr: u32) -> u32 {
79 do_query(&$contract::query, env_ptr, msg_ptr)
80 }
81
82 $crate::create_entry_points!(@migration; $contract, $migration);
83
84 // Other C externs like interface_version_7, allocate, deallocate are available
85 // automatically because we `use cosmwasm_std`.
86 }
87 };
88
89 ($contract:ident) => {
90 $crate::create_entry_points!(@inner; $contract, migration = false);
91 };
92}
93
94/// This macro is very similar to the `create_entry_points` macro, except it also requires the `migrate` method:
95/// ```
96/// # use cosmwasm_std::{
97/// # Storage, Api, Querier, DepsMut, Env, StdError, Response, MessageInfo,
98/// # };
99/// # type MigrateMsg = ();
100/// pub fn migrate(
101/// deps: DepsMut,
102/// _env: Env,
103/// msg: MigrateMsg,
104/// ) -> Result<Response, StdError> {
105/// # Ok(Default::default())
106/// }
107/// ```
108/// where `MigrateMsg` is a type that implements `DeserializeOwned + JsonSchema`.
109///
110/// # Example
111///
112/// ```ignore
113/// use contract; // The contract module
114///
115/// cosmwasm_std::create_entry_points_with_migration!(contract);
116/// ```
117#[macro_export]
118macro_rules! create_entry_points_with_migration {
119 ($contract:ident) => {
120 $crate::create_entry_points!(@inner; $contract, migration = true);
121 };
122}