-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: charge fee when opening a jit channel
- Loading branch information
Showing
13 changed files
with
315 additions
and
7 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
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 |
---|---|---|
@@ -0,0 +1,149 @@ | ||
use crate::config; | ||
use crate::event::subscriber::Subscriber; | ||
use crate::event::EventInternal; | ||
use crate::event::EventType; | ||
use crate::ln_dlc; | ||
use ln_dlc_node::node::rust_dlc_manager::ChannelId; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
use std::sync::Arc; | ||
use std::sync::Mutex; | ||
use tokio::runtime::Handle; | ||
|
||
#[derive(Clone)] | ||
pub struct ChannelSubscriber { | ||
pub open_channel_tx: Arc<Mutex<Option<EsploraTransaction>>>, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Debug, Clone)] | ||
pub struct EsploraTransaction { | ||
pub txid: String, | ||
pub fee: u32, | ||
} | ||
|
||
impl Subscriber for ChannelSubscriber { | ||
fn notify(&self, event: &EventInternal) { | ||
match event { | ||
EventInternal::ChannelReady(channel_id) => { | ||
self.fetch_channel_opening_fee(channel_id); | ||
} | ||
EventInternal::PaymentClaimed(amount_msats) => { | ||
self.pay_channel_fees(*amount_msats); | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
fn events(&self) -> Vec<EventType> { | ||
vec![EventType::ChannelReady, EventType::PaymentClaimed] | ||
} | ||
} | ||
|
||
impl ChannelSubscriber { | ||
pub fn new() -> Self { | ||
Self { | ||
open_channel_tx: Arc::new(Mutex::new(None)), | ||
} | ||
} | ||
|
||
// attempts to pay channel fee with received payment (amount in msats) | ||
fn pay_channel_fees(&self, amount_msats: u64) { | ||
tracing::debug!("Received payment of {amount_msats}."); | ||
if let Some(transaction) = self.get_funding_transaction() { | ||
tracing::debug!("Trying to pay channel opening fees of {}", transaction.fee); | ||
let funding_tx_fees_msats = (transaction.fee * 1000) as u64; | ||
|
||
if funding_tx_fees_msats > amount_msats { | ||
tracing::warn!("Trying to pay fees with an amount smaller than the fees!") | ||
} | ||
|
||
let receiver = config::get_coordinator_info().pubkey; | ||
match ln_dlc::send_spontaneous_payment(funding_tx_fees_msats, receiver) { | ||
Ok(payment_hash) => { | ||
let payment_hash_as_str = hex::encode(payment_hash.0); | ||
// unset the funding transaction marking it as being paid. | ||
self.unset_funding_transaction(); | ||
tracing::info!("Successfully paid funding transaction fees of {funding_tx_fees_msats} to {receiver}. Payment hash: {payment_hash_as_str}"); | ||
} | ||
Err(e) => { | ||
tracing::error!("Failed to pay funding transaction fees of {funding_tx_fees_msats} to {receiver}. Error: {e:#}"); | ||
} | ||
} | ||
} else { | ||
tracing::debug!("No pending funding transaction found!"); | ||
} | ||
} | ||
|
||
// Fetches the channel opening fee from esplora | ||
fn fetch_channel_opening_fee(&self, channel_id: &ChannelId) { | ||
let channel_id_as_str = hex::encode(channel_id); | ||
tracing::debug!("Received new inbound channel with id {channel_id_as_str}"); | ||
|
||
match ln_dlc::get_funding_transaction(channel_id) { | ||
Ok(txid) => { | ||
let transaction: Option<EsploraTransaction> = tokio::task::block_in_place(|| { | ||
Handle::current().block_on(async move { | ||
tracing::debug!( | ||
"-------------------> Querying {}", | ||
format!("{}/tx/{txid}", config::get_esplora_endpoint()) | ||
); | ||
|
||
match reqwest::get(format!("{}tx/{txid}", config::get_esplora_endpoint())) | ||
.await | ||
{ | ||
Ok(response) => { | ||
// tracing::debug!("---------------> Response: {}", | ||
// response.text().await.unwrap()); | ||
match response.json().await { | ||
Ok(response) => Some(response), | ||
Err(e) => { | ||
tracing::error!( | ||
"Failed to fetch transaction from esplora. Error: {e:#}" | ||
); | ||
None | ||
} | ||
} | ||
} | ||
Err(e) => { | ||
tracing::error!( | ||
"Failed to fetch transaction from esplora. Error: {e:#}" | ||
); | ||
None | ||
} | ||
} | ||
}) | ||
}); | ||
|
||
if let Some(transaction) = transaction { | ||
tracing::debug!( | ||
"Successfully fetched transaction fees of {} for new inbound channel with id {channel_id_as_str}", | ||
transaction.fee | ||
); | ||
self.set_funding_transaction(transaction); | ||
} | ||
} | ||
Err(e) => tracing::error!("{e:#}"), | ||
} | ||
} | ||
|
||
fn set_funding_transaction(&self, transaction: EsploraTransaction) { | ||
*self | ||
.open_channel_tx | ||
.lock() | ||
.expect("Mutex to not be poisoned") = Some(transaction); | ||
} | ||
|
||
fn unset_funding_transaction(&self) { | ||
*self | ||
.open_channel_tx | ||
.lock() | ||
.expect("Mutex to not be poisoned") = None; | ||
} | ||
|
||
fn get_funding_transaction(&self) -> Option<EsploraTransaction> { | ||
self.open_channel_tx | ||
.lock() | ||
.expect("Mutex to not be poisoned") | ||
.clone() | ||
} | ||
} |
Oops, something went wrong.