Skip to content
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

Feature/module ics20 actions #536

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
644 changes: 493 additions & 151 deletions framework/Cargo.lock

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ schemars = "0.8"
serde = { version = "1.0", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.50" }
protobuf = { version = "2", features = ["with-bytes"] }
anybuf = { version = "0.5.0" }

clap = { version = "4.0.32", features = ["derive"] }
semver = "1.0"
Expand Down Expand Up @@ -131,6 +132,19 @@ incremental = false
# abstract-polytone-note = { git = "https://github.com/AbstractSDK/polytone.git", branch = "bump/cw2" }
# abstract-cw-orch-polytone = { git = "https://github.com/AbstractSDK/polytone.git", branch = "bump/cw2" }

abstract-cw-multi-test = { git = "https://github.com/abstractsdk/cw-multi-test-fork", branch = "feature/add-ics20-transfer-data" }
# abstract-cw-multi-test = { path = "../../cw-multi-test" }
cw-orch-interchain-core = { git = "https://github.com/abstractsdk/cw-orchestrator", branch = "interchain/add-ics-hooks-ack" }
cw-orch-core = { git = "https://github.com/abstractsdk/cw-orchestrator", branch = "interchain/add-ics-hooks-ack" }
cw-orch-mock = { git = "https://github.com/abstractsdk/cw-orchestrator", branch = "interchain/add-ics-hooks-ack" }
cw-orch-interchain = { git = "https://github.com/abstractsdk/cw-orchestrator", branch = "interchain/add-ics-hooks-ack" }
cw-orch-interchain-daemon = { git = "https://github.com/abstractsdk/cw-orchestrator", branch = "interchain/add-ics-hooks-ack" }
cw-orch-interchain-mock = { git = "https://github.com/abstractsdk/cw-orchestrator", branch = "interchain/add-ics-hooks-ack" }
# cw-orch-interchain-core = { path = "../../cw-orchestrator/packages/interchain/interchain-core" }
# cw-orch-core = { path = "../../cw-orchestrator/packages/cw-orch-core" }
# cw-orch-interchain-daemon = { path = "../../cw-orchestrator/packages/interchain/interchain-daemon" }
# cw-orch-interchain-mock = { path = "../../cw-orchestrator/packages/interchain/interchain-mock" }

[workspace.metadata.cargo-udeps.ignore]
# ensures CI doens't flag workspace-hack as unused dep
normal = ["workspace-hack"]
Expand Down
6 changes: 5 additions & 1 deletion framework/contracts/account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ abstract-macros = { workspace = true }
abstract-ica = { workspace = true }

abstract-xion = { workspace = true, optional = true }
prost = "0.13.3"
serde_json = "1.0.132"
json-patch = "3.0.1"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
workspace-hack = { version = "0.1", path = "../../workspace-hack" }
Expand All @@ -56,8 +59,9 @@ workspace-hack = { version = "0.1", path = "../../workspace-hack" }
cw20 = { workspace = true }
# cw721-base = { version = "0.18.0", features = ["library"] }
# cw721 = { version = "0.18.0" }
abstract-interface = { workspace = true }
abstract-interface = { workspace = true, features = ["interchain"] }
cw-orch = { workspace = true, features = ["snapshot-testing"] }
cw-orch-interchain = { workspace = true }
anyhow = { workspace = true }
ans-host = { workspace = true }
registry = { workspace = true }
Expand Down
42 changes: 26 additions & 16 deletions framework/contracts/account/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use abstract_std::{
},
registry::state::LOCAL_ACCOUNT_SEQUENCE,
};

