1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! Withdrawal & WithdrawalContext utility module.

use eth_types::Address;

use crate::Error;

/// Context of a [`Withdrawal`].
#[derive(Debug, Default)]
pub struct WithdrawalContext {
    /// Unique identifier of a withdrawal. This value starts from 0 and then increases
    /// monotonically.
    id: u64,
}

impl WithdrawalContext {
    /// Return id of this withdrawal.
    pub fn id(&self) -> u64 {
        self.id
    }
}

/// Result of the parsing of an Ethereum Withdrawal.
#[derive(Debug, Copy, Clone, Default)]
pub struct Withdrawal {
    /// Unique identifier of a withdrawal in the whole history of withdrawals.
    pub id: u64,
    /// Unique identifier of a validator.
    pub validator_id: u64,
    /// Address to be withdrawn to.
    pub address: Address,
    /// Withdrawal amount in Gwei.
    pub amount: u64,
}

impl Withdrawal {
    /// Create a new Self
    pub fn new(id: u64, validator_id: u64, address: Address, amount: u64) -> Result<Self, Error> {
        Ok(Self {
            id,
            validator_id,
            address,
            amount,
        })
    }
    /// Return the amount in this withdrawal
    pub fn amount_in_wei(&self) -> u64 {
        self.amount * (10 ^ 9)
    }

    /// Constructor for padding withdrawal in withdrawal circuit
    pub fn padding_withdrawal(id: usize) -> Self {
        Self {
            id: id as u64,
            ..Default::default()
        }
    }
}