1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
//! # Executor
//! The executor provides function for executing commands on the Account.
//!
use crate::{
features::{AccountIdentification, ModuleIdentification},
AbstractSdkResult, AccountAction,
};
use abstract_core::proxy::ExecuteMsg;
use abstract_macros::with_abstract_event;
use cosmwasm_std::{wasm_execute, CosmosMsg, Deps, ReplyOn, Response, SubMsg};
/// Execute an `AccountAction` on the Account.
pub trait Execution: AccountIdentification + ModuleIdentification {
/**
API for executing [`AccountAction`]s on the Account.
Group your actions together in a single execute call if possible.
Executing [`CosmosMsg`] on the account is possible by creating an [`AccountAction`].
# Example
```
use abstract_sdk::prelude::*;
# use cosmwasm_std::testing::mock_dependencies;
# use abstract_sdk::mock_module::MockModule;
# let module = MockModule::new();
# let deps = mock_dependencies();
let executor: Executor<MockModule> = module.executor(deps.as_ref());
```
*/
fn executor<'a>(&'a self, deps: Deps<'a>) -> Executor<Self> {
Executor { base: self, deps }
}
}
impl<T> Execution for T where T: AccountIdentification + ModuleIdentification {}
/**
API for executing [`AccountAction`]s on the Account.
Group your actions together in a single execute call if possible.
Executing [`CosmosMsg`] on the account is possible by creating an [`AccountAction`].
# Example
```
use abstract_sdk::prelude::*;
# use cosmwasm_std::testing::mock_dependencies;
# use abstract_sdk::mock_module::MockModule;
# let module = MockModule::new();
# let deps = mock_dependencies();
let executor: Executor<MockModule> = module.executor(deps.as_ref());
```
*/
#[derive(Clone)]
pub struct Executor<'a, T: Execution> {
base: &'a T,
deps: Deps<'a>,
}
impl<'a, T: Execution> Executor<'a, T> {
/// Execute a single message on the `ModuleActionWithData` endpoint.
fn execute_with_data(&self, msg: CosmosMsg) -> AbstractSdkResult<CosmosMsg> {
Ok(wasm_execute(
self.base.proxy_address(self.deps)?.to_string(),
&ExecuteMsg::ModuleActionWithData { msg },
vec![],
)?
.into())
}
/// Execute the msgs on the Account.
/// These messages will be executed on the proxy contract and the sending module must be whitelisted.
pub fn execute(&self, actions: Vec<AccountAction>) -> AbstractSdkResult<CosmosMsg> {
let msgs = actions.into_iter().flat_map(|a| a.messages()).collect();
Ok(wasm_execute(
self.base.proxy_address(self.deps)?.to_string(),
&ExecuteMsg::ModuleAction { msgs },
vec![],
)?
.into())
}
/// Execute the msgs on the Account.
/// These messages will be executed on the proxy contract and the sending module must be whitelisted.
/// The execution will be executed in a submessage and the reply will be sent to the provided `reply_on`.
pub fn execute_with_reply(
&self,
actions: Vec<AccountAction>,
reply_on: ReplyOn,
id: u64,
) -> AbstractSdkResult<SubMsg> {
let msg = self.execute(actions)?;
let sub_msg = SubMsg {
id,
msg,
gas_limit: None,
reply_on,
};
Ok(sub_msg)
}
/// Execute a single msg on the Account.
/// This message will be executed on the proxy contract. Any data returned from the execution will be forwarded to the proxy's response through a reply.
/// The resulting data should be available in the reply of the specified ID.
pub fn execute_with_reply_and_data(
&self,
actions: CosmosMsg,
reply_on: ReplyOn,
id: u64,
) -> AbstractSdkResult<SubMsg> {
let msg = self.execute_with_data(actions)?;
let sub_msg = SubMsg {
id,
msg,
gas_limit: None,
reply_on,
};
Ok(sub_msg)
}
/// Execute the msgs on the Account.
/// These messages will be executed on the proxy contract and the sending module must be whitelisted.
/// Return a "standard" response for the executed messages. (with the provided action).
pub fn execute_with_response(
&self,
actions: Vec<AccountAction>,
action: &str,
) -> AbstractSdkResult<Response> {
let msg = self.execute(actions)?;
let resp = Response::default();
Ok(with_abstract_event!(resp, self.base.module_id(), action).add_message(msg))
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::mock_module::*;
use abstract_core::proxy::ExecuteMsg;
use abstract_testing::prelude::*;
use cosmwasm_std::{testing::*, *};
use speculoos::prelude::*;
fn mock_bank_send(amount: Vec<Coin>) -> AccountAction {
AccountAction::from(CosmosMsg::Bank(BankMsg::Send {
to_address: "to_address".to_string(),
amount,
}))
}
fn flatten_actions(actions: Vec<AccountAction>) -> Vec<CosmosMsg> {
actions.into_iter().flat_map(|a| a.messages()).collect()
}
mod execute {
use super::*;
use cosmwasm_std::to_binary;
/// Tests that no error is thrown with empty messages provided
#[test]
fn empty_actions() {
let deps = mock_dependencies();
let stub = MockModule::new();
let executor = stub.executor(deps.as_ref());
let messages = vec![];
let actual_res = executor.execute(messages.clone());
assert_that!(actual_res).is_ok();
let expected = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: TEST_PROXY.to_string(),
msg: to_binary(&ExecuteMsg::ModuleAction {
msgs: flatten_actions(messages),
})
.unwrap(),
funds: vec![],
});
assert_that!(actual_res.unwrap()).is_equal_to(expected);
}
#[test]
fn with_actions() {
let deps = mock_dependencies();
let stub = MockModule::new();
let executor = stub.executor(deps.as_ref());
// build a bank message
let messages = vec![mock_bank_send(coins(100, "juno"))];
let actual_res = executor.execute(messages.clone());
assert_that!(actual_res).is_ok();
let expected = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: TEST_PROXY.to_string(),
msg: to_binary(&ExecuteMsg::ModuleAction {
msgs: flatten_actions(messages),
})
.unwrap(),
// funds should be empty
funds: vec![],
});
assert_that!(actual_res.unwrap()).is_equal_to(expected);
}
}
mod execute_with_reply {
use super::*;
/// Tests that no error is thrown with empty messages provided
#[test]
fn empty_actions() {
let deps = mock_dependencies();
let stub = MockModule::new();
let executor = stub.executor(deps.as_ref());
let empty_actions = vec![];
let expected_reply_on = ReplyOn::Success;
let expected_reply_id = 10952;
let actual_res = executor.execute_with_reply(
empty_actions.clone(),
expected_reply_on.clone(),
expected_reply_id,
);
assert_that!(actual_res).is_ok();
let expected = SubMsg {
id: expected_reply_id,
msg: CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: TEST_PROXY.to_string(),
msg: to_binary(&ExecuteMsg::ModuleAction {
msgs: flatten_actions(empty_actions),
})
.unwrap(),
funds: vec![],
}),
gas_limit: None,
reply_on: expected_reply_on,
};
assert_that!(actual_res.unwrap()).is_equal_to(expected);
}
#[test]
fn with_actions() {
let deps = mock_dependencies();
let stub = MockModule::new();
let executor = stub.executor(deps.as_ref());
// build a bank message
let action = vec![mock_bank_send(coins(1, "denom"))];
// reply on never
let expected_reply_on = ReplyOn::Never;
let expected_reply_id = 1;
let actual_res = executor.execute_with_reply(
action.clone(),
expected_reply_on.clone(),
expected_reply_id,
);
assert_that!(actual_res).is_ok();
let expected = SubMsg {
id: expected_reply_id,
msg: CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: TEST_PROXY.to_string(),
msg: to_binary(&ExecuteMsg::ModuleAction {
msgs: flatten_actions(action),
})
.unwrap(),
// funds should be empty
funds: vec![],
}),
gas_limit: None,
reply_on: expected_reply_on,
};
assert_that!(actual_res.unwrap()).is_equal_to(expected);
}
}
mod execute_with_response {
use super::*;
use cosmwasm_std::coins;
/// Tests that no error is thrown with empty messages provided
#[test]
fn empty_actions() {
let deps = mock_dependencies();
let stub = MockModule::new();
let executor = stub.executor(deps.as_ref());
let empty_actions = vec![];
let expected_action = "THIS IS AN ACTION";
let actual_res = executor.execute_with_response(empty_actions.clone(), expected_action);
let expected_msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: TEST_PROXY.to_string(),
msg: to_binary(&ExecuteMsg::ModuleAction {
msgs: flatten_actions(empty_actions),
})
.unwrap(),
funds: vec![],
});
let expected = Response::new()
.add_event(
Event::new("abstract")
.add_attribute("contract", stub.module_id())
.add_attribute("action", expected_action),
)
.add_message(expected_msg);
assert_that!(actual_res).is_ok().is_equal_to(expected);
}
#[test]
fn with_actions() {
let deps = mock_dependencies();
let stub = MockModule::new();
let executor = stub.executor(deps.as_ref());
// build a bank message
let action = vec![mock_bank_send(coins(1, "denom"))];
let expected_action = "provide liquidity";
let actual_res = executor.execute_with_response(action.clone(), expected_action);
let expected_msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: TEST_PROXY.to_string(),
msg: to_binary(&ExecuteMsg::ModuleAction {
msgs: flatten_actions(action),
})
.unwrap(),
// funds should be empty
funds: vec![],
});
let expected = Response::new()
.add_event(
Event::new("abstract")
.add_attribute("contract", stub.module_id())
.add_attribute("action", expected_action),
)
.add_message(expected_msg);
assert_that!(actual_res).is_ok().is_equal_to(expected);
}
}
}