Skip to content

Commit 393b939

Browse files
committed
fix: centralize logic and errors
1 parent 190355b commit 393b939

9 files changed

Lines changed: 724 additions & 1274 deletions

File tree

crates/charon-eth2/src/deposit/amounts.rs

Lines changed: 0 additions & 228 deletions
This file was deleted.
Lines changed: 6 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,19 @@
1-
use serde::{Deserialize, Serialize};
2-
3-
/// Gwei represents an amount in Gwei (1 ETH = 1,000,000,000 Gwei)
4-
/// This matches eth2p0.Gwei from the Go implementation.
5-
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
6-
pub struct Gwei(pub u64);
7-
8-
impl Gwei {
9-
/// Create a new Gwei amount
10-
pub const fn new(amount: u64) -> Self {
11-
Self(amount)
12-
}
13-
14-
/// Get the inner u64 value
15-
pub const fn as_u64(self) -> u64 {
16-
self.0
17-
}
18-
}
19-
20-
impl From<u64> for Gwei {
21-
fn from(value: u64) -> Self {
22-
Self(value)
23-
}
24-
}
25-
26-
impl From<Gwei> for u64 {
27-
fn from(value: Gwei) -> Self {
28-
value.0
29-
}
30-
}
31-
32-
impl std::fmt::Display for Gwei {
33-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34-
write!(f, "{}", self.0)
35-
}
36-
}
37-
38-
impl std::ops::Add for Gwei {
39-
type Output = Self;
40-
41-
fn add(self, rhs: Self) -> Self::Output {
42-
Self(self.0.saturating_add(rhs.0))
43-
}
44-
}
45-
46-
impl std::ops::Mul<u64> for Gwei {
47-
type Output = Self;
48-
49-
fn mul(self, rhs: u64) -> Self::Output {
50-
Self(self.0.saturating_mul(rhs))
51-
}
52-
}
53-
54-
impl std::ops::Mul<Gwei> for u64 {
55-
type Output = Gwei;
56-
57-
fn mul(self, rhs: Gwei) -> Self::Output {
58-
Gwei(self.saturating_mul(rhs.0))
59-
}
60-
}
61-
62-
impl std::ops::Sub for Gwei {
63-
type Output = Self;
64-
65-
fn sub(self, rhs: Self) -> Self::Output {
66-
Self(self.0.saturating_sub(rhs.0))
67-
}
68-
}
69-
70-
impl std::ops::Div<u64> for Gwei {
71-
type Output = Self;
72-
73-
fn div(self, rhs: u64) -> Self::Output {
74-
Self(self.0 / rhs)
75-
}
76-
}
77-
78-
// TreeHash implementation for Gwei (delegates to u64)
79-
impl tree_hash::TreeHash for Gwei {
80-
fn tree_hash_type() -> tree_hash::TreeHashType {
81-
u64::tree_hash_type()
82-
}
83-
84-
fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
85-
self.0.tree_hash_packed_encoding()
86-
}
87-
88-
fn tree_hash_packing_factor() -> usize {
89-
u64::tree_hash_packing_factor()
90-
}
91-
92-
fn tree_hash_root(&self) -> tree_hash::Hash256 {
93-
self.0.tree_hash_root()
94-
}
95-
}
1+
use super::types::Gwei;
962

973
/// One ETH in Gwei (1 ETH = 1,000,000,000 Gwei)
98-
pub const ONE_ETH_IN_GWEI: Gwei = Gwei(1_000_000_000);
4+
pub const ONE_ETH_IN_GWEI: Gwei = 1_000_000_000;
995

1006
/// Minimum allowed deposit amount (1 ETH)
101-
pub const MIN_DEPOSIT_AMOUNT: Gwei = Gwei(1_000_000_000);
7+
pub const MIN_DEPOSIT_AMOUNT: Gwei = 1_000_000_000;
1028

1039
/// Default deposit amount (32 ETH)
104-
pub const DEFAULT_DEPOSIT_AMOUNT: Gwei = Gwei(32_000_000_000);
10+
pub const DEFAULT_DEPOSIT_AMOUNT: Gwei = 32_000_000_000;
10511

10612
/// Maximum allowed deposit amount when compounding is enabled (2048 ETH)
107-
pub const MAX_COMPOUNDING_DEPOSIT_AMOUNT: Gwei = Gwei(2_048_000_000_000);
13+
pub const MAX_COMPOUNDING_DEPOSIT_AMOUNT: Gwei = 2_048_000_000_000;
10814

10915
/// Maximum allowed deposit amount when compounding is disabled (32 ETH)
110-
pub const MAX_STANDARD_DEPOSIT_AMOUNT: Gwei = Gwei(32_000_000_000);
16+
pub const MAX_STANDARD_DEPOSIT_AMOUNT: Gwei = 32_000_000_000;
11117

11218
/// Deposit CLI version for compatibility
11319
pub const DEPOSIT_CLI_VERSION: &str = "2.7.0";
@@ -121,16 +27,3 @@ pub const EIP7251_ADDRESS_WITHDRAWAL_PREFIX: u8 = 0x02;
12127
/// DOMAIN_DEPOSIT type as per ETH2 spec
12228
/// See: https://benjaminion.xyz/eth2-annotated-spec/phase0/beacon-chain/#domain-types
12329
pub const DEPOSIT_DOMAIN_TYPE: [u8; 4] = [0x03, 0x00, 0x00, 0x00];
124-
125-
/// Fork version type (4 bytes).
126-
/// Corresponds to eth2p0.Version in Go implementation.
127-
pub type Version = [u8; 4];
128-
129-
/// Domain type (32 bytes).
130-
/// Corresponds to eth2p0.Domain in Go implementation.
131-
pub type Domain = [u8; 32];
132-
133-
/// Root type (32 bytes).
134-
/// Corresponds to eth2p0.Root in Go implementation.
135-
pub type Root = [u8; 32];
136-

0 commit comments

Comments
 (0)