Skip to content

Commit 457224b

Browse files
authored
Merge pull request #307 from input-output-hk/unify_queries
refactor(gov): move governance query methods to query_group
2 parents a6b5fd0 + dec1eb0 commit 457224b

File tree

3 files changed

+107
-150
lines changed

3 files changed

+107
-150
lines changed

cardano_clusterlib/gov_group.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from cardano_clusterlib import gov_action_group
66
from cardano_clusterlib import gov_committee_group
77
from cardano_clusterlib import gov_drep_group
8-
from cardano_clusterlib import gov_query_group
98
from cardano_clusterlib import gov_vote_group
109
from cardano_clusterlib import types as itp
1110

@@ -20,7 +19,6 @@ def __init__(self, clusterlib_obj: "itp.ClusterLib") -> None:
2019
self._action_group: gov_action_group.GovActionGroup | None = None
2120
self._committee_group: gov_committee_group.GovCommitteeGroup | None = None
2221
self._drep_group: gov_drep_group.GovDrepGroup | None = None
23-
self._query_group: gov_query_group.GovQueryGroup | None = None
2422
self._vote_group: gov_vote_group.GovVoteGroup | None = None
2523

2624
@property
@@ -48,13 +46,6 @@ def drep(self) -> gov_drep_group.GovDrepGroup:
4846
self._drep_group = gov_drep_group.GovDrepGroup(clusterlib_obj=self._clusterlib_obj)
4947
return self._drep_group
5048

51-
@property
52-
def query(self) -> gov_query_group.GovQueryGroup:
53-
"""Query group."""
54-
if not self._query_group:
55-
self._query_group = gov_query_group.GovQueryGroup(clusterlib_obj=self._clusterlib_obj)
56-
return self._query_group
57-
5849
@property
5950
def vote(self) -> gov_vote_group.GovVoteGroup:
6051
"""Vote group."""

cardano_clusterlib/gov_query_group.py

Lines changed: 0 additions & 141 deletions
This file was deleted.

cardano_clusterlib/query_group.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ class QueryGroup:
2323
def __init__(self, clusterlib_obj: "itp.ClusterLib") -> None:
2424
self._clusterlib_obj = clusterlib_obj
2525

26+
def _get_cred_args(
27+
self,
28+
drep_script_hash: str = "",
29+
drep_vkey: str = "",
30+
drep_vkey_file: itp.FileType | None = None,
31+
drep_key_hash: str = "",
32+
) -> list[str]:
33+
"""Get arguments for script or verification key."""
34+
if drep_script_hash:
35+
cred_args = ["--drep-script-hash", str(drep_script_hash)]
36+
elif drep_vkey:
37+
cred_args = ["--drep-verification-key", str(drep_vkey)]
38+
elif drep_vkey_file:
39+
cred_args = ["--drep-verification-key-file", str(drep_vkey_file)]
40+
elif drep_key_hash:
41+
cred_args = ["--drep-key-hash", str(drep_key_hash)]
42+
else:
43+
cred_args = []
44+
45+
return cred_args
46+
2647
def query_cli(
2748
self, cli_args: itp.UnpackableSequence, cli_sub_args: itp.UnpackableSequence = ()
2849
) -> str:
@@ -516,5 +537,91 @@ def get_slot_number(self, timestamp: datetime.datetime) -> int:
516537

517538
return slot_number
518539

540+
def get_constitution(self) -> dict[str, tp.Any]:
541+
"""Get the constitution."""
542+
out: dict[str, tp.Any] = json.loads(self.query_cli(["constitution"]))
543+
return out
544+
545+
def get_gov_state(self) -> dict[str, tp.Any]:
546+
"""Get the governance state."""
547+
out: dict[str, tp.Any] = json.loads(self.query_cli(["gov-state"]))
548+
return out
549+
550+
def get_drep_state(
551+
self,
552+
drep_script_hash: str = "",
553+
drep_vkey: str = "",
554+
drep_vkey_file: itp.FileType | None = None,
555+
drep_key_hash: str = "",
556+
) -> list[list[dict[str, tp.Any]]]:
557+
"""Get the DRep state.
558+
559+
When no key is provided, query all DReps.
560+
561+
Args:
562+
drep_script_hash: DRep script hash (hex-encoded, optional).
563+
drep_vkey: DRep verification key (Bech32 or hex-encoded).
564+
drep_vkey_file: Filepath of the DRep verification key.
565+
drep_key_hash: DRep verification key hash (either Bech32-encoded or hex-encoded).
566+
567+
Returns:
568+
list[list[dict[str, Any]]]: DRep state.
569+
"""
570+
cred_args = self._get_cred_args(
571+
drep_script_hash=drep_script_hash,
572+
drep_vkey=drep_vkey,
573+
drep_vkey_file=drep_vkey_file,
574+
drep_key_hash=drep_key_hash,
575+
)
576+
if not cred_args:
577+
cred_args = ["--all-dreps"]
578+
579+
out: list[list[dict[str, tp.Any]]] = json.loads(self.query_cli(["drep-state", *cred_args]))
580+
return out
581+
582+
def get_drep_stake_distribution(
583+
self,
584+
drep_script_hash: str = "",
585+
drep_vkey: str = "",
586+
drep_vkey_file: itp.FileType | None = None,
587+
drep_key_hash: str = "",
588+
) -> dict[str, tp.Any]:
589+
"""Get the DRep stake distribution.
590+
591+
When no key is provided, query all DReps.
592+
593+
Args:
594+
drep_script_hash: DRep script hash (hex-encoded, optional).
595+
drep_vkey: DRep verification key (Bech32 or hex-encoded).
596+
drep_vkey_file: Filepath of the DRep verification key.
597+
drep_key_hash: DRep verification key hash (either Bech32-encoded or hex-encoded).
598+
599+
Returns:
600+
dict[str, Any]: DRep stake distribution.
601+
"""
602+
cred_args = self._get_cred_args(
603+
drep_script_hash=drep_script_hash,
604+
drep_vkey=drep_vkey,
605+
drep_vkey_file=drep_vkey_file,
606+
drep_key_hash=drep_key_hash,
607+
)
608+
if not cred_args:
609+
cred_args = ["--all-dreps"]
610+
611+
out: list[list] | dict[str, tp.Any] = json.loads(
612+
self.query_cli(["drep-stake-distribution", *cred_args])
613+
)
614+
recs: dict[str, tp.Any] = {i[0]: i[1] for i in out} if isinstance(out, list) else out
615+
return recs
616+
617+
def get_committee_state(self) -> dict[str, tp.Any]:
618+
"""Get the committee state."""
619+
out: dict[str, tp.Any] = json.loads(self.query_cli(["committee-state"]))
620+
return out
621+
622+
def get_treasury(self) -> int:
623+
"""Get the treasury value."""
624+
return int(self.query_cli(["treasury"]))
625+
519626
def __repr__(self) -> str:
520627
return f"<{self.__class__.__name__}: clusterlib_obj={id(self._clusterlib_obj)}>"

0 commit comments

Comments
 (0)