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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use alloc::vec::Vec;

use crate::{
    bytesrepr::{self, FromBytes, ToBytes},
    CLType, CLTyped, PublicKey, URef, U512,
};

/// Unbonding purse.
#[cfg_attr(test, derive(Debug))]
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct UnbondingPurse {
    /// Unbonding Purse.
    pub purse: URef,
    /// Unbonding Origin.
    pub origin: PublicKey,
    /// Unbonding Era.
    pub era_of_withdrawal: u64,
    /// Unbonding Amount.
    pub amount: U512,
}

impl ToBytes for UnbondingPurse {
    fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
        let mut result = bytesrepr::allocate_buffer(self)?;
        result.extend(&self.purse.to_bytes()?);
        result.extend(&self.origin.to_bytes()?);
        result.extend(&self.era_of_withdrawal.to_bytes()?);
        result.extend(&self.amount.to_bytes()?);
        Ok(result)
    }
    fn serialized_length(&self) -> usize {
        self.purse.serialized_length()
            + self.origin.serialized_length()
            + self.era_of_withdrawal.serialized_length()
            + self.amount.serialized_length()
    }
}

impl FromBytes for UnbondingPurse {
    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
        let (purse, bytes) = FromBytes::from_bytes(bytes)?;
        let (origin, bytes) = FromBytes::from_bytes(bytes)?;
        let (era_of_withdrawal, bytes) = FromBytes::from_bytes(bytes)?;
        let (amount, bytes) = FromBytes::from_bytes(bytes)?;
        Ok((
            UnbondingPurse {
                purse,
                origin,
                era_of_withdrawal,
                amount,
            },
            bytes,
        ))
    }
}

impl CLTyped for UnbondingPurse {
    fn cl_type() -> CLType {
        CLType::Any
    }
}

#[cfg(test)]
mod tests {
    use super::UnbondingPurse;
    use crate::{bytesrepr, AccessRights, PublicKey, URef, U512};

    #[test]
    fn serialization_roundtrip() {
        let public_key = PublicKey::Ed25519([42; 32]);
        let unbonding_purse = UnbondingPurse {
            purse: URef::new([42; 32], AccessRights::READ_ADD_WRITE),
            origin: public_key,
            era_of_withdrawal: u64::max_value(),
            amount: U512::max_value() - 1,
        };
        bytesrepr::test_serialization_roundtrip(&unbonding_purse);
    }
}