Skip to main content

m10_sdk/account/id/
ext.rs

1use super::AccountId;
2use m10_protos::sdk;
3
4/// Extension trait for [`AccountId`]
5pub trait AccountIdExt {
6    fn involves_account(&self, id: AccountId) -> bool;
7}
8
9impl AccountIdExt for [u8] {
10    #[inline]
11    fn involves_account(&self, id: AccountId) -> bool {
12        AccountId::try_from_be_slice(self)
13            .map(|x| x.is_eq_or_descendant_of(id))
14            .unwrap_or(false)
15    }
16}
17
18impl AccountIdExt for sdk::FinalizedTransfer {
19    #[inline]
20    fn involves_account(&self, id: AccountId) -> bool {
21        self.transfer_steps.iter().any(|step| {
22            step.from_account_id.involves_account(id) || step.to_account_id.involves_account(id)
23        })
24    }
25}
26
27impl AccountIdExt for sdk::CreateTransfer {
28    #[inline]
29    fn involves_account(&self, id: AccountId) -> bool {
30        self.transfer_steps
31            .iter()
32            .any(|step| step.involves_account(id))
33    }
34}
35
36impl AccountIdExt for sdk::TransferStep {
37    #[inline]
38    fn involves_account(&self, id: AccountId) -> bool {
39        self.from_account_id.involves_account(id) || self.to_account_id.involves_account(id)
40    }
41}
42
43impl AccountIdExt for sdk::Target {
44    #[inline]
45    fn involves_account(&self, id: AccountId) -> bool {
46        self.target
47            .as_ref()
48            .map(|t| match t {
49                sdk::target::Target::AccountId(to_id) => to_id.involves_account(id),
50                sdk::target::Target::AnyAccount(()) => true,
51            })
52            .unwrap_or(false)
53    }
54}
55
56impl AccountIdExt for sdk::Action {
57    #[inline]
58    fn involves_account(&self, id: AccountId) -> bool {
59        self.from_account.involves_account(id)
60            || self
61                .target
62                .as_ref()
63                .map(|x| x.involves_account(id))
64                .unwrap_or(false)
65    }
66}
67
68impl AccountIdExt for sdk::InvokeAction {
69    #[inline]
70    fn involves_account(&self, id: AccountId) -> bool {
71        self.from_account.involves_account(id)
72            || self
73                .target
74                .as_ref()
75                .map(|x| x.involves_account(id))
76                .unwrap_or(false)
77    }
78}
79
80impl AccountIdExt for sdk::CreateLedgerAccount {
81    #[inline]
82    fn involves_account(&self, id: AccountId) -> bool {
83        self.parent_id.involves_account(id)
84    }
85}
86
87impl AccountIdExt for sdk::CreateToken {
88    #[inline]
89    fn involves_account(&self, id: AccountId) -> bool {
90        self.account_id.involves_account(id)
91    }
92}
93
94impl AccountIdExt for sdk::RedeemToken {
95    #[inline]
96    fn involves_account(&self, id: AccountId) -> bool {
97        self.account_id.involves_account(id)
98    }
99}
100
101impl AccountIdExt for sdk::SetFreezeState {
102    #[inline]
103    fn involves_account(&self, id: AccountId) -> bool {
104        self.account_id.involves_account(id)
105    }
106}
107
108impl AccountIdExt for sdk::SetInstrument {
109    #[inline]
110    fn involves_account(&self, id: AccountId) -> bool {
111        self.account_id.involves_account(id)
112    }
113}
114
115impl AccountIdExt for sdk::SetBalanceLimit {
116    #[inline]
117    fn involves_account(&self, id: AccountId) -> bool {
118        self.account_id.involves_account(id)
119    }
120}
121
122impl AccountIdExt for (&sdk::transaction_data::Data, &sdk::TransactionResponse) {
123    #[inline]
124    fn involves_account(&self, id: AccountId) -> bool {
125        use sdk::transaction_data::Data;
126        match self.0 {
127            Data::InvokeAction(action) => action.involves_account(id),
128            Data::DocumentOperations(_) => false,
129            Data::CreateLedgerAccount(create_account) => create_account.involves_account(id),
130            Data::SetFreezeState(set_frozen) => set_frozen.involves_account(id),
131            Data::Transfer(transfer) | Data::InitiateTransfer(transfer) => {
132                transfer.involves_account(id)
133            }
134            Data::CommitTransfer(_) => self
135                .1
136                .transfer_committed
137                .as_ref()
138                .map(|transfer| transfer.involves_account(id))
139                .unwrap_or(false),
140            Data::SetInstrument(set_instrument) => set_instrument.involves_account(id),
141            Data::SetBalanceLimit(set_balance_limit) => set_balance_limit.involves_account(id),
142            Data::CreateToken(create_token) => create_token.involves_account(id),
143            Data::RedeemToken(redeem_token) => redeem_token.involves_account(id),
144        }
145    }
146}
147
148impl AccountIdExt for (&sdk::TransactionRequestPayload, &sdk::TransactionResponse) {
149    #[inline]
150    fn involves_account(&self, id: AccountId) -> bool {
151        self.0
152            .data
153            .as_ref()
154            .and_then(|x| x.data.as_ref())
155            .map(|x| (x, self.1).involves_account(id))
156            .unwrap_or(false)
157    }
158}
159
160impl AccountIdExt for sdk::FinalizedTransaction {
161    #[inline]
162    fn involves_account(&self, id: AccountId) -> bool {
163        if let (Some(req), Some(resp)) = (&self.request, &self.response) {
164            (req, resp).involves_account(id)
165        } else {
166            false
167        }
168    }
169}