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
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::all)]
#[macro_use]
extern crate alloc;
pub use {fixnum, paste};
use fixnum::typenum::{Unsigned, U18};
use fixnum::FixedPoint;
#[cfg(any(feature = "test", test))]
pub mod mock;
#[cfg(any(feature = "test", test))]
pub mod test_utils;
mod balance_unit;
pub mod cache_storage;
pub mod eth;
mod fixed_wrapper;
pub mod macros;
pub mod migrations;
mod primitives;
pub mod serialization;
mod swap_amount;
mod traits;
pub mod utils;
pub mod weights;
use codec::Encode;
use sp_core::hash::H512;
use sp_runtime::traits::UniqueSaturatedInto;
use sp_runtime::TransactionOutcome;
pub use traits::Config;
pub mod prelude {
pub use super::balance_unit::*;
pub use super::fixed_wrapper::*;
pub use super::primitives::*;
pub use super::serialization::*;
pub use super::swap_amount::*;
pub use super::traits::*;
pub use super::weights::*;
pub use super::{Fixed, FixedInner};
pub use fixnum;
}
use sp_core::crypto::AccountId32;
pub use macros::*;
pub use primitives::*;
pub use traits::*;
pub use utils::*;
pub type Asset<T, GetAssetId> = currencies::Currency<T, GetAssetId>;
pub type Fixed = FixedPoint<FixedInner, FixedPrecision>;
pub type FixedInner = i128;
type FixedPrecision = U18;
pub type Price = Fixed;
pub type Amount = i128;
pub type BasisPoints = u16;
pub const FIXED_PRECISION: u32 = FixedPrecision::U32;
pub fn with_transaction<T, E>(f: impl FnOnce() -> Result<T, E>) -> Result<T, E>
where
E: From<sp_runtime::DispatchError>,
{
frame_support::storage::with_transaction(|| {
let result = f();
if result.is_ok() {
TransactionOutcome::Commit(result)
} else {
TransactionOutcome::Rollback(result)
}
})
}
pub fn hash<T: Encode>(val: &T) -> H512 {
H512::from_slice(blake2_rfc::blake2b::blake2b(64, &[], &val.encode()).as_bytes())
}
pub fn hash_to_u128_pair<T: Encode>(val: &T) -> (u128, u128) {
let data = blake2_rfc::blake2b::blake2b(32, &[], &val.encode());
let bytes = data.as_bytes();
let mut result: (u128, u128) = (0, 0);
for i in 0..16 {
result.0 += (bytes[i] as u128) << (8 * i);
result.1 += (bytes[i + 16] as u128) << (8 * i);
}
result
}
pub fn comm_merkle_op<T: Encode>(val_a: &T, val_b: &T) -> H512 {
use sp_std::ops::BitXor;
let hash_u = H512::from_slice(
blake2_rfc::blake2b::blake2b(64, &[], &(val_a, val_b).encode()).as_bytes(),
);
let hash_v = H512::from_slice(
blake2_rfc::blake2b::blake2b(64, &[], &(val_b, val_a).encode()).as_bytes(),
);
hash_u.bitxor(hash_v)
}
pub fn sort_with_hash_key<'a, T: Encode, V>(
hash_key: H512,
pair_a: (&'a T, &'a V),
pair_b: (&'a T, &'a V),
) -> ((&'a T, &'a V), (&'a T, &'a V)) {
use sp_std::ops::BitXor;
let hash_a = hash(pair_a.0);
let hash_b = hash(pair_b.0);
if hash_key.bitxor(hash_a) < hash_key.bitxor(hash_b) {
(pair_a, pair_b)
} else {
(pair_b, pair_a)
}
}
pub const TECH_ACCOUNT_MAGIC_PREFIX: [u8; 16] = [
84, 115, 79, 144, 249, 113, 160, 44, 96, 155, 45, 104, 78, 97, 181, 87,
];
impl IsRepresentation for AccountId32 {
fn is_representation(&self) -> bool {
let b: [u8; 32] = self.clone().into();
b[0..16] == TECH_ACCOUNT_MAGIC_PREFIX
}
}
type BlockNumberOf<T> = <T as frame_system::Config>::BlockNumber;
type MomentOf<T> = <T as pallet_timestamp::Config>::Moment;
pub fn convert_block_number_to_timestamp<T: Config + pallet_timestamp::Config>(
unlocking_block: BlockNumberOf<T>,
current_block: BlockNumberOf<T>,
current_timestamp: MomentOf<T>,
) -> MomentOf<T> {
if unlocking_block > current_block {
let num_of_seconds: u32 =
((unlocking_block - current_block) * 6u32.into()).unique_saturated_into();
let mut timestamp: T::Moment = num_of_seconds.into();
timestamp = timestamp * 1000u32.into();
current_timestamp + timestamp
} else {
let num_of_seconds: u32 =
((current_block - unlocking_block) * 6u32.into()).unique_saturated_into();
let mut timestamp: T::Moment = num_of_seconds.into();
timestamp = timestamp * 1000u32.into();
current_timestamp - timestamp
}
}