kaleido-cli 0.1.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kaleido_cli/__init__.py +3 -0
- kaleido_cli/__main__.py +6 -0
- kaleido_cli/app.py +204 -0
- kaleido_cli/commands/__init__.py +1 -0
- kaleido_cli/commands/asset.py +850 -0
- kaleido_cli/commands/channel.py +855 -0
- kaleido_cli/commands/config_cmd.py +113 -0
- kaleido_cli/commands/market.py +241 -0
- kaleido_cli/commands/node.py +874 -0
- kaleido_cli/commands/node_swap.py +274 -0
- kaleido_cli/commands/payment.py +364 -0
- kaleido_cli/commands/peer.py +108 -0
- kaleido_cli/commands/swap.py +460 -0
- kaleido_cli/commands/wallet.py +487 -0
- kaleido_cli/config.py +88 -0
- kaleido_cli/context.py +41 -0
- kaleido_cli/docker_manager.py +463 -0
- kaleido_cli/onboarding.py +275 -0
- kaleido_cli/output.py +123 -0
- kaleido_cli/utils/__init__.py +1 -0
- kaleido_cli/utils/assets.py +40 -0
- kaleido_cli/utils/channel_orders.py +822 -0
- kaleido_cli/utils/errors.py +17 -0
- kaleido_cli/utils/pairs.py +133 -0
- kaleido_cli/utils/prompts.py +102 -0
- kaleido_cli/utils/quotes.py +163 -0
- kaleido_cli/utils/swaps.py +142 -0
- kaleido_cli-0.1.2.dist-info/METADATA +416 -0
- kaleido_cli-0.1.2.dist-info/RECORD +32 -0
- kaleido_cli-0.1.2.dist-info/WHEEL +4 -0
- kaleido_cli-0.1.2.dist-info/entry_points.txt +2 -0
- kaleido_cli-0.1.2.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,822 @@
|
|
|
1
|
+
"""Channel order and LSP helper utilities."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Awaitable
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from datetime import UTC, datetime
|
|
8
|
+
from time import perf_counter
|
|
9
|
+
from typing import Any, TypeVar
|
|
10
|
+
|
|
11
|
+
import typer
|
|
12
|
+
from kaleido_sdk import (
|
|
13
|
+
ChannelOrderResponse,
|
|
14
|
+
CreateOrderRequest,
|
|
15
|
+
EstimateFeesRequest,
|
|
16
|
+
EstimateFeesResponse,
|
|
17
|
+
Layer,
|
|
18
|
+
LspInfoResponse,
|
|
19
|
+
OrderRequest,
|
|
20
|
+
PairQuoteRequest,
|
|
21
|
+
PaymentState,
|
|
22
|
+
SwapLegInput,
|
|
23
|
+
)
|
|
24
|
+
from kaleido_sdk.rln import ConnectPeerRequest
|
|
25
|
+
|
|
26
|
+
from kaleido_cli.output import (
|
|
27
|
+
is_interactive,
|
|
28
|
+
is_json_mode,
|
|
29
|
+
output_collection,
|
|
30
|
+
output_model,
|
|
31
|
+
print_error,
|
|
32
|
+
print_info,
|
|
33
|
+
print_panel,
|
|
34
|
+
print_success,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
CHANNEL_LSP_CREATE_ORDER_PATH = "/api/v1/lsps1/create_order"
|
|
38
|
+
CHANNEL_LSP_GET_ORDER_PATH = "/api/v1/lsps1/get_order"
|
|
39
|
+
CHANNEL_ORDER_HTTP_TIMEOUT = 30.0
|
|
40
|
+
|
|
41
|
+
T = TypeVar("T")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(slots=True)
|
|
45
|
+
class ChannelOrderParams:
|
|
46
|
+
client_pubkey: str
|
|
47
|
+
lsp_balance_sat: int
|
|
48
|
+
client_balance_sat: int
|
|
49
|
+
required_channel_confirmations: int
|
|
50
|
+
funding_confirms_within_blocks: int
|
|
51
|
+
channel_expiry_blocks: int
|
|
52
|
+
token: str | None
|
|
53
|
+
refund_onchain_address: str | None
|
|
54
|
+
announce_channel: bool
|
|
55
|
+
asset_id: str | None
|
|
56
|
+
lsp_asset_amount: int | None
|
|
57
|
+
client_asset_amount: int | None
|
|
58
|
+
rfq_id: str | None
|
|
59
|
+
email: str | None
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@dataclass(slots=True)
|
|
63
|
+
class ChannelFeeEstimateParams:
|
|
64
|
+
lsp_balance_sat: int
|
|
65
|
+
client_balance_sat: int
|
|
66
|
+
channel_expiry_blocks: int
|
|
67
|
+
token: str | None
|
|
68
|
+
asset_id: str | None
|
|
69
|
+
lsp_asset_amount: int | None
|
|
70
|
+
client_asset_amount: int | None
|
|
71
|
+
rfq_id: str | None
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _parse_iso_datetime(value: str) -> datetime | None:
|
|
75
|
+
candidate = value
|
|
76
|
+
if candidate.endswith("Z"):
|
|
77
|
+
candidate = f"{candidate[:-1]}+00:00"
|
|
78
|
+
try:
|
|
79
|
+
return datetime.fromisoformat(candidate)
|
|
80
|
+
except ValueError:
|
|
81
|
+
return None
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _normalize_channel_lsp_datetimes(value: Any, key: str | None = None) -> Any:
|
|
85
|
+
if isinstance(value, dict):
|
|
86
|
+
return {k: _normalize_channel_lsp_datetimes(v, k) for k, v in value.items()}
|
|
87
|
+
if isinstance(value, list):
|
|
88
|
+
return [_normalize_channel_lsp_datetimes(item, key) for item in value]
|
|
89
|
+
if key is not None and key.endswith("_at") and isinstance(value, str):
|
|
90
|
+
parsed = _parse_iso_datetime(value)
|
|
91
|
+
if parsed is not None and parsed.tzinfo is None:
|
|
92
|
+
return parsed.replace(tzinfo=UTC).isoformat()
|
|
93
|
+
return value
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
async def _post_channel_lsp(client: Any, path: str, body: Any) -> dict[str, Any]:
|
|
97
|
+
data = await client.maker._http.maker_post(path, data=body)
|
|
98
|
+
if not isinstance(data, dict):
|
|
99
|
+
raise TypeError(f"Unexpected channel LSP response type for {path}: {type(data).__name__}")
|
|
100
|
+
return data
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _normalize_channel_order_response(data: dict[str, Any]) -> ChannelOrderResponse:
|
|
104
|
+
return ChannelOrderResponse.model_validate(_normalize_channel_lsp_datetimes(data))
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
async def _submit_channel_order(client: Any, body: CreateOrderRequest) -> ChannelOrderResponse:
|
|
108
|
+
data = await _post_channel_lsp(client, CHANNEL_LSP_CREATE_ORDER_PATH, body)
|
|
109
|
+
return _normalize_channel_order_response(data)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
async def _fetch_channel_order(client: Any, body: OrderRequest) -> ChannelOrderResponse:
|
|
113
|
+
data = await _post_channel_lsp(client, CHANNEL_LSP_GET_ORDER_PATH, body)
|
|
114
|
+
return _normalize_channel_order_response(data)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _prompt_optional_text(prompt: str) -> str | None:
|
|
118
|
+
raw = typer.prompt(prompt, default="")
|
|
119
|
+
return raw.strip() or None
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _prompt_optional_int(prompt: str) -> int | None:
|
|
123
|
+
raw = typer.prompt(prompt, default="")
|
|
124
|
+
if raw.strip() == "":
|
|
125
|
+
return None
|
|
126
|
+
try:
|
|
127
|
+
return int(raw)
|
|
128
|
+
except ValueError:
|
|
129
|
+
print_error(f"{prompt} must be an integer.")
|
|
130
|
+
raise typer.Exit(1)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _range_text(min_value: int | None, max_value: int | None) -> str:
|
|
134
|
+
if min_value is None and max_value is None:
|
|
135
|
+
return "any"
|
|
136
|
+
if min_value is None:
|
|
137
|
+
return f"<= {max_value}"
|
|
138
|
+
if max_value is None:
|
|
139
|
+
return f">= {min_value}"
|
|
140
|
+
return f"{min_value} -> {max_value}"
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _validate_int_range(
|
|
144
|
+
value: int,
|
|
145
|
+
label: str,
|
|
146
|
+
*,
|
|
147
|
+
min_value: int | None = None,
|
|
148
|
+
max_value: int | None = None,
|
|
149
|
+
) -> int:
|
|
150
|
+
if min_value is not None and value < min_value:
|
|
151
|
+
print_error(f"{label} must be at least {min_value}.")
|
|
152
|
+
raise typer.Exit(1)
|
|
153
|
+
if max_value is not None and value > max_value:
|
|
154
|
+
print_error(f"{label} must be at most {max_value}.")
|
|
155
|
+
raise typer.Exit(1)
|
|
156
|
+
return value
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def _prompt_int_in_range(
|
|
160
|
+
prompt: str,
|
|
161
|
+
*,
|
|
162
|
+
min_value: int | None = None,
|
|
163
|
+
max_value: int | None = None,
|
|
164
|
+
default: int | None = None,
|
|
165
|
+
) -> int:
|
|
166
|
+
suffix = f" ({_range_text(min_value, max_value)})"
|
|
167
|
+
prompt_kwargs: dict[str, Any] = {"type": int, "show_default": default is not None}
|
|
168
|
+
if default is not None:
|
|
169
|
+
prompt_kwargs["default"] = default
|
|
170
|
+
value = typer.prompt(f"{prompt}{suffix}", **prompt_kwargs)
|
|
171
|
+
return _validate_int_range(value, prompt, min_value=min_value, max_value=max_value)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _lsp_options_limits(lsp_info: LspInfoResponse | None) -> dict[str, int | None]:
|
|
175
|
+
options = lsp_info.options if lsp_info is not None else None
|
|
176
|
+
return {
|
|
177
|
+
"min_lsp_balance_sat": getattr(options, "min_initial_lsp_balance_sat", None),
|
|
178
|
+
"max_lsp_balance_sat": getattr(options, "max_initial_lsp_balance_sat", None),
|
|
179
|
+
"min_client_balance_sat": getattr(options, "min_initial_client_balance_sat", None),
|
|
180
|
+
"max_client_balance_sat": getattr(options, "max_initial_client_balance_sat", None),
|
|
181
|
+
"min_channel_balance_sat": getattr(options, "min_channel_balance_sat", None),
|
|
182
|
+
"max_channel_balance_sat": getattr(options, "max_channel_balance_sat", None),
|
|
183
|
+
"min_required_confirmations": getattr(options, "min_required_channel_confirmations", None),
|
|
184
|
+
"min_funding_within_blocks": getattr(options, "min_funding_confirms_within_blocks", None),
|
|
185
|
+
"max_expiry_blocks": getattr(options, "max_channel_expiry_blocks", None),
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _print_lsp_order_limits(lsp_info: LspInfoResponse) -> None:
|
|
190
|
+
limits = _lsp_options_limits(lsp_info)
|
|
191
|
+
output_model(
|
|
192
|
+
{
|
|
193
|
+
"lsp_balance_sat": _range_text(
|
|
194
|
+
limits["min_lsp_balance_sat"], limits["max_lsp_balance_sat"]
|
|
195
|
+
),
|
|
196
|
+
"client_balance_sat": _range_text(
|
|
197
|
+
limits["min_client_balance_sat"], limits["max_client_balance_sat"]
|
|
198
|
+
),
|
|
199
|
+
"total_channel_balance_sat": _range_text(
|
|
200
|
+
limits["min_channel_balance_sat"], limits["max_channel_balance_sat"]
|
|
201
|
+
),
|
|
202
|
+
"required_confirmations_min": limits["min_required_confirmations"],
|
|
203
|
+
"funding_within_blocks_min": limits["min_funding_within_blocks"],
|
|
204
|
+
"expiry_blocks_max": limits["max_expiry_blocks"],
|
|
205
|
+
},
|
|
206
|
+
title="LSP Channel Limits",
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def _find_lsp_asset(lsp_info: LspInfoResponse | None, asset_id_or_ticker: str | None):
|
|
211
|
+
if lsp_info is None or not asset_id_or_ticker:
|
|
212
|
+
return None
|
|
213
|
+
normalized = asset_id_or_ticker.lower()
|
|
214
|
+
for asset in lsp_info.assets or []:
|
|
215
|
+
if (asset.asset_id or "").lower() == normalized or asset.ticker.lower() == normalized:
|
|
216
|
+
return asset
|
|
217
|
+
return None
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def _format_elapsed(seconds: float) -> str:
|
|
221
|
+
if seconds < 1:
|
|
222
|
+
return f"{seconds * 1000:.0f}ms"
|
|
223
|
+
return f"{seconds:.1f}s"
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
async def _timed_step(label: str, awaitable: Awaitable[T]) -> T:
|
|
227
|
+
if not is_json_mode():
|
|
228
|
+
print_info(f"{label}...")
|
|
229
|
+
started_at = perf_counter()
|
|
230
|
+
try:
|
|
231
|
+
result = await awaitable
|
|
232
|
+
except Exception:
|
|
233
|
+
if not is_json_mode():
|
|
234
|
+
print_error(f"{label} failed after {_format_elapsed(perf_counter() - started_at)}")
|
|
235
|
+
raise
|
|
236
|
+
if not is_json_mode():
|
|
237
|
+
print_success(f"{label} finished in {_format_elapsed(perf_counter() - started_at)}")
|
|
238
|
+
return result
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _print_lsp_asset_options(lsp_info: LspInfoResponse) -> None:
|
|
242
|
+
for idx, asset in enumerate(lsp_info.assets or [], start=1):
|
|
243
|
+
print_info(
|
|
244
|
+
f"{idx}. {asset.ticker} ({asset.name}) "
|
|
245
|
+
f"asset={asset.asset_id} "
|
|
246
|
+
f"lsp={asset.min_initial_lsp_amount}->{asset.max_initial_lsp_amount} "
|
|
247
|
+
f"client={asset.min_initial_client_amount}->{asset.max_initial_client_amount} "
|
|
248
|
+
f"channel={asset.min_channel_amount}->{asset.max_channel_amount}"
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def _prompt_lsp_asset(lsp_info: LspInfoResponse) -> str | None:
|
|
253
|
+
assets = lsp_info.assets or []
|
|
254
|
+
if not assets:
|
|
255
|
+
print_info("The LSP did not report asset-backed channel options.")
|
|
256
|
+
return _prompt_optional_text("Asset ID (rgb:...)")
|
|
257
|
+
_print_lsp_asset_options(lsp_info)
|
|
258
|
+
selected = _prompt_int_in_range(
|
|
259
|
+
"Select asset option number", min_value=1, max_value=len(assets)
|
|
260
|
+
)
|
|
261
|
+
return assets[selected - 1].asset_id
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def _validate_lsp_amounts(
|
|
265
|
+
*,
|
|
266
|
+
lsp_info: LspInfoResponse | None,
|
|
267
|
+
lsp_balance_sat: int,
|
|
268
|
+
client_balance_sat: int,
|
|
269
|
+
required_channel_confirmations: int,
|
|
270
|
+
funding_confirms_within_blocks: int,
|
|
271
|
+
channel_expiry_blocks: int,
|
|
272
|
+
) -> None:
|
|
273
|
+
limits = _lsp_options_limits(lsp_info)
|
|
274
|
+
_validate_int_range(
|
|
275
|
+
lsp_balance_sat,
|
|
276
|
+
"--lsp-balance",
|
|
277
|
+
min_value=limits["min_lsp_balance_sat"],
|
|
278
|
+
max_value=limits["max_lsp_balance_sat"],
|
|
279
|
+
)
|
|
280
|
+
_validate_int_range(
|
|
281
|
+
client_balance_sat,
|
|
282
|
+
"--client-balance",
|
|
283
|
+
min_value=limits["min_client_balance_sat"],
|
|
284
|
+
max_value=limits["max_client_balance_sat"],
|
|
285
|
+
)
|
|
286
|
+
_validate_int_range(
|
|
287
|
+
lsp_balance_sat + client_balance_sat,
|
|
288
|
+
"Total channel balance",
|
|
289
|
+
min_value=limits["min_channel_balance_sat"],
|
|
290
|
+
max_value=limits["max_channel_balance_sat"],
|
|
291
|
+
)
|
|
292
|
+
_validate_int_range(
|
|
293
|
+
required_channel_confirmations,
|
|
294
|
+
"--confirmations",
|
|
295
|
+
min_value=limits["min_required_confirmations"],
|
|
296
|
+
)
|
|
297
|
+
_validate_int_range(
|
|
298
|
+
funding_confirms_within_blocks,
|
|
299
|
+
"--funding-within",
|
|
300
|
+
min_value=limits["min_funding_within_blocks"],
|
|
301
|
+
)
|
|
302
|
+
_validate_int_range(
|
|
303
|
+
channel_expiry_blocks,
|
|
304
|
+
"--expiry-blocks",
|
|
305
|
+
min_value=1,
|
|
306
|
+
max_value=limits["max_expiry_blocks"],
|
|
307
|
+
)
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def _validate_asset_amounts(
|
|
311
|
+
*,
|
|
312
|
+
lsp_asset: Any,
|
|
313
|
+
lsp_asset_amount: int | None,
|
|
314
|
+
client_asset_amount: int | None,
|
|
315
|
+
) -> None:
|
|
316
|
+
if lsp_asset_amount is None:
|
|
317
|
+
print_error("--lsp-asset-amount is required when --asset-id is set.")
|
|
318
|
+
raise typer.Exit(1)
|
|
319
|
+
_validate_int_range(
|
|
320
|
+
lsp_asset_amount,
|
|
321
|
+
"--lsp-asset-amount",
|
|
322
|
+
min_value=lsp_asset.min_initial_lsp_amount,
|
|
323
|
+
max_value=lsp_asset.max_initial_lsp_amount,
|
|
324
|
+
)
|
|
325
|
+
if client_asset_amount is not None:
|
|
326
|
+
_validate_int_range(
|
|
327
|
+
client_asset_amount,
|
|
328
|
+
"--client-asset-amount",
|
|
329
|
+
min_value=lsp_asset.min_initial_client_amount,
|
|
330
|
+
max_value=min(lsp_asset.max_initial_client_amount, lsp_asset_amount),
|
|
331
|
+
)
|
|
332
|
+
if client_asset_amount > lsp_asset_amount:
|
|
333
|
+
print_error("--client-asset-amount must be less than or equal to --lsp-asset-amount.")
|
|
334
|
+
raise typer.Exit(1)
|
|
335
|
+
total_asset_amount = lsp_asset_amount + (client_asset_amount or 0)
|
|
336
|
+
_validate_int_range(
|
|
337
|
+
total_asset_amount,
|
|
338
|
+
"Total channel asset amount",
|
|
339
|
+
min_value=lsp_asset.min_channel_amount,
|
|
340
|
+
max_value=lsp_asset.max_channel_amount,
|
|
341
|
+
)
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def _normalize_optional_text(value: str | None) -> str | None:
|
|
345
|
+
if value is None:
|
|
346
|
+
return None
|
|
347
|
+
stripped = value.strip()
|
|
348
|
+
return stripped or None
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def _resolve_channel_fee_estimate_params(
|
|
352
|
+
*,
|
|
353
|
+
lsp_balance_sat: int | None,
|
|
354
|
+
client_balance_sat: int | None,
|
|
355
|
+
channel_expiry_blocks: int,
|
|
356
|
+
token: str | None,
|
|
357
|
+
asset_id: str | None,
|
|
358
|
+
lsp_asset_amount: int | None,
|
|
359
|
+
client_asset_amount: int | None,
|
|
360
|
+
rfq_id: str | None,
|
|
361
|
+
) -> ChannelFeeEstimateParams:
|
|
362
|
+
resolved_lsp_balance_sat: int
|
|
363
|
+
if lsp_balance_sat is not None:
|
|
364
|
+
resolved_lsp_balance_sat = lsp_balance_sat
|
|
365
|
+
elif is_interactive():
|
|
366
|
+
resolved_lsp_balance_sat = typer.prompt("LSP balance in channel (satoshis)", type=int)
|
|
367
|
+
else:
|
|
368
|
+
print_error("--lsp-balance is required in non-interactive mode.")
|
|
369
|
+
raise typer.Exit(1)
|
|
370
|
+
|
|
371
|
+
resolved_client_balance_sat: int
|
|
372
|
+
if client_balance_sat is not None:
|
|
373
|
+
resolved_client_balance_sat = client_balance_sat
|
|
374
|
+
elif is_interactive():
|
|
375
|
+
resolved_client_balance_sat = typer.prompt("Client balance in channel (satoshis)", type=int)
|
|
376
|
+
else:
|
|
377
|
+
print_error("--client-balance is required in non-interactive mode.")
|
|
378
|
+
raise typer.Exit(1)
|
|
379
|
+
|
|
380
|
+
if is_interactive():
|
|
381
|
+
channel_expiry_blocks = typer.prompt(
|
|
382
|
+
"Channel expiry blocks",
|
|
383
|
+
type=int,
|
|
384
|
+
default=channel_expiry_blocks,
|
|
385
|
+
)
|
|
386
|
+
|
|
387
|
+
resolved_token = _normalize_optional_text(token)
|
|
388
|
+
resolved_asset_id = _normalize_optional_text(asset_id)
|
|
389
|
+
resolved_rfq_id = _normalize_optional_text(rfq_id)
|
|
390
|
+
|
|
391
|
+
if is_interactive():
|
|
392
|
+
if resolved_asset_id is None and typer.confirm(
|
|
393
|
+
"Estimate fees for an RGB-backed channel?", default=False
|
|
394
|
+
):
|
|
395
|
+
resolved_asset_id = _prompt_optional_text("Asset ID (rgb:...)")
|
|
396
|
+
|
|
397
|
+
if resolved_asset_id is not None:
|
|
398
|
+
if lsp_asset_amount is None:
|
|
399
|
+
lsp_asset_amount = _prompt_optional_int(
|
|
400
|
+
"[OPTIONAL] LSP RGB asset amount (Enter to skip)"
|
|
401
|
+
)
|
|
402
|
+
if client_asset_amount is None:
|
|
403
|
+
client_asset_amount = _prompt_optional_int(
|
|
404
|
+
"[OPTIONAL] Client RGB asset amount (Enter to skip)"
|
|
405
|
+
)
|
|
406
|
+
else:
|
|
407
|
+
lsp_asset_amount = None
|
|
408
|
+
client_asset_amount = None
|
|
409
|
+
|
|
410
|
+
if resolved_token is None:
|
|
411
|
+
resolved_token = _prompt_optional_text(
|
|
412
|
+
"[OPTIONAL] Authentication token (Enter to skip)"
|
|
413
|
+
)
|
|
414
|
+
if resolved_rfq_id is None:
|
|
415
|
+
resolved_rfq_id = _prompt_optional_text("[OPTIONAL] RFQ ID (Enter to skip)")
|
|
416
|
+
|
|
417
|
+
if (
|
|
418
|
+
lsp_asset_amount is not None or client_asset_amount is not None
|
|
419
|
+
) and resolved_asset_id is None:
|
|
420
|
+
print_error("--lsp-asset-amount and --client-asset-amount require --asset-id.")
|
|
421
|
+
raise typer.Exit(1)
|
|
422
|
+
|
|
423
|
+
return ChannelFeeEstimateParams(
|
|
424
|
+
lsp_balance_sat=resolved_lsp_balance_sat,
|
|
425
|
+
client_balance_sat=resolved_client_balance_sat,
|
|
426
|
+
channel_expiry_blocks=channel_expiry_blocks,
|
|
427
|
+
token=resolved_token,
|
|
428
|
+
asset_id=resolved_asset_id,
|
|
429
|
+
lsp_asset_amount=lsp_asset_amount,
|
|
430
|
+
client_asset_amount=client_asset_amount,
|
|
431
|
+
rfq_id=resolved_rfq_id,
|
|
432
|
+
)
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
def _resolve_channel_order_params(
|
|
436
|
+
*,
|
|
437
|
+
client_pubkey: str | None,
|
|
438
|
+
default_client_pubkey: str | None,
|
|
439
|
+
lsp_info: LspInfoResponse | None,
|
|
440
|
+
lsp_balance_sat: int | None,
|
|
441
|
+
client_balance_sat: int | None,
|
|
442
|
+
required_channel_confirmations: int,
|
|
443
|
+
funding_confirms_within_blocks: int,
|
|
444
|
+
channel_expiry_blocks: int,
|
|
445
|
+
refund_onchain_address: str | None,
|
|
446
|
+
announce_channel: bool,
|
|
447
|
+
asset_id: str | None,
|
|
448
|
+
lsp_asset_amount: int | None,
|
|
449
|
+
client_asset_amount: int | None,
|
|
450
|
+
email: str | None,
|
|
451
|
+
) -> ChannelOrderParams:
|
|
452
|
+
resolved_client_pubkey: str
|
|
453
|
+
if client_pubkey is not None:
|
|
454
|
+
resolved_client_pubkey = client_pubkey
|
|
455
|
+
elif default_client_pubkey is not None:
|
|
456
|
+
resolved_client_pubkey = default_client_pubkey
|
|
457
|
+
if is_interactive():
|
|
458
|
+
print_info(f"Using local node pubkey: {resolved_client_pubkey}")
|
|
459
|
+
elif is_interactive():
|
|
460
|
+
resolved_client_pubkey = typer.prompt("Client Lightning node public key")
|
|
461
|
+
else:
|
|
462
|
+
print_error("CLIENT_PUBKEY argument is required in non-interactive mode.")
|
|
463
|
+
raise typer.Exit(1)
|
|
464
|
+
|
|
465
|
+
if is_interactive() and lsp_info is not None:
|
|
466
|
+
_print_lsp_order_limits(lsp_info)
|
|
467
|
+
|
|
468
|
+
limits = _lsp_options_limits(lsp_info)
|
|
469
|
+
resolved_lsp_balance_sat: int
|
|
470
|
+
if lsp_balance_sat is not None:
|
|
471
|
+
resolved_lsp_balance_sat = _validate_int_range(
|
|
472
|
+
lsp_balance_sat,
|
|
473
|
+
"--lsp-balance",
|
|
474
|
+
min_value=limits["min_lsp_balance_sat"],
|
|
475
|
+
max_value=limits["max_lsp_balance_sat"],
|
|
476
|
+
)
|
|
477
|
+
elif is_interactive():
|
|
478
|
+
resolved_lsp_balance_sat = _prompt_int_in_range(
|
|
479
|
+
"LSP balance in channel (satoshis)",
|
|
480
|
+
min_value=limits["min_lsp_balance_sat"],
|
|
481
|
+
max_value=limits["max_lsp_balance_sat"],
|
|
482
|
+
)
|
|
483
|
+
else:
|
|
484
|
+
print_error("--lsp-balance is required in non-interactive mode.")
|
|
485
|
+
raise typer.Exit(1)
|
|
486
|
+
|
|
487
|
+
resolved_client_balance_sat: int
|
|
488
|
+
if client_balance_sat is not None:
|
|
489
|
+
resolved_client_balance_sat = _validate_int_range(
|
|
490
|
+
client_balance_sat,
|
|
491
|
+
"--client-balance",
|
|
492
|
+
min_value=limits["min_client_balance_sat"],
|
|
493
|
+
max_value=limits["max_client_balance_sat"],
|
|
494
|
+
)
|
|
495
|
+
elif is_interactive():
|
|
496
|
+
resolved_client_balance_sat = _prompt_int_in_range(
|
|
497
|
+
"Client balance in channel (satoshis)",
|
|
498
|
+
min_value=limits["min_client_balance_sat"],
|
|
499
|
+
max_value=limits["max_client_balance_sat"],
|
|
500
|
+
)
|
|
501
|
+
else:
|
|
502
|
+
print_error("--client-balance is required in non-interactive mode.")
|
|
503
|
+
raise typer.Exit(1)
|
|
504
|
+
|
|
505
|
+
if is_interactive():
|
|
506
|
+
required_channel_confirmations = _prompt_int_in_range(
|
|
507
|
+
"Required channel confirmations",
|
|
508
|
+
min_value=limits["min_required_confirmations"],
|
|
509
|
+
default=required_channel_confirmations,
|
|
510
|
+
)
|
|
511
|
+
funding_confirms_within_blocks = _prompt_int_in_range(
|
|
512
|
+
"Funding confirms within blocks",
|
|
513
|
+
min_value=limits["min_funding_within_blocks"],
|
|
514
|
+
default=funding_confirms_within_blocks,
|
|
515
|
+
)
|
|
516
|
+
channel_expiry_blocks = _prompt_int_in_range(
|
|
517
|
+
"Channel expiry blocks",
|
|
518
|
+
min_value=1,
|
|
519
|
+
max_value=limits["max_expiry_blocks"],
|
|
520
|
+
default=channel_expiry_blocks,
|
|
521
|
+
)
|
|
522
|
+
|
|
523
|
+
resolved_refund_onchain_address = _normalize_optional_text(refund_onchain_address)
|
|
524
|
+
resolved_asset_id = _normalize_optional_text(asset_id)
|
|
525
|
+
resolved_email = _normalize_optional_text(email)
|
|
526
|
+
|
|
527
|
+
if is_interactive():
|
|
528
|
+
if resolved_asset_id is None and typer.confirm(
|
|
529
|
+
"Attach an RGB asset to the channel order?", default=False
|
|
530
|
+
):
|
|
531
|
+
if lsp_info is not None:
|
|
532
|
+
resolved_asset_id = _prompt_lsp_asset(lsp_info)
|
|
533
|
+
else:
|
|
534
|
+
resolved_asset_id = _prompt_optional_text("Asset ID (rgb:...)")
|
|
535
|
+
|
|
536
|
+
if resolved_asset_id is not None:
|
|
537
|
+
lsp_asset = _find_lsp_asset(lsp_info, resolved_asset_id)
|
|
538
|
+
if lsp_info is not None and lsp_asset is None:
|
|
539
|
+
print_error(f"Asset {resolved_asset_id!r} is not available from the LSP.")
|
|
540
|
+
raise typer.Exit(1)
|
|
541
|
+
if lsp_asset_amount is None:
|
|
542
|
+
if lsp_asset is not None:
|
|
543
|
+
lsp_asset_amount = _prompt_int_in_range(
|
|
544
|
+
"LSP RGB asset amount (raw units)",
|
|
545
|
+
min_value=lsp_asset.min_initial_lsp_amount,
|
|
546
|
+
max_value=lsp_asset.max_initial_lsp_amount,
|
|
547
|
+
)
|
|
548
|
+
else:
|
|
549
|
+
lsp_asset_amount = typer.prompt("LSP RGB asset amount (raw units)", type=int)
|
|
550
|
+
if client_asset_amount is None:
|
|
551
|
+
if lsp_asset is not None:
|
|
552
|
+
max_client_asset_amount = lsp_asset.max_initial_client_amount
|
|
553
|
+
if lsp_asset_amount is not None:
|
|
554
|
+
max_client_asset_amount = min(max_client_asset_amount, lsp_asset_amount)
|
|
555
|
+
client_asset_amount = _prompt_int_in_range(
|
|
556
|
+
"Client RGB asset amount (raw units)",
|
|
557
|
+
min_value=lsp_asset.min_initial_client_amount,
|
|
558
|
+
max_value=max_client_asset_amount,
|
|
559
|
+
default=0,
|
|
560
|
+
)
|
|
561
|
+
else:
|
|
562
|
+
client_asset_amount = typer.prompt(
|
|
563
|
+
"Client RGB asset amount (raw units)", type=int, default=0
|
|
564
|
+
)
|
|
565
|
+
else:
|
|
566
|
+
lsp_asset_amount = None
|
|
567
|
+
client_asset_amount = None
|
|
568
|
+
|
|
569
|
+
announce_channel = typer.confirm("Announce channel publicly?", default=announce_channel)
|
|
570
|
+
|
|
571
|
+
if resolved_email is None:
|
|
572
|
+
resolved_email = _prompt_optional_text("[OPTIONAL] Contact email (Enter to skip)")
|
|
573
|
+
|
|
574
|
+
if (
|
|
575
|
+
lsp_asset_amount is not None or client_asset_amount is not None
|
|
576
|
+
) and resolved_asset_id is None:
|
|
577
|
+
print_error("--lsp-asset-amount and --client-asset-amount require --asset-id.")
|
|
578
|
+
raise typer.Exit(1)
|
|
579
|
+
if resolved_asset_id is not None:
|
|
580
|
+
lsp_asset = _find_lsp_asset(lsp_info, resolved_asset_id)
|
|
581
|
+
if lsp_info is not None and lsp_asset is None:
|
|
582
|
+
print_error(f"Asset {resolved_asset_id!r} is not available from the LSP.")
|
|
583
|
+
raise typer.Exit(1)
|
|
584
|
+
if lsp_asset is not None:
|
|
585
|
+
resolved_asset_id = lsp_asset.asset_id or resolved_asset_id
|
|
586
|
+
_validate_asset_amounts(
|
|
587
|
+
lsp_asset=lsp_asset,
|
|
588
|
+
lsp_asset_amount=lsp_asset_amount,
|
|
589
|
+
client_asset_amount=client_asset_amount,
|
|
590
|
+
)
|
|
591
|
+
elif lsp_asset_amount is None:
|
|
592
|
+
print_error("--lsp-asset-amount is required when --asset-id is set.")
|
|
593
|
+
raise typer.Exit(1)
|
|
594
|
+
|
|
595
|
+
_validate_lsp_amounts(
|
|
596
|
+
lsp_info=lsp_info,
|
|
597
|
+
lsp_balance_sat=resolved_lsp_balance_sat,
|
|
598
|
+
client_balance_sat=resolved_client_balance_sat,
|
|
599
|
+
required_channel_confirmations=required_channel_confirmations,
|
|
600
|
+
funding_confirms_within_blocks=funding_confirms_within_blocks,
|
|
601
|
+
channel_expiry_blocks=channel_expiry_blocks,
|
|
602
|
+
)
|
|
603
|
+
|
|
604
|
+
return ChannelOrderParams(
|
|
605
|
+
client_pubkey=resolved_client_pubkey,
|
|
606
|
+
lsp_balance_sat=resolved_lsp_balance_sat,
|
|
607
|
+
client_balance_sat=resolved_client_balance_sat,
|
|
608
|
+
required_channel_confirmations=required_channel_confirmations,
|
|
609
|
+
funding_confirms_within_blocks=funding_confirms_within_blocks,
|
|
610
|
+
channel_expiry_blocks=channel_expiry_blocks,
|
|
611
|
+
token=None,
|
|
612
|
+
refund_onchain_address=resolved_refund_onchain_address,
|
|
613
|
+
announce_channel=announce_channel,
|
|
614
|
+
asset_id=resolved_asset_id,
|
|
615
|
+
lsp_asset_amount=lsp_asset_amount,
|
|
616
|
+
client_asset_amount=client_asset_amount or None,
|
|
617
|
+
rfq_id=None,
|
|
618
|
+
email=resolved_email,
|
|
619
|
+
)
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
def _build_channel_order_request(params: ChannelOrderParams) -> CreateOrderRequest:
|
|
623
|
+
return CreateOrderRequest(
|
|
624
|
+
client_pubkey=params.client_pubkey,
|
|
625
|
+
lsp_balance_sat=params.lsp_balance_sat,
|
|
626
|
+
client_balance_sat=params.client_balance_sat,
|
|
627
|
+
required_channel_confirmations=params.required_channel_confirmations,
|
|
628
|
+
funding_confirms_within_blocks=params.funding_confirms_within_blocks,
|
|
629
|
+
channel_expiry_blocks=params.channel_expiry_blocks,
|
|
630
|
+
token=params.token,
|
|
631
|
+
refund_onchain_address=params.refund_onchain_address,
|
|
632
|
+
announce_channel=params.announce_channel,
|
|
633
|
+
asset_id=params.asset_id,
|
|
634
|
+
lsp_asset_amount=params.lsp_asset_amount,
|
|
635
|
+
client_asset_amount=params.client_asset_amount,
|
|
636
|
+
rfq_id=params.rfq_id,
|
|
637
|
+
email=params.email,
|
|
638
|
+
)
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
async def _create_channel_order(client: Any, params: ChannelOrderParams) -> ChannelOrderResponse:
|
|
642
|
+
return await _submit_channel_order(client, _build_channel_order_request(params))
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
def _peer_pubkey_from_connection_url(connection_url: str | None) -> str | None:
|
|
646
|
+
if not connection_url:
|
|
647
|
+
return None
|
|
648
|
+
return connection_url.split("@", 1)[0].strip() or None
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
async def _ensure_lsp_peer_connected(client: Any, lsp_info: LspInfoResponse) -> None:
|
|
652
|
+
connection_url = lsp_info.lsp_connection_url
|
|
653
|
+
lsp_pubkey = _peer_pubkey_from_connection_url(connection_url)
|
|
654
|
+
if not connection_url or not lsp_pubkey:
|
|
655
|
+
print_error("LSP did not report a connection URL.")
|
|
656
|
+
raise typer.Exit(1)
|
|
657
|
+
|
|
658
|
+
peers = await _timed_step(
|
|
659
|
+
f"Checking LSP peer connection: {lsp_pubkey}",
|
|
660
|
+
client.rln.list_peers(),
|
|
661
|
+
)
|
|
662
|
+
connected_pubkeys = {peer.pubkey for peer in (peers.peers or [])}
|
|
663
|
+
if lsp_pubkey in connected_pubkeys:
|
|
664
|
+
print_info(f"Already connected to LSP peer: {lsp_pubkey}")
|
|
665
|
+
return
|
|
666
|
+
await _timed_step(
|
|
667
|
+
f"Connecting to LSP peer: {connection_url}",
|
|
668
|
+
client.rln.connect_peer(ConnectPeerRequest(peer_pubkey_and_addr=connection_url)),
|
|
669
|
+
)
|
|
670
|
+
print_success(f"LSP peer connected: {lsp_pubkey}")
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
async def _autofill_refund_address(client: Any, params: ChannelOrderParams) -> None:
|
|
674
|
+
if params.refund_onchain_address:
|
|
675
|
+
return
|
|
676
|
+
address = await _timed_step("Fetching refund onchain address", client.rln.get_address())
|
|
677
|
+
params.refund_onchain_address = address.address
|
|
678
|
+
print_info(f"Using refund onchain address from local node: {params.refund_onchain_address}")
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
def _quote_leg_summary(leg: Any) -> str:
|
|
682
|
+
ticker = getattr(leg, "ticker", None) or getattr(leg, "asset_id", "asset")
|
|
683
|
+
amount = getattr(leg, "amount", None)
|
|
684
|
+
if amount is None:
|
|
685
|
+
return str(ticker)
|
|
686
|
+
return f"{amount} {ticker}"
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
def _quote_amount_summary(quote: Any) -> str:
|
|
690
|
+
return (
|
|
691
|
+
f"receive {_quote_leg_summary(quote.to_asset)} for {_quote_leg_summary(quote.from_asset)}"
|
|
692
|
+
)
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
async def _attach_client_asset_quote(
|
|
696
|
+
client: Any,
|
|
697
|
+
params: ChannelOrderParams,
|
|
698
|
+
*,
|
|
699
|
+
yes: bool,
|
|
700
|
+
) -> None:
|
|
701
|
+
if not params.asset_id or not params.client_asset_amount or params.client_asset_amount <= 0:
|
|
702
|
+
params.rfq_id = None
|
|
703
|
+
return
|
|
704
|
+
|
|
705
|
+
quote = await _timed_step(
|
|
706
|
+
"Fetching RFQ quote",
|
|
707
|
+
client.maker.get_quote(
|
|
708
|
+
PairQuoteRequest(
|
|
709
|
+
from_asset=SwapLegInput(asset_id="BTC", layer=Layer.BTC_LN, amount=None),
|
|
710
|
+
to_asset=SwapLegInput(
|
|
711
|
+
asset_id=params.asset_id,
|
|
712
|
+
layer=Layer.RGB_LN,
|
|
713
|
+
amount=params.client_asset_amount,
|
|
714
|
+
),
|
|
715
|
+
)
|
|
716
|
+
),
|
|
717
|
+
)
|
|
718
|
+
if is_json_mode():
|
|
719
|
+
if not yes:
|
|
720
|
+
print_error("--yes is required in JSON mode to accept the RFQ price.")
|
|
721
|
+
raise typer.Exit(1)
|
|
722
|
+
else:
|
|
723
|
+
quote_summary = _quote_amount_summary(quote)
|
|
724
|
+
print_info(f"Quoted amount: {quote_summary}")
|
|
725
|
+
if is_interactive():
|
|
726
|
+
if not typer.confirm(f"Accept quoted amount ({quote_summary})?", default=False):
|
|
727
|
+
print_error("Channel order cancelled before creation.")
|
|
728
|
+
raise typer.Exit(0)
|
|
729
|
+
elif not yes:
|
|
730
|
+
print_error("--yes is required in non-interactive mode to accept the RFQ price.")
|
|
731
|
+
raise typer.Exit(1)
|
|
732
|
+
params.rfq_id = quote.rfq_id
|
|
733
|
+
print_info(f"Using RFQ ID: {params.rfq_id}")
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
async def _get_channel_order(
|
|
737
|
+
client: Any, order_id: str, access_token: str = ""
|
|
738
|
+
) -> ChannelOrderResponse:
|
|
739
|
+
return await _fetch_channel_order(
|
|
740
|
+
client,
|
|
741
|
+
OrderRequest(order_id=order_id, access_token=access_token),
|
|
742
|
+
)
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
def _channel_wallet_payment_summary(order: ChannelOrderResponse) -> dict[str, Any]:
|
|
746
|
+
bolt11 = order.payment.bolt11
|
|
747
|
+
onchain = order.payment.onchain
|
|
748
|
+
return {
|
|
749
|
+
"order_id": order.order_id,
|
|
750
|
+
"order_state": order.order_state,
|
|
751
|
+
"offchain_payment_state": bolt11.state,
|
|
752
|
+
"offchain_order_total_sat": bolt11.order_total_sat,
|
|
753
|
+
"offchain_fee_total_sat": bolt11.fee_total_sat,
|
|
754
|
+
"offchain_expires_at": bolt11.expires_at,
|
|
755
|
+
"onchain_payment_state": onchain.state,
|
|
756
|
+
"onchain_order_total_sat": onchain.order_total_sat,
|
|
757
|
+
"onchain_fee_total_sat": onchain.fee_total_sat,
|
|
758
|
+
"onchain_address": onchain.address,
|
|
759
|
+
"onchain_min_confirmations": onchain.min_onchain_payment_confirmations,
|
|
760
|
+
"onchain_expires_at": onchain.expires_at,
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
def _can_pay_channel_order(order: ChannelOrderResponse) -> bool:
|
|
765
|
+
return order.payment.bolt11.state == PaymentState.EXPECT_PAYMENT
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
async def _estimate_channel_order_fees(
|
|
769
|
+
client: Any, params: ChannelFeeEstimateParams
|
|
770
|
+
) -> EstimateFeesResponse:
|
|
771
|
+
body = EstimateFeesRequest(
|
|
772
|
+
lsp_balance_sat=params.lsp_balance_sat,
|
|
773
|
+
client_balance_sat=params.client_balance_sat,
|
|
774
|
+
channel_expiry_blocks=params.channel_expiry_blocks,
|
|
775
|
+
token=params.token,
|
|
776
|
+
asset_id=params.asset_id,
|
|
777
|
+
lsp_asset_amount=params.lsp_asset_amount,
|
|
778
|
+
client_asset_amount=params.client_asset_amount,
|
|
779
|
+
rfq_id=params.rfq_id,
|
|
780
|
+
)
|
|
781
|
+
return await client.maker.estimate_lsp_fees(body)
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
def _print_channel_order_fees(resp: EstimateFeesResponse, *, title: str) -> None:
|
|
785
|
+
output_model(resp, title=title)
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
def _humanize_key(key: str) -> str:
|
|
789
|
+
return key.replace("_", " ").capitalize()
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
def _short_id(value: str | None, *, prefix: int = 16, suffix: int = 8) -> str:
|
|
793
|
+
if not value:
|
|
794
|
+
return "-"
|
|
795
|
+
if len(value) <= prefix + suffix + 1:
|
|
796
|
+
return value
|
|
797
|
+
return f"{value[:prefix]}…{value[-suffix:]}"
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
def _print_lsp_info(resp: LspInfoResponse) -> None:
|
|
801
|
+
print_panel("LSP Connection", resp.lsp_connection_url or "-", style="blue")
|
|
802
|
+
|
|
803
|
+
if resp.options is not None:
|
|
804
|
+
output_model(
|
|
805
|
+
{_humanize_key(key): value for key, value in resp.options.model_dump().items()},
|
|
806
|
+
title="Channel Options",
|
|
807
|
+
)
|
|
808
|
+
output_collection(
|
|
809
|
+
"LSP Assets",
|
|
810
|
+
[
|
|
811
|
+
{
|
|
812
|
+
**asset.model_dump(),
|
|
813
|
+
"asset_id": _short_id(asset.asset_id),
|
|
814
|
+
"client_range": f"{asset.min_initial_client_amount} -> {asset.max_initial_client_amount}",
|
|
815
|
+
"lsp_range": f"{asset.min_initial_lsp_amount} -> {asset.max_initial_lsp_amount}",
|
|
816
|
+
"channel_range": f"{asset.min_channel_amount} -> {asset.max_channel_amount}",
|
|
817
|
+
}
|
|
818
|
+
for asset in (resp.assets or [])
|
|
819
|
+
],
|
|
820
|
+
item_title="LSP Asset — {index}",
|
|
821
|
+
empty_msg="No asset-backed channel options reported.",
|
|
822
|
+
)
|