-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDeriveBalanceStateUseCaseTest.kt
More file actions
645 lines (557 loc) · 25.4 KB
/
Copy pathDeriveBalanceStateUseCaseTest.kt
File metadata and controls
645 lines (557 loc) · 25.4 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
package to.bitkit.usecases
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test
import org.lightningdevkit.ldknode.BalanceDetails
import org.lightningdevkit.ldknode.BalanceSource
import org.lightningdevkit.ldknode.ChannelDetails
import org.lightningdevkit.ldknode.LightningBalance
import org.lightningdevkit.ldknode.OutPoint
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.mockito.kotlin.wheneverBlocking
import to.bitkit.data.SettingsData
import to.bitkit.data.SettingsStore
import to.bitkit.data.entities.TransferEntity
import to.bitkit.models.TransferType
import to.bitkit.repositories.HwWalletRepo
import to.bitkit.repositories.LightningRepo
import to.bitkit.repositories.LightningState
import to.bitkit.repositories.TransferRepo
import to.bitkit.test.BaseUnitTest
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class DeriveBalanceStateUseCaseTest : BaseUnitTest() {
private val lightningRepo: LightningRepo = mock()
private val transferRepo: TransferRepo = mock()
private val settingsStore: SettingsStore = mock()
private val hwWalletRepo: HwWalletRepo = mock()
private lateinit var sut: DeriveBalanceStateUseCase
@Before
fun setUp() {
runBlocking {
whenever(settingsStore.data).thenReturn(flowOf(SettingsData()))
whenever(lightningRepo.lightningState).thenReturn(MutableStateFlow(LightningState()))
whenever(transferRepo.activeTransfers).thenReturn(flowOf(emptyList()))
whenever(hwWalletRepo.wallets).thenReturn(MutableStateFlow(persistentListOf()))
wheneverBlocking { lightningRepo.listSpendableOutputs() }.thenReturn(Result.success(emptyList()))
wheneverBlocking { lightningRepo.getChannelFundableBalance() }.thenReturn(0uL)
whenever(transferRepo.resolveChannelIdForTransfer(any(), any())).thenAnswer { invocation ->
val transfer = invocation.getArgument<TransferEntity>(0)
transfer.channelId
}
wheneverBlocking {
lightningRepo.estimateSendAllFee(anyOrNull(), anyOrNull(), anyOrNull())
}.thenReturn(Result.success(1000uL))
}
sut = DeriveBalanceStateUseCase(
bgDispatcher = testDispatcher,
lightningRepo = lightningRepo,
transferRepo = transferRepo,
settingsStore = settingsStore,
hwWalletRepo = hwWalletRepo,
)
}
@Test
fun `should calculate LSP order transfer to spending using transfer amount`() = test {
val balance = newBalanceDetails()
val amountSats = 50_000uL
val transfers = listOf(
newTransferEntity(
type = TransferType.TO_SPENDING,
amountSats = amountSats.toLong(),
lspOrderId = "lsp-order-id",
channelId = null
)
)
whenever(lightningRepo.getChannels()).thenReturn(emptyList())
whenever(transferRepo.activeTransfers).thenReturn(flowOf(transfers))
val result = sut()
assertTrue(result.isSuccess)
val balanceState = result.getOrThrow()
assertEquals(amountSats, balanceState.balanceInTransferToSpending)
assertEquals(
balance.totalOnchainBalanceSats,
balanceState.totalOnchainSats,
"Onchain balance unchanged - LDK already reflects sent payment to LSP"
)
assertEquals(
balance.totalLightningBalanceSats,
balanceState.totalLightningSats,
"Lightning balance unchanged - channel not open yet"
)
}
@Test
fun `should subtract estimated fee from channelFundableBalance`() = test {
val fundableBalance = 75_000uL
val fee = 2_000uL
val balance = BalanceDetails(
totalOnchainBalanceSats = 100_000u,
spendableOnchainBalanceSats = 0u,
totalAnchorChannelsReserveSats = 10_000u,
totalLightningBalanceSats = 50_000u,
lightningBalances = emptyList(),
pendingBalancesFromChannelClosures = emptyList(),
)
whenever(lightningRepo.getChannelFundableBalance()).thenReturn(fundableBalance)
whenever(lightningRepo.getBalancesAsync()).thenReturn(Result.success(balance))
whenever(lightningRepo.getChannels()).thenReturn(emptyList())
whenever(transferRepo.activeTransfers).thenReturn(flowOf(emptyList()))
wheneverBlocking {
lightningRepo.estimateSendAllFee(anyOrNull(), anyOrNull(), anyOrNull())
}.thenReturn(Result.success(fee))
val result = sut()
assertTrue(result.isSuccess)
assertEquals(fundableBalance - fee, result.getOrThrow().channelFundableBalance)
}
@Test
fun `should use fallback fee for channelFundableBalance when fee estimation fails`() = test {
val fundableBalance = 100_000uL
val expectedFallback = (fundableBalance.toDouble() * DeriveBalanceStateUseCase.FALLBACK_FEE_PERCENT).toULong()
val balance = BalanceDetails(
totalOnchainBalanceSats = 100_000u,
spendableOnchainBalanceSats = 0u,
totalAnchorChannelsReserveSats = 10_000u,
totalLightningBalanceSats = 50_000u,
lightningBalances = emptyList(),
pendingBalancesFromChannelClosures = emptyList(),
)
whenever(lightningRepo.getChannelFundableBalance()).thenReturn(fundableBalance)
whenever(lightningRepo.getBalancesAsync()).thenReturn(Result.success(balance))
whenever(lightningRepo.getChannels()).thenReturn(emptyList())
whenever(transferRepo.activeTransfers).thenReturn(flowOf(emptyList()))
wheneverBlocking {
lightningRepo.estimateSendAllFee(anyOrNull(), anyOrNull(), anyOrNull())
}.thenReturn(Result.failure(Exception("Fee estimation failed")))
val result = sut()
assertTrue(result.isSuccess)
assertEquals(fundableBalance - expectedFallback, result.getOrThrow().channelFundableBalance)
}
@Test
fun `should return zero channelFundableBalance when fundable balance is zero`() = test {
val balance = BalanceDetails(
totalOnchainBalanceSats = 100_000u,
spendableOnchainBalanceSats = 0u,
totalAnchorChannelsReserveSats = 10_000u,
totalLightningBalanceSats = 50_000u,
lightningBalances = emptyList(),
pendingBalancesFromChannelClosures = emptyList(),
)
whenever(lightningRepo.getChannelFundableBalance()).thenReturn(0uL)
whenever(lightningRepo.getBalancesAsync()).thenReturn(Result.success(balance))
whenever(lightningRepo.getChannels()).thenReturn(emptyList())
whenever(transferRepo.activeTransfers).thenReturn(flowOf(emptyList()))
val result = sut()
assertTrue(result.isSuccess)
assertEquals(0uL, result.getOrThrow().channelFundableBalance)
}
@Test
fun `should calculate manual channel transfer to spending using channel balance`() = test {
val channelId = "manual-channel-id"
val amountSats = 30_000uL
val channelBalance = newChannelBalance(channelId, amountSats)
val balance = newBalanceDetails().copy(lightningBalances = listOf(channelBalance))
wheneverBlocking { lightningRepo.getBalancesAsync() }.thenReturn(Result.success(balance))
val channel = mock<ChannelDetails> {
on { this.channelId } doReturn channelId
on { isChannelReady } doReturn false
}
val transfers = listOf(
newTransferEntity(
type = TransferType.MANUAL_SETUP,
amountSats = amountSats.toLong(),
channelId = channelId,
lspOrderId = null
)
)
whenever(lightningRepo.getChannels()).thenReturn(listOf(channel))
whenever(transferRepo.activeTransfers).thenReturn(flowOf(transfers))
val result = sut()
assertTrue(result.isSuccess)
val balanceState = result.getOrThrow()
assertEquals(amountSats, balanceState.balanceInTransferToSpending)
assertEquals(
balance.totalOnchainBalanceSats,
balanceState.totalOnchainSats,
"Onchain balance unchanged - funding tx already spent UTXO"
)
assertEquals(
balance.totalLightningBalanceSats - amountSats,
balanceState.totalLightningSats,
"Lightning balance reduced - pending channel not ready"
)
}
@Test
fun `should move LSP order transfer to pending channel once LDK reports the balance`() = test {
val channelId = "lsp-pending-channel-id"
val amountSats = 50_000uL
val channelBalance = newChannelBalance(channelId, amountSats)
val balance = newBalanceDetails().copy(
lightningBalances = listOf(channelBalance),
totalLightningBalanceSats = amountSats,
)
whenever(lightningRepo.getBalancesAsync()).thenReturn(Result.success(balance))
val channel = mock<ChannelDetails> {
on { this.channelId } doReturn channelId
on { isChannelReady } doReturn false
}
val transfers = listOf(
newTransferEntity(
type = TransferType.TO_SPENDING,
amountSats = amountSats.toLong(),
channelId = null,
lspOrderId = "lsp-order-id",
)
)
whenever(lightningRepo.getChannels()).thenReturn(listOf(channel))
whenever(transferRepo.activeTransfers).thenReturn(flowOf(transfers))
whenever(transferRepo.resolveChannelIdForTransfer(any(), any())).thenReturn(channelId)
val result = sut()
assertTrue(result.isSuccess)
val balanceState = result.getOrThrow()
assertEquals(amountSats, balanceState.balanceInTransferToSpending)
assertEquals(
0uL,
balanceState.totalLightningSats,
"Lightning balance reduced while the discovered LSP channel is still pending"
)
}
@Test
fun `should not double count LSP transfer when pending channel resolves before order refresh`() = test {
val channelId = "lsp-pending-channel-id"
val fundingTxId = "funding-tx-id"
val amountSats = 50_000uL
val channelBalance = newChannelBalance(channelId, amountSats)
val balance = newBalanceDetails().copy(
lightningBalances = listOf(channelBalance),
totalLightningBalanceSats = amountSats,
)
whenever(lightningRepo.getBalancesAsync()).thenReturn(Result.success(balance))
val channel = mock<ChannelDetails> {
on { this.channelId } doReturn channelId
on { isChannelReady } doReturn false
on { fundingTxo } doReturn OutPoint(txid = fundingTxId, vout = 0u)
}
val transfers = listOf(
newTransferEntity(
type = TransferType.TO_SPENDING,
amountSats = amountSats.toLong(),
channelId = null,
fundingTxId = fundingTxId,
lspOrderId = "lsp-order-id",
)
)
whenever(lightningRepo.getChannels()).thenReturn(listOf(channel))
whenever(transferRepo.activeTransfers).thenReturn(flowOf(transfers))
whenever(transferRepo.resolveChannelIdForTransfer(any(), any())).thenAnswer { invocation ->
val transfer = invocation.getArgument<TransferEntity>(0)
val channels = invocation.getArgument<List<ChannelDetails>>(1)
channels.find { it.fundingTxo?.txid == transfer.fundingTxId }?.channelId
}
val result = sut()
assertTrue(result.isSuccess)
val balanceState = result.getOrThrow()
assertEquals(amountSats, balanceState.balanceInTransferToSpending)
assertEquals(0uL, balanceState.totalLightningSats)
}
@Test
fun `should not count manual channel as pending when ready`() = test {
newBalanceDetails()
val channelId = "ready-channel-id"
val channel = mock<ChannelDetails> {
on { this.channelId } doReturn channelId
on { isChannelReady } doReturn true
}
val transfers = listOf(
newTransferEntity(
type = TransferType.MANUAL_SETUP,
amountSats = 30_000L,
channelId = channelId,
lspOrderId = null
)
)
whenever(lightningRepo.getChannels()).thenReturn(listOf(channel))
whenever(transferRepo.activeTransfers).thenReturn(flowOf(transfers))
val result = sut()
assertTrue(result.isSuccess)
val balanceState = result.getOrThrow()
assertEquals(0uL, balanceState.balanceInTransferToSpending)
}
@Test
fun `should count closing channel balance for transfer to savings`() = test {
val channelId = "closing-channel-id"
val amountSats = 40_000uL
val closingChannelBalance = newClosingChannelBalance(channelId, amountSats)
val balance = newBalanceDetails().copy(lightningBalances = listOf(closingChannelBalance))
wheneverBlocking { lightningRepo.getBalancesAsync() }.thenReturn(Result.success(balance))
val transfers = listOf(
newTransferEntity(
type = TransferType.FORCE_CLOSE,
amountSats = amountSats.toLong(),
channelId = channelId,
lspOrderId = null
)
)
whenever(lightningRepo.getChannels()).thenReturn(emptyList())
whenever(transferRepo.activeTransfers).thenReturn(flowOf(transfers))
wheneverBlocking { transferRepo.resolveChannelIdForTransfer(any(), any()) }.thenReturn(channelId)
val result = sut()
assertTrue(result.isSuccess)
val balanceState = result.getOrThrow()
assertEquals(amountSats, balanceState.balanceInTransferToSavings)
assertEquals(
balance.totalOnchainBalanceSats,
balanceState.totalOnchainSats,
"Onchain balance unchanged - closing balance not yet confirmed/swept"
)
assertEquals(
balance.totalLightningBalanceSats - amountSats,
balanceState.totalLightningSats,
"Lightning balance reduced - channel closing balance"
)
}
@Test
fun `should subtract coop close balance from lightning without showing transfer in progress`() = test {
val channelId = "closing-channel-id"
val amountSats = 40_000uL
val closingChannelBalance = newClosingChannelBalance(channelId, amountSats)
val balance = newBalanceDetails().copy(
lightningBalances = listOf(closingChannelBalance),
totalLightningBalanceSats = amountSats,
)
wheneverBlocking { lightningRepo.getBalancesAsync() }.thenReturn(Result.success(balance))
val transfers = listOf(
newTransferEntity(
type = TransferType.COOP_CLOSE,
amountSats = amountSats.toLong(),
channelId = channelId,
lspOrderId = null
)
)
whenever(lightningRepo.getChannels()).thenReturn(emptyList())
whenever(transferRepo.activeTransfers).thenReturn(flowOf(transfers))
wheneverBlocking { transferRepo.resolveChannelIdForTransfer(any(), any()) }.thenReturn(channelId)
val result = sut()
assertTrue(result.isSuccess)
val balanceState = result.getOrThrow()
assertEquals(0uL, balanceState.balanceInTransferToSavings, "No transfer in progress for coop close")
assertEquals(
0uL,
balanceState.totalLightningSats,
"Lightning balance reduced - coop close balance subtracted"
)
}
@Test
fun `should subtract lingering coop close claimable from lightning while keeping new channel balance`() = test {
val closedChannelId = "closed-channel-id"
val newChannelId = "new-channel-id"
val closedChannelSats = 1_531_123uL
val newChannelSats = 62_158uL
val lingeringClosingBalance = newClosingChannelBalance(closedChannelId, closedChannelSats)
val newChannelBalance = newChannelBalance(newChannelId, newChannelSats)
val balance = newBalanceDetails().copy(
totalOnchainBalanceSats = closedChannelSats,
totalLightningBalanceSats = 1_593_281uL,
lightningBalances = listOf(lingeringClosingBalance, newChannelBalance),
)
whenever(lightningRepo.getBalancesAsync()).thenReturn(Result.success(balance))
val newChannel = mock<ChannelDetails> {
on { channelId } doReturn newChannelId
on { isChannelReady } doReturn true
}
whenever(lightningRepo.getChannels()).thenReturn(listOf(newChannel))
whenever(transferRepo.activeTransfers).thenReturn(flowOf(emptyList()))
val result = sut()
assertTrue(result.isSuccess)
val balanceState = result.getOrThrow()
assertEquals(closedChannelSats, balanceState.totalOnchainSats)
assertEquals(newChannelSats, balanceState.totalLightningSats)
assertEquals(0uL, balanceState.balanceInTransferToSavings)
assertEquals(0uL, balanceState.balanceInTransferToSpending)
}
@Test
fun `should keep non coop close claimable in lightning`() = test {
val channelId = "force-closed-channel-id"
val amountSats = 40_000uL
val closingChannelBalance = newClosingChannelBalance(
id = channelId,
sats = amountSats,
source = BalanceSource.COUNTERPARTY_FORCE_CLOSED,
)
val balance = newBalanceDetails().copy(
lightningBalances = listOf(closingChannelBalance),
totalLightningBalanceSats = amountSats,
)
whenever(lightningRepo.getBalancesAsync()).thenReturn(Result.success(balance))
whenever(lightningRepo.getChannels()).thenReturn(emptyList())
whenever(transferRepo.activeTransfers).thenReturn(flowOf(emptyList()))
val result = sut()
assertTrue(result.isSuccess)
val balanceState = result.getOrThrow()
assertEquals(amountSats, balanceState.totalLightningSats)
}
@Test
fun `should calculate zero max send onchain when spendable balance is zero`() = test {
val balance = newBalanceDetails().copy(totalOnchainBalanceSats = 50_000u)
wheneverBlocking { lightningRepo.getBalancesAsync() }.thenReturn(Result.success(balance))
whenever(lightningRepo.getChannels()).thenReturn(emptyList())
val result = sut()
assertTrue(result.isSuccess)
val balanceState = result.getOrThrow()
assertEquals(0u, balanceState.maxSendOnchainSats)
}
@Test
fun `should calculate max send onchain with successful fee estimation`() = test {
val spendableAmount = 100_000uL
val feeAmount = 2_000uL
val expectedMaxSend = spendableAmount - feeAmount // 98_000
val balance = newBalanceDetails().copy(spendableOnchainBalanceSats = spendableAmount)
wheneverBlocking { lightningRepo.getBalancesAsync() }.thenReturn(Result.success(balance))
whenever(lightningRepo.getChannels()).thenReturn(emptyList())
wheneverBlocking {
lightningRepo.estimateSendAllFee(anyOrNull(), anyOrNull(), anyOrNull())
}.thenReturn(Result.success(feeAmount))
val result = sut()
assertTrue(result.isSuccess)
val balanceState = result.getOrThrow()
assertEquals(expectedMaxSend, balanceState.maxSendOnchainSats)
}
@Test
fun `should calculate fallback max send onchain when fee estimation fails`() = test {
val spendableAmount = 100_000uL
val expectedFallback = (spendableAmount.toDouble() * DeriveBalanceStateUseCase.FALLBACK_FEE_PERCENT).toULong()
val expectedMaxSend = spendableAmount - expectedFallback
val balance = newBalanceDetails().copy(spendableOnchainBalanceSats = spendableAmount)
whenever(lightningRepo.getBalancesAsync()).thenReturn(Result.success(balance))
whenever(lightningRepo.getChannels()).thenReturn(emptyList())
wheneverBlocking {
lightningRepo.estimateSendAllFee(anyOrNull(), anyOrNull(), anyOrNull())
}.thenReturn(Result.failure(Exception("Fee estimation failed")))
val result = sut()
assertTrue(result.isSuccess)
val balanceState = result.getOrThrow()
assertEquals(expectedMaxSend, balanceState.maxSendOnchainSats)
}
@Test
fun `should return zero max send when fee exceeds spendable balance`() = test {
val spendableAmount = 100_000uL
val excessiveFee = 150_000uL // Fee exceeds balance
val balance = newBalanceDetails().copy(spendableOnchainBalanceSats = spendableAmount)
wheneverBlocking { lightningRepo.getBalancesAsync() }.thenReturn(Result.success(balance))
whenever(lightningRepo.getChannels()).thenReturn(emptyList())
wheneverBlocking {
lightningRepo.estimateSendAllFee(anyOrNull(), anyOrNull(), anyOrNull())
}.thenReturn(Result.success(excessiveFee))
val result = sut()
assertTrue(result.isSuccess)
val balanceState = result.getOrThrow()
assertEquals(0u, balanceState.maxSendOnchainSats)
}
@Test
fun `should count both spending and savings transfers correctly`() = test {
val toSpending = 30_000uL
val toSavings = 20_000uL
val spendingChannelId = "spending-channel-id"
val savingsChannelId = "savings-channel-id"
val balance = newBalanceDetails().copy(
totalLightningBalanceSats = 50_000u,
lightningBalances = listOf(
newChannelBalance(spendingChannelId, toSpending),
newClosingChannelBalance(savingsChannelId, toSavings)
),
)
wheneverBlocking { lightningRepo.getBalancesAsync() }.thenReturn(Result.success(balance))
val spendingChannel = mock<ChannelDetails> {
on { channelId } doReturn spendingChannelId
on { isChannelReady } doReturn false
}
val transfers = listOf(
newTransferEntity(
type = TransferType.MANUAL_SETUP,
amountSats = toSpending.toLong(),
channelId = spendingChannelId,
lspOrderId = null
),
newTransferEntity(
type = TransferType.FORCE_CLOSE,
amountSats = toSavings.toLong(),
channelId = savingsChannelId,
lspOrderId = null
)
)
whenever(lightningRepo.getChannels()).thenReturn(listOf(spendingChannel))
whenever(transferRepo.activeTransfers).thenReturn(flowOf(transfers))
wheneverBlocking { transferRepo.resolveChannelIdForTransfer(any(), any()) }.thenAnswer { invocation ->
val transfer = invocation.getArgument<TransferEntity>(0)
transfer.channelId
}
val result = sut()
assertTrue(result.isSuccess)
val balanceState = result.getOrThrow()
assertEquals(toSpending, balanceState.balanceInTransferToSpending)
assertEquals(toSavings, balanceState.balanceInTransferToSavings)
assertEquals(
balance.totalOnchainBalanceSats,
balanceState.totalOnchainSats,
"Onchain unchanged - closing balance not yet confirmed/swept, manual channel already reflected"
)
assertEquals(
balance.totalLightningBalanceSats - toSpending - toSavings,
balanceState.totalLightningSats,
"Lightning reduced by manual channel (30k) + transfer to savings (20k)"
)
}
private suspend fun newBalanceDetails() = BalanceDetails(
totalOnchainBalanceSats = 100_000u,
spendableOnchainBalanceSats = 0u,
totalAnchorChannelsReserveSats = 10_000u,
totalLightningBalanceSats = 50_000u,
lightningBalances = emptyList(),
pendingBalancesFromChannelClosures = emptyList(),
).also {
whenever(lightningRepo.getBalancesAsync()).thenReturn(Result.success(it))
}
private fun newChannelBalance(id: String, sats: ULong = 30000u) = LightningBalance.ClaimableOnChannelClose(
channelId = id,
counterpartyNodeId = "node-id",
amountSatoshis = sats,
transactionFeeSatoshis = 0u,
outboundPaymentHtlcRoundedMsat = 0u,
outboundForwardedHtlcRoundedMsat = 0u,
inboundClaimingHtlcRoundedMsat = 0u,
inboundHtlcRoundedMsat = 0u,
)
private fun newClosingChannelBalance(
id: String,
sats: ULong,
source: BalanceSource = BalanceSource.COOP_CLOSE,
) = LightningBalance.ClaimableAwaitingConfirmations(
channelId = id,
counterpartyNodeId = "node-id",
amountSatoshis = sats,
confirmationHeight = 344u,
source = source,
)
private fun newTransferEntity(
type: TransferType,
amountSats: Long,
channelId: String? = null,
fundingTxId: String? = null,
lspOrderId: String? = null,
) = TransferEntity(
id = "test-transfer-${System.currentTimeMillis()}",
type = type,
amountSats = amountSats,
channelId = channelId,
fundingTxId = fundingTxId,
lspOrderId = lspOrderId,
isSettled = false,
createdAt = System.currentTimeMillis(),
)
}