Skip to main content

superposition_assets/
lib.rs

1#![no_std]
2
3#[repr(u8)]
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum Asset {
6    USDC = 0,
7    ARB = 1,
8    WETH = 2
9}
10
11const fn decode(x: &[u8]) -> [u8; 20] {
12    match const_hex::const_decode_to_array::<20>(x) {
13        Ok(r) => r,
14        Err(_) => panic!(),
15    }
16}
17
18impl From<Asset> for [u8; 20] {
19    fn from(x: Asset) -> Self {
20        match x {
21            Asset::USDC => decode(b"af88d065e77c8cC2239327C5EDb3A432268e5831"),
22            Asset::ARB => decode(b"912ce59144191c1204e64559fe8253a0e49e6548"),
23            Asset::WETH => decode(b"82af49447d8a07e3bd95bd0d56f35241523fbab1"),
24        }
25    }
26}
27
28#[derive(Clone, Copy, Debug, PartialEq, Eq)]
29pub struct InvalidAsset;
30
31impl TryFrom<u8> for Asset {
32    type Error = InvalidAsset;
33
34    fn try_from(x: u8) -> Result<Self, Self::Error> {
35        match x {
36            0 => Ok(Asset::USDC),
37            1 => Ok(Asset::ARB),
38            2 => Ok(Asset::WETH),
39            _ => Err(InvalidAsset),
40        }
41    }
42}
43
44macro_rules! impl_asset_int {
45    ($($ty:ty),* $(,)?) => {
46        $(
47            impl TryFrom<$ty> for Asset {
48                type Error = InvalidAsset;
49
50                fn try_from(x: $ty) -> Result<Self, Self::Error> {
51                    let x = u8::try_from(x).map_err(|_| InvalidAsset)?;
52                    Asset::try_from(x)
53                }
54            }
55
56            impl From<Asset> for $ty {
57                fn from(x: Asset) -> Self {
58                    x as u8 as $ty
59                }
60            }
61        )*
62    };
63}
64
65impl_asset_int!(u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);