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
//! Module to set list of available currencies and implement functions for
//! currency operations

#![cfg_attr(not(feature = "std"), no_std)]

use core::slice::Iter;
use frame_support::codec::{Decode, Encode};
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_runtime::RuntimeDebug;

/// List of all currencies used in system
#[derive(Encode, Decode, Clone, Copy, PartialEq, RuntimeDebug, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[repr(u8)]
pub enum Currency {
    Unknown = 0,
    Usd = 1,
    Eq = 2,
    Eth = 3,
    Btc = 4,
    Eos = 5,
    Dot = 6,
}

impl Currency {
    pub fn iterator() -> Iter<'static, Currency> {
        static CURRENCIES: [Currency; 5] = [
            Currency::Eq,
            Currency::Eth,
            Currency::Btc,
            Currency::Eos,
            Currency::Dot,
        ];
        CURRENCIES.iter()
    }

    pub fn iterator_with_usd() -> Iter<'static, Currency> {
        static CURRENCIES: [Currency; 6] = [
            Currency::Eq,
            Currency::Eth,
            Currency::Btc,
            Currency::Usd,
            Currency::Eos,
            Currency::Dot,
        ];
        CURRENCIES.iter()
    }
}

impl Eq for Currency {}

impl Default for Currency {
    fn default() -> Currency {
        Currency::Unknown
    }
}

impl Currency {
    pub fn value(&self) -> u8 {
        match *self {
            Currency::Unknown => 0x0,
            Currency::Usd => 0x1,
            Currency::Eq => 0x2,
            Currency::Eth => 0x3,
            Currency::Btc => 0x4,
            Currency::Eos => 0x5,
            Currency::Dot => 0x6,
        }
    }
}

impl From<u8> for Currency {
    fn from(orig: u8) -> Self {
        match orig {
            0x0 => Currency::Unknown,
            0x1 => Currency::Usd,
            0x2 => Currency::Eq,
            0x3 => Currency::Eth,
            0x4 => Currency::Btc,
            0x5 => Currency::Eos,
            0x6 => Currency::Dot,
            _ => Currency::Unknown,
        }
    }
}

pub mod test {
    use crate::currency::Currency;
    use sp_std::cmp::Ordering;

    #[derive(Copy, Clone)]
    pub struct CurrencyTag {
        pub currency: Currency,
    }
    impl CurrencyTag {
        pub fn new(currency: Currency) -> CurrencyTag {
            CurrencyTag { currency }
        }
        fn value(&self) -> i32 {
            match self.currency {
                Currency::Btc => 0,
                Currency::Eth => 1,
                Currency::Eos => 2,
                Currency::Usd => 3,
                Currency::Eq => 4,
                Currency::Dot => 5,
                _ => panic!("Unexpected currency"),
            }
        }
    }
    impl PartialEq for CurrencyTag {
        fn eq(&self, _other: &Self) -> bool {
            self.value() == self.value()
        }
    }
    impl Eq for CurrencyTag {}
    impl PartialOrd for CurrencyTag {
        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
            Some(self.value().cmp(&other.value()))
        }
    }
    impl Ord for CurrencyTag {
        fn cmp(&self, other: &Self) -> Ordering {
            self.value().cmp(&other.value())
        }
    }
}