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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
use crate::objects::currency_amount::CurrencyAmount;
use crate::objects::entity::Entity;
use crate::objects::on_chain_transaction::OnChainTransaction;
use crate::objects::transaction::Transaction;
use crate::objects::transaction_status::TransactionStatus;
use crate::types::custom_date_formats::custom_date_format;
use crate::types::custom_date_formats::custom_date_format_option;
use crate::types::entity_wrapper::EntityWrapper;
use crate::types::get_entity::GetEntity;
use chrono::{DateTime, Utc};
use serde::Deserialize;
use std::vec::Vec;

/// This is an object representing a transaction which closes a channel on the Lightning Network. This operation allocates balances back to the local and remote nodes.
#[derive(Clone, Deserialize)]
pub struct ChannelClosingTransaction {
    /// The unique identifier of this entity across all Lightspark systems. Should be treated as an opaque string.
    #[serde(rename = "channel_closing_transaction_id")]
    pub id: String,

    /// The date and time when this transaction was initiated.
    #[serde(
        with = "custom_date_format",
        rename = "channel_closing_transaction_created_at"
    )]
    pub created_at: DateTime<Utc>,

    /// The date and time when the entity was last updated.
    #[serde(
        with = "custom_date_format",
        rename = "channel_closing_transaction_updated_at"
    )]
    pub updated_at: DateTime<Utc>,

    /// The current status of this transaction.
    #[serde(rename = "channel_closing_transaction_status")]
    pub status: TransactionStatus,

    /// The date and time when this transaction was completed or failed.
    #[serde(
        with = "custom_date_format_option",
        rename = "channel_closing_transaction_resolved_at"
    )]
    pub resolved_at: Option<DateTime<Utc>>,

    /// The amount of money involved in this transaction.
    #[serde(rename = "channel_closing_transaction_amount")]
    pub amount: CurrencyAmount,

    /// The hash of this transaction, so it can be uniquely identified on the Lightning Network.
    #[serde(rename = "channel_closing_transaction_transaction_hash")]
    pub transaction_hash: Option<String>,

    /// The fees that were paid by the wallet sending the transaction to commit it to the Bitcoin blockchain.
    #[serde(rename = "channel_closing_transaction_fees")]
    pub fees: Option<CurrencyAmount>,

    /// The hash of the block that included this transaction. This will be null for unconfirmed transactions.
    #[serde(rename = "channel_closing_transaction_block_hash")]
    pub block_hash: Option<String>,

    /// The height of the block that included this transaction. This will be zero for unconfirmed transactions.
    #[serde(rename = "channel_closing_transaction_block_height")]
    pub block_height: i64,

    /// The Bitcoin blockchain addresses this transaction was sent to.
    #[serde(rename = "channel_closing_transaction_destination_addresses")]
    pub destination_addresses: Vec<String>,

    /// The number of blockchain confirmations for this transaction in real time.
    #[serde(rename = "channel_closing_transaction_num_confirmations")]
    pub num_confirmations: Option<i64>,

    /// If known, the channel this transaction is closing.
    #[serde(rename = "channel_closing_transaction_channel")]
    pub channel: Option<EntityWrapper>,
}

impl OnChainTransaction for ChannelClosingTransaction {
    /// The fees that were paid by the wallet sending the transaction to commit it to the Bitcoin blockchain.
    fn get_fees(&self) -> Option<CurrencyAmount> {
        self.fees.clone()
    }

    /// The hash of the block that included this transaction. This will be null for unconfirmed transactions.
    fn get_block_hash(&self) -> Option<String> {
        self.block_hash.clone()
    }

    /// The height of the block that included this transaction. This will be zero for unconfirmed transactions.
    fn get_block_height(&self) -> i64 {
        self.block_height
    }

    /// The Bitcoin blockchain addresses this transaction was sent to.
    fn get_destination_addresses(&self) -> Vec<String> {
        self.destination_addresses.clone()
    }

    /// The number of blockchain confirmations for this transaction in real time.
    fn get_num_confirmations(&self) -> Option<i64> {
        self.num_confirmations
    }

    fn type_name(&self) -> &'static str {
        "ChannelClosingTransaction"
    }
}

impl Transaction for ChannelClosingTransaction {
    /// The current status of this transaction.
    fn get_status(&self) -> TransactionStatus {
        self.status.clone()
    }

    /// The date and time when this transaction was completed or failed.
    fn get_resolved_at(&self) -> Option<DateTime<Utc>> {
        self.resolved_at
    }

    /// The amount of money involved in this transaction.
    fn get_amount(&self) -> CurrencyAmount {
        self.amount.clone()
    }

    /// The hash of this transaction, so it can be uniquely identified on the Lightning Network.
    fn get_transaction_hash(&self) -> Option<String> {
        self.transaction_hash.clone()
    }

    fn type_name(&self) -> &'static str {
        "ChannelClosingTransaction"
    }
}

impl Entity for ChannelClosingTransaction {
    /// The unique identifier of this entity across all Lightspark systems. Should be treated as an opaque string.
    fn get_id(&self) -> String {
        self.id.clone()
    }

    /// The date and time when the entity was first created.
    fn get_created_at(&self) -> DateTime<Utc> {
        self.created_at
    }

    /// The date and time when the entity was last updated.
    fn get_updated_at(&self) -> DateTime<Utc> {
        self.updated_at
    }

    fn type_name(&self) -> &'static str {
        "ChannelClosingTransaction"
    }
}

impl GetEntity for ChannelClosingTransaction {
    fn get_entity_query() -> String {
        format!(
            "
        query GetEntity($id: ID!) {{
            entity(id: $id) {{
                ... on ChannelClosingTransaction {{
                    ... ChannelClosingTransactionFragment
                }}
            }}
        }}

        {}",
            FRAGMENT
        )
    }
}

pub const FRAGMENT: &str = "
fragment ChannelClosingTransactionFragment on ChannelClosingTransaction {
    __typename
    channel_closing_transaction_id: id
    channel_closing_transaction_created_at: created_at
    channel_closing_transaction_updated_at: updated_at
    channel_closing_transaction_status: status
    channel_closing_transaction_resolved_at: resolved_at
    channel_closing_transaction_amount: amount {
        __typename
        currency_amount_original_value: original_value
        currency_amount_original_unit: original_unit
        currency_amount_preferred_currency_unit: preferred_currency_unit
        currency_amount_preferred_currency_value_rounded: preferred_currency_value_rounded
        currency_amount_preferred_currency_value_approx: preferred_currency_value_approx
    }
    channel_closing_transaction_transaction_hash: transaction_hash
    channel_closing_transaction_fees: fees {
        __typename
        currency_amount_original_value: original_value
        currency_amount_original_unit: original_unit
        currency_amount_preferred_currency_unit: preferred_currency_unit
        currency_amount_preferred_currency_value_rounded: preferred_currency_value_rounded
        currency_amount_preferred_currency_value_approx: preferred_currency_value_approx
    }
    channel_closing_transaction_block_hash: block_hash
    channel_closing_transaction_block_height: block_height
    channel_closing_transaction_destination_addresses: destination_addresses
    channel_closing_transaction_num_confirmations: num_confirmations
    channel_closing_transaction_channel: channel {
        id
    }
}
";