Skip to content

Commit

Permalink
Merge pull request #21 from lightsparkdev/feat/cancelinvoice
Browse files Browse the repository at this point in the history
Add a cancel_invoice function.
  • Loading branch information
jklein24 authored Nov 28, 2023
2 parents 57002ef + a7b3478 commit bbcf4a0
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 36 deletions.
34 changes: 34 additions & 0 deletions lightspark/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,40 @@ impl<K: OperationSigningKey> LightsparkClient<K> {
Ok(result)
}

/// Cancels an existing unpaid invoice and returns that invoice. Cancelled invoices cannot be paid.
pub async fn cancel_invoice(&self, invoice_id: &str) -> Result<Invoice, Error> {
let operation = format!(
"mutation CancelInvoice(
$invoice_id: ID!
) {{
cancel_invoice(input: {{
invoice_id: $invoice_id
}}) {{
invoice {{
...InvoiceFragment
}}
}}
}}
{}
",
invoice::FRAGMENT
);

let mut variables: HashMap<&str, Value> = HashMap::new();
variables.insert("invoice_id", invoice_id.into());

let value = serde_json::to_value(variables).map_err(Error::ConversionError)?;
let json = self
.requester
.execute_graphql(&operation, Some(value))
.await?;

let result = serde_json::from_value(json["cancel_invoice"]["invoice"].clone())
.map_err(Error::JsonError)?;
Ok(result)
}

