casper_types/transfer/
transfer_v2.rs

1use alloc::vec::Vec;
2
3#[cfg(feature = "datasize")]
4use datasize::DataSize;
5#[cfg(feature = "json-schema")]
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8
9use crate::{
10    account::AccountHash,
11    bytesrepr::{self, FromBytes, ToBytes},
12    transaction::TransactionHash,
13    Gas, InitiatorAddr, URef, U512,
14};
15
16/// Represents a version 2 transfer from one purse to another.
17#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Debug)]
18#[cfg_attr(feature = "datasize", derive(DataSize))]
19#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
20#[serde(deny_unknown_fields)]
21pub struct TransferV2 {
22    /// Transaction that created the transfer.
23    pub transaction_hash: TransactionHash,
24    /// Entity from which transfer was executed.
25    pub from: InitiatorAddr,
26    /// Account to which funds are transferred.
27    pub to: Option<AccountHash>,
28    /// Source purse.
29    pub source: URef,
30    /// Target purse.
31    pub target: URef,
32    /// Transfer amount.
33    pub amount: U512,
34    /// Gas.
35    pub gas: Gas,
36    /// User-defined ID.
37    pub id: Option<u64>,
38}
39
40impl TransferV2 {
41    /// Creates a [`TransferV2`].
42    #[allow(clippy::too_many_arguments)]
43    pub fn new(
44        transaction_hash: TransactionHash,
45        from: InitiatorAddr,
46        to: Option<AccountHash>,
47        source: URef,
48        target: URef,
49        amount: U512,
50        gas: Gas,
51        id: Option<u64>,
52    ) -> Self {
53        TransferV2 {
54            transaction_hash,
55            from,
56            to,
57            source,
58            target,
59            amount,
60            gas,
61            id,
62        }
63    }
64}
65
66impl ToBytes for TransferV2 {
67    fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
68        let mut buf = Vec::new();
69        self.write_bytes(&mut buf)?;
70        Ok(buf)
71    }
72
73    fn serialized_length(&self) -> usize {
74        self.transaction_hash.serialized_length()
75            + self.from.serialized_length()
76            + self.to.serialized_length()
77            + self.source.serialized_length()
78            + self.target.serialized_length()
79            + self.amount.serialized_length()
80            + self.gas.serialized_length()
81            + self.id.serialized_length()
82    }
83
84    fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
85        self.transaction_hash.write_bytes(writer)?;
86        self.from.write_bytes(writer)?;
87        self.to.write_bytes(writer)?;
88        self.source.write_bytes(writer)?;
89        self.target.write_bytes(writer)?;
90        self.amount.write_bytes(writer)?;
91        self.gas.write_bytes(writer)?;
92        self.id.write_bytes(writer)
93    }
94}
95
96impl FromBytes for TransferV2 {
97    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
98        let (transaction_hash, remainder) = TransactionHash::from_bytes(bytes)?;
99        let (from, remainder) = InitiatorAddr::from_bytes(remainder)?;
100        let (to, remainder) = <Option<AccountHash>>::from_bytes(remainder)?;
101        let (source, remainder) = URef::from_bytes(remainder)?;
102        let (target, remainder) = URef::from_bytes(remainder)?;
103        let (amount, remainder) = U512::from_bytes(remainder)?;
104        let (gas, remainder) = Gas::from_bytes(remainder)?;
105        let (id, remainder) = <Option<u64>>::from_bytes(remainder)?;
106        Ok((
107            TransferV2 {
108                transaction_hash,
109                from,
110                to,
111                source,
112                target,
113                amount,
114                gas,
115                id,
116            },
117            remainder,
118        ))
119    }
120}