Skip to content

Commit

Permalink
track payment amt before send
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Dec 4, 2024
1 parent 4abfb0e commit ec07878
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
16 changes: 8 additions & 8 deletions src/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ pub async fn pay_lightning(

info!("Paying invoice {invoice}");

state
.payments
.add_payment(
x_forwarded_for,
invoice.amount_milli_satoshis().unwrap_or(0) / 1000,
)
.await;

let response = lightning_client
.send_payment_sync(lnrpc::SendRequest {
payment_request: invoice.to_string(),
Expand All @@ -121,13 +129,5 @@ pub async fn pay_lightning(
response.payment_preimage
};

state
.payments
.add_payment(
x_forwarded_for,
invoice.amount_milli_satoshis().unwrap_or(0) / 1000,
)
.await;

Ok(hex::encode(payment_preimage))
}
22 changes: 11 additions & 11 deletions src/nostr_dms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ async fn handle_event(event: Event, state: AppState) -> anyhow::Result<()> {
return Err(anyhow::anyhow!("Too many payments"));
}

state
.payments
.add_payment(&event.pubkey.to_string(), amount.to_sat())
.await;

// track for address too
state
.payments
.add_payment(&address.to_string(), amount.to_sat())
.await;

let resp = {
let mut wallet_client = state.lightning_client.clone();
info!("Sending {amount} to {address} from nostr dm");
Expand All @@ -204,17 +215,6 @@ async fn handle_event(event: Event, state: AppState) -> anyhow::Result<()> {
wallet_client.send_coins(req).await?.into_inner()
};

state
.payments
.add_payment(&event.pubkey.to_string(), amount.to_sat())
.await;

// track for address too
state
.payments
.add_payment(&address.to_string(), amount.to_sat())
.await;

let txid = resp.txid;

info!("Sent onchain tx: {txid}");
Expand Down
22 changes: 11 additions & 11 deletions src/onchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ pub async fn pay_onchain(
anyhow::bail!("max amount is 1,000,000");
}

state
.payments
.add_payment(x_forwarded_for, amount.to_sat())
.await;

// track for address too
state
.payments
.add_payment(&address.to_string(), amount.to_sat())
.await;

let resp = {
let mut wallet_client = state.lightning_client.clone();
info!("Sending {amount} to {address}");
Expand All @@ -64,17 +75,6 @@ pub async fn pay_onchain(
wallet_client.send_coins(req).await?.into_inner()
};

state
.payments
.add_payment(x_forwarded_for, amount.to_sat())
.await;

// track for address too
state
.payments
.add_payment(&address.to_string(), amount.to_sat())
.await;

OnchainResponse {
txid: resp.txid,
address: address.to_string(),
Expand Down

0 comments on commit ec07878

Please sign in to comment.