Skip to content

Commit 2dd347d

Browse files
committed
Add blockfrost support for transaction evaluation
1 parent 3103f7d commit 2dd347d

File tree

4 files changed

+85
-54
lines changed

4 files changed

+85
-54
lines changed

poetry.lock

Lines changed: 51 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pycardano/backend/blockfrost.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import os
22
import tempfile
33
import time
4-
from typing import List, Union
4+
from typing import Dict, List, Union
55

66
from blockfrost import ApiUrls, BlockFrostApi
77

88
from pycardano.address import Address
99
from pycardano.backend.base import ChainContext, GenesisParameters, ProtocolParameters
10+
from pycardano.exception import TransactionFailedException
1011
from pycardano.hash import SCRIPT_HASH_SIZE, DatumHash, ScriptHash
1112
from pycardano.network import Network
13+
from pycardano.plutus import ExecutionUnits
1214
from pycardano.transaction import (
1315
Asset,
1416
AssetName,
@@ -158,3 +160,32 @@ def submit_tx(self, cbor: Union[bytes, str]):
158160
f.write(cbor)
159161
self.api.transaction_submit(f.name)
160162
os.remove(f.name)
163+
164+
def evaluate_tx(self, cbor: Union[bytes, str]) -> Dict[str, ExecutionUnits]:
165+
"""Evaluate execution units of a transaction.
166+
167+
Args:
168+
cbor (Union[bytes, str]): The serialized transaction to be evaluated.
169+
170+
Returns:
171+
Dict[str, ExecutionUnits]: A list of execution units calculated for each of the transaction's redeemers
172+
173+
Raises:
174+
:class:`TransactionFailedException`: When fails to evaluate the transaction.
175+
"""
176+
if isinstance(cbor, bytes):
177+
cbor = cbor.hex()
178+
with tempfile.NamedTemporaryFile(delete=False, mode="w") as f:
179+
f.write(cbor)
180+
result = self.api.transaction_evaluate(f.name).result
181+
os.remove(f.name)
182+
return_val = {}
183+
if not hasattr(result, "EvaluationResult"):
184+
raise TransactionFailedException(result)
185+
else:
186+
for k in vars(result.EvaluationResult):
187+
return_val[k] = ExecutionUnits(
188+
getattr(result.EvaluationResult, k).memory,
189+
getattr(result.EvaluationResult, k).steps,
190+
)
191+
return return_val

pycardano/txbuilder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class TransactionBuilder:
107107
init=False, default_factory=lambda: {}
108108
)
109109

110-
_should_estimate_execution_units: bool = None
110+
_should_estimate_execution_units: bool = field(init=False, default=None)
111111

112112
def add_input(self, utxo: UTxO) -> TransactionBuilder:
113113
"""Add a specific UTxO to transaction's inputs.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ python = "^3.7"
2525
PyNaCl = "^1.4.0"
2626
cbor2 = "^5.4.2"
2727
typeguard = "^2.13.3"
28-
blockfrost-python = "^0.4.3"
28+
blockfrost-python = "^0.4.4"
2929
websocket-client = "^1.2.3"
3030

3131
[tool.poetry.dev-dependencies]

0 commit comments

Comments
 (0)