Skip to content

Commit b5451ca

Browse files
committed
Only raise exception when minting assets whose values are out of bound
1 parent 819a1ea commit b5451ca

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

pycardano/transaction.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,6 @@ class Asset(DictCBORSerializable):
8181

8282
VALUE_TYPE = int
8383

84-
def validate(self):
85-
for n in self:
86-
if self[n] < _MIN_INT64 or self[n] > _MAX_INT64:
87-
raise InvalidDataException(
88-
f"Asset amount must be between {_MIN_INT64} and {_MAX_INT64}: \n {self[n]}"
89-
)
90-
9184
def union(self, other: Asset) -> Asset:
9285
return self + other
9386

@@ -570,6 +563,15 @@ class TransactionBody(MapCBORSerializable):
570563
},
571564
)
572565

566+
def validate(self):
567+
if (
568+
self.mint
569+
and self.mint.count(lambda p, n, v: v < _MIN_INT64 or v > _MAX_INT64) > 0
570+
):
571+
raise InvalidDataException(
572+
f"Mint amount must be between {_MIN_INT64} and {_MAX_INT64}. \n Mint amount: {self.mint}"
573+
)
574+
573575
def hash(self) -> bytes:
574576
return blake2b(self.to_cbor(), TRANSACTION_HASH_SIZE, encoder=RawEncoder) # type: ignore
575577

test/pycardano/test_transaction.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,12 @@ class TestDatum(PlutusData):
472472

473473

474474
def test_out_of_bound_asset():
475-
bad_asset = Asset({AssetName(b"abc"): 1 << 64})
475+
a = Asset({AssetName(b"abc"): 1 << 64})
476476

477+
a.to_cbor_hex() # okay to have out of bound asset
478+
479+
tx = TransactionBody(mint=MultiAsset({ScriptHash(b"1" * SCRIPT_HASH_SIZE): a}))
480+
481+
# Not okay only when minting
477482
with pytest.raises(InvalidDataException):
478-
bad_asset.to_cbor_hex()
483+
tx.to_cbor_hex()

0 commit comments

Comments
 (0)