|
11 | 11 | from typeguard import typechecked
|
12 | 12 |
|
13 | 13 | from pycardano.address import Address
|
14 |
| -from pycardano.exception import InvalidOperationException |
| 14 | +from pycardano.exception import InvalidDataException, InvalidOperationException |
15 | 15 | from pycardano.hash import (
|
16 | 16 | TRANSACTION_HASH_SIZE,
|
17 | 17 | AuxiliaryDataHash,
|
@@ -182,6 +182,24 @@ def filter(
|
182 | 182 |
|
183 | 183 | return new_multi_asset
|
184 | 184 |
|
| 185 | + def count(self, criteria=Callable[[ScriptHash, AssetName, int], bool]) -> int: |
| 186 | + """Count number of distinct assets that satisfy a certain criteria. |
| 187 | +
|
| 188 | + Args: |
| 189 | + criteria: A function that takes in three input arguments (policy_id, asset_name, amount) and returns a |
| 190 | + bool. |
| 191 | +
|
| 192 | + Returns: |
| 193 | + int: Total number of distinct assets that satisfy the criteria. |
| 194 | + """ |
| 195 | + count = 0 |
| 196 | + for p in self: |
| 197 | + for n in self[p]: |
| 198 | + if criteria(p, n, self[p][n]): |
| 199 | + count += 1 |
| 200 | + |
| 201 | + return count |
| 202 | + |
185 | 203 |
|
186 | 204 | @typechecked
|
187 | 205 | @dataclass(repr=False)
|
@@ -236,6 +254,21 @@ class TransactionOutput(ArrayCBORSerializable):
|
236 | 254 |
|
237 | 255 | datum_hash: DatumHash = field(default=None, metadata={"optional": True})
|
238 | 256 |
|
| 257 | + def validate(self): |
| 258 | + if isinstance(self.amount, int) and self.amount < 0: |
| 259 | + raise InvalidDataException( |
| 260 | + f"Transaction output cannot have negative amount of ADA or " |
| 261 | + f"native asset: \n {self.amount}" |
| 262 | + ) |
| 263 | + if isinstance(self.amount, Value) and ( |
| 264 | + self.amount.coin < 0 |
| 265 | + or self.amount.multi_asset.count(lambda p, n, v: v < 0) > 0 |
| 266 | + ): |
| 267 | + raise InvalidDataException( |
| 268 | + f"Transaction output cannot have negative amount of ADA or " |
| 269 | + f"native asset: \n {self.amount}" |
| 270 | + ) |
| 271 | + |
239 | 272 | @property
|
240 | 273 | def lovelace(self) -> int:
|
241 | 274 | if isinstance(self.amount, int):
|
|
0 commit comments