pub async fn fund_node(
&self,
node_id: &str,
Expand Down
2 changes: 1 addition & 1 deletion lightspark/src/objects/account_to_nodes_connection.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
use crate::objects::connection::Connection;
use serde::{Deserialize, Serialize};
use std::vec::Vec;

use crate::objects::lightspark_node::LightsparkNodeEnum;
use crate::objects::page_info::PageInfo;
use std::vec::Vec;

/// A connection between an account and the nodes it manages.
#[derive(Debug, Clone, Deserialize, Serialize)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
use crate::objects::connection::Connection;
use crate::objects::payment_request::PaymentRequestEnum;
use serde::{Deserialize, Serialize};

use crate::objects::connection::Connection;
use crate::objects::page_info::PageInfo;
use crate::objects::payment_request::PaymentRequestEnum;
use std::vec::Vec;

#[derive(Debug, Clone, Deserialize, Serialize)]
Expand Down
6 changes: 3 additions & 3 deletions lightspark/src/objects/account_to_transactions_connection.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
use crate::objects::connection::Connection;
use crate::objects::currency_amount::CurrencyAmount;
use crate::objects::page_info::PageInfo;
use crate::objects::transaction::TransactionEnum;
use serde::{Deserialize, Serialize};

use crate::objects::connection::Connection;
use crate::objects::currency_amount::CurrencyAmount;
use crate::objects::page_info::PageInfo;
use std::vec::Vec;

#[derive(Debug, Clone, Deserialize, Serialize)]
Expand Down
7 changes: 7 additions & 0 deletions lightspark/src/objects/cancel_invoice_input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CancelInvoiceInput {
pub invoice_id: String,
}
18 changes: 18 additions & 0 deletions lightspark/src/objects/cancel_invoice_output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
use crate::types::entity_wrapper::EntityWrapper;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CancelInvoiceOutput {
#[serde(rename = "cancel_invoice_output_invoice")]
pub invoice: EntityWrapper,
}

pub const FRAGMENT: &str = "
fragment CancelInvoiceOutputFragment on CancelInvoiceOutput {
__typename
cancel_invoice_output_invoice: invoice {
id
}
}
";
6 changes: 3 additions & 3 deletions lightspark/src/objects/invoice_data.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
use crate::objects::currency_amount::CurrencyAmount;
use crate::objects::payment_request_data::PaymentRequestData;
use crate::types::custom_date_formats::custom_date_format;
use serde::{Deserialize, Serialize};

use crate::objects::bitcoin_network::BitcoinNetwork;
use crate::objects::currency_amount::CurrencyAmount;
use crate::objects::node::NodeEnum;
use crate::objects::payment_request_data::PaymentRequestData;
use crate::types::custom_date_formats::custom_date_format;
use chrono::{DateTime, Utc};

/// This object represents the data associated with a BOLT #11 invoice. You can retrieve this object to receive the relevant data associated with a specific invoice.
Expand Down
5 changes: 3 additions & 2 deletions lightspark/src/objects/lightspark_node.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
use crate::objects::entity::Entity;
use crate::objects::node::Node;

use super::lightspark_node_with_o_s_k::LightsparkNodeWithOSK;
use super::lightspark_node_with_remote_signing::LightsparkNodeWithRemoteSigning;
use crate::objects::balances::Balances;
use crate::objects::blockchain_balance::BlockchainBalance;
use crate::objects::currency_amount::CurrencyAmount;
use crate::objects::entity::Entity;
use crate::objects::lightspark_node_status::LightsparkNodeStatus;
use crate::objects::node::Node;
use crate::types::entity_wrapper::EntityWrapper;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value;
Expand Down
18 changes: 9 additions & 9 deletions lightspark/src/objects/lightspark_node_with_o_s_k.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
use crate::objects::blockchain_balance::BlockchainBalance;
use crate::objects::channel_status::ChannelStatus;
use crate::objects::lightspark_node_status::LightsparkNodeStatus;
use crate::objects::secret::Secret;
use serde::{Deserialize, Serialize};
use std::vec::Vec;

use crate::error::Error;
use crate::objects::balances::Balances;
use crate::objects::bitcoin_network::BitcoinNetwork;
use crate::objects::blockchain_balance::BlockchainBalance;
use crate::objects::channel_status::ChannelStatus;
use crate::objects::currency_amount::CurrencyAmount;
use crate::objects::entity::Entity;
use crate::objects::lightspark_node::LightsparkNode;
use crate::objects::lightspark_node_to_channels_connection::LightsparkNodeToChannelsConnection;
use crate::objects::node::Node;
use crate::objects::node_address_type::NodeAddressType;
use crate::objects::node_to_addresses_connection::NodeToAddressesConnection;
use crate::types::custom_date_formats::custom_date_format;
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;

use crate::objects::bitcoin_network::BitcoinNetwork;
use crate::objects::lightspark_node_status::LightsparkNodeStatus;
use crate::objects::node_address_type::NodeAddressType;
use crate::objects::secret::Secret;
use std::vec::Vec;

/// This is a Lightspark node with OSK.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct LightsparkNodeWithOSK {
Expand Down
16 changes: 8 additions & 8 deletions lightspark/src/objects/lightspark_node_with_remote_signing.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
use crate::objects::blockchain_balance::BlockchainBalance;
use crate::objects::channel_status::ChannelStatus;
use crate::objects::lightspark_node_status::LightsparkNodeStatus;
use serde::{Deserialize, Serialize};
use std::vec::Vec;

use crate::error::Error;
use crate::objects::balances::Balances;
use crate::objects::bitcoin_network::BitcoinNetwork;
use crate::objects::blockchain_balance::BlockchainBalance;
use crate::objects::channel_status::ChannelStatus;
use crate::objects::currency_amount::CurrencyAmount;
use crate::objects::entity::Entity;
use crate::objects::lightspark_node::LightsparkNode;
use crate::objects::lightspark_node_to_channels_connection::LightsparkNodeToChannelsConnection;
use crate::objects::node::Node;
use crate::objects::node_address_type::NodeAddressType;
use crate::objects::node_to_addresses_connection::NodeToAddressesConnection;
use crate::types::custom_date_formats::custom_date_format;
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;

use crate::objects::lightspark_node_status::LightsparkNodeStatus;
use crate::objects::node_address_type::NodeAddressType;
use crate::objects::node_to_addresses_connection::NodeToAddressesConnection;
use std::vec::Vec;

/// This is a Lightspark node with remote signing.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct LightsparkNodeWithRemoteSigning {
Expand Down
2 changes: 2 additions & 0 deletions lightspark/src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub mod api_token;
pub mod balances;
pub mod bitcoin_network;
pub mod blockchain_balance;
pub mod cancel_invoice_input;
pub mod cancel_invoice_output;
pub mod channel;
pub mod channel_closing_transaction;
pub mod channel_fees;
Expand Down
10 changes: 5 additions & 5 deletions lightspark/src/objects/outgoing_payment.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
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 serde::{Deserialize, Serialize};
use serde_json::Value;
use std::vec::Vec;

use crate::error::Error;
use crate::objects::currency_amount::CurrencyAmount;
Expand All @@ -16,11 +11,16 @@ use crate::objects::payment_request_data::PaymentRequestDataEnum;
use crate::objects::post_transaction_data::PostTransactionData;
use crate::objects::rich_text::RichText;
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 crate::types::graphql_requester::GraphQLRequester;
use chrono::{DateTime, Utc};
use serde_json::Value;
use std::collections::HashMap;
use std::vec::Vec;

/// This object represents a Lightning Network payment sent from a Lightspark Node. You can retrieve this object to receive payment related information about any payment sent from your Lightspark Node on the Lightning Network.
#[derive(Debug, Clone, Deserialize, Serialize)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
use crate::objects::connection::Connection;
use crate::objects::payment_request::PaymentRequestEnum;
use serde::{Deserialize, Serialize};

use crate::objects::connection::Connection;
use crate::objects::page_info::PageInfo;
use crate::objects::payment_request::PaymentRequestEnum;
use std::vec::Vec;

#[derive(Debug, Clone, Deserialize, Serialize)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
use crate::objects::connection::Connection;
use crate::objects::page_info::PageInfo;
use crate::objects::transaction::TransactionEnum;
use serde::{Deserialize, Serialize};

use crate::objects::page_info::PageInfo;
use std::vec::Vec;

#[derive(Debug, Clone, Deserialize, Serialize)]
Expand Down

0 comments on commit bbcf4a0

Please sign in to comment.