Skip to content

Commit 0bd4afe

Browse files
committed
refactor: Extract payment creation logic into create_payment_from_tx
1 parent 5d0fb14 commit 0bd4afe

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
@@ -179,45 +179,14 @@ impl Wallet {
179179
(PaymentStatus::Pending, ConfirmationStatus::Unconfirmed)
180180
},
181181
};
182-
// TODO: It would be great to introduce additional variants for
183-
// `ChannelFunding` and `ChannelClosing`. For the former, we could just
184-
// take a reference to `ChannelManager` here and check against
185-
// `list_channels`. But for the latter the best approach is much less
186-
// clear: for force-closes/HTLC spends we should be good querying
187-
// `OutputSweeper::tracked_spendable_outputs`, but regular channel closes
188-
// (i.e., `SpendableOutputDescriptor::StaticOutput` variants) are directly
189-
// spent to a wallet address. The only solution I can come up with is to
190-
// create and persist a list of 'static pending outputs' that we could use
191-
// here to determine the `PaymentKind`, but that's not really satisfactory, so
192-
// we're punting on it until we can come up with a better solution.
193-
let kind = crate::payment::PaymentKind::Onchain { txid, status: confirmation_status };
194-
let fee = locked_wallet.calculate_fee(&wtx.tx_node.tx).unwrap_or(Amount::ZERO);
195-
let (sent, received) = locked_wallet.sent_and_received(&wtx.tx_node.tx);
196-
let (direction, amount_msat) = if sent > received {
197-
let direction = PaymentDirection::Outbound;
198-
let amount_msat = Some(
199-
sent.to_sat().saturating_sub(fee.to_sat()).saturating_sub(received.to_sat())
200-
* 1000,
201-
);
202-
(direction, amount_msat)
203-
} else {
204-
let direction = PaymentDirection::Inbound;
205-
let amount_msat = Some(
206-
received.to_sat().saturating_sub(sent.to_sat().saturating_sub(fee.to_sat()))
207-
* 1000,
208-
);
209-
(direction, amount_msat)
210-
};
211182

212-
let fee_paid_msat = Some(fee.to_sat() * 1000);
213-
214-
let payment = PaymentDetails::new(
183+
let payment = self.create_payment_from_tx(
184+
locked_wallet,
185+
txid,
215186
id,
216-
kind,
217-
amount_msat,
218-
fee_paid_msat,
219-
direction,
187+
&wtx.tx_node.tx,
220188
payment_status,
189+
confirmation_status,
221190
);
222191

223192
self.payment_store.insert_or_update(payment)?;
@@ -793,6 +762,46 @@ impl Wallet {
793762

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

798807
impl Listen for Wallet {

0 commit comments

Comments
 (0)