-
Notifications
You must be signed in to change notification settings - Fork 375
Added entry_points!
macro to reduce boilerplate when writing contracts
#437
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1845a82
added `entry_points!` macro to reduce boilerplate when writing contracts
reuvenpo 9252844
different macros for contracts with and without migration
reuvenpo 30d2d73
added clarification about the custom types
reuvenpo 37fd053
fixed small textual error
reuvenpo e97feec
added examples to the macro docs
reuvenpo 0f33c07
fixed typos
reuvenpo 0ae13e8
`entry_points*` macros renamed to `create_entry_points*`
reuvenpo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,4 @@ | ||
pub mod contract; | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
mod wasm { | ||
use super::contract; | ||
use cosmwasm_std::{ | ||
do_handle, do_init, do_migrate, do_query, ExternalApi, ExternalQuerier, ExternalStorage, | ||
}; | ||
|
||
#[no_mangle] | ||
extern "C" fn init(env_ptr: u32, msg_ptr: u32) -> u32 { | ||
do_init( | ||
&contract::init::<ExternalStorage, ExternalApi, ExternalQuerier>, | ||
env_ptr, | ||
msg_ptr, | ||
) | ||
} | ||
|
||
#[no_mangle] | ||
extern "C" fn handle(env_ptr: u32, msg_ptr: u32) -> u32 { | ||
do_handle( | ||
&contract::handle::<ExternalStorage, ExternalApi, ExternalQuerier>, | ||
env_ptr, | ||
msg_ptr, | ||
) | ||
} | ||
|
||
#[no_mangle] | ||
extern "C" fn migrate(env_ptr: u32, msg_ptr: u32) -> u32 { | ||
do_migrate( | ||
&contract::migrate::<ExternalStorage, ExternalApi, ExternalQuerier>, | ||
env_ptr, | ||
msg_ptr, | ||
) | ||
} | ||
|
||
#[no_mangle] | ||
extern "C" fn query(msg_ptr: u32) -> u32 { | ||
do_query( | ||
&contract::query::<ExternalStorage, ExternalApi, ExternalQuerier>, | ||
msg_ptr, | ||
) | ||
} | ||
|
||
// Other C externs like cosmwasm_vm_version_1, allocate, deallocate are available | ||
// automatically because we `use cosmwasm_std`. | ||
} | ||
cosmwasm_std::entry_points_with_migration!(contract); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,4 @@ | ||
pub mod contract; | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
mod wasm { | ||
use super::contract; | ||
use cosmwasm_std::{ | ||
do_handle, do_init, do_query, ExternalApi, ExternalQuerier, ExternalStorage, | ||
}; | ||
|
||
#[no_mangle] | ||
extern "C" fn init(env_ptr: u32, msg_ptr: u32) -> u32 { | ||
do_init( | ||
&contract::init::<ExternalStorage, ExternalApi, ExternalQuerier>, | ||
env_ptr, | ||
msg_ptr, | ||
) | ||
} | ||
|
||
#[no_mangle] | ||
extern "C" fn handle(env_ptr: u32, msg_ptr: u32) -> u32 { | ||
do_handle( | ||
&contract::handle::<ExternalStorage, ExternalApi, ExternalQuerier>, | ||
env_ptr, | ||
msg_ptr, | ||
) | ||
} | ||
|
||
#[no_mangle] | ||
extern "C" fn query(msg_ptr: u32) -> u32 { | ||
do_query( | ||
&contract::query::<ExternalStorage, ExternalApi, ExternalQuerier>, | ||
msg_ptr, | ||
) | ||
} | ||
|
||
// Other C externs like cosmwasm_vm_version_1, allocate, deallocate are available | ||
// automatically because we `use cosmwasm_std`. | ||
} | ||
cosmwasm_std::entry_points!(contract); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/// This macro generates the boilerplate required to call into the | ||
/// contract-specific logic from the entry-points to the WASM module. | ||
/// | ||
/// It macro should be invoked in a global scope, and the argument to the macro | ||
reuvenpo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// should be the name of a rust module that is imported in the invocation scope. | ||
/// The module should export three functions with the following signatures: | ||
/// ``` | ||
/// # use cosmwasm_std::{ | ||
/// # Storage, Api, Querier, Extern, Env, StdResult, Binary, | ||
/// # InitResult, HandleResult, QueryResult, | ||
/// # }; | ||
/// # | ||
/// # type InitMsg = (); | ||
reuvenpo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// pub fn init<S: Storage, A: Api, Q: Querier>( | ||
/// deps: &mut Extern<S, A, Q>, | ||
/// env: Env, | ||
/// msg: InitMsg, | ||
/// ) -> InitResult { | ||
/// # Ok(Default::default()) | ||
/// } | ||
/// | ||
/// # type HandleMsg = (); | ||
/// pub fn handle<S: Storage, A: Api, Q: Querier>( | ||
/// deps: &mut Extern<S, A, Q>, | ||
/// env: Env, | ||
/// msg: HandleMsg, | ||
/// ) -> HandleResult { | ||
/// # Ok(Default::default()) | ||
/// } | ||
/// | ||
/// # type QueryMsg = (); | ||
/// pub fn query<S: Storage, A: Api, Q: Querier>( | ||
/// deps: &Extern<S, A, Q>, | ||
/// msg: QueryMsg, | ||
/// ) -> QueryResult { | ||
/// # Ok(Binary(Vec::new())) | ||
/// } | ||
/// ``` | ||
/// Where `InitMsg`, `HandleMsg`, and `QueryMsg` are types that implement `DeserializeOwned + JsonSchema` | ||
/// | ||
/// # Example | ||
/// | ||
/// ```ignore | ||
/// use contract; // The contract module | ||
/// | ||
/// cosmwasm_std::entry_points!(contract); | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! entry_points { | ||
reuvenpo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(@migration; $contract:ident, true) => { | ||
#[no_mangle] | ||
extern "C" fn migrate(env_ptr: u32, msg_ptr: u32) -> u32 { | ||
do_migrate( | ||
&$contract::migrate::<ExternalStorage, ExternalApi, ExternalQuerier>, | ||
env_ptr, | ||
msg_ptr, | ||
) | ||
} | ||
}; | ||
|
||
(@migration; $contract:ident, false) => {}; | ||
|
||
(@inner; $contract:ident, migration = $migration:tt) => { | ||
mod wasm { | ||
use super::$contract; | ||
use cosmwasm_std::{ | ||
do_handle, do_init, do_migrate, do_query, ExternalApi, ExternalQuerier, | ||
ExternalStorage, | ||
}; | ||
|
||
#[no_mangle] | ||
extern "C" fn init(env_ptr: u32, msg_ptr: u32) -> u32 { | ||
do_init( | ||
&$contract::init::<ExternalStorage, ExternalApi, ExternalQuerier>, | ||
env_ptr, | ||
msg_ptr, | ||
) | ||
} | ||
|
||
#[no_mangle] | ||
extern "C" fn handle(env_ptr: u32, msg_ptr: u32) -> u32 { | ||
do_handle( | ||
&$contract::handle::<ExternalStorage, ExternalApi, ExternalQuerier>, | ||
env_ptr, | ||
msg_ptr, | ||
) | ||
} | ||
|
||
#[no_mangle] | ||
extern "C" fn query(msg_ptr: u32) -> u32 { | ||
do_query( | ||
&$contract::query::<ExternalStorage, ExternalApi, ExternalQuerier>, | ||
msg_ptr, | ||
) | ||
} | ||
|
||
$crate::entry_points!(@migration; $contract, $migration); | ||
|
||
// Other C externs like cosmwasm_vm_version_1, allocate, deallocate are available | ||
// automatically because we `use cosmwasm_std`. | ||
} | ||
}; | ||
|
||
($contract:ident) => { | ||
$crate::entry_points!(@inner; $contract, migration = false); | ||
}; | ||
} | ||
|
||
/// This macro is very similar to the `entry_points` macro, except it also requires the `migrate` method: | ||
/// ``` | ||
/// # use cosmwasm_std::{ | ||
/// # Storage, Api, Querier, Extern, Env, StdResult, Binary, MigrateResult, | ||
/// # }; | ||
/// # type MigrateMsg = (); | ||
/// pub fn migrate<S: Storage, A: Api, Q: Querier>( | ||
/// deps: &mut Extern<S, A, Q>, | ||
/// _env: Env, | ||
/// msg: MigrateMsg, | ||
/// ) -> MigrateResult { | ||
/// # Ok(Default::default()) | ||
/// } | ||
/// ``` | ||
/// Where `MigrateMsg` is a type that implements `DeserializeOwned + JsonSchema` | ||
/// | ||
/// # Example | ||
/// | ||
/// ```ignore | ||
/// use contract; // The contract module | ||
/// | ||
/// cosmwasm_std::entry_points_with_migration!(contract); | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! entry_points_with_migration { | ||
($contract:ident) => { | ||
$crate::entry_points!(@inner; $contract, migration = true); | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
mod coins; | ||
mod encoding; | ||
mod entry_points; | ||
mod errors; | ||
mod init_handle; | ||
#[cfg(feature = "iterator")] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.