Skip to content

Commit 72b9f89

Browse files
authored
feat(receive): add logic for additional CJIT (#145)
1 parent abe28c1 commit 72b9f89

File tree

7 files changed

+74
-28
lines changed

7 files changed

+74
-28
lines changed

Bitkit/ViewModels/WalletViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ class WalletViewModel: ObservableObject {
422422
}
423423
}
424424

425-
var incomingLightningCapacitySats: UInt64? {
425+
var totalInboundLightningSats: UInt64? {
426426
guard let channels else {
427427
return nil
428428
}

Bitkit/Views/Settings/Advanced/LightningConnectionsView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ struct LightningConnectionsView: View {
241241
}
242242

243243
private var receivingCapacity: UInt64 {
244-
wallet.incomingLightningCapacitySats ?? 0
244+
wallet.totalInboundLightningSats ?? 0
245245
}
246246

247247
private var pendingChannels: [ChannelDetails] {

Bitkit/Views/Wallets/Receive/ReceiveCjitAmount.swift

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,8 @@ struct ReceiveCjitAmount: View {
6767
Spacer()
6868

6969
CustomButton(title: t("common__continue"), isDisabled: amountSats < minimumAmount) {
70-
// Wait until node is running if it's in starting state
71-
if await wallet.waitForNodeToRun() {
72-
// Only proceed if node is running
73-
do {
74-
let entry = try await blocktank.createCjit(amountSats: amountSats, description: "Bitkit")
75-
navigationPath.append(.cjitConfirm(entry: entry, receiveAmountSats: amountSats))
76-
} catch {
77-
app.toast(error)
78-
Logger.error(error)
79-
}
80-
} else {
81-
// Show error if node is not running or timed out
82-
app.toast(type: .warning, title: "Lightning not ready", description: "Lightning node must be running to create an invoice")
70+
Task {
71+
await onContinue()
8372
}
8473
}
8574
}
@@ -90,4 +79,21 @@ struct ReceiveCjitAmount: View {
9079
try? await blocktank.refreshMinCjitSats()
9180
}
9281
}
82+
83+
private func onContinue() async {
84+
// Wait until node is running if it's in starting state
85+
if await wallet.waitForNodeToRun() {
86+
// Only proceed if node is running
87+
do {
88+
let entry = try await blocktank.createCjit(amountSats: amountSats, description: "Bitkit")
89+
navigationPath.append(.cjitConfirm(entry: entry, receiveAmountSats: amountSats, isAdditional: false))
90+
} catch {
91+
app.toast(error)
92+
Logger.error(error)
93+
}
94+
} else {
95+
// Show error if node is not running or timed out
96+
app.toast(type: .warning, title: "Lightning not ready", description: "Lightning node must be running to create an invoice")
97+
}
98+
}
9399
}

Bitkit/Views/Wallets/Receive/ReceiveCjitConfirmation.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ struct ReceiveCjitConfirmation: View {
55
@Binding var navigationPath: [ReceiveRoute]
66
let entry: IcJitEntry
77
let receiveAmountSats: UInt64
8+
let isAdditional: Bool
89

910
@EnvironmentObject private var currency: CurrencyViewModel
1011

@@ -36,7 +37,7 @@ struct ReceiveCjitConfirmation: View {
3637

3738
BodyMText(
3839
t(
39-
"wallet__receive_connect_initial",
40+
isAdditional ? "wallet__receive_connect_additional" : "wallet__receive_connect_initial",
4041
variables: [
4142
"networkFee": formattedNetworkFee(),
4243
"serviceFee": formattedServiceFee(),
@@ -64,7 +65,7 @@ struct ReceiveCjitConfirmation: View {
6465

6566
HStack(spacing: 16) {
6667
CustomButton(title: t("common__learn_more"), variant: .secondary) {
67-
navigationPath.append(.cjitLearnMore(entry: entry, receiveAmountSats: receiveAmountSats))
68+
navigationPath.append(.cjitLearnMore(entry: entry, receiveAmountSats: receiveAmountSats, isAdditional: isAdditional))
6869
}
6970

7071
CustomButton(title: t("common__continue")) {

Bitkit/Views/Wallets/Receive/ReceiveCjitLearnMore.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import SwiftUI
44
struct ReceiveCjitLearnMore: View {
55
let entry: IcJitEntry
66
let receiveAmountSats: UInt64
7+
let isAdditional: Bool
78

89
@Environment(\.dismiss) var dismiss
910

10-
// TODO: check if additional CJIT
11-
let isAdditional = false
12-
1311
var text: String {
1412
isAdditional
1513
? t("wallet__receive_liquidity__text_additional")

Bitkit/Views/Wallets/Receive/ReceiveEdit.swift

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import SwiftUI
22

33
struct ReceiveEdit: View {
44
@EnvironmentObject private var app: AppViewModel
5+
@EnvironmentObject private var blocktank: BlocktankViewModel
56
@EnvironmentObject private var currency: CurrencyViewModel
7+
@EnvironmentObject private var transfer: TransferViewModel
68
@EnvironmentObject private var wallet: WalletViewModel
79
@Environment(\.dismiss) private var dismiss
810

@@ -13,6 +15,10 @@ struct ReceiveEdit: View {
1315
@State private var isAmountInputFocused: Bool = false
1416
@FocusState private var isNoteEditorFocused: Bool
1517

18+
var amountSats: UInt64 {
19+
amountViewModel.amountSats
20+
}
21+
1622
var body: some View {
1723
VStack(spacing: 0) {
1824
SheetHeader(title: t("wallet__receive_specify"))
@@ -122,10 +128,17 @@ struct ReceiveEdit: View {
122128
// Wait until node is running if it's in starting state
123129
if await wallet.waitForNodeToRun() {
124130
do {
125-
wallet.invoiceAmountSats = amountViewModel.amountSats
131+
wallet.invoiceAmountSats = amountSats
126132
wallet.invoiceNote = note
127133
try await wallet.refreshBip21(forceRefreshBolt11: true)
128-
dismiss()
134+
135+
// Check if CJIT flow should be shown
136+
if needsAdditionalCjit() {
137+
let entry = try await blocktank.createCjit(amountSats: amountSats, description: note)
138+
navigationPath.append(.cjitConfirm(entry: entry, receiveAmountSats: amountSats, isAdditional: true))
139+
} else {
140+
dismiss()
141+
}
129142
} catch {
130143
app.toast(error)
131144
}
@@ -139,6 +152,34 @@ struct ReceiveEdit: View {
139152
}
140153
}
141154

155+
private func needsAdditionalCjit() -> Bool {
156+
let isGeoBlocked = app.isGeoBlocked ?? false
157+
let minimumAmount = blocktank.minCjitSats ?? 0
158+
let inboundCapacity = wallet.totalInboundLightningSats ?? 0
159+
let invoiceAmount = amountViewModel.amountSats
160+
161+
// Calculate maxClientBalance using TransferViewModel
162+
let maxChannelSize = blocktank.info?.options.maxChannelSizeSat ?? 0
163+
let maxClientBalance = transfer.getMaxClientBalance(maxChannelSize: UInt64(maxChannelSize))
164+
165+
if
166+
// user is geo-blocked
167+
isGeoBlocked ||
168+
// failed to get minimum amount
169+
minimumAmount == 0 ||
170+
// amount is less than minimum CJIT amount
171+
invoiceAmount < minimumAmount ||
172+
// there is enough inbound capacity
173+
invoiceAmount <= inboundCapacity ||
174+
// amount is above the maximum client balance
175+
invoiceAmount > maxClientBalance
176+
{
177+
return false
178+
}
179+
180+
return true
181+
}
182+
142183
@ViewBuilder
143184
private var numberPadButtons: some View {
144185
HStack(alignment: .bottom) {

Bitkit/Views/Wallets/Receive/ReceiveSheet.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ enum ReceiveRoute: Hashable {
66
case edit
77
case tag
88
case cjitAmount
9-
case cjitLearnMore(entry: IcJitEntry, receiveAmountSats: UInt64)
10-
case cjitConfirm(entry: IcJitEntry, receiveAmountSats: UInt64)
9+
case cjitConfirm(entry: IcJitEntry, receiveAmountSats: UInt64, isAdditional: Bool)
10+
case cjitLearnMore(entry: IcJitEntry, receiveAmountSats: UInt64, isAdditional: Bool)
1111
}
1212

1313
struct ReceiveConfig {
@@ -62,10 +62,10 @@ struct ReceiveSheet: View {
6262
ReceiveTag(navigationPath: $navigationPath)
6363
case .cjitAmount:
6464
ReceiveCjitAmount(navigationPath: $navigationPath)
65-
case let .cjitLearnMore(entry, receiveAmountSats):
66-
ReceiveCjitLearnMore(entry: entry, receiveAmountSats: receiveAmountSats)
67-
case let .cjitConfirm(entry, receiveAmountSats):
68-
ReceiveCjitConfirmation(navigationPath: $navigationPath, entry: entry, receiveAmountSats: receiveAmountSats)
65+
case let .cjitConfirm(entry, receiveAmountSats, isAdditional):
66+
ReceiveCjitConfirmation(navigationPath: $navigationPath, entry: entry, receiveAmountSats: receiveAmountSats, isAdditional: isAdditional)
67+
case let .cjitLearnMore(entry, receiveAmountSats, isAdditional):
68+
ReceiveCjitLearnMore(entry: entry, receiveAmountSats: receiveAmountSats, isAdditional: isAdditional)
6969
}
7070
}
7171
}

0 commit comments

Comments
 (0)