casper_types/transfer/
transfer_v1.rs

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