Skip to content

Commit 7a80ff1

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

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

pycardano/backend/kupo.py

+27
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(
@@ -253,3 +256,27 @@ def evaluate_tx_cbor(self, cbor: Union[bytes, str]) -> Dict[str, ExecutionUnits]
253256
:class:`TransactionFailedException`: When fails to evaluate the transaction.
254257
"""
255258
return self._wrapped_backend.evaluate_tx_cbor(cbor)
259+
260+
def get_metadata_cbor(
261+
self, tx_id: TransactionId, slot: int
262+
) -> Optional[RawCBOR]:
263+
"""Get metadata cbor from Kupo.
264+
265+
Args:
266+
tx_id (TransactionId): Transaction id for metadata to query.
267+
slot (int): Slot number.
268+
269+
Returns:
270+
Optional[RawCBOR]: Metadata cbor."""
271+
if self._kupo_url is None:
272+
raise AssertionError(
273+
"kupo_url object attribute has not been assigned properly."
274+
)
275+
276+
kupo_metadata_url = self._kupo_url + f"/metadata/{slot}?transaction_id={tx_id}"
277+
metadata_result = requests.get(kupo_metadata_url).json()
278+
279+
if metadata_result and metadata_result[0]["transaction_id"] == tx_id:
280+
return RawCBOR(bytes.fromhex(metadata_result[0]["raw"]))
281+
282+
return None

0 commit comments

Comments
 (0)