-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBlocktankRegtestScreen.kt
More file actions
296 lines (286 loc) · 13.2 KB
/
BlocktankRegtestScreen.kt
File metadata and controls
296 lines (286 loc) · 13.2 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
package to.bitkit.ui.settings
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment.Companion.CenterVertically
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import kotlinx.coroutines.launch
import to.bitkit.R
import to.bitkit.models.Toast
import to.bitkit.ui.appViewModel
import to.bitkit.ui.components.ButtonSize
import to.bitkit.ui.components.Caption
import to.bitkit.ui.components.Caption13Up
import to.bitkit.ui.components.PrimaryButton
import to.bitkit.ui.components.VerticalSpacer
import to.bitkit.ui.scaffold.AppTopBar
import to.bitkit.ui.scaffold.DrawerNavIcon
import to.bitkit.ui.scaffold.ScreenColumn
import to.bitkit.ui.theme.Colors
import to.bitkit.ui.walletViewModel
import to.bitkit.utils.Logger
@Suppress("CyclomaticComplexMethod")
@Composable
fun BlocktankRegtestScreen(
navController: NavController,
viewModel: BlocktankRegtestViewModel = hiltViewModel(),
) {
val coroutineScope = rememberCoroutineScope()
val wallet = walletViewModel ?: return
val app = appViewModel ?: return
val walletState by wallet.walletState.collectAsStateWithLifecycle()
ScreenColumn {
AppTopBar(
titleText = "Blocktank Regtest",
onBackClick = { navController.popBackStack() },
actions = { DrawerNavIcon() },
)
Column(
verticalArrangement = Arrangement.spacedBy(8.dp),
modifier = Modifier
.padding(horizontal = 16.dp)
.verticalScroll(rememberScrollState())
.imePadding()
) {
var depositAddress by remember { mutableStateOf(walletState.onchainAddress) }
var depositAmount by remember { mutableStateOf("100000") }
var mineBlockCount by remember { mutableStateOf("1") }
var paymentInvoice by remember { mutableStateOf("") }
var paymentAmount by remember { mutableStateOf("") }
var fundingTxId by remember { mutableStateOf("") }
var vout by remember { mutableStateOf("0") }
var forceCloseAfter by remember { mutableStateOf("86400") }
var isDepositing by remember { mutableStateOf(false) }
var isMining by remember { mutableStateOf(false) }
VerticalSpacer(16.dp)
Caption(text = "These actions are executed on the staging Blocktank server node.")
// Deposit Section
Caption13Up("DEPOSIT", color = Colors.White64, modifier = Modifier.padding(top = 20.dp))
OutlinedTextField(
value = depositAddress,
onValueChange = { depositAddress = it },
label = { Text(stringResource(R.string.wallet__activity_address)) },
singleLine = true,
modifier = Modifier
.padding(vertical = 4.dp)
.fillMaxWidth()
)
OutlinedTextField(
value = depositAmount,
onValueChange = { depositAmount = it },
label = { Text("Amount (sats)") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
modifier = Modifier
.padding(vertical = 4.dp)
.fillMaxWidth()
)
PrimaryButton(
text = if (isDepositing) "Depositing..." else "Make Deposit",
size = ButtonSize.Small,
onClick = {
coroutineScope.launch {
Logger.debug("Initiating regtest deposit with address: $depositAddress, amount: $depositAmount")
isDepositing = true
runCatching {
val sats = depositAmount.toULongOrNull() ?: error("Invalid deposit amount: $depositAmount")
val txId = viewModel.regtestDeposit(depositAddress, sats)
Logger.debug("Deposit successful with txId: $txId")
app.toast(
type = Toast.ToastType.SUCCESS,
title = "Success",
description = "Deposit successful. TxID: $txId",
)
}.onFailure {
Logger.error("Deposit failed", it)
app.toast(
type = Toast.ToastType.ERROR,
title = "Failed to deposit",
description = it.message.orEmpty(),
)
}
isDepositing = false
wallet.refreshState()
}
},
enabled = depositAddress.isNotEmpty() && !isDepositing,
)
// Mining Section
Caption13Up("MINING", color = Colors.White64, modifier = Modifier.padding(top = 20.dp))
Row(
verticalAlignment = CenterVertically,
modifier = Modifier
.padding(vertical = 4.dp)
.fillMaxWidth()
) {
OutlinedTextField(
value = mineBlockCount,
onValueChange = { mineBlockCount = it },
placeholder = { Text("Block Count") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
modifier = Modifier.weight(1f)
)
TextButton(
onClick = {
coroutineScope.launch {
Logger.debug("Starting regtest mining with block count: $mineBlockCount")
isMining = true
runCatching {
val count =
mineBlockCount.toUIntOrNull() ?: error("Invalid block count: $mineBlockCount")
viewModel.regtestMine(count)
Logger.debug("Successfully mined $count blocks")
app.toast(
type = Toast.ToastType.SUCCESS,
title = "Success",
description = "Successfully mined $count blocks",
)
}.onFailure {
Logger.error("Mining failed", it)
app.toast(
type = Toast.ToastType.ERROR,
title = "Failed to mine",
description = it.message.orEmpty(),
)
}
isMining = false
wallet.refreshState()
}
},
enabled = !isMining,
) {
Text(if (isMining) "Mining..." else "Mine Blocks")
}
}
// Lightning Payment Section
Caption13Up("LIGHTNING PAYMENT", color = Colors.White64, modifier = Modifier.padding(top = 20.dp))
OutlinedTextField(
value = paymentInvoice,
onValueChange = { paymentInvoice = it },
label = { Text("Invoice") },
modifier = Modifier
.padding(vertical = 4.dp)
.fillMaxWidth()
)
OutlinedTextField(
value = paymentAmount,
onValueChange = { paymentAmount = it },
label = { Text("Amount (optional, sats)") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
modifier = Modifier
.padding(vertical = 4.dp)
.fillMaxWidth()
)
PrimaryButton(
text = "Pay Invoice",
size = ButtonSize.Small,
onClick = {
coroutineScope.launch {
Logger.debug("Initiating regtest payment with invoice: $paymentInvoice, amount: $paymentAmount")
runCatching {
val amount = if (paymentAmount.isEmpty()) null else paymentAmount.toULongOrNull()
val paymentId = viewModel.regtestPay(paymentInvoice, amount)
Logger.debug("Payment successful with ID: $paymentId")
app.toast(
type = Toast.ToastType.SUCCESS,
title = "Success",
description = "Payment successful. ID: $paymentId",
)
}.onFailure {
Logger.error("Payment failed", it)
app.toast(
type = Toast.ToastType.ERROR,
title = "Failed to pay invoice from LND",
description = it.message.orEmpty(),
)
}
}
},
enabled = paymentInvoice.isNotEmpty(),
)
// Channel Close Section
Caption13Up("CHANNEL CLOSE", color = Colors.White64, modifier = Modifier.padding(top = 20.dp))
OutlinedTextField(
value = fundingTxId,
onValueChange = { fundingTxId = it },
label = { Text("Funding TxID") },
modifier = Modifier
.padding(vertical = 4.dp)
.fillMaxWidth()
)
OutlinedTextField(
value = vout,
onValueChange = { vout = it },
label = { Text("Vout") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
modifier = Modifier
.padding(vertical = 4.dp)
.fillMaxWidth()
)
OutlinedTextField(
value = forceCloseAfter,
onValueChange = { forceCloseAfter = it },
label = { Text("Force Close After (seconds)") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
modifier = Modifier
.padding(vertical = 4.dp)
.fillMaxWidth()
)
PrimaryButton(
text = "Close Channel",
size = ButtonSize.Small,
onClick = {
coroutineScope.launch {
Logger.debug(
"Initiating channel close with " +
"fundingTxId: '$fundingTxId', " +
"vout: '$vout', " +
"forceCloseAfter: '$forceCloseAfter'."
)
runCatching {
val voutNum = vout.toUIntOrNull() ?: error("Invalid Vout: $vout")
val closeAfter =
forceCloseAfter.toUIntOrNull() ?: error("Invalid Force Close After: $forceCloseAfter")
val closingTxId = viewModel.regtestCloseChannel(
fundingTxId = fundingTxId,
vout = voutNum,
forceCloseAfterS = closeAfter,
)
Logger.debug("Channel closed successfully with txId: $closingTxId")
app.toast(
type = Toast.ToastType.SUCCESS,
title = "Success",
description = "Channel closed. Closing TxID: $closingTxId"
)
}.onFailure {
Logger.error("Channel close failed", it)
app.toast(it)
}
}
},
enabled = fundingTxId.isNotEmpty(),
)
VerticalSpacer(16.dp)
}
}
}