|
5 | 5 | from typing import Dict, List, Optional, Union
|
6 | 6 |
|
7 | 7 | import cbor2
|
8 |
| -from blockfrost import ApiUrls, BlockFrostApi |
| 8 | +from blockfrost import ApiError, ApiUrls, BlockFrostApi |
9 | 9 | from blockfrost.utils import Namespace
|
10 | 10 |
|
11 | 11 | from pycardano.address import Address
|
@@ -222,13 +222,31 @@ def utxos(self, address: str) -> List[UTxO]:
|
222 | 222 |
|
223 | 223 | return utxos
|
224 | 224 |
|
225 |
| - def submit_tx(self, cbor: Union[bytes, str]): |
| 225 | + def submit_tx(self, cbor: Union[bytes, str]) -> str: |
| 226 | + """Submit a transaction. |
| 227 | +
|
| 228 | + Args: |
| 229 | + cbor (Union[bytes, str]): The serialized transaction to be submitted. |
| 230 | +
|
| 231 | + Returns: |
| 232 | + str: The transaction hash. |
| 233 | +
|
| 234 | + Raises: |
| 235 | + :class:`TransactionFailedException`: When fails to submit the transaction. |
| 236 | + """ |
226 | 237 | if isinstance(cbor, str):
|
227 | 238 | cbor = bytes.fromhex(cbor)
|
228 | 239 | with tempfile.NamedTemporaryFile(delete=False) as f:
|
229 | 240 | f.write(cbor)
|
230 |
| - self.api.transaction_submit(f.name) |
| 241 | + try: |
| 242 | + response = self.api.transaction_submit(f.name) |
| 243 | + except ApiError as e: |
| 244 | + os.remove(f.name) |
| 245 | + raise TransactionFailedException( |
| 246 | + f"Failed to submit transaction. Error code: {e.status_code}. Error message: {e.message}" |
| 247 | + ) from e |
231 | 248 | os.remove(f.name)
|
| 249 | + return response |
232 | 250 |
|
233 | 251 | def evaluate_tx(self, cbor: Union[bytes, str]) -> Dict[str, ExecutionUnits]:
|
234 | 252 | """Evaluate execution units of a transaction.
|
|
0 commit comments