casper_types/transfer/
transfer_v2.rs

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use alloc::vec::Vec;

#[cfg(feature = "datasize")]
use datasize::DataSize;
#[cfg(feature = "json-schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{
    account::AccountHash,
    bytesrepr::{self, FromBytes, ToBytes},
    transaction::TransactionHash,
    Gas, InitiatorAddr, URef, U512,
};

/// Represents a version 2 transfer from one purse to another.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Debug)]
#[cfg_attr(feature = "datasize", derive(DataSize))]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct TransferV2 {
    /// Transaction that created the transfer.
    pub transaction_hash: TransactionHash,
    /// Entity from which transfer was executed.
    pub from: InitiatorAddr,
    /// Account to which funds are transferred.
    pub to: Option<AccountHash>,
    /// Source purse.
    pub source: URef,
    /// Target purse.
    pub target: URef,
    /// Transfer amount.
    pub amount: U512,
    /// Gas.
    pub gas: Gas,
    /// User-defined ID.
    pub id: Option<u64>,
}

impl TransferV2 {
    /// Creates a [`TransferV2`].
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        transaction_hash: TransactionHash,
        from: InitiatorAddr,
        to: Option<AccountHash>,
        source: URef,
        target: URef,
        amount: U512,
        gas: Gas,
        id: Option<u64>,
    ) -> Self {
        TransferV2 {
            transaction_hash,
            from,
            to,
            source,
            target,
            amount,
            gas,
            id,
        }
    }
}

impl ToBytes for TransferV2 {
    fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
        let mut buf = Vec::new();
        self.write_bytes(&mut buf)?;
        Ok(buf)
    }

    fn serialized_length(&self) -> usize {
        self.transaction_hash.serialized_length()
            + self.from.serialized_length()
            + self.to.serialized_length()
            + self.source.serialized_length()
            + self.target.serialized_length()
            + self.amount.serialized_length()
            + self.gas.serialized_length()
            + self.id.serialized_length()
    }

    fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
        self.transaction_hash.write_bytes(writer)?;
        self.from.write_bytes(writer)?;
        self.to.write_bytes(writer)?;
        self.source.write_bytes(writer)?;
        self.target.write_bytes(writer)?;
        self.amount.write_bytes(writer)?;
        self.gas.write_bytes(writer)?;
        self.id.write_bytes(writer)
    }
}

impl FromBytes for TransferV2 {
    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
        let (transaction_hash, remainder) = TransactionHash::from_bytes(bytes)?;
        let (from, remainder) = InitiatorAddr::from_bytes(remainder)?;
        let (to, remainder) = <Option<AccountHash>>::from_bytes(remainder)?;
        let (source, remainder) = URef::from_bytes(remainder)?;
        let (target, remainder) = URef::from_bytes(remainder)?;
        let (amount, remainder) = U512::from_bytes(remainder)?;
        let (gas, remainder) = Gas::from_bytes(remainder)?;
        let (id, remainder) = <Option<u64>>::from_bytes(remainder)?;
        Ok((
            TransferV2 {
                transaction_hash,
                from,
                to,
                source,
                target,
                amount,
                gas,
                id,
            },
            remainder,
        ))
    }
}