abstract_sdk/
account_action.rs

1use cosmwasm_std::CosmosMsg;
2
3/// Encapsulates an action on the account.
4/// When a method returns an AccountAction, this means this message needs to be dispatched to the account using the [`Execution`](crate::Execution) api.
5///
6/// If required you can create an AccountAction from a CosmosMsg using `AccountAction::from(msg)`.
7#[derive(Debug, Default, PartialEq, Clone)]
8#[must_use = "Pass AccountAction to the Executor, see the docs"]
9pub struct AccountAction(Vec<CosmosMsg>);
10
11impl AccountAction {
12    /// Access the underlying messages
13    /// Don't use this to execute the action, use the `Execution` API instead.
14    pub fn messages(&self) -> Vec<CosmosMsg> {
15        self.0.clone()
16    }
17
18    /// Merge two AccountActions into one.
19    pub fn merge(&mut self, other: AccountAction) {
20        self.0.extend(other.0)
21    }
22
23    /// Creates an account action from multiple messages
24    pub fn from_vec<T>(msgs: Vec<T>) -> Self
25    where
26        T: Into<CosmosMsg>,
27    {
28        Self(msgs.into_iter().map(Into::into).collect())
29    }
30}
31
32impl<T> From<T> for AccountAction
33where
34    T: Into<CosmosMsg>,
35{
36    fn from(m: T) -> Self {
37        Self(vec![m.into()])
38    }
39}
40
41#[cfg(test)]
42mod test {
43    use cosmwasm_std::coins;
44
45    use super::*;
46
47    #[coverage_helper::test]
48    fn account_action() {
49        let mut account_action =
50            AccountAction::from_vec(vec![CosmosMsg::Bank(cosmwasm_std::BankMsg::Burn {
51                amount: coins(5, "test"),
52            })]);
53        assert_eq!(
54            account_action.messages(),
55            vec![CosmosMsg::Bank(cosmwasm_std::BankMsg::Burn {
56                amount: coins(5, "test"),
57            })]
58        );
59
60        // merge
61        account_action.merge(account_action.clone());
62        assert_eq!(
63            account_action.messages(),
64            vec![
65                CosmosMsg::Bank(cosmwasm_std::BankMsg::Burn {
66                    amount: coins(5, "test"),
67                }),
68                CosmosMsg::Bank(cosmwasm_std::BankMsg::Burn {
69                    amount: coins(5, "test"),
70                })
71            ]
72        )
73    }
74}