|
1 | 1 | import re |
2 | | -from typing import Optional |
| 2 | +from typing import Optional, cast |
3 | 3 |
|
4 | 4 | import typer |
5 | 5 | from typer import Context |
6 | 6 |
|
7 | 7 | from communex._common import IPFS_REGEX, BalanceUnit, format_balance |
8 | | -from communex.balance import to_nano |
| 8 | +from communex.balance import from_nano, to_nano |
9 | 9 | from communex.cli._common import ( |
10 | 10 | make_custom_context, |
11 | 11 | print_table_from_plain_dict, |
12 | 12 | ) |
| 13 | +from communex.compat.key import local_key_addresses |
13 | 14 | from communex.errors import ChainTransactionError |
14 | 15 | from communex.faucet.powv2 import solve_for_difficulty_fast |
15 | 16 |
|
@@ -339,3 +340,32 @@ def bridge_withdraw( |
339 | 340 | context.error(f"Failed to withdraw {amount} tokens: {e}") |
340 | 341 | else: |
341 | 342 | context.info(f"Withdrew {amount}$j successfully") |
| 343 | + |
| 344 | + |
| 345 | +@balance_app.command() |
| 346 | +def bridged_balance( |
| 347 | + ctx: Context, |
| 348 | + key: Optional[str] = None, |
| 349 | +): |
| 350 | + context = make_custom_context(ctx) |
| 351 | + client = context.com_client() |
| 352 | + with context.progress_status("Getting bridged balance..."): |
| 353 | + bridge_map = client.query_map( |
| 354 | + "Bridged", params=[], extract_value=False |
| 355 | + )["Bridged"] |
| 356 | + bridge_map = cast(dict[str, int], bridge_map) |
| 357 | + if key is None: |
| 358 | + local_keys = local_key_addresses(context.password_manager) |
| 359 | + local_bridge_map = { |
| 360 | + address: from_nano(ammount) |
| 361 | + for address, ammount in bridge_map.items() |
| 362 | + if address in local_keys.values() |
| 363 | + } |
| 364 | + else: |
| 365 | + key = cast(str, context.resolve_key_ss58(key, None)) |
| 366 | + bridged_amount = from_nano(bridge_map.get(key, 0)) |
| 367 | + local_bridge_map = {key: bridged_amount} |
| 368 | + |
| 369 | + print_table_from_plain_dict( |
| 370 | + local_bridge_map, ["Address", "Amount"], context.console |
| 371 | + ) |
0 commit comments