arb_stylus/
ink.rs

1/// Gas unit for EVM gas accounting.
2#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
3#[must_use]
4pub struct Gas(pub u64);
5
6/// Ink unit for Stylus computation metering.
7/// 1 EVM gas = ink_price ink (default 10,000).
8#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
9#[must_use]
10pub struct Ink(pub u64);
11
12macro_rules! impl_math {
13    ($t:ident) => {
14        impl std::ops::Add for $t {
15            type Output = Self;
16            fn add(self, rhs: Self) -> Self {
17                Self(self.0 + rhs.0)
18            }
19        }
20
21        impl std::ops::AddAssign for $t {
22            fn add_assign(&mut self, rhs: Self) {
23                self.0 += rhs.0;
24            }
25        }
26
27        impl std::ops::Sub for $t {
28            type Output = Self;
29            fn sub(self, rhs: Self) -> Self {
30                Self(self.0 - rhs.0)
31            }
32        }
33
34        impl std::ops::SubAssign for $t {
35            fn sub_assign(&mut self, rhs: Self) {
36                self.0 -= rhs.0;
37            }
38        }
39
40        impl std::ops::Mul<u64> for $t {
41            type Output = Self;
42            fn mul(self, rhs: u64) -> Self {
43                Self(self.0 * rhs)
44            }
45        }
46
47        impl $t {
48            pub const fn saturating_add(self, rhs: Self) -> Self {
49                Self(self.0.saturating_add(rhs.0))
50            }
51
52            pub const fn saturating_sub(self, rhs: Self) -> Self {
53                Self(self.0.saturating_sub(rhs.0))
54            }
55
56            pub const fn add(self, rhs: Self) -> Self {
57                Self(self.0 + rhs.0)
58            }
59
60            pub const fn sub(self, rhs: Self) -> Self {
61                Self(self.0 - rhs.0)
62            }
63
64            pub const fn mul(self, rhs: u64) -> Self {
65                Self(self.0 * rhs)
66            }
67
68            pub fn to_be_bytes(self) -> [u8; 8] {
69                self.0.to_be_bytes()
70            }
71        }
72    };
73}
74
75impl_math!(Gas);
76impl_math!(Ink);