Skip to content

Commit

Permalink
add helper methods to update child classes
Browse files Browse the repository at this point in the history
  • Loading branch information
pm47 committed Dec 11, 2024
1 parent 2774f21 commit 600c04d
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.acinq.lightning.db

import fr.acinq.bitcoin.*
import fr.acinq.lightning.Lightning
import fr.acinq.lightning.MilliSatoshi
import fr.acinq.lightning.ShortChannelId
import fr.acinq.lightning.channel.ChannelManagementFees
Expand Down Expand Up @@ -173,6 +174,20 @@ sealed class LightningIncomingPayment(val paymentPreimage: ByteVector32) : Incom

/** A payment expires if it is a [Bolt11IncomingPayment] and its invoice has expired. */
fun isExpired(): Boolean = this is Bolt11IncomingPayment && this.paymentRequest.isExpired()

companion object {
/** Helper method to facilitate updating child classes */
fun LightningIncomingPayment.addReceivedParts(parts: List<Received.Part>, receivedAt: Long): LightningIncomingPayment {
val newReceived = when (val received = this.received) {
null -> Received(parts, receivedAt)
else -> received.copy(parts = received.parts + parts)
}
return when (this) {
is Bolt11IncomingPayment -> copy(received = newReceived)
is Bolt12IncomingPayment -> copy(received = newReceived)
}
}
}
}

/** A normal, Bolt11 invoice-based lightning payment. */
Expand Down Expand Up @@ -210,6 +225,22 @@ sealed class OnChainIncomingPayment : IncomingPayment() {
* used), but both sides have to agree that the funds are usable, a.k.a. "locked".
*/
override val completedAt: Long? get() = lockedAt

companion object {
/** Helper method to facilitate updating child classes */
fun OnChainIncomingPayment.setLocked(lockedAt: Long): OnChainIncomingPayment =
when (this) {
is NewChannelIncomingPayment -> copy(lockedAt = lockedAt)
is SpliceInIncomingPayment -> copy(lockedAt = lockedAt)
}

/** Helper method to facilitate updating child classes */
fun OnChainIncomingPayment.setConfirmed(confirmedAt: Long): OnChainIncomingPayment =
when (this) {
is NewChannelIncomingPayment -> copy(confirmedAt = confirmedAt)
is SpliceInIncomingPayment -> copy(confirmedAt = confirmedAt)
}
}
}

/**
Expand Down

0 comments on commit 600c04d

Please sign in to comment.