use cosmwasm_std::{
ensure_eq, wasm_execute, Addr, Binary, Coins, Deps, DepsMut, Env, MessageInfo, Reply, Response,
StdResult,
Expand All @@ -36,19 +37,24 @@ use crate::{
execution::{
add_auth_method, admin_execute, admin_execute_on_module, execute_msgs,
execute_msgs_with_data, execute_on_module, ica_action, remove_auth_method,
send_funds_with_actions,
},
ics20::ics20_hook_callback,
modules::{
_install_modules, install_modules,
migration::{assert_modules_dependency_requirements, upgrade_modules},
uninstall_module, MIGRATE_CONTEXT,
},
msg::{ExecuteMsg, InstantiateMsg, QueryMsg},
msg::{ExecuteMsg, InstantiateMsg, QueryMsg, SudoMsg},
queries::{
handle_account_info_query, handle_config_query, handle_module_address_query,
handle_module_info_query, handle_module_versions_query, handle_sub_accounts_query,
handle_top_level_owner_query,
},
reply::{admin_action_reply, forward_response_reply, register_dependencies},
reply::{
admin_action_reply, forward_response_reply, register_dependencies,
register_sub_sequent_messages,
},
sub_account::{
create_sub_account, handle_sub_account_action, maybe_update_sub_account_governance,
remove_account_from_contracts,
Expand All @@ -66,6 +72,7 @@ pub const FORWARD_RESPONSE_REPLY_ID: u64 = 1;
pub const ADMIN_ACTION_REPLY_ID: u64 = 2;
pub const REGISTER_MODULES_DEPENDENCIES_REPLY_ID: u64 = 3;
pub const ASSERT_MODULE_DEPENDENCIES_REQUIREMENTS_REPLY_ID: u64 = 4;
pub const IBC_TOKEN_FLOW: u64 = 5;

#[cfg_attr(feature = "export", cosmwasm_std::entry_point)]
pub fn instantiate(
Expand Down Expand Up @@ -268,24 +275,24 @@ pub fn execute(mut deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg)

match msg {
// ## Execution ##
ExecuteMsg::Execute { msgs } => execute_msgs(deps, &info.sender, msgs),
ExecuteMsg::Execute { msgs } => execute_msgs(deps, env, &info.sender, msgs),
ExecuteMsg::AdminExecute { addr, msg } => {
let addr = deps.api.addr_validate(&addr)?;
admin_execute(deps, info, addr, msg)
}
ExecuteMsg::ExecuteWithData { msg } => {
execute_msgs_with_data(deps, &info.sender, msg)
execute_msgs_with_data(deps, env, &info.sender, msg)
}
ExecuteMsg::ExecuteOnModule {
module_id,
exec_msg,
funds,
} => execute_on_module(deps, info, module_id, exec_msg, funds),
} => execute_on_module(deps, env, info, module_id, exec_msg, funds),
ExecuteMsg::AdminExecuteOnModule { module_id, msg } => {
admin_execute_on_module(deps, info, module_id, msg)
}
ExecuteMsg::IcaAction { action_query_msg } => {
ica_action(deps, info, action_query_msg)
ica_action(deps, env, info, action_query_msg)
}

// ## Configuration ##
Expand Down Expand Up @@ -357,6 +364,11 @@ pub fn execute(mut deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg)
}
#[allow(unused)]
ExecuteMsg::RemoveAuthMethod { id } => remove_auth_method(deps, env, id),
ExecuteMsg::SendFundsWithActions {
amount,
host_chain,
actions,
} => send_funds_with_actions(deps, info, env, host_chain, amount, actions),
}
}
}?;
Expand All @@ -373,6 +385,7 @@ pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> AccountResult {
ASSERT_MODULE_DEPENDENCIES_REQUIREMENTS_REPLY_ID => {
assert_modules_dependency_requirements(deps)
}
IBC_TOKEN_FLOW => register_sub_sequent_messages(deps, msg),

_ => Err(AccountError::UnexpectedReply {}),
}
Expand Down Expand Up @@ -416,17 +429,14 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
}
}

