Skip to content

Add error handling to blockfrost submit_tx method #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions pycardano/backend/blockfrost.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Dict, List, Optional, Union

import cbor2
from blockfrost import ApiUrls, BlockFrostApi
from blockfrost import ApiError, ApiUrls, BlockFrostApi
from blockfrost.utils import Namespace

from pycardano.address import Address
Expand Down Expand Up @@ -222,13 +222,31 @@ def utxos(self, address: str) -> List[UTxO]:

return utxos

def submit_tx(self, cbor: Union[bytes, str]):
def submit_tx(self, cbor: Union[bytes, str]) -> str:
"""Submit a transaction.

Args:
cbor (Union[bytes, str]): The serialized transaction to be submitted.

Returns:
str: The transaction hash.

Raises:
:class:`TransactionFailedException`: When fails to submit the transaction.
"""
if isinstance(cbor, str):
cbor = bytes.fromhex(cbor)
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(cbor)
self.api.transaction_submit(f.name)
try:
response = self.api.transaction_submit(f.name)
except ApiError as e:
os.remove(f.name)
raise TransactionFailedException(
f"Failed to submit transaction. Error code: {e.status_code}. Error message: {e.message}"
) from e
os.remove(f.name)
return response

def evaluate_tx(self, cbor: Union[bytes, str]) -> Dict[str, ExecutionUnits]:
"""Evaluate execution units of a transaction.
Expand Down