-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathActivities.kt
More file actions
154 lines (137 loc) · 4.05 KB
/
Copy pathActivities.kt
File metadata and controls
154 lines (137 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package to.bitkit.ext
import com.synonym.bitkitcore.Activity
import com.synonym.bitkitcore.LightningActivity
import com.synonym.bitkitcore.OnchainActivity
import com.synonym.bitkitcore.PaymentState
import com.synonym.bitkitcore.PaymentType
fun Activity.rawId(): String = when (this) {
is Activity.Lightning -> v1.id
is Activity.Onchain -> v1.id
}
fun Activity.txType(): PaymentType = when (this) {
is Activity.Lightning -> v1.txType
is Activity.Onchain -> v1.txType
}
/**
* Calculates the total value of an activity based on its type.
*
* For `Lightning` activity, the total value = `value + fee`.
*
* For `Onchain` activity:
* - If it is a send, the total value = `value + fee`.
* - Otherwise it's equal to `value`.
*
* @return The total value as an `ULong`.
*/
fun Activity.totalValue() = when (this) {
is Activity.Lightning -> v1.value + (v1.fee ?: 0u)
is Activity.Onchain -> when (v1.txType) {
PaymentType.SENT -> v1.value + v1.fee
else -> v1.value
}
}
fun Activity.isBoosted() = when (this) {
is Activity.Onchain -> v1.isBoosted
else -> false
}
fun Activity.isFinished() = when (this) {
is Activity.Onchain -> v1.confirmed
is Activity.Lightning -> v1.status != PaymentState.PENDING
}
fun Activity.isBoosting(): Boolean = isBoosted() && !isFinished() && doesExist()
fun Activity.isSent() = when (this) {
is Activity.Lightning -> v1.txType == PaymentType.SENT
is Activity.Onchain -> v1.txType == PaymentType.SENT
}
fun Activity.matchesPaymentId(paymentHashOrTxId: String): Boolean = when (this) {
is Activity.Lightning -> paymentHashOrTxId == v1.id
is Activity.Onchain -> paymentHashOrTxId == v1.txId
}
fun Activity.isTransfer() = this is Activity.Onchain && this.v1.isTransfer
fun Activity.doesExist() = this is Activity.Onchain && this.v1.doesExist
fun Activity.paymentState(): PaymentState? = when (this) {
is Activity.Lightning -> this.v1.status
is Activity.Onchain -> null
}
fun Activity.Onchain.boostType() = when (this.v1.txType) {
PaymentType.SENT -> BoostType.RBF
PaymentType.RECEIVED -> BoostType.CPFP
}
fun Activity.timestamp() = when (this) {
is Activity.Lightning -> v1.timestamp
is Activity.Onchain -> when (v1.confirmed) {
true -> v1.confirmTimestamp ?: v1.timestamp
else -> v1.timestamp
}
}
enum class BoostType { RBF, CPFP }
@Suppress("LongParameterList")
fun LightningActivity.Companion.create(
id: String,
txType: PaymentType,
status: PaymentState,
value: ULong,
invoice: String,
timestamp: ULong,
fee: ULong = 0u,
message: String = "",
preimage: String? = null,
createdAt: ULong? = timestamp,
updatedAt: ULong? = createdAt,
seenAt: ULong? = null,
) = LightningActivity(
id = id,
txType = txType,
status = status,
value = value,
fee = fee,
invoice = invoice,
message = message,
timestamp = timestamp,
preimage = preimage,
createdAt = createdAt,
updatedAt = updatedAt,
seenAt = seenAt,
)
@Suppress("LongParameterList")
fun OnchainActivity.Companion.create(
id: String,
txType: PaymentType,
txId: String,
value: ULong,
fee: ULong,
address: String,
timestamp: ULong,
confirmed: Boolean = false,
feeRate: ULong = 1u,
isBoosted: Boolean = false,
boostTxIds: List<String> = emptyList(),
isTransfer: Boolean = false,
doesExist: Boolean = true,
confirmTimestamp: ULong? = null,
channelId: String? = null,
transferTxId: String? = null,
createdAt: ULong? = timestamp,
updatedAt: ULong? = createdAt,
seenAt: ULong? = null,
) = OnchainActivity(
id = id,
txType = txType,
txId = txId,
value = value,
fee = fee,
feeRate = feeRate,
address = address,
confirmed = confirmed,
timestamp = timestamp,
isBoosted = isBoosted,
boostTxIds = boostTxIds,
isTransfer = isTransfer,
doesExist = doesExist,
confirmTimestamp = confirmTimestamp,
channelId = channelId,
transferTxId = transferTxId,
createdAt = createdAt,
updatedAt = updatedAt,
seenAt = seenAt,
)