waves_rust/model/transaction/
genesis_transaction.rs

1use crate::error::{Error, Result};
2use crate::model::{Address, ByteString};
3use crate::util::JsonDeserializer;
4use crate::waves_proto::GenesisTransactionData;
5use serde_json::Value;
6
7const TYPE: u8 = 1;
8
9#[derive(Clone, Eq, PartialEq, Debug)]
10pub struct GenesisTransactionInfo {
11    recipient: Address,
12    amount: u64,
13}
14
15impl GenesisTransactionInfo {
16    pub fn new(recipient: Address, amount: u64) -> Self {
17        Self { recipient, amount }
18    }
19
20    pub fn recipient(&self) -> Address {
21        self.recipient.clone()
22    }
23
24    pub fn amount(&self) -> u64 {
25        self.amount
26    }
27}
28
29#[derive(Clone, Eq, PartialEq, Debug)]
30pub struct GenesisTransaction {
31    recipient: Address,
32    amount: u64,
33}
34
35impl GenesisTransaction {
36    pub fn new(recipient: Address, amount: u64) -> Self {
37        Self { recipient, amount }
38    }
39
40    pub fn recipient(&self) -> Address {
41        self.recipient.clone()
42    }
43
44    pub fn amount(&self) -> u64 {
45        self.amount
46    }
47
48    pub fn tx_type() -> u8 {
49        TYPE
50    }
51}
52
53impl TryFrom<&GenesisTransaction> for GenesisTransactionData {
54    type Error = Error;
55
56    fn try_from(value: &GenesisTransaction) -> Result<Self> {
57        Ok(GenesisTransactionData {
58            recipient_address: value.recipient().bytes(),
59            amount: value.amount() as i64,
60        })
61    }
62}
63
64impl TryFrom<&Value> for GenesisTransactionInfo {
65    type Error = Error;
66
67    fn try_from(value: &Value) -> Result<Self> {
68        let amount = JsonDeserializer::safe_to_int_from_field(value, "amount")?;
69        let recipient = JsonDeserializer::safe_to_string_from_field(value, "recipient")?;
70        Ok(GenesisTransactionInfo {
71            recipient: Address::from_string(&recipient)?,
72            amount: amount as u64,
73        })
74    }
75}
76
77impl TryFrom<&Value> for GenesisTransaction {
78    type Error = Error;
79
80    fn try_from(value: &Value) -> Result<Self> {
81        let amount = JsonDeserializer::safe_to_int_from_field(value, "amount")?;
82        let recipient = JsonDeserializer::safe_to_string_from_field(value, "recipient")?;
83        Ok(GenesisTransaction {
84            recipient: Address::from_string(&recipient)?,
85            amount: amount as u64,
86        })
87    }
88}
89
90#[cfg(test)]
91mod tests {
92    use crate::error::Result;
93    use crate::model::{
94        Address, ByteString, GenesisTransaction, GenesisTransactionInfo, SignedTransaction,
95        TransactionInfoResponse,
96    };
97    use crate::waves_proto::GenesisTransactionData;
98    use serde_json::Value;
99    use std::borrow::Borrow;
100    use std::fs;
101
102    #[test]
103    fn test_json_to_genesis_transaction() -> Result<()> {
104        let data =
105            fs::read_to_string("./tests/resources/genesis_rs.json").expect("Unable to read file");
106        let json: Value = serde_json::from_str(&data).expect("failed to generate json from str");
107
108        let genesis_tx: SignedTransaction = json.borrow().try_into()?;
109
110        let genesis_tx_info: TransactionInfoResponse = json.borrow().try_into()?;
111
112        assert_eq!(genesis_tx.id().expect("failed id"), genesis_tx_info.id());
113
114        let genesis_from_json: GenesisTransactionInfo = json.borrow().try_into().unwrap();
115
116        assert_eq!(400000000000000, genesis_from_json.amount());
117        assert_eq!(
118            "3My3KZgFQ3CrVHgz6vGRt8687sH4oAA1qp8",
119            genesis_from_json.recipient().encoded()
120        );
121
122        println!("{:#?}", genesis_tx_info);
123        Ok(())
124    }
125
126    #[test]
127    fn test_create_genesis_transaction() -> Result<()> {
128        let transaction = GenesisTransaction::new(
129            Address::from_string("3My3KZgFQ3CrVHgz6vGRt8687sH4oAA1qp8")?,
130            10,
131        );
132
133        assert_eq!(
134            transaction.recipient().encoded(),
135            "3My3KZgFQ3CrVHgz6vGRt8687sH4oAA1qp8"
136        );
137        assert_eq!(transaction.amount(), 10);
138
139        Ok(())
140    }
141
142    #[test]
143    fn test_genesis_transaction_to_proto() -> Result<()> {
144        let transaction = &GenesisTransaction::new(
145            Address::from_string("3My3KZgFQ3CrVHgz6vGRt8687sH4oAA1qp8")?,
146            10,
147        );
148
149        let proto: GenesisTransactionData = transaction.try_into()?;
150        assert_eq!(transaction.recipient().bytes(), proto.recipient_address);
151        assert_eq!(transaction.amount(), proto.amount as u64);
152
153        Ok(())
154    }
155}