Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Implementation of sending a message
  • Loading branch information
Kayanski committed Nov 5, 2024
commit 74335a925d2146bc94d45661e78cd38d5101d4b1
1 change: 1 addition & 0 deletions framework/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions framework/contracts/account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ abstract-macros = { workspace = true }
abstract-ica = { workspace = true }

abstract-xion = { workspace = true, optional = true }
prost = "0.13.3"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
workspace-hack = { version = "0.1", path = "../../workspace-hack" }
Expand Down
78 changes: 73 additions & 5 deletions framework/contracts/account/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use abstract_std::{
account::{
state::{AccountInfo, WhitelistedModules, INFO, SUSPENSION_STATUS, WHITELISTED_MODULES},
UpdateSubAccountAction,
ICS20PacketIdentifier, UpdateSubAccountAction,
},
module_factory::SimulateInstallModulesResponse,
objects::{
Expand All @@ -22,8 +22,9 @@
registry::state::LOCAL_ACCOUNT_SEQUENCE,
};
use cosmwasm_std::{
ensure_eq, wasm_execute, Addr, Binary, Coins, Deps, DepsMut, Env, MessageInfo, Reply, Response,
StdResult,
ensure_eq, wasm_execute, Addr, Binary, Coins, Deps, DepsMut, Env, IbcAckCallbackMsg,
IbcBasicResponse, IbcSourceCallbackMsg, IbcTimeoutCallbackMsg, MessageInfo, Reply, Response,
StdAck, StdResult,
};

pub use crate::migrate::migrate;
Expand All @@ -33,19 +34,23 @@
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,
},
modules::{
_install_modules, install_modules,
migration::{assert_modules_dependency_requirements, upgrade_modules},
uninstall_module, MIGRATE_CONTEXT,
},
msg::{ExecuteMsg, InstantiateMsg, QueryMsg},
msg::{ExecuteMsg, InstantiateMsg, QueryMsg, ICS20_CALLBACKS},
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 @@ -63,6 +68,7 @@
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 @@ -353,6 +359,11 @@
}
#[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),

Check warning on line 366 in framework/contracts/account/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/contract.rs#L363-L366

Added lines #L363 - L366 were not covered by tests
}
}
}
Expand All @@ -367,6 +378,7 @@
ASSERT_MODULE_DEPENDENCIES_REQUIREMENTS_REPLY_ID => {
assert_modules_dependency_requirements(deps)
}
IBC_TOKEN_FLOW => register_sub_sequent_messages(deps, msg),

Check warning on line 381 in framework/contracts/account/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/contract.rs#L381

Added line #L381 was not covered by tests

_ => Err(AccountError::UnexpectedReply {}),
}
Expand Down Expand Up @@ -405,6 +417,62 @@
}
}

#[cfg_attr(feature = "export", cosmwasm_std::entry_point)]
pub fn ibc_source_callback(
deps: DepsMut,
env: Env,
msg: IbcSourceCallbackMsg,
) -> StdResult<IbcBasicResponse> {
match msg {

Check warning on line 426 in framework/contracts/account/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/contract.rs#L421-L426

Added lines #L421 - L426 were not covered by tests
IbcSourceCallbackMsg::Acknowledgement(IbcAckCallbackMsg {
acknowledgement,
original_packet,
relayer: _,
..
}) => {
let packet_identifier = ICS20PacketIdentifier {
channel_id: original_packet.src.channel_id,
sequence: original_packet.sequence,
};

Check warning on line 436 in framework/contracts/account/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/contract.rs#L428-L436

Added lines #L428 - L436 were not covered by tests

let (outcome, stored_msgs) =
if acknowledgement.data == StdAck::success(b"\x01").to_binary() {

Check warning on line 439 in framework/contracts/account/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/contract.rs#L438-L439

Added lines #L438 - L439 were not covered by tests
(
"result",
ICS20_CALLBACKS
.load(deps.storage, packet_identifier.clone())?
.into_iter()
.map(|msg| wasm_execute(&env.contract.address, &msg, vec![]))
.collect::<Result<Vec<_>, _>>()?,

Check warning on line 446 in framework/contracts/account/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/contract.rs#L442-L446

Added lines #L442 - L446 were not covered by tests
)
} else {
("failure", vec![])

Check warning on line 449 in framework/contracts/account/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/contract.rs#L449

Added line #L449 was not covered by tests
};

ICS20_CALLBACKS.remove(deps.storage, packet_identifier.clone());

Ok(IbcBasicResponse::new()
.add_attribute("action", "ibc_source_callback")
.add_attribute("outcome", outcome)
.add_messages(stored_msgs))

Check warning on line 457 in framework/contracts/account/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/contract.rs#L452-L457

Added lines #L452 - L457 were not covered by tests
}
IbcSourceCallbackMsg::Timeout(IbcTimeoutCallbackMsg {
packet, relayer: _, ..
}) => {
ICS20_CALLBACKS.remove(
deps.storage,
ICS20PacketIdentifier {
channel_id: packet.src.channel_id,
sequence: packet.sequence,
},
);
Ok(IbcBasicResponse::new()
.add_attribute("action", "ibc_source_callback")
.add_attribute("outcome", "timeout"))

Check warning on line 471 in framework/contracts/account/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/contract.rs#L460-L471

Added lines #L460 - L471 were not covered by tests
}
}
}

