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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use crate::error::{Error, Result};
use crate::model::{Amount, AssetId, ByteString};
use crate::util::JsonDeserializer;
use crate::waves_proto::{Amount as ProtoAmount, BurnTransactionData};
use serde_json::{Map, Value};

const TYPE: u8 = 6;

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct BurnTransactionInfo {
    amount: Amount,
}

impl BurnTransactionInfo {
    pub fn new(amount: Amount) -> Self {
        Self { amount }
    }

    pub fn amount(&self) -> Amount {
        self.amount.clone()
    }
}

impl TryFrom<&Value> for BurnTransactionInfo {
    type Error = Error;

    fn try_from(value: &Value) -> Result<Self> {
        let amount = JsonDeserializer::safe_to_int_from_field(value, "amount")?;
        let asset_id = match value["assetId"].as_str() {
            Some(asset) => Some(AssetId::from_string(asset)?),
            None => None,
        };

        Ok(BurnTransactionInfo {
            amount: Amount::new(amount as u64, asset_id),
        })
    }
}

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct BurnTransaction {
    amount: Amount,
}

impl BurnTransaction {
    pub fn new(amount: Amount) -> Self {
        Self { amount }
    }

    pub fn tx_type() -> u8 {
        TYPE
    }

    pub fn amount(&self) -> Amount {
        self.amount.clone()
    }
}

impl TryFrom<&Value> for BurnTransaction {
    type Error = Error;

    fn try_from(value: &Value) -> Result<Self> {
        let amount = JsonDeserializer::safe_to_int_from_field(value, "amount")?;
        let asset_id = match value["assetId"].as_str() {
            Some(asset) => Some(AssetId::from_string(asset)?),
            None => None,
        };

        Ok(BurnTransaction {
            amount: Amount::new(amount as u64, asset_id),
        })
    }
}

impl TryFrom<&BurnTransaction> for Map<String, Value> {
    type Error = Error;

    fn try_from(value: &BurnTransaction) -> Result<Self> {
        let mut issue_tx_json = Map::new();
        issue_tx_json.insert(
            "assetId".to_owned(),
            value
                .amount
                .asset_id()
                .map(|asset| asset.encoded().into())
                .unwrap_or(Value::Null),
        );
        issue_tx_json.insert("amount".to_owned(), value.amount.value().into());
        Ok(issue_tx_json)
    }
}

impl TryFrom<&BurnTransaction> for BurnTransactionData {
    type Error = Error;

    fn try_from(value: &BurnTransaction) -> Result<Self> {
        let asset_id = match value.amount.asset_id() {
            Some(asset) => asset.bytes(),
            None => vec![],
        };
        let amount = Some(ProtoAmount {
            asset_id,
            amount: value.amount.value() as i64,
        });

        Ok(BurnTransactionData {
            asset_amount: amount,
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::error::Result;
    use crate::model::{Amount, AssetId, BurnTransaction, BurnTransactionInfo, ByteString};
    use serde_json::{json, Map, Value};
    use std::borrow::Borrow;
    use std::fs;

    #[test]
    fn test_json_to_burn_transaction() -> Result<()> {
        let data =
            fs::read_to_string("./tests/resources/burn_rs.json").expect("Unable to read file");
        let json: Value = serde_json::from_str(&data).expect("failed to generate json from str");

        let burn_from_json: BurnTransactionInfo = json.borrow().try_into()?;

        assert_eq!(
            "8bt2MZjuUCJPmfucPfaZPTXqrxmoCHCC8gVnbjZ7bhH6",
            burn_from_json.amount().asset_id().unwrap().encoded()
        );
        assert_eq!(12, burn_from_json.amount().value());
        Ok(())
    }

    #[test]
    fn test_burn_transaction_to_json() -> Result<()> {
        let burn_transaction = &BurnTransaction::new(Amount::new(
            13,
            Some(AssetId::from_string(
                "8bt2MZjuUCJPmfucPfaZPTXqrxmoCHCC8gVnbjZ7bhH6",
            )?),
        ));
        let map: Map<String, Value> = burn_transaction.try_into()?;
        let json: Value = map.into();
        let expected_json = json!({
            "amount": 13,
            "assetId": "8bt2MZjuUCJPmfucPfaZPTXqrxmoCHCC8gVnbjZ7bhH6"
        });
        assert_eq!(expected_json, json);
        Ok(())
    }
}