bittensor-cli 9.1.4__py3-none-any.whl → 9.3.0__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.
@@ -1,4 +1,5 @@
1
1
  import asyncio
2
+ import json
2
3
  from typing import TYPE_CHECKING, Union, Optional
3
4
 
4
5
  from bittensor_wallet import Wallet
@@ -19,6 +20,7 @@ from bittensor_cli.src.bittensor.utils import (
19
20
  blocks_to_duration,
20
21
  float_to_u64,
21
22
  float_to_u16,
23
+ json_console,
22
24
  )
23
25
 
24
26
  if TYPE_CHECKING:
@@ -350,6 +352,19 @@ def display_votes(
350
352
  return "\n".join(vote_list)
351
353
 
352
354
 
355
+ def serialize_vote_data(
356
+ vote_data: "ProposalVoteData", delegate_info: dict[str, DelegatesDetails]
357
+ ) -> list[dict[str, bool]]:
358
+ vote_list = {}
359
+ for address in vote_data.ayes:
360
+ f_add = delegate_info[address].display if address in delegate_info else address
361
+ vote_list[f_add] = True
362
+ for address in vote_data.nays:
363
+ f_add = delegate_info[address].display if address in delegate_info else address
364
+ vote_list[f_add] = False
365
+ return vote_list
366
+
367
+
353
368
  def format_call_data(call_data: dict) -> str:
354
369
  # Extract the module and call details
355
370
  module, call_details = next(iter(call_data.items()))
@@ -559,6 +574,7 @@ async def sudo_set_hyperparameter(
559
574
  netuid: int,
560
575
  param_name: str,
561
576
  param_value: Optional[str],
577
+ json_output: bool,
562
578
  ):
563
579
  """Set subnet hyperparameters."""
564
580
 
@@ -584,17 +600,22 @@ async def sudo_set_hyperparameter(
584
600
  f"Hyperparameter [dark_orange]{param_name}[/dark_orange] value is not within bounds. "
585
601
  f"Value is {normalized_value} but must be {value}"
586
602
  )
587
- return
603
+ return False
588
604
  success = await set_hyperparameter_extrinsic(
589
605
  subtensor, wallet, netuid, param_name, value
590
606
  )
607
+ if json_output:
608
+ return success
591
609
  if success:
592
610
  console.print("\n")
593
611
  print_verbose("Fetching hyperparameters")
594
- return await get_hyperparameters(subtensor, netuid=netuid)
612
+ await get_hyperparameters(subtensor, netuid=netuid)
613
+ return success
595
614
 
596
615
 
597
- async def get_hyperparameters(subtensor: "SubtensorInterface", netuid: int):
616
+ async def get_hyperparameters(
617
+ subtensor: "SubtensorInterface", netuid: int, json_output: bool = False
618
+ ) -> bool:
598
619
  """View hyperparameters of a subnetwork."""
599
620
  print_verbose("Fetching hyperparameters")
600
621
  if not await subtensor.subnet_exists(netuid):
@@ -607,32 +628,44 @@ async def get_hyperparameters(subtensor: "SubtensorInterface", netuid: int):
607
628
  return False
608
629
 
609
630
  table = Table(
610
- Column("[white]HYPERPARAMETER", style=COLOR_PALETTE["SUDO"]["HYPERPARAMETER"]),
611
- Column("[white]VALUE", style=COLOR_PALETTE["SUDO"]["VALUE"]),
612
- Column("[white]NORMALIZED", style=COLOR_PALETTE["SUDO"]["NORMALIZED"]),
613
- title=f"[{COLOR_PALETTE['GENERAL']['HEADER']}]\nSubnet Hyperparameters\n NETUID: "
614
- f"[{COLOR_PALETTE['GENERAL']['SUBHEADING']}]{netuid}"
631
+ Column("[white]HYPERPARAMETER", style=COLOR_PALETTE.SU.HYPERPARAMETER),
632
+ Column("[white]VALUE", style=COLOR_PALETTE.SU.VALUE),
633
+ Column("[white]NORMALIZED", style=COLOR_PALETTE.SU.NORMAL),
634
+ title=f"[{COLOR_PALETTE.G.HEADER}]\nSubnet Hyperparameters\n NETUID: "
635
+ f"[{COLOR_PALETTE.G.SUBHEAD}]{netuid}"
615
636
  f"{f' ({subnet_info.subnet_name})' if subnet_info.subnet_name is not None else ''}"
616
- f"[/{COLOR_PALETTE['GENERAL']['SUBHEADING']}]"
617
- f" - Network: [{COLOR_PALETTE['GENERAL']['SUBHEADING']}]{subtensor.network}[/{COLOR_PALETTE['GENERAL']['SUBHEADING']}]\n",
637
+ f"[/{COLOR_PALETTE.G.SUBHEAD}]"
638
+ f" - Network: [{COLOR_PALETTE.G.SUBHEAD}]{subtensor.network}[/{COLOR_PALETTE.G.SUBHEAD}]\n",
618
639
  show_footer=True,
619
640
  width=None,
620
641
  pad_edge=False,
621
642
  box=box.SIMPLE,
622
643
  show_edge=True,
623
644
  )
645
+ dict_out = []
624
646
 
625
647
  normalized_values = normalize_hyperparameters(subnet)
626
648
 
627
649
  for param, value, norm_value in normalized_values:
628
650
  table.add_row(" " + param, value, norm_value)
629
-
630
- console.print(table)
651
+ dict_out.append(
652
+ {
653
+ "hyperparameter": param,
654
+ "value": value,
655
+ "normalized_value": norm_value,
656
+ }
657
+ )
658
+ if json_output:
659
+ json_console.print(json.dumps(dict_out))
660
+ else:
661
+ console.print(table)
631
662
  return True
632
663
 
633
664
 
634
- async def get_senate(subtensor: "SubtensorInterface"):
635
- """View Bittensor's senate memebers"""
665
+ async def get_senate(
666
+ subtensor: "SubtensorInterface", json_output: bool = False
667
+ ) -> None:
668
+ """View Bittensor's senate members"""
636
669
  with console.status(
637
670
  f":satellite: Syncing with chain: [white]{subtensor}[/white] ...",
638
671
  spinner="aesthetic",
@@ -663,21 +696,27 @@ async def get_senate(subtensor: "SubtensorInterface"):
663
696
  border_style="bright_black",
664
697
  leading=True,
665
698
  )
699
+ dict_output = []
666
700
 
667
701
  for ss58_address in senate_members:
702
+ member_name = (
703
+ delegate_info[ss58_address].display
704
+ if ss58_address in delegate_info
705
+ else "~"
706
+ )
668
707
  table.add_row(
669
- (
670
- delegate_info[ss58_address].display
671
- if ss58_address in delegate_info
672
- else "~"
673
- ),
708
+ member_name,
674
709
  ss58_address,
675
710
  )
676
-
711
+ dict_output.append({"name": member_name, "ss58_address": ss58_address})
712
+ if json_output:
713
+ json_console.print(json.dumps(dict_output))
677
714
  return console.print(table)
678
715
 
679
716
 
680
- async def proposals(subtensor: "SubtensorInterface", verbose: bool):
717
+ async def proposals(
718
+ subtensor: "SubtensorInterface", verbose: bool, json_output: bool = False
719
+ ) -> None:
681
720
  console.print(
682
721
  ":satellite: Syncing with chain: [white]{}[/white] ...".format(
683
722
  subtensor.network
@@ -723,6 +762,7 @@ async def proposals(subtensor: "SubtensorInterface", verbose: bool):
723
762
  width=None,
724
763
  border_style="bright_black",
725
764
  )
765
+ dict_output = []
726
766
  for hash_, (call_data, vote_data) in all_proposals.items():
727
767
  blocks_remaining = vote_data.end - current_block
728
768
  if blocks_remaining > 0:
@@ -741,6 +781,7 @@ async def proposals(subtensor: "SubtensorInterface", verbose: bool):
741
781
  if vote_data.threshold > 0
742
782
  else 0
743
783
  )
784
+ f_call_data = format_call_data(call_data)
744
785
  table.add_row(
745
786
  hash_ if verbose else f"{hash_[:4]}...{hash_[-4:]}",
746
787
  str(vote_data.threshold),
@@ -748,8 +789,21 @@ async def proposals(subtensor: "SubtensorInterface", verbose: bool):
748
789
  f"{len(vote_data.nays)} ({nays_threshold:.2f}%)",
749
790
  display_votes(vote_data, registered_delegate_info),
750
791
  vote_end_cell,
751
- format_call_data(call_data),
792
+ f_call_data,
793
+ )
794
+ dict_output.append(
795
+ {
796
+ "hash": hash_,
797
+ "threshold": vote_data.threshold,
798
+ "ayes": len(vote_data.ayes),
799
+ "nays": len(vote_data.nays),
800
+ "votes": serialize_vote_data(vote_data, registered_delegate_info),
801
+ "end": vote_data.end,
802
+ "call_data": f_call_data,
803
+ }
752
804
  )
805
+ if json_output:
806
+ json_console.print(json.dumps(dict_output))
753
807
  console.print(table)
754
808
  console.print(
755
809
  "\n[dim]* Both Ayes and Nays percentages are calculated relative to the proposal's threshold.[/dim]"