use crate::error::Error;
use crate::objects::currency_amount::CurrencyAmount;
use crate::objects::entity::Entity;
use crate::objects::request_initiator::RequestInitiator;
use crate::objects::withdrawal_mode::WithdrawalMode;
use crate::objects::withdrawal_request_status::WithdrawalRequestStatus;
use crate::objects::withdrawal_request_to_channel_closing_transactions_connection::WithdrawalRequestToChannelClosingTransactionsConnection;
use crate::objects::withdrawal_request_to_channel_opening_transactions_connection::WithdrawalRequestToChannelOpeningTransactionsConnection;
use crate::objects::withdrawal_request_to_withdrawals_connection::WithdrawalRequestToWithdrawalsConnection;
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 crate::types::graphql_requester::GraphQLRequester;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct WithdrawalRequest {
#[serde(rename = "withdrawal_request_id")]
pub id: String,
#[serde(with = "custom_date_format", rename = "withdrawal_request_created_at")]
pub created_at: DateTime<Utc>,
#[serde(with = "custom_date_format", rename = "withdrawal_request_updated_at")]
pub updated_at: DateTime<Utc>,
#[serde(rename = "withdrawal_request_requested_amount")]
pub requested_amount: CurrencyAmount,
#[serde(rename = "withdrawal_request_amount")]
pub amount: CurrencyAmount,
#[serde(rename = "withdrawal_request_estimated_amount")]
pub estimated_amount: Option<CurrencyAmount>,
#[serde(rename = "withdrawal_request_amount_withdrawn")]
pub amount_withdrawn: Option<CurrencyAmount>,
#[serde(rename = "withdrawal_request_total_fees")]
pub total_fees: Option<CurrencyAmount>,
#[serde(rename = "withdrawal_request_bitcoin_address")]
pub bitcoin_address: String,
#[serde(rename = "withdrawal_request_withdrawal_mode")]
pub withdrawal_mode: WithdrawalMode,
#[serde(rename = "withdrawal_request_status")]
pub status: WithdrawalRequestStatus,
#[serde(
with = "custom_date_format_option",
rename = "withdrawal_request_completed_at"
)]
pub completed_at: Option<DateTime<Utc>>,
#[serde(rename = "withdrawal_request_withdrawal")]
pub withdrawal: Option<EntityWrapper>,
#[serde(rename = "withdrawal_request_idempotency_key")]
pub idempotency_key: Option<String>,
#[serde(rename = "withdrawal_request_initiator")]
pub initiator: RequestInitiator,
#[serde(rename = "__typename")]
pub typename: String,
}
impl Entity for WithdrawalRequest {
fn get_id(&self) -> String {
self.id.clone()
}
fn get_created_at(&self) -> DateTime<Utc> {
self.created_at
}
fn get_updated_at(&self) -> DateTime<Utc> {
self.updated_at
}
fn type_name(&self) -> &'static str {
"WithdrawalRequest"
}
}
impl GetEntity for WithdrawalRequest {
fn get_entity_query() -> String {
format!(
"
query GetEntity($id: ID!) {{
entity(id: $id) {{
... on WithdrawalRequest {{
... WithdrawalRequestFragment
}}
}}
}}
{}",
FRAGMENT
)
}
}
pub const FRAGMENT: &str = "
fragment WithdrawalRequestFragment on WithdrawalRequest {
__typename
withdrawal_request_id: id
withdrawal_request_created_at: created_at
withdrawal_request_updated_at: updated_at
withdrawal_request_requested_amount: requested_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
}
withdrawal_request_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
}
withdrawal_request_estimated_amount: estimated_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
}
withdrawal_request_amount_withdrawn: amount_withdrawn {
__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
}
withdrawal_request_total_fees: total_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
}
withdrawal_request_bitcoin_address: bitcoin_address
withdrawal_request_withdrawal_mode: withdrawal_mode
withdrawal_request_status: status
withdrawal_request_completed_at: completed_at
withdrawal_request_withdrawal: withdrawal {
id
}
withdrawal_request_idempotency_key: idempotency_key
withdrawal_request_initiator: initiator
}
";
impl WithdrawalRequest {
pub async fn get_channel_closing_transactions(
&self,
requester: &impl GraphQLRequester,
first: Option<i64>,
after: Option<String>,
) -> Result<WithdrawalRequestToChannelClosingTransactionsConnection, Error> {
let query = "query FetchWithdrawalRequestToChannelClosingTransactionsConnection($entity_id: ID!, $first: Int, $after: String) {
entity(id: $entity_id) {
... on WithdrawalRequest {
channel_closing_transactions(, first: $first, after: $after) {
__typename
withdrawal_request_to_channel_closing_transactions_connection_count: count
withdrawal_request_to_channel_closing_transactions_connection_page_info: page_info {
__typename
page_info_has_next_page: has_next_page
page_info_has_previous_page: has_previous_page
page_info_start_cursor: start_cursor
page_info_end_cursor: end_cursor
}
withdrawal_request_to_channel_closing_transactions_connection_entities: entities {
__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
}
}
}
}
}
}";
let mut variables: HashMap<&str, Value> = HashMap::new();
variables.insert("entity_id", self.id.clone().into());
variables.insert("first", first.into());
variables.insert("after", after.into());
let value = serde_json::to_value(variables).map_err(Error::ConversionError)?;
let result = requester.execute_graphql(query, Some(value)).await?;
let json = result["entity"]["channel_closing_transactions"].clone();
let result = serde_json::from_value(json).map_err(Error::JsonError)?;
Ok(result)
}
pub async fn get_channel_opening_transactions(
&self,
requester: &impl GraphQLRequester,
first: Option<i64>,
after: Option<String>,
) -> Result<WithdrawalRequestToChannelOpeningTransactionsConnection, Error> {
let query = "query FetchWithdrawalRequestToChannelOpeningTransactionsConnection($entity_id: ID!, $first: Int, $after: String) {
entity(id: $entity_id) {
... on WithdrawalRequest {
channel_opening_transactions(, first: $first, after: $after) {
__typename
withdrawal_request_to_channel_opening_transactions_connection_count: count
withdrawal_request_to_channel_opening_transactions_connection_page_info: page_info {
__typename
page_info_has_next_page: has_next_page
page_info_has_previous_page: has_previous_page
page_info_start_cursor: start_cursor
page_info_end_cursor: end_cursor
}
withdrawal_request_to_channel_opening_transactions_connection_entities: entities {
__typename
channel_opening_transaction_id: id
channel_opening_transaction_created_at: created_at
channel_opening_transaction_updated_at: updated_at
channel_opening_transaction_status: status
channel_opening_transaction_resolved_at: resolved_at
channel_opening_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_opening_transaction_transaction_hash: transaction_hash
channel_opening_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_opening_transaction_block_hash: block_hash
channel_opening_transaction_block_height: block_height
channel_opening_transaction_destination_addresses: destination_addresses
channel_opening_transaction_num_confirmations: num_confirmations
channel_opening_transaction_channel: channel {
id
}
}
}
}
}
}";
let mut variables: HashMap<&str, Value> = HashMap::new();
variables.insert("entity_id", self.id.clone().into());
variables.insert("first", first.into());
variables.insert("after", after.into());
let value = serde_json::to_value(variables).map_err(Error::ConversionError)?;
let result = requester.execute_graphql(query, Some(value)).await?;
let json = result["entity"]["channel_opening_transactions"].clone();
let result = serde_json::from_value(json).map_err(Error::JsonError)?;
Ok(result)
}
pub async fn get_withdrawals(
&self,
requester: &impl GraphQLRequester,
first: Option<i64>,
) -> Result<WithdrawalRequestToWithdrawalsConnection, Error> {
let query = "query FetchWithdrawalRequestToWithdrawalsConnection($entity_id: ID!, $first: Int) {
entity(id: $entity_id) {
... on WithdrawalRequest {
withdrawals(, first: $first) {
__typename
withdrawal_request_to_withdrawals_connection_count: count
withdrawal_request_to_withdrawals_connection_entities: entities {
__typename
withdrawal_id: id
withdrawal_created_at: created_at
withdrawal_updated_at: updated_at
withdrawal_status: status
withdrawal_resolved_at: resolved_at
withdrawal_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
}
withdrawal_transaction_hash: transaction_hash
withdrawal_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
}
withdrawal_block_hash: block_hash
withdrawal_block_height: block_height
withdrawal_destination_addresses: destination_addresses
withdrawal_num_confirmations: num_confirmations
withdrawal_origin: origin {
id
}
}
}
}
}
}";
let mut variables: HashMap<&str, Value> = HashMap::new();
variables.insert("entity_id", self.id.clone().into());
variables.insert("first", first.into());
let value = serde_json::to_value(variables).map_err(Error::ConversionError)?;
let result = requester.execute_graphql(query, Some(value)).await?;
let json = result["entity"]["withdrawals"].clone();
let result = serde_json::from_value(json).map_err(Error::JsonError)?;
Ok(result)
}
}