#[cfg(feature = "xion")]
#[cfg_attr(feature = "export", cosmwasm_std::entry_point)]
pub fn sudo(
deps: DepsMut,
env: Env,
msg: abstract_xion::contract::AccountSudoMsg,
) -> abstract_xion::error::ContractResult<Response> {
if let abstract_xion::contract::AccountSudoMsg::BeforeTx { .. } = &msg {
AUTH_ADMIN.save(deps.storage, &true)?;
};
abstract_xion::contract::sudo(deps, env, msg)
pub fn sudo(deps: DepsMut, env: Env, msg: SudoMsg) -> AccountResult {
match msg {
SudoMsg::IBCLifecycleComplete(msg) => ics20_hook_callback(deps, env, msg),

#[cfg(feature = "xion")]
SudoMsg::Xion(msg) => abstract_xion::contract::sudo(deps, env, msg).map_err(Into::into),
}
}

/// Verifies that *sender* is the owner of *nft_id* of contract *nft_addr*
Expand Down
23 changes: 22 additions & 1 deletion framework/contracts/account/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use abstract_sdk::std::objects::module::ModuleInfo;
use abstract_std::{
objects::{registry::RegistryError, validation::ValidationError},
objects::{
ans_host::AnsHostError, registry::RegistryError, validation::ValidationError,
TruncatedChainId,
},
AbstractError,
};
use cosmwasm_std::{Instantiate2AddressError, StdError};
Expand All @@ -26,6 +29,15 @@ pub enum AccountError {
#[error(transparent)]
RegistryError(#[from] RegistryError),

#[error(transparent)]
AnsHostError(#[from] AnsHostError),

#[error(transparent)]
ProstDecodeError(#[from] prost::DecodeError),

#[error("{0}")]
SerdeJsonError(String),

#[error("Your account is currently suspended")]
AccountSuspended {},

Expand Down Expand Up @@ -118,4 +130,13 @@ pub enum AccountError {
#[cfg(feature = "xion")]
#[error(transparent)]
AbstractXion(#[from] abstract_xion::error::ContractError),

#[error("Chain {0} not registered in the IBC Client")]
ChainNotRegistered(TruncatedChainId),
}

impl From<serde_json::Error> for AccountError {
fn from(value: serde_json::Error) -> Self {
Self::SerdeJsonError(value.to_string())
}
}
105 changes: 93 additions & 12 deletions framework/contracts/account/src/execution.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
use abstract_sdk::std::account::state::WHITELISTED_MODULES;
use abstract_sdk::{
feature_objects::AnsHost, std::account::state::WHITELISTED_MODULES, HookMemoBuilder, Resolve,
};
use abstract_std::{
account::state::{ACCOUNT_MODULES, CALLING_TO_AS_ADMIN},
objects::ownership,
ICA_CLIENT,
account::state::{ACCOUNT_ID, ACCOUNT_MODULES, CALLING_TO_AS_ADMIN},
ibc::PACKET_LIFETIME,
ibc_client::state::IBC_INFRA,
ibc_host,
objects::{ownership, ChannelEntry, TruncatedChainId},
IBC_CLIENT, ICA_CLIENT, ICS20,
};
use cosmwasm_std::{
Addr, Binary, Coin, CosmosMsg, DepsMut, Empty, Env, MessageInfo, StdError, SubMsg, WasmMsg,
WasmQuery,
to_json_binary, Addr, Binary, Coin, CosmosMsg, DepsMut, Empty, Env, IbcMsg, IbcTimeout,
MessageInfo, Response, StdError, SubMsg, WasmMsg, WasmQuery,
};

use crate::{
contract::{AccountResponse, AccountResult, ADMIN_ACTION_REPLY_ID, FORWARD_RESPONSE_REPLY_ID},
contract::{
AccountResponse, AccountResult, ADMIN_ACTION_REPLY_ID, FORWARD_RESPONSE_REPLY_ID,
IBC_TOKEN_FLOW,
},
error::AccountError,
modules::load_module_addr,
msg::ExecuteMsg,
reply::TokenFlowPayload,
};

/// Check that sender either whitelisted or governance
pub(crate) fn assert_whitelisted_or_owner(deps: &mut DepsMut, sender: &Addr) -> AccountResult<()> {
pub(crate) fn assert_whitelisted_or_owner(
deps: &mut DepsMut,
env: &Env,
sender: &Addr,
) -> AccountResult<()> {
#[cfg(feature = "xion")]
{
if let Some(is_admin) = crate::state::AUTH_ADMIN.may_load(deps.storage)? {
Expand All @@ -27,6 +41,10 @@ pub(crate) fn assert_whitelisted_or_owner(deps: &mut DepsMut, sender: &Addr) ->
}
}
}
// Account can execute on itself
if env.contract.address == sender {
return Ok(());
}
let whitelisted_modules = WHITELISTED_MODULES.load(deps.storage)?;
if whitelisted_modules.0.contains(sender)
|| ownership::assert_nested_owner(deps.storage, &deps.querier, sender).is_ok()
Expand All @@ -41,10 +59,11 @@ pub(crate) fn assert_whitelisted_or_owner(deps: &mut DepsMut, sender: &Addr) ->
/// Permission: Module
pub fn execute_msgs(
mut deps: DepsMut,
env: Env,
msg_sender: &Addr,
msgs: Vec<CosmosMsg<Empty>>,
) -> AccountResult {
assert_whitelisted_or_owner(&mut deps, msg_sender)?;
assert_whitelisted_or_owner(&mut deps, &env, msg_sender)?;

Ok(AccountResponse::action("execute_module_action").add_messages(msgs))
}
Expand All @@ -53,10 +72,11 @@ pub fn execute_msgs(
/// Permission: Module
pub fn execute_msgs_with_data(
mut deps: DepsMut,
env: Env,
msg_sender: &Addr,
msg: CosmosMsg<Empty>,
) -> AccountResult {
assert_whitelisted_or_owner(&mut deps, msg_sender)?;
assert_whitelisted_or_owner(&mut deps, &env, msg_sender)?;

let submsg = SubMsg::reply_on_success(msg, FORWARD_RESPONSE_REPLY_ID);

Expand All @@ -67,6 +87,7 @@ pub fn execute_msgs_with_data(
/// This is a simple wrapper around [`ExecuteMsg::Execute`](abstract_std::account::ExecuteMsg::Execute).
pub fn execute_on_module(
deps: DepsMut,
env: Env,
info: MessageInfo,
module_id: String,
exec_msg: Binary,
Expand All @@ -75,6 +96,7 @@ pub fn execute_on_module(
let module_addr = load_module_addr(deps.storage, &module_id)?;
execute_msgs(
deps,
env,
&info.sender,
vec![CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: module_addr.into(),
Expand Down Expand Up @@ -149,8 +171,13 @@ pub fn remove_auth_method(_deps: DepsMut, _env: Env, _id: u8) -> AccountResult {
/// It then fires a smart-query on that address of type [`QueryMsg::IcaAction`](abstract_ica::msg::QueryMsg).
///
/// The resulting `Vec<CosmosMsg>` are then executed on the account contract.
pub fn ica_action(mut deps: DepsMut, msg_info: MessageInfo, action_query: Binary) -> AccountResult {
assert_whitelisted_or_owner(&mut deps, &msg_info.sender)?;
pub fn ica_action(
mut deps: DepsMut,
env: Env,
msg_info: MessageInfo,
action_query: Binary,
) -> AccountResult {
assert_whitelisted_or_owner(&mut deps, &env, &msg_info.sender)?;

let ica_client_address = ACCOUNT_MODULES
.may_load(deps.storage, ICA_CLIENT)?
Expand All @@ -171,6 +198,60 @@ pub fn ica_action(mut deps: DepsMut, msg_info: MessageInfo, action_query: Binary
Ok(AccountResponse::action("ica_action").add_messages(res.msgs))
}

pub fn send_funds_with_actions(
mut deps: DepsMut,
msg_info: MessageInfo,
env: Env,
host_chain: TruncatedChainId,
funds: Coin,
actions: Vec<ExecuteMsg>,
) -> AccountResult {
assert_whitelisted_or_owner(&mut deps, &env, &msg_info.sender)?;
// We send a funds message to the host saying we want to deposit on a remote account
host_chain.verify()?;

let ans = AnsHost::new(deps.api, &env)?;

let ics20_channel_entry = ChannelEntry {
connected_chain: host_chain.clone(),
protocol: ICS20.to_string(),
};
let ics20_channel_id = ics20_channel_entry.resolve(&deps.querier, &ans)?;

let ibc_client_module = load_module_addr(deps.storage, IBC_CLIENT)?;
let remote_host = IBC_INFRA
.query(&deps.querier, ibc_client_module, &host_chain)?
.ok_or(AccountError::ChainNotRegistered(host_chain))?;

// Hook for sending the funds correctly to the sender
let action_memo = HookMemoBuilder::new(
remote_host.remote_abstract_host.clone(),
&ibc_host::ExecuteMsg::Fund {
src_account: ACCOUNT_ID.load(deps.storage)?,
src_chain: TruncatedChainId::from_chain_id(&env.block.chain_id),
},
)
.callback(&env)
.build()?;

let transfer_msg = IbcMsg::Transfer {
channel_id: ics20_channel_id.clone(),
to_address: remote_host.remote_abstract_host,
amount: funds,
timeout: IbcTimeout::with_timestamp(env.block.time.plus_seconds(PACKET_LIFETIME)),
memo: Some(action_memo),
};

Ok(Response::new().add_submessage(
SubMsg::reply_on_success(transfer_msg, IBC_TOKEN_FLOW).with_payload(to_json_binary(
&TokenFlowPayload {
channel_id: ics20_channel_id,
msgs: actions,
},
)?),
))
}

#[cfg(test)]
mod test {
use crate::contract::execute;
Expand Down
Loading
Loading