Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
Implementation of app helper macros (#371)
Browse files Browse the repository at this point in the history
* First interface macro implementation

* formatting

* formatting

* Added app_messages macro

* Modified imports

* formatting

* Added interface definition

* Modified app-name

* add sudo endpoint to contract_wrapper

---------

Co-authored-by: cyberhoward <cyberhoward@protonmail.com>
Co-authored-by: CyberHoward <88450409+CyberHoward@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 14, 2023
1 parent 2bcc9e5 commit 1d9e1c5
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 24 deletions.
10 changes: 6 additions & 4 deletions packages/abstract-app/examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ pub struct CounterInitMsg;
#[cosmwasm_schema::cw_serde]
pub struct CounterExecMsg;

impl app::AppExecuteMsg for CounterExecMsg {}

#[cosmwasm_schema::cw_serde]
pub struct CounterQueryMsg;

impl app::AppQueryMsg for CounterQueryMsg {}

#[cosmwasm_schema::cw_serde]
pub struct CounterMigrateMsg;

Expand All @@ -27,6 +23,8 @@ pub struct CounterReceiveMsg;
#[cosmwasm_schema::cw_serde]
pub struct CounterSudoMsg;

abstract_app::app_messages!(CounterApp, CounterExecMsg, CounterQueryMsg);

use abstract_app::{AppContract, AppError};

use abstract_sdk::AbstractSdkError;
Expand Down Expand Up @@ -82,6 +80,10 @@ pub const COUNTER_APP: CounterApp = CounterApp::new(COUNTER_ID, APP_VERSION, Non
abstract_app::export_endpoints!(COUNTER_APP, CounterApp);
// ANCHOR_END: export

// ANCHOR: interface
abstract_app::create_interface!(COUNTER_APP, CounterApp);
// ANCHOR_END: interface

mod handlers {
#![allow(non_upper_case_globals)]
use abstract_sdk::base::*;
Expand Down
103 changes: 103 additions & 0 deletions packages/abstract-app/src/interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#[macro_export]
/// Creates the interface for working with the app wth cw-orch
macro_rules! create_interface {
($app_const:expr, $app_type:ident) => {
mod _wrapper_fns{
use super::*;
pub fn instantiate(
deps: ::cosmwasm_std::DepsMut,
env: ::cosmwasm_std::Env,
info: ::cosmwasm_std::MessageInfo,
msg: <$app_type as ::abstract_sdk::base::InstantiateEndpoint>::InstantiateMsg,
) -> Result<::cosmwasm_std::Response, <$app_type as ::abstract_sdk::base::Handler>::Error> {
use ::abstract_sdk::base::InstantiateEndpoint;
$app_const.instantiate(deps, env, info, msg)
}

pub fn execute(
deps: ::cosmwasm_std::DepsMut,
env: ::cosmwasm_std::Env,
info: ::cosmwasm_std::MessageInfo,
msg: <$app_type as ::abstract_sdk::base::ExecuteEndpoint>::ExecuteMsg,
) -> Result<::cosmwasm_std::Response, <$app_type as ::abstract_sdk::base::Handler>::Error> {
use ::abstract_sdk::base::ExecuteEndpoint;
$app_const.execute(deps, env, info, msg)
}

pub fn query(
deps: ::cosmwasm_std::Deps,
env: ::cosmwasm_std::Env,
msg: <$app_type as abstract_sdk::base::QueryEndpoint>::QueryMsg,
) -> Result<::cosmwasm_std::Binary, <$app_type as ::abstract_sdk::base::Handler>::Error> {
use ::abstract_sdk::base::QueryEndpoint;
$app_const.query(deps, env, msg)
}

pub fn migrate(
deps: ::cosmwasm_std::DepsMut,
env: ::cosmwasm_std::Env,
msg: <$app_type as abstract_sdk::base::MigrateEndpoint>::MigrateMsg,
) -> Result<::cosmwasm_std::Response, <$app_type as ::abstract_sdk::base::Handler>::Error> {
use ::abstract_sdk::base::MigrateEndpoint;
$app_const.migrate(deps, env, msg)
}

pub fn reply(
deps: ::cosmwasm_std::DepsMut,
env: ::cosmwasm_std::Env,
msg: ::cosmwasm_std::Reply,
) -> Result<::cosmwasm_std::Response, <$app_type as ::abstract_sdk::base::Handler>::Error> {
use ::abstract_sdk::base::ReplyEndpoint;
$app_const.reply(deps, env, msg)
}

pub fn sudo(
deps: ::cosmwasm_std::DepsMut,
env: ::cosmwasm_std::Env,
msg: <$app_type as ::abstract_sdk::base::Handler>::SudoMsg,
) -> Result<::cosmwasm_std::Response, <$app_type as ::abstract_sdk::base::Handler>::Error> {
use ::abstract_sdk::base::SudoEndpoint;
$app_const.sudo(deps, env, msg)
}

pub type InstantiateMsg = <$app_type as ::abstract_sdk::base::InstantiateEndpoint>::InstantiateMsg;
pub type ExecuteMsg = <$app_type as ::abstract_sdk::base::ExecuteEndpoint>::ExecuteMsg;
pub type QueryMsg = <$app_type as ::abstract_sdk::base::QueryEndpoint>::QueryMsg;
pub type MigrateMsg = <$app_type as ::abstract_sdk::base::MigrateEndpoint>::MigrateMsg;
}

pub mod interface{
use super::_wrapper_fns;
#[::cw_orch::interface(_wrapper_fns::InstantiateMsg, _wrapper_fns::ExecuteMsg, _wrapper_fns::QueryMsg, _wrapper_fns::MigrateMsg)]
pub struct $app_type;


impl <Chain: ::cw_orch::prelude::CwEnv> ::cw_orch::prelude::Uploadable for $app_type<Chain> {
fn wasm(&self) -> ::cw_orch::prelude::WasmPath {
let wasm_name = env!("CARGO_CRATE_NAME").replace('-', "_");
::cw_orch::prelude::ArtifactsDir::auto(Some(env!("CARGO_MANIFEST_DIR").to_string()))
.find_wasm_path(&wasm_name).unwrap()
}

fn wrapper(
&self,
) -> Box<dyn ::cw_orch::prelude::MockContract<::cosmwasm_std::Empty, ::cosmwasm_std::Empty>> {
Box::new(
::cw_orch::prelude::ContractWrapper::new_with_empty(
_wrapper_fns::execute,
_wrapper_fns::instantiate,
_wrapper_fns::query,
)
.with_reply(_wrapper_fns::reply)
.with_migrate(_wrapper_fns::migrate)
.with_sudo(_wrapper_fns::sudo),
)
}
}

impl<Chain: ::cw_orch::prelude::CwEnv> ::abstract_interface::AppDeployer<Chain> for $app_type<Chain> {}
}


};
}
2 changes: 2 additions & 0 deletions packages/abstract-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod endpoints;
pub mod error;
pub mod features;
pub(crate) mod handler;
pub mod msgs;
#[cfg(feature = "schema")]
pub mod schema;
pub mod state;
Expand All @@ -10,6 +11,7 @@ pub(crate) use abstract_sdk::base::*;
pub use crate::state::AppContract;
pub use error::AppError;
pub type AppResult<C = Empty> = Result<Response<C>, AppError>;
mod interface;

use cosmwasm_std::{Empty, Response};
#[cfg(feature = "test-utils")]
Expand Down
17 changes: 17 additions & 0 deletions packages/abstract-app/src/msgs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#[macro_export]
/// Groups code that is needed on every app.
/// This registers the types for safety when using Messages
/// This is also used to indicate that The Query And Execute messages or used as app messages
macro_rules! app_messages {
($app_type:ty, $app_execute_msg: ty, $app_query_msg: ty) => {
/// Abstract App instantiate msg
pub type InstantiateMsg =
<$app_type as ::abstract_sdk::base::InstantiateEndpoint>::InstantiateMsg;
pub type ExecuteMsg = <$app_type as ::abstract_sdk::base::ExecuteEndpoint>::ExecuteMsg;
pub type QueryMsg = <$app_type as ::abstract_sdk::base::QueryEndpoint>::QueryMsg;
pub type MigrateMsg = <$app_type as ::abstract_sdk::base::MigrateEndpoint>::MigrateMsg;

impl ::abstract_core::app::AppExecuteMsg for $app_execute_msg {}
impl ::abstract_core::app::AppQueryMsg for $app_query_msg {}
};
}
20 changes: 0 additions & 20 deletions packages/abstract-core/src/objects/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,6 @@ impl Display for Namespace {
}
}

impl<'a> PrimaryKey<'a> for &Namespace {
type Prefix = ();

type SubPrefix = ();

type Suffix = Self;

type SuperSuffix = Self;

fn key(&self) -> Vec<cw_storage_plus::Key> {
self.0.key()
}
}

impl<'a> Prefixer<'a> for &Namespace {
fn prefix(&self) -> Vec<Key> {
self.0.prefix()
}
}

impl KeyDeserialize for &Namespace {
type Output = Namespace;

Expand Down

0 comments on commit 1d9e1c5

Please sign in to comment.