/home/runner/work/creditcoin/creditcoin/pallets/creditcoin/src/helpers.rs
Line | Count | Source (jump to first uncovered line) |
1 | | mod external_address; |
2 | | mod register_transfer; |
3 | | |
4 | | use crate::{ |
5 | | pallet::*, |
6 | | types::{Address, AddressId}, |
7 | | DealOrderId, Error, Guid, Id, TransferId, |
8 | | }; |
9 | | pub use external_address::{address_is_well_formed, generate_external_address}; |
10 | | #[cfg(any(test, feature = "runtime-benchmarks"))] |
11 | | pub use external_address::{EVMAddress, PublicToAddress}; |
12 | | use frame_support::ensure; |
13 | | #[cfg(any(test, feature = "runtime-benchmarks"))] |
14 | | use frame_support::traits::Get; |
15 | | use frame_system::pallet_prelude::*; |
16 | | use sp_runtime::RuntimeAppPublic; |
17 | | use sp_std::prelude::*; |
18 | | use tracing as log; |
19 | | |
20 | | #[allow(unused_macros)] |
21 | | macro_rules! try_get { |
22 | | ($storage: ident <$t: ident>, $key: expr, $err: ident) => { |
23 | | crate::pallet::$storage::<$t>::try_get($key).map_err(|()| crate::pallet::Error::<$t>::$err) |
24 | | }; |
25 | | } |
26 | | |
27 | | macro_rules! try_get_id { |
28 | | ($storage: ident <$t: ident>, $key: expr, $err: ident) => { |
29 | | <crate::pallet::$storage<$t> as DoubleMapExt<_, _, _, _, _, _, _, _, _, _>>::try_get_id( |
30 | | $key, |
31 | | ) |
32 | | .map_err(|()| crate::pallet::Error::<$t>::$err) |
33 | | }; |
34 | | } |
35 | | |
36 | | type DealOrderFor<T> = crate::DealOrder< |
37 | | <T as frame_system::Config>::AccountId, |
38 | | <T as frame_system::Config>::BlockNumber, |
39 | | <T as frame_system::Config>::Hash, |
40 | | <T as pallet_timestamp::Config>::Moment, |
41 | | >; |
42 | | type TransferFor<T> = crate::Transfer< |
43 | | <T as frame_system::Config>::AccountId, |
44 | | <T as frame_system::Config>::BlockNumber, |
45 | | <T as frame_system::Config>::Hash, |
46 | | <T as pallet_timestamp::Config>::Moment, |
47 | | >; |
48 | | |
49 | | impl<T: Config> Pallet<T> { |
50 | 901 | pub fn block_number() -> BlockNumberFor<T> { |
51 | 901 | <frame_system::Pallet<T>>::block_number() |
52 | 901 | } |
53 | 151 | pub fn timestamp() -> T::Moment { |
54 | 151 | <pallet_timestamp::Pallet<T>>::get() |
55 | 151 | } |
56 | 699 | pub fn get_address(address_id: &AddressId<T::Hash>) -> Result<Address<T::AccountId>, Error<T>> { |
57 | 699 | Self::addresses(&address_id).ok_or(Error::<T>::NonExistentAddress) |
58 | 699 | } |
59 | | |
60 | 30 | pub fn authority_id() -> Option<T::AccountId> { |
61 | 30 | let local_keys = crate::crypto::Public::all() |
62 | 30 | .into_iter() |
63 | 30 | .map(|p| sp_core::sr25519::Public::from(p).into()29 ) |
64 | 30 | .collect::<Vec<T::FromAccountId>>(); |
65 | 30 | |
66 | 30 | log::trace!(target: "OCW", "local keys {local_keys:?}"); |
67 | | |
68 | 30 | Authorities::<T>::iter_keys().find_map(|auth| { |
69 | 29 | let acct = auth.clone().into(); |
70 | 29 | local_keys.contains(&acct).then_some(auth) |
71 | 30 | }) |
72 | 30 | } |
73 | | |
74 | 30 | pub fn try_mutate_deal_order_and_transfer( |
75 | 30 | deal_order_id: &DealOrderId<T::BlockNumber, T::Hash>, |
76 | 30 | transfer_id: &TransferId<T::Hash>, |
77 | 30 | mutate_deal: impl FnOnce( |
78 | 30 | &mut DealOrderFor<T>, |
79 | 30 | ) -> Result<Option<crate::Event<T>>, crate::Error<T>>, |
80 | 30 | mutate_transfer: impl FnOnce( |
81 | 30 | &mut TransferFor<T>, |
82 | 30 | &DealOrderFor<T>, |
83 | 30 | ) -> Result<Option<crate::Event<T>>, crate::Error<T>>, |
84 | 30 | ) -> Result<(), crate::Error<T>> { |
85 | 30 | let result = DealOrders::<T>::try_mutate( |
86 | 30 | deal_order_id.expiration(), |
87 | 30 | deal_order_id.hash(), |
88 | 30 | |value| { |
89 | 30 | let deal_order = value.as_mut().ok_or(crate::Error::<T>::NonExistentDealOrder)?0 ; |
90 | 30 | let deal_event20 = mutate_deal(deal_order)?10 ; |
91 | | |
92 | 20 | let transfer_event12 = Transfers::<T>::try_mutate(transfer_id, |value| { |
93 | 20 | let transfer = value.as_mut().ok_or(crate::Error::<T>::NonExistentTransfer)?0 ; |
94 | 20 | mutate_transfer(transfer, deal_order) |
95 | 20 | })?8 ; |
96 | | |
97 | 12 | Ok((deal_event, transfer_event)) |
98 | 30 | }, |
99 | 30 | ); |
100 | 30 | |
101 | 30 | match result { |
102 | 12 | Ok((deal_event, transfer_event)) => { |
103 | 12 | if let Some(event) = deal_event { |
104 | 12 | Self::deposit_event(event); |
105 | 12 | }0 |
106 | 12 | if let Some(event) = transfer_event { |
107 | 12 | Self::deposit_event(event) |
108 | 0 | } |
109 | | |
110 | 12 | Ok(()) |
111 | | }, |
112 | 18 | Err(e) => Err(e), |
113 | | } |
114 | 30 | } |
115 | | |
116 | 285 | pub fn use_guid(guid: &Guid) -> Result<(), Error<T>> { |
117 | 285 | ensure!(!<UsedGuids<T>>::contains_key(guid.clone()), Error::<T>::GuidAlreadyUsed2 ); |
118 | 283 | UsedGuids::<T>::insert(guid, ()); |
119 | 283 | Ok(()) |
120 | 285 | } |
121 | | } |
122 | | |
123 | 4 | pub fn non_paying_error<T: Config>( |
124 | 4 | error: crate::Error<T>, |
125 | 4 | ) -> frame_support::dispatch::DispatchErrorWithPostInfo { |
126 | 4 | frame_support::dispatch::DispatchErrorWithPostInfo { |
127 | 4 | error: error.into(), |
128 | 4 | post_info: frame_support::dispatch::PostDispatchInfo { |
129 | 4 | actual_weight: None, |
130 | 4 | pays_fee: frame_support::weights::Pays::No, |
131 | 4 | }, |
132 | 4 | } |
133 | 4 | } |
134 | | |
135 | | #[cfg(any(test, feature = "runtime-benchmarks"))] |
136 | | #[extend::ext(name = HexToAddress)] |
137 | | pub(crate) impl<'a> &'a str { |
138 | 786 | fn hex_to_address(self) -> crate::ExternalAddress { |
139 | 786 | hex::decode(self.trim_start_matches("0x")).unwrap().try_into().unwrap() |
140 | 786 | } |
141 | 353 | fn into_bounded<S>(self) -> frame_support::BoundedVec<u8, S> |
142 | 353 | where |
143 | 353 | S: Get<u32>, |
144 | 353 | { |
145 | 353 | self.as_bytes().into_bounded() |
146 | 353 | } |
147 | | } |
148 | | |
149 | | #[cfg(any(test, feature = "runtime-benchmarks"))] |
150 | | #[extend::ext] |
151 | | pub(crate) impl<'a, S, T> &'a [T] |
152 | | where |
153 | | S: Get<u32>, |
154 | | T: Clone, |
155 | | { |
156 | 0 | fn try_into_bounded(self) -> Result<frame_support::BoundedVec<T, S>, ()> { |
157 | 0 | core::convert::TryFrom::try_from(self.to_vec()) |
158 | 0 | } |
159 | 353 | fn into_bounded(self) -> frame_support::BoundedVec<T, S> { |
160 | 353 | core::convert::TryFrom::try_from(self.to_vec()).unwrap() |
161 | 353 | } |
162 | | } |