forked from lightningdevkit/ldk-node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move onchain payments API to
OnchainPaymentsHandler
- Loading branch information
Showing
9 changed files
with
92 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
//! Handlers for different types of payments. | ||
mod bolt11; | ||
mod onchain; | ||
mod spontaneous; | ||
|
||
pub use bolt11::Bolt11PaymentsHandler; | ||
pub use onchain::OnchainPaymentsHandler; | ||
pub use spontaneous::SpontaneousPaymentsHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//! Holds a payment handler allowing to send and receive on-chain payments. | ||
use crate::error::Error; | ||
use crate::logger::{log_error, log_info, FilesystemLogger, Logger}; | ||
use crate::types::Wallet; | ||
|
||
use bitcoin::{Address, Txid}; | ||
|
||
use std::sync::{Arc, RwLock}; | ||
|
||
/// A payment handler allowing to send and receive on-chain payments. | ||
/// | ||
/// Should be retrieved by calling [`Node::onchain`]. | ||
/// | ||
/// [`Node::onchain`]: crate::Node::onchain | ||
pub struct OnchainPaymentsHandler { | ||
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>, | ||
wallet: Arc<Wallet>, | ||
logger: Arc<FilesystemLogger>, | ||
} | ||
|
||
impl OnchainPaymentsHandler { | ||
pub(crate) fn new( | ||
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>, wallet: Arc<Wallet>, | ||
logger: Arc<FilesystemLogger>, | ||
) -> Self { | ||
Self { runtime, wallet, logger } | ||
} | ||
|
||
/// Retrieve a new on-chain/funding address. | ||
pub fn new_onchain_address(&self) -> Result<Address, Error> { | ||
let funding_address = self.wallet.get_new_address()?; | ||
log_info!(self.logger, "Generated new funding address: {}", funding_address); | ||
Ok(funding_address) | ||
} | ||
|
||
/// Send an on-chain payment to the given address. | ||
pub fn send_to_onchain_address( | ||
&self, address: &bitcoin::Address, amount_sats: u64, | ||
) -> Result<Txid, Error> { | ||
let rt_lock = self.runtime.read().unwrap(); | ||
if rt_lock.is_none() { | ||
return Err(Error::NotRunning); | ||
} | ||
|
||
let cur_balance = self.wallet.get_balance()?; | ||
if cur_balance.get_spendable() < amount_sats { | ||
log_error!(self.logger, "Unable to send payment due to insufficient funds."); | ||
return Err(Error::InsufficientFunds); | ||
} | ||
self.wallet.send_to_address(address, Some(amount_sats)) | ||
} | ||
|
||
/// Send an on-chain payment to the given address, draining all the available funds. | ||
pub fn send_all_to_onchain_address(&self, address: &bitcoin::Address) -> Result<Txid, Error> { | ||
let rt_lock = self.runtime.read().unwrap(); | ||
if rt_lock.is_none() { | ||
return Err(Error::NotRunning); | ||
} | ||
|
||
self.wallet.send_to_address(address, None) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters