-
Notifications
You must be signed in to change notification settings - Fork 67
/
currency.rs
436 lines (366 loc) · 9.53 KB
/
currency.rs
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! CurrencyId implementation
use codec::{CompactAs, Decode, Encode, EncodeLike, MaxEncodedLen, WrapperTypeEncode};
use composable_support::validation::Validate;
use composable_traits::{assets::Asset, currency::Exponent};
use scale_info::TypeInfo;
use sp_runtime::{
sp_std::{ops::Deref, vec::Vec},
DispatchError, RuntimeDebug,
};
use crate::prelude::*;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use xcm::{latest::prelude::*, v3};
pub trait WellKnownCurrency {
const NATIVE: CurrencyId;
const RELAY_NATIVE: CurrencyId;
fn local_to_remote(id: CurrencyId) -> Option<MultiLocation> {
match id {
id if id == Self::NATIVE => Some(MultiLocation::here()),
id if id == Self::RELAY_NATIVE => Some(MultiLocation::parent()),
_ => None,
}
}
fn remote_to_local(id: MultiLocation) -> Option<CurrencyId> {
match id {
MultiLocation { parents: 0, interior: Junctions::Here } => Some(Self::NATIVE),
MultiLocation { parents: 1, interior: Junctions::Here } => Some(Self::RELAY_NATIVE),
_ => None,
}
}
}
#[derive(
Encode,
Decode,
MaxEncodedLen,
Eq,
PartialEq,
Copy,
Clone,
RuntimeDebug,
PartialOrd,
Ord,
TypeInfo,
CompactAs,
Hash,
)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[repr(transparent)]
#[cfg_attr(feature = "std", serde(transparent))]
pub struct CurrencyId(pub u128);
impl FromStr for CurrencyId {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
u128::from_str(s).map(CurrencyId).map_err(|_| ())
}
}
impl Display for CurrencyId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let CurrencyId(id) = self;
write!(f, "{}", id)
}
}
#[macro_export]
macro_rules! list_assets {
(
$(
$(#[$attr:meta])*
pub const $NAME:ident: CurrencyId = CurrencyId($id:literal $(, $decimals:expr )? );
)*
) => {
$(
$(#[$attr])*
pub const $NAME: CurrencyId = CurrencyId($id);
)*
pub fn native_asset_name(id: u128) -> Result<&'static str, &'static str> {
match id {
$($id => Ok(stringify!($NAME)),)*
_ => Err("Invalid native asset")
}
}
pub fn to_native_id(name: &str) -> Result<CurrencyId, &'static str> {
match name {
$(stringify!($NAME) => Ok(CurrencyId::$NAME),)*
_ => Err("Invalid native asset")
}
}
pub fn remote_decimals_for_local(id: CurrencyId) -> Option<Exponent> {
match id {
$(
$( CurrencyId::$NAME => $decimals, )?
)*
_ => None,
}
}
pub fn list_assets() -> Vec<Asset<u128, VersionedMultiLocation>> {
[
$(Asset {
id: CurrencyId::$NAME.0 as u128,
name: Some(stringify!($NAME).as_bytes().to_vec()),
ratio: None,
decimals: Self::remote_decimals_for_local(CurrencyId::$NAME).unwrap_or(Self::decimals()),
foreign_id: None,
existential_deposit: 0_u128,
},)*
]
.to_vec()
}
}
}
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
impl CurrencyId {
pub const INVALID: CurrencyId = CurrencyId(0);
list_assets! {
// Native Tokens (1 - 100)
/// Native from Picasso
pub const PICA: CurrencyId = CurrencyId(
1
);
/// Native from Composable
pub const LAYR: CurrencyId = CurrencyId(2);
/// Kusama native token
pub const KSM: CurrencyId = CurrencyId(
4
);
// From Picasso
pub const PBLO: CurrencyId = CurrencyId(
5
);
/// DOT from Polkadot
pub const DOT: CurrencyId = CurrencyId(6, None);
pub const KSM_USDT_LPT: CurrencyId = CurrencyId(105, None);
pub const PICA_USDT_LPT: CurrencyId = CurrencyId(106, None);
pub const PICA_KSM_LPT: CurrencyId = CurrencyId(107, None);
/// Staked asset xPICA Token
pub const xPICA: CurrencyId = CurrencyId(1001, None);
/// Staked asset xLAYR Token
pub const xLAYR: CurrencyId = CurrencyId(1002, None);
/// Staked asset xPBLO Token
pub const xPBLO: CurrencyId = CurrencyId(1005, None);
// fNFT Collection IDs (2001 - 100_000_000_000)
/// PICA Stake fNFT Collection
pub const PICA_STAKE_FNFT_COLLECTION: CurrencyId = CurrencyId(2001, None);
/// PBLO Stake fNFT Collection
pub const PBLO_STAKE_FNFT_COLLECTION: CurrencyId = CurrencyId(2005, None);
// Non-Native Tokens (101 - 1000)
/// Karura KAR
pub const KAR: CurrencyId = CurrencyId(
101
);
/// BIFROST BNC
pub const BNC: CurrencyId = CurrencyId(102, None);
/// BIFROST vKSM
pub const vKSM: CurrencyId = CurrencyId(103, None);
/// Moonriver MOVR
pub const MOVR: CurrencyId = CurrencyId(104, None);
/// Karura stable coin(Acala Dollar), not native.
pub const kUSD: CurrencyId = CurrencyId(
129
);
/// Statemine USDT
pub const USDT: CurrencyId = CurrencyId(
130,
Some(6)
);
/// Statemint USDT
pub const USDTP: CurrencyId = CurrencyId(
140,
Some(6)
);
pub const USDC: CurrencyId = CurrencyId(131, None);
/// Wrapped BTC
pub const wBTC: CurrencyId = CurrencyId(132, None);
/// Wrapped ETH
pub const wETH: CurrencyId = CurrencyId(133, None);
/// Staked asset xKSM Token
pub const xKSM: CurrencyId = CurrencyId(1004, None);
}
#[inline(always)]
pub const fn decimals() -> Exponent {
12
}
pub fn unit<T: From<u64>>() -> T {
T::from(10_u64.pow(Self::decimals().into()))
}
pub fn milli<T: From<u64> + Div<Output = T>>() -> T {
Self::unit::<T>() / T::from(1000_u64)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, TypeInfo)]
pub struct ValidateCurrencyId;
impl Validate<CurrencyId, ValidateCurrencyId> for ValidateCurrencyId {
fn validate(input: CurrencyId) -> Result<CurrencyId, &'static str> {
if input != CurrencyId::INVALID {
Ok(input)
} else {
Err("Invalid Currency")
}
}
}
impl Validate<u64, ValidateCurrencyId> for ValidateCurrencyId {
fn validate(input: u64) -> Result<u64, &'static str> {
if input != 0_u64 {
Ok(input)
} else {
Err("Invalid Currency")
}
}
}
impl Validate<u128, ValidateCurrencyId> for ValidateCurrencyId {
fn validate(input: u128) -> Result<u128, &'static str> {
if input != 0_u128 {
Ok(input)
} else {
Err("Invalid Currency")
}
}
}
impl Default for CurrencyId {
#[inline]
fn default() -> Self {
CurrencyId::INVALID
}
}
impl Deref for CurrencyId {
type Target = u128;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<CurrencyId> for u128 {
#[inline]
fn from(id: CurrencyId) -> Self {
id.0
}
}
impl From<u128> for CurrencyId {
#[inline]
fn from(raw: u128) -> Self {
CurrencyId(raw)
}
}
impl From<CurrencyId> for xcm::latest::Junction {
fn from(this: CurrencyId) -> Self {
xcm::latest::Junction::GeneralIndex(this.0)
}
}
mod ops {
use super::CurrencyId;
use core::ops::{Add, Mul};
use sp_runtime::traits::{Bounded, CheckedAdd, CheckedMul, One, Saturating, Zero};
impl Add for CurrencyId {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
CurrencyId(self.0.add(rhs.0))
}
}
impl Mul for CurrencyId {
type Output = CurrencyId;
fn mul(self, rhs: Self) -> Self::Output {
CurrencyId(self.0.mul(rhs.0))
}
}
impl CheckedAdd for CurrencyId {
fn checked_add(&self, v: &Self) -> Option<Self> {
Some(CurrencyId(self.0.checked_add(v.0)?))
}
}
impl CheckedMul for CurrencyId {
fn checked_mul(&self, v: &Self) -> Option<Self> {
Some(CurrencyId(self.0.checked_mul(v.0)?))
}
}
impl Zero for CurrencyId {
fn zero() -> Self {
CurrencyId(0)
}
fn is_zero(&self) -> bool {
self.0.is_zero()
}
}
impl One for CurrencyId {
fn one() -> Self {
CurrencyId(u128::one())
}
}
impl Bounded for CurrencyId {
fn min_value() -> Self {
CurrencyId(u128::min_value())
}
fn max_value() -> Self {
CurrencyId(u128::max_value())
}
}
impl Saturating for CurrencyId {
fn saturating_add(self, rhs: Self) -> Self {
self.0.saturating_add(rhs.0).into()
}
fn saturating_sub(self, rhs: Self) -> Self {
<u128 as Saturating>::saturating_sub(self.0, rhs.0).into()
}
fn saturating_mul(self, rhs: Self) -> Self {
<u128 as Saturating>::saturating_mul(self.0, rhs.0).into()
}
fn saturating_pow(self, exp: usize) -> Self {
<u128 as Saturating>::saturating_pow(self.0, exp).into()
}
}
}
#[allow(clippy::large_enum_variant)]
#[derive(RuntimeDebug, Decode, Encode, Clone, PartialEq, Eq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum ForeignAssetId {
Xcm(VersionedMultiLocation),
IbcIcs20(PrefixedDenom),
}
#[derive(
Ord, PartialOrd, RuntimeDebug, Decode, Encode, Clone, PartialEq, Eq, TypeInfo, MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum VersionedMultiLocation {
#[codec(index = 3)]
V3(v3::MultiLocation),
}
impl From<VersionedMultiLocation> for ForeignAssetId {
fn from(this: VersionedMultiLocation) -> Self {
Self::Xcm(this)
}
}
impl From<PrefixedDenom> for ForeignAssetId {
fn from(this: PrefixedDenom) -> Self {
Self::IbcIcs20(this)
}
}
type InnerDenom = ibc_rs_scale::applications::transfer::PrefixedDenom;
#[derive(Debug, Decode, Clone, PartialEq, Eq, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(transparent))]
pub struct PrefixedDenom(pub InnerDenom);
impl FromStr for PrefixedDenom {
type Err = DispatchError;
fn from_str(s: &str) -> Result<Self, DispatchError> {
InnerDenom::from_str(s)
.map_err(|_| DispatchError::Other("PrefixedDenom parse failed"))
.map(Self)
}
}
impl WrapperTypeEncode for PrefixedDenom {}
impl EncodeLike for PrefixedDenom {}
impl core::ops::Deref for PrefixedDenom {
type Target = InnerDenom;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<InnerDenom> for PrefixedDenom {
fn from(this: InnerDenom) -> Self {
Self(this)
}
}
impl MaxEncodedLen for PrefixedDenom {
fn max_encoded_len() -> usize {
2048
}
}