Skip to main content

superposition_assets/
lib.rs

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