Skip to main content

superposition_assets/
lib.rs

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