Skip to content

Commit a8d5828

Browse files
committed
Refactor KupoChainContextExtension in kupo.py
1 parent ea03c63 commit a8d5828

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

pycardano/backend/kupo.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
MultiAsset,
2222
TransactionInput,
2323
TransactionOutput,
24+
TransactionId,
2425
UTxO,
2526
Value,
2627
)
@@ -197,6 +198,8 @@ def _utxos_kupo(self, address: str) -> List[UTxO]:
197198
)
198199
if datum_hash and result.get("datum_type", "inline"):
199200
datum = self._get_datum_from_kupo(result["datum_hash"])
201+
if datum:
202+
datum_hash = None
200203

201204
if not result["value"]["assets"]:
202205
tx_out = TransactionOutput(
@@ -247,9 +250,34 @@ def evaluate_tx_cbor(self, cbor: Union[bytes, str]) -> Dict[str, ExecutionUnits]
247250
cbor (Union[bytes, str]): The serialized transaction to be evaluated.
248251
249252
Returns:
250-
Dict[str, ExecutionUnits]: A list of execution units calculated for each of the transaction's redeemers
253+
Dict[str, ExecutionUnits]: A list of execution units calculated for
254+
each of the transaction's redeemers
251255
252256
Raises:
253257
:class:`TransactionFailedException`: When fails to evaluate the transaction.
254258
"""
255259
return self._wrapped_backend.evaluate_tx_cbor(cbor)
260+
261+
def get_metadata_cbor(
262+
self, tx_id: TransactionId, slot: int
263+
) -> Optional[RawCBOR]:
264+
"""Get metadata cbor from Kupo.
265+
266+
Args:
267+
tx_id (TransactionId): Transaction id for metadata to query.
268+
slot (int): Slot number.
269+
270+
Returns:
271+
Optional[RawCBOR]: Metadata cbor."""
272+
if self._kupo_url is None:
273+
raise AssertionError(
274+
"kupo_url object attribute has not been assigned properly."
275+
)
276+
277+
kupo_metadata_url = self._kupo_url + f"/metadata/{slot}?transaction_id={tx_id}"
278+
metadata_result = requests.get(kupo_metadata_url).json()
279+
280+
if metadata_result and metadata_result[0]["transaction_id"] == tx_id:
281+
return RawCBOR(bytes.fromhex(metadata_result[0]["raw"]))
282+
283+
return None

0 commit comments

Comments
 (0)