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
use crate::structs::{ContentionKey, ErrorInfo, Hash, Output, OutputType, StandardContractType, StateSelector, CurrencyAmount, UtxoEntry, Observation, StandardData};
use crate::transaction::amount_data;
use crate::{Address, HashClear, RgResult, SafeOption};

pub fn output_data(address: Vec<u8>, amount: u64) -> Output {
    Output::new(&Address::address_data(address).expect(""), amount as i64)
}

pub fn tx_output_data(address: Address, amount: u64) -> Output {
    Output::new(&address, amount as i64)
}

impl HashClear for Output {
    fn hash_clear(&mut self) {}
}

impl Output {

    pub fn is_request(&self) -> bool {
        self.output_type == Some(OutputType::RequestCall as i32)
    }

    pub fn is_deploy(&self) -> bool {
        self.output_type == Some(OutputType::Deploy as i32)
    }

    pub fn code(&self) -> Option<Vec<u8>> {
        self.contract.as_ref()
            .and_then(|d| d.code_execution_contract.as_ref())
            .and_then(|d| d.code.as_ref())
            .map(|d| d.value.clone())
    }

    pub fn validate_deploy_code(&self) -> RgResult<Vec<u8>> {
        // Validate deploy
        if self.is_deploy() {
            if let Some(d) = self.contract.as_ref()
                .and_then(|d| d.code_execution_contract.as_ref())
                .and_then(|d| d.code.as_ref())
                .map(|d| d.value.clone())
                .filter(|d| !d.is_empty())
                .filter(|d| Address::script_hash(d).ok() == self.address)
            {
                return Ok(d);
            }
        }
        Err(ErrorInfo::error_info("Not a deploy"))
    }

    pub fn pay_update_descendents(&self) -> bool {
        self.contract.as_ref().map(|c| c.pay_update_descendents).unwrap_or(false)
    }

    pub fn request_data(&self) -> RgResult<&Vec<u8>> {
        if self.is_request() {
            if let Some(d) = self.data.as_ref().and_then(|d| d.request.as_ref()) {
                return Ok(&d.value);
            }
        }
        Err(ErrorInfo::error_info("Not a request"))
    }

    pub fn request_contention_key(&self) -> RgResult<ContentionKey> {
        let option = self.address.safe_get_msg("Missing address")?;
        let sel = self.request_selector()?;
        Ok(ContentionKey::contract_request(option, sel))
    }

    pub fn request_selector(&self) -> RgResult<Option<&StateSelector>> {
        if self.is_request() {
            return Ok(self.data.as_ref().and_then(|d|
                d.standard_request.as_ref().and_then(|r| r.selector.as_ref())));
        }
        Err(ErrorInfo::error_info("Not a request"))
    }

    pub fn new(address: &Address, amount: i64) -> Output {
        Output {
            address: Some(address.clone()),
            product_id: None,
            counter_party_proofs: vec![],
            data: amount_data(amount as u64),
            contract: None,
            output_type: None,
            utxo_id: None
        }
    }
    pub fn from_data(data: StandardData) -> Self {
        let mut o = Output::default();
        o.data = Some(data);
        o
    }

    pub fn is_swap(&self) -> bool {
        self.contract.as_ref().and_then(|c| c.standard_contract_type)
            .filter(|&c| c == StandardContractType::Swap as i32).is_some()
    }

    pub fn is_peer_data(&self) -> bool {
        self.data.as_ref().and_then(|c| c.peer_data.as_ref()).is_some()
    }

    pub fn to_utxo_entry(
        &self,
        transaction_hash: &Vec<u8>,
        output_index: u32,
        time: u64,
    ) -> UtxoEntry {
        return UtxoEntry::from_output(self, transaction_hash, output_index as i64, time as i64);
    }

    pub fn utxo_entry(
        &self,
        transaction_hash: &Hash,
        output_index: u32,
        time: u64,
    ) -> UtxoEntry {
        return UtxoEntry::from_output(
            self, &transaction_hash.vec(),
            output_index as i64, time as i64
        );
    }

    pub fn amount(&self) -> u64 {
        self.data.as_ref().unwrap().amount.unwrap() as u64
    }

    pub fn safe_ensure_amount(&self) -> Result<&i64, ErrorInfo> {
        self.data.safe_get_msg("Missing data field on output")?
            .amount.safe_get_msg("Missing amount field on output")
    }

    pub fn observation(&self) -> RgResult<&Observation> {
        self.data.safe_get_msg("Missing data field on output")?
            .observation.safe_get_msg("Missing observation field on output")
    }

    pub fn opt_amount(&self) -> Option<i64> {
        self.data.safe_get_msg("Missing data field on output").ok().and_then(|data| data.amount)
    }

    pub fn opt_amount_typed(&self) -> Option<CurrencyAmount> {
        self.data.safe_get_msg("Missing data field on output").ok().and_then(|data| data.amount)
            .map(|a| CurrencyAmount::from(a))
    }

    pub fn rounded_amount(&self) -> f64 {
        crate::transaction::rounded_balance(self.amount())
    }
}