Check warning on line 474 in framework/contracts/account/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/contract.rs#L474

Added line #L474 was not covered by tests

#[cfg(feature = "xion")]
#[cfg_attr(feature = "export", cosmwasm_std::entry_point)]
pub fn sudo(
Expand Down
14 changes: 13 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,12 @@ pub enum AccountError {
#[error(transparent)]
RegistryError(#[from] RegistryError),

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

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

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

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

#[error("Chain {0} not registered in the IBC Client")]
ChainNotRegistered(TruncatedChainId),
}
71 changes: 64 additions & 7 deletions framework/contracts/account/src/execution.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
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,
};

/// Check that sender either whitelisted or governance
Expand Down Expand Up @@ -171,6 +180,54 @@
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, &msg_info.sender)?;

Check warning on line 191 in framework/contracts/account/src/execution.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/execution.rs#L183-L191

Added lines #L183 - L191 were not covered by tests
// We send a funds message to the host saying we want to deposit on a remote account
host_chain.verify()?;

Check warning on line 193 in framework/contracts/account/src/execution.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/execution.rs#L193

Added line #L193 was not covered by tests

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

Check warning on line 195 in framework/contracts/account/src/execution.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/execution.rs#L195

Added line #L195 was not covered by tests

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)?;

Check warning on line 201 in framework/contracts/account/src/execution.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/execution.rs#L197-L201

Added lines #L197 - L201 were not covered by tests

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))?;

Check warning on line 206 in framework/contracts/account/src/execution.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/execution.rs#L203-L206

Added lines #L203 - L206 were not covered by tests

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),
},
)
.build()?;

Check warning on line 215 in framework/contracts/account/src/execution.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/execution.rs#L208-L215

Added lines #L208 - L215 were not covered by tests

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(&actions)?),

Check warning on line 227 in framework/contracts/account/src/execution.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/execution.rs#L217-L227

Added lines #L217 - L227 were not covered by tests
))
}

Check warning on line 229 in framework/contracts/account/src/execution.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/execution.rs#L229

Added line #L229 was not covered by tests

#[cfg(test)]
mod test {
use crate::contract::execute;
Expand Down
5 changes: 5 additions & 0 deletions framework/contracts/account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub use abstract_xion;
// re-export based on the feature
pub mod msg {
pub use abstract_std::account::{MigrateMsg, QueryMsg};
use abstract_std::{account::ICS20PacketIdentifier, objects::storage_namespaces};
use cw_storage_plus::Map;

#[cfg(feature = "xion")]
pub type Authenticator = crate::abstract_xion::auth::AddAuthenticator;
Expand All @@ -33,6 +35,9 @@ pub mod msg {

pub type ExecuteMsg = abstract_std::account::ExecuteMsg<Authenticator>;
pub type InstantiateMsg = abstract_std::account::InstantiateMsg<Authenticator>;

pub const ICS20_CALLBACKS: Map<ICS20PacketIdentifier, Vec<ExecuteMsg>> =
Map::new(storage_namespaces::account::ICS20_CALLBACKS);
}

#[cfg(test)]
Expand Down
38 changes: 36 additions & 2 deletions framework/contracts/account/src/reply.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use crate::{
contract::{AccountResponse, AccountResult},
modules::INSTALL_MODULES_CONTEXT,
msg::{ExecuteMsg, ICS20_CALLBACKS},
};
use abstract_std::{
account::state::CALLING_TO_AS_ADMIN,
account::{state::CALLING_TO_AS_ADMIN, ICS20PacketIdentifier},
objects::{
module::{assert_module_data_validity, Module},
module_reference::ModuleReference,
},
};
use cosmwasm_std::{DepsMut, Reply, Response, StdError};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{from_json, DepsMut, Reply, Response, StdError};

/// Add the message's data to the response
pub(crate) fn forward_response_reply(result: Reply) -> AccountResult {
Expand Down Expand Up @@ -76,3 +78,35 @@

Ok(Response::new())
}

use prost::Message;
#[derive(Clone, PartialEq, Message)]

Check warning on line 83 in framework/contracts/account/src/reply.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/reply.rs#L83

Added line #L83 was not covered by tests
struct MsgTransferResponse {
#[prost(uint64, tag = "1")]
pub sequence: u64,
}

pub fn register_sub_sequent_messages(deps: DepsMut, reply: Reply) -> AccountResult {
let res = reply.result.into_result().map_err(StdError::generic_err)?;
let transfer_response = MsgTransferResponse::decode(res.msg_responses[0].value.as_slice())?;

Check warning on line 91 in framework/contracts/account/src/reply.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/reply.rs#L89-L91

Added lines #L89 - L91 were not covered by tests

let payload: TokenFlowPayload = from_json(reply.payload)?;

Check warning on line 93 in framework/contracts/account/src/reply.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/reply.rs#L93

Added line #L93 was not covered by tests

// We register the callback for later use
ICS20_CALLBACKS.save(
deps.storage,
ICS20PacketIdentifier {
channel_id: payload.channel_id,
sequence: transfer_response.sequence,
},
&payload.msgs,
)?;

Check warning on line 103 in framework/contracts/account/src/reply.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/reply.rs#L96-L103

Added lines #L96 - L103 were not covered by tests

Ok(Response::new())
}

Check warning on line 106 in framework/contracts/account/src/reply.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/reply.rs#L105-L106

Added lines #L105 - L106 were not covered by tests

#[cw_serde]

Check warning on line 108 in framework/contracts/account/src/reply.rs

View check run for this annotation

Codecov / codecov/patch

framework/contracts/account/src/reply.rs#L108

Added line #L108 was not covered by tests
pub struct TokenFlowPayload {
channel_id: String,
msgs: Vec<ExecuteMsg>,
}
10 changes: 4 additions & 6 deletions framework/contracts/native/ibc-host/src/account_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use abstract_sdk::{
};
use abstract_std::{
account::{self, ModuleInstallConfig},
ibc::PACKET_LIFETIME,
objects::{module::ModuleInfo, module_reference::ModuleReference, AccountId, TruncatedChainId},
registry::Account,
ACCOUNT,
};
use cosmwasm_std::{
instantiate2_address, to_json_binary, wasm_execute, CosmosMsg, Deps, DepsMut, Empty, Env,
IbcMsg, Response, SubMsg, WasmMsg,
instantiate2_address, to_json_binary, wasm_execute, Coin, CosmosMsg, Deps, DepsMut, Empty, Env, IbcMsg, Response, SubMsg, WasmMsg
};

use crate::{
Expand All @@ -20,9 +20,6 @@ use crate::{
HostError,
};

// one hour
const PACKET_LIFETIME: u64 = 60 * 60;

/// Creates and registers account for remote Account
#[allow(clippy::too_many_arguments)]
pub fn receive_register(
Expand All @@ -35,6 +32,7 @@ pub fn receive_register(
namespace: Option<String>,
install_modules: Vec<ModuleInstallConfig>,
with_reply: bool,
funds: Vec<Coin>,
) -> HostResult {
let registry = RegistryContract::new(deps.api, &env)?;
// verify that the origin last chain is the chain related to this channel, and that it is not `Local`
Expand Down Expand Up @@ -78,7 +76,7 @@ pub fn receive_register(
code_id,
label: account_id.to_string(),
msg: to_json_binary(&create_account_msg)?,
funds: vec![],
funds,
salt,
};

Expand Down
Loading
Loading