Skip to content

Commit 6f85d49

Browse files
REFACTOR. pull out utxo ogmios query methods with explicit return type
REFACTOR. re-order methods so that helper functions are right beneath the main function for readability
1 parent 40e83dc commit 6f85d49

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

pycardano/backend/ogmios.py

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ def _query_chain_tip(self) -> JSON:
104104
args = {"query": "chainTip"}
105105
return self._request(OgmiosQueryType.Query, args)
106106

107+
def _query_utxos_by_address(self, address: str) -> List[List[JSON]]:
108+
args = {"query": {"utxo": [address]}}
109+
return self._request(OgmiosQueryType.Query, args)
110+
111+
def _query_utxos_by_tx_id(self, tx_id: str, index: int) -> List[List[JSON]]:
112+
args = {"query": {"utxo": [{"txId": tx_id, "index": index}]}}
113+
return self._request(OgmiosQueryType.Query, args)
114+
107115
def _check_chain_tip_and_update(self):
108116
slot = self.last_block_slot
109117
if self._last_known_block_slot != slot:
@@ -211,28 +219,21 @@ def last_block_slot(self) -> int:
211219
result = self._query_chain_tip()
212220
return result["slot"]
213221

214-
def _extract_asset_info(self, asset_hash: str) -> Tuple[str, ScriptHash, AssetName]:
215-
policy_hex, asset_name_hex = asset_hash.split(".")
216-
policy = ScriptHash.from_primitive(policy_hex)
217-
asset_name = AssetName.from_primitive(asset_name_hex)
218-
219-
return policy_hex, policy, asset_name
220-
221-
def _check_utxo_unspent(self, tx_id: str, index: int) -> bool:
222-
"""Check whether an UTxO is unspent with Ogmios.
222+
def utxos(self, address: str) -> List[UTxO]:
223+
"""Get all UTxOs associated with an address.
223224
224225
Args:
225-
tx_id (str): transaction id.
226-
index (int): transaction index.
227-
"""
228-
229-
args = {"query": {"utxo": [{"txId": tx_id, "index": index}]}}
230-
results = self._request(OgmiosQueryType.Query, args)
226+
address (str): An address encoded with bech32.
231227
232-
if results:
233-
return True
228+
Returns:
229+
List[UTxO]: A list of UTxOs.
230+
"""
231+
if self._kupo_url:
232+
utxos = self._utxos_kupo(address)
234233
else:
235-
return False
234+
utxos = self._utxos_ogmios(address)
235+
236+
return utxos
236237

237238
def _utxos_kupo(self, address: str) -> List[UTxO]:
238239
"""Get all UTxOs associated with an address with Kupo.
@@ -302,6 +303,23 @@ def _utxos_kupo(self, address: str) -> List[UTxO]:
302303

303304
return utxos
304305

306+
def _check_utxo_unspent(self, tx_id: str, index: int) -> bool:
307+
"""Check whether an UTxO is unspent with Ogmios.
308+
309+
Args:
310+
tx_id (str): transaction id.
311+
index (int): transaction index.
312+
"""
313+
results = self._query_utxos_by_tx_id(tx_id, index)
314+
return len(results) > 0
315+
316+
def _extract_asset_info(self, asset_hash: str) -> Tuple[str, ScriptHash, AssetName]:
317+
policy_hex, asset_name_hex = asset_hash.split(".")
318+
policy = ScriptHash.from_primitive(policy_hex)
319+
asset_name = AssetName.from_primitive(asset_name_hex)
320+
321+
return policy_hex, policy, asset_name
322+
305323
def _utxos_ogmios(self, address: str) -> List[UTxO]:
306324
"""Get all UTxOs associated with an address with Ogmios.
307325
@@ -311,12 +329,9 @@ def _utxos_ogmios(self, address: str) -> List[UTxO]:
311329
Returns:
312330
List[UTxO]: A list of UTxOs.
313331
"""
314-
315-
args = {"query": {"utxo": [address]}}
316-
results = self._request(OgmiosQueryType.Query, args)
332+
results = self._query_utxos_by_address(address)
317333

318334
utxos = []
319-
320335
for result in results:
321336
in_ref = result[0]
322337
output = result[1]
@@ -374,22 +389,6 @@ def _utxos_ogmios(self, address: str) -> List[UTxO]:
374389

375390
return utxos
376391

377-
def utxos(self, address: str) -> List[UTxO]:
378-
"""Get all UTxOs associated with an address.
379-
380-
Args:
381-
address (str): An address encoded with bech32.
382-
383-
Returns:
384-
List[UTxO]: A list of UTxOs.
385-
"""
386-
if self._kupo_url:
387-
utxos = self._utxos_kupo(address)
388-
else:
389-
utxos = self._utxos_ogmios(address)
390-
391-
return utxos
392-
393392
def submit_tx(self, cbor: Union[bytes, str]):
394393
"""Submit a transaction to the blockchain.
395394

0 commit comments

Comments
 (0)