waves_rust/model/transaction/action/
script_transfer.rs

1use serde_json::Value;
2
3use crate::error::{Error, Result};
4use crate::model::{Address, Amount, AssetId};
5use crate::util::JsonDeserializer;
6
7#[derive(Eq, PartialEq, Clone, Debug)]
8pub struct ScriptTransfer {
9    recipient: Address,
10    amount: Amount,
11}
12
13impl ScriptTransfer {
14    pub fn new(recipient: Address, amount: Amount) -> Self {
15        Self { recipient, amount }
16    }
17
18    pub fn recipient(&self) -> Address {
19        self.recipient.clone()
20    }
21
22    pub fn amount(&self) -> Amount {
23        self.amount.clone()
24    }
25}
26
27impl TryFrom<&Value> for ScriptTransfer {
28    type Error = Error;
29
30    fn try_from(value: &Value) -> Result<ScriptTransfer> {
31        let address = JsonDeserializer::safe_to_string_from_field(value, "address")?;
32        let amount = JsonDeserializer::safe_to_int_from_field(value, "amount")?;
33        let asset_id = match value["asset"].as_str() {
34            Some(asset) => Some(AssetId::from_string(asset)?),
35            None => None,
36        };
37
38        Ok(ScriptTransfer {
39            recipient: Address::from_string(&address)?,
40            amount: Amount::new(amount as u64, asset_id),
41        })
42    }
43}