Skip to content

Commit 1c2b2f6

Browse files
committed
refactor: Extract payment creation logic into create_payment_from_tx
1 parent 4caf1b6 commit 1c2b2f6

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

src/wallet/mod.rs

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -192,45 +192,14 @@ impl Wallet {
192192
(PaymentStatus::Pending, ConfirmationStatus::Unconfirmed)
193193
},
194194
};
195-
// TODO: It would be great to introduce additional variants for
196-
// `ChannelFunding` and `ChannelClosing`. For the former, we could just
197-
// take a reference to `ChannelManager` here and check against
198-
// `list_channels`. But for the latter the best approach is much less
199-
// clear: for force-closes/HTLC spends we should be good querying
200-
// `OutputSweeper::tracked_spendable_outputs`, but regular channel closes
201-
// (i.e., `SpendableOutputDescriptor::StaticOutput` variants) are directly
202-
// spent to a wallet address. The only solution I can come up with is to
203-
// create and persist a list of 'static pending outputs' that we could use
204-
// here to determine the `PaymentKind`, but that's not really satisfactory, so
205-
// we're punting on it until we can come up with a better solution.
206-
let kind = crate::payment::PaymentKind::Onchain { txid, status: confirmation_status };
207-
let fee = locked_wallet.calculate_fee(&wtx.tx_node.tx).unwrap_or(Amount::ZERO);
208-
let (sent, received) = locked_wallet.sent_and_received(&wtx.tx_node.tx);
209-
let (direction, amount_msat) = if sent > received {
210-
let direction = PaymentDirection::Outbound;
211-
let amount_msat = Some(
212-
sent.to_sat().saturating_sub(fee.to_sat()).saturating_sub(received.to_sat())
213-
* 1000,
214-
);
215-
(direction, amount_msat)
216-
} else {
217-
let direction = PaymentDirection::Inbound;
218-
let amount_msat = Some(
219-
received.to_sat().saturating_sub(sent.to_sat().saturating_sub(fee.to_sat()))
220-
* 1000,
221-
);
222-
(direction, amount_msat)
223-
};
224195

225-
let fee_paid_msat = Some(fee.to_sat() * 1000);
226-
227-
let payment = PaymentDetails::new(
196+
let payment = self.create_payment_from_tx(
197+
locked_wallet,
198+
txid,
228199
id,
229-
kind,
230-
amount_msat,
231-
fee_paid_msat,
232-
direction,
200+
&wtx.tx_node.tx,
233201
payment_status,
202+
confirmation_status,
234203
);
235204

236205
self.payment_store.insert_or_update(payment)?;
@@ -806,6 +775,46 @@ impl Wallet {
806775

807776
Ok(tx)
808777
}
778+
779+
fn create_payment_from_tx(
780+
&self, locked_wallet: &PersistedWallet<KVStoreWalletPersister>, txid: Txid,
781+
payment_id: PaymentId, tx: &Transaction, payment_status: PaymentStatus,
782+
confirmation_status: ConfirmationStatus,
783+
) -> PaymentDetails {
784+
// TODO: It would be great to introduce additional variants for
785+
// `ChannelFunding` and `ChannelClosing`. For the former, we could just
786+
// take a reference to `ChannelManager` here and check against
787+
// `list_channels`. But for the latter the best approach is much less
788+
// clear: for force-closes/HTLC spends we should be good querying
789+
// `OutputSweeper::tracked_spendable_outputs`, but regular channel closes
790+
// (i.e., `SpendableOutputDescriptor::StaticOutput` variants) are directly
791+
// spent to a wallet address. The only solution I can come up with is to
792+
// create and persist a list of 'static pending outputs' that we could use
793+
// here to determine the `PaymentKind`, but that's not really satisfactory, so
794+
// we're punting on it until we can come up with a better solution.
795+
796+
let kind = crate::payment::PaymentKind::Onchain { txid, status: confirmation_status };
797+
798+
let fee = locked_wallet.calculate_fee(tx).unwrap_or(Amount::ZERO);
799+
let (sent, received) = locked_wallet.sent_and_received(tx);
800+
let (direction, amount_msat) = if sent > received {
801+
let direction = PaymentDirection::Outbound;
802+
let amount_msat = Some(
803+
sent.to_sat().saturating_sub(fee.to_sat()).saturating_sub(received.to_sat()) * 1000,
804+
);
805+
(direction, amount_msat)
806+
} else {
807+
let direction = PaymentDirection::Inbound;
808+
let amount_msat = Some(
809+
received.to_sat().saturating_sub(sent.to_sat().saturating_sub(fee.to_sat())) * 1000,
810+
);
811+
(direction, amount_msat)
812+
};
813+
814+
let fee_paid_msat = Some(fee.to_sat() * 1000);
815+
816+
PaymentDetails::new(payment_id, kind, amount_msat, fee_paid_msat, direction, payment_status)
817+
}
809818
}
810819

811820
impl Listen for Wallet {

0 commit comments

Comments
 (0)