-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added automatic into for msgs (#389)
* Added automatic into for msgs * Fix impl into and move to into instead of from * Clippiedd * Ok, good * Added impl into deprecation * Added docs + chanelog
- Loading branch information
Showing
11 changed files
with
382 additions
and
85 deletions.
There are no files selected for viewing
This file contains 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 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 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,222 @@ | ||
use cosmwasm_schema::cw_serde; | ||
use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult}; | ||
use cw_orch::prelude::*; | ||
use msg::{ | ||
execute::{BaseExecMsg, BaseExecMsgFns as _, MintingExecMsg, MintingExecMsgFns as _}, | ||
query::{ | ||
BalanceResponse, BaseQueryMsg, BaseQueryMsgFns as _, MinterResponse, MintingQueryMsg, | ||
MintingQueryMsgFns as _, | ||
}, | ||
}; | ||
|
||
#[cw_orch::interface(Empty, ExecuteMsg, QueryMsg, Empty)] | ||
pub struct Cw20; | ||
|
||
#[cw_orch::interface(Empty, MintingExecMsg, Empty, Empty)] | ||
pub struct Cw20Minter; | ||
|
||
#[cw_orch::interface(Empty, BaseExecMsg, Empty, Empty)] | ||
pub struct Cw20Base; | ||
|
||
pub fn instantiate( | ||
_deps: DepsMut, | ||
_env: Env, | ||
_info: MessageInfo, | ||
_msg: Empty, | ||
) -> StdResult<Response> { | ||
Ok(Response::new().add_attribute("action", "instantiate")) | ||
} | ||
|
||
pub fn execute( | ||
_deps: DepsMut, | ||
_env: Env, | ||
_info: MessageInfo, | ||
_msg: ExecuteMsg, | ||
) -> StdResult<Response> { | ||
Ok(Response::new()) | ||
} | ||
|
||
pub fn minter_execute( | ||
_deps: DepsMut, | ||
_env: Env, | ||
_info: MessageInfo, | ||
_msg: MintingExecMsg, | ||
) -> StdResult<Response> { | ||
Ok(Response::new()) | ||
} | ||
|
||
pub fn base_execute( | ||
_deps: DepsMut, | ||
_env: Env, | ||
_info: MessageInfo, | ||
_msg: BaseExecMsg, | ||
) -> StdResult<Response> { | ||
Ok(Response::new()) | ||
} | ||
|
||
pub fn query(_deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> { | ||
match msg { | ||
QueryMsg::Minting(minting) => match minting { | ||
MintingQueryMsg::Minter {} => to_json_binary(&MinterResponse { | ||
minter: "minter".to_string(), | ||
}), | ||
}, | ||
QueryMsg::Base(base_msg) => match base_msg { | ||
BaseQueryMsg::Balance { address: _ } => to_json_binary(&BalanceResponse { | ||
balance: 167u128.into(), | ||
}), | ||
}, | ||
} | ||
} | ||
|
||
pub fn migrate(_deps: DepsMut, _env: Env, _msg: Empty) -> StdResult<Response> { | ||
Ok(Response::new()) | ||
} | ||
|
||
impl<Chain> Uploadable for Cw20<Chain> { | ||
/// Returns a CosmWasm contract wrapper | ||
fn wrapper() -> Box<dyn MockContract<Empty>> { | ||
Box::new(ContractWrapper::new_with_empty(execute, instantiate, query).with_migrate(migrate)) | ||
} | ||
} | ||
|
||
impl<Chain> Uploadable for Cw20Minter<Chain> { | ||
/// Returns a CosmWasm contract wrapper | ||
fn wrapper() -> Box<dyn MockContract<Empty>> { | ||
Box::new( | ||
ContractWrapper::new_with_empty(minter_execute, instantiate, query) | ||
.with_migrate(migrate), | ||
) | ||
} | ||
} | ||
|
||
impl<Chain> Uploadable for Cw20Base<Chain> { | ||
/// Returns a CosmWasm contract wrapper | ||
fn wrapper() -> Box<dyn MockContract<Empty>> { | ||
Box::new( | ||
ContractWrapper::new_with_empty(base_execute, instantiate, query).with_migrate(migrate), | ||
) | ||
} | ||
} | ||
|
||
pub fn main() -> anyhow::Result<()> { | ||
let mock = MockBech32::new("mock"); | ||
|
||
let contract = Cw20::new("cw20", mock.clone()); | ||
contract.upload()?; | ||
contract.instantiate(&Empty {}, None, None)?; | ||
|
||
contract.mint(150_100u128.into(), "nicoco".to_string())?; | ||
contract.send( | ||
150_100u128.into(), | ||
"nicoco".to_string(), | ||
Binary::from_base64("cXNk")?, | ||
)?; | ||
let minter_response = contract.minter()?; | ||
let balance = contract.balance("nicoco".to_string())?; | ||
assert_eq!(minter_response.minter, "minter"); | ||
assert_eq!(balance.balance.u128(), 167); | ||
|
||
let contract = Cw20Minter::new("cw20_minter", mock.clone()); | ||
contract.upload()?; | ||
contract.instantiate(&Empty {}, None, None)?; | ||
contract.mint(150_100u128.into(), "nicoco".to_string())?; | ||
|
||
let contract = Cw20Base::new("cw20_base", mock.clone()); | ||
contract.upload()?; | ||
contract.instantiate(&Empty {}, None, None)?; | ||
contract.send( | ||
150_100u128.into(), | ||
"nicoco".to_string(), | ||
Binary::from_base64("cXNk")?, | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cw_serde] | ||
pub enum ExecuteMsg { | ||
Minting(MintingExecMsg), | ||
Base(BaseExecMsg), | ||
} | ||
|
||
impl From<MintingExecMsg> for ExecuteMsg { | ||
fn from(value: MintingExecMsg) -> Self { | ||
Self::Minting(value) | ||
} | ||
} | ||
|
||
impl From<BaseExecMsg> for ExecuteMsg { | ||
fn from(value: BaseExecMsg) -> Self { | ||
Self::Base(value) | ||
} | ||
} | ||
|
||
#[cw_serde] | ||
pub enum QueryMsg { | ||
Minting(MintingQueryMsg), | ||
Base(BaseQueryMsg), | ||
} | ||
impl From<MintingQueryMsg> for QueryMsg { | ||
fn from(value: MintingQueryMsg) -> Self { | ||
Self::Minting(value) | ||
} | ||
} | ||
|
||
impl From<BaseQueryMsg> for QueryMsg { | ||
fn from(value: BaseQueryMsg) -> Self { | ||
Self::Base(value) | ||
} | ||
} | ||
|
||
mod msg { | ||
pub mod execute { | ||
|
||
use cosmwasm_schema::cw_serde; | ||
use cosmwasm_std::{Binary, Uint128}; | ||
|
||
#[cw_serde] | ||
#[derive(cw_orch::ExecuteFns)] | ||
pub enum MintingExecMsg { | ||
Mint { recipient: String, amount: Uint128 }, | ||
} | ||
|
||
#[cw_serde] | ||
#[derive(cw_orch::ExecuteFns)] | ||
pub enum BaseExecMsg { | ||
Send { | ||
contract: String, | ||
amount: Uint128, | ||
msg: Binary, | ||
}, | ||
} | ||
} | ||
|
||
pub mod query { | ||
|
||
use cosmwasm_schema::{cw_serde, QueryResponses}; | ||
use cosmwasm_std::Uint128; | ||
|
||
#[cw_serde] | ||
#[derive(QueryResponses, cw_orch::QueryFns)] | ||
pub enum MintingQueryMsg { | ||
#[returns(MinterResponse)] | ||
Minter {}, | ||
} | ||
|
||
#[cw_serde] | ||
#[derive(QueryResponses, cw_orch::QueryFns)] | ||
pub enum BaseQueryMsg { | ||
#[returns(BalanceResponse)] | ||
Balance { address: String }, | ||
} | ||
#[cw_serde] | ||
pub struct MinterResponse { | ||
pub minter: String, | ||
} | ||
#[cw_serde] | ||
pub struct BalanceResponse { | ||
pub balance: Uint128, | ||
} | ||
} | ||
} |
This file contains 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 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,50 @@ | ||
// ANCHOR: underlying_into | ||
use cw_orch::interface; | ||
use cw_orch::prelude::*; | ||
|
||
// An execute message that is generic. | ||
#[cosmwasm_schema::cw_serde] | ||
pub enum GenericExecuteMsg<T> { | ||
Generic(T), | ||
Nested(NestedMessageType), | ||
} | ||
|
||
// This is the message that will be used on our contract | ||
type ExecuteMsg = GenericExecuteMsg<Foo>; | ||
#[cosmwasm_schema::cw_serde] | ||
#[derive(cw_orch::ExecuteFns)] | ||
pub enum Foo { | ||
Bar { a: String }, | ||
} | ||
|
||
impl From<Foo> for ExecuteMsg { | ||
fn from(msg: Foo) -> Self { | ||
ExecuteMsg::Generic(msg) | ||
} | ||
} | ||
|
||
#[cosmwasm_schema::cw_serde] | ||
#[derive(cw_orch::ExecuteFns)] | ||
pub enum NestedMessageType { | ||
Test { b: u64 }, | ||
} | ||
|
||
impl From<NestedMessageType> for ExecuteMsg { | ||
fn from(msg: NestedMessageType) -> Self { | ||
ExecuteMsg::Nested(msg) | ||
} | ||
} | ||
|
||
#[interface(Empty, ExecuteMsg, Empty, Empty)] | ||
struct Example<Chain>; | ||
|
||
impl<Chain: CwEnv> Example<Chain> { | ||
pub fn test_macro(&self) { | ||
// function `bar` is available now! | ||
self.bar("hello".to_string()).unwrap(); | ||
|
||
// function `test` is available now! | ||
self.test(65u64).unwrap(); | ||
} | ||
} | ||
// ANCHOR_END: underlying_into |
This file contains 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
Oops, something went wrong.