From d4ec506fc42a449f5e528a23f824e26ac45a538a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=BCndler?= Date: Tue, 20 Feb 2024 20:35:44 +0100 Subject: [PATCH 1/3] Fix crucial references to withdrawal (#316) --- pycardano/txbuilder.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pycardano/txbuilder.py b/pycardano/txbuilder.py index 85bbe763..ab27a780 100644 --- a/pycardano/txbuilder.py +++ b/pycardano/txbuilder.py @@ -313,12 +313,12 @@ def add_withdrawal_script( TransactionBuilder: Current transaction builder. """ if redeemer: - if redeemer.tag is not None and redeemer.tag != RedeemerTag.REWARD: + if redeemer.tag is not None and redeemer.tag != RedeemerTag.WITHDRAWAL: raise InvalidArgumentException( - f"Expect the redeemer tag's type to be {RedeemerTag.REWARD}, " + f"Expect the redeemer tag's type to be {RedeemerTag.WITHDRAWAL}, " f"but got {redeemer.tag} instead." ) - redeemer.tag = RedeemerTag.REWARD + redeemer.tag = RedeemerTag.WITHDRAWAL self._consolidate_redeemer(redeemer) if isinstance(script, UTxO): @@ -1259,9 +1259,7 @@ def _update_execution_units( assert ( r.tag is not None ), "Expected tag of redeemer to be set, but found None" - tagname = ( - r.tag.name.lower() if r.tag != RedeemerTag.REWARD else "withdrawal" - ) + tagname = r.tag.name.lower() key = f"{tagname}:{r.index}" if ( key not in estimated_execution_units From 4b95a568c05504466f81a28924ed3539bffe76d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=BCndler?= Date: Wed, 21 Feb 2024 12:49:50 +0100 Subject: [PATCH 2/3] Allow subtracting assets if not existing before Negative values were allowed before as well so it is only consequential to allow subtracting from 0 --- pycardano/transaction.py | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/pycardano/transaction.py b/pycardano/transaction.py index fa324059..833df586 100644 --- a/pycardano/transaction.py +++ b/pycardano/transaction.py @@ -14,7 +14,7 @@ from pycardano.address import Address from pycardano.certificate import Certificate -from pycardano.exception import InvalidDataException, InvalidOperationException +from pycardano.exception import InvalidDataException from pycardano.hash import ( TRANSACTION_HASH_SIZE, AuxiliaryDataHash, @@ -95,14 +95,7 @@ def __iadd__(self, other: Asset) -> Asset: def __sub__(self, other: Asset) -> Asset: new_asset = deepcopy(self) for n in other: - if n not in new_asset: - raise InvalidOperationException( - f"Asset: {new_asset} does not have asset with name: {n}" - ) - # According to ledger rule, the value of an asset could be negative, so we don't check the value here and - # will leave the check to users when necessary. - # https://github.com/input-output-hk/cardano-ledger/blob/master/eras/alonzo/test-suite/cddl-files/alonzo.cddl#L378 - new_asset[n] -= other[n] + new_asset[n] = new_asset.get(n, 0) - other[n] return new_asset def __eq__(self, other): @@ -135,9 +128,7 @@ def union(self, other: MultiAsset) -> MultiAsset: def __add__(self, other): new_multi_asset = deepcopy(self) for p in other: - if p not in new_multi_asset: - new_multi_asset[p] = Asset() - new_multi_asset[p] += other[p] + new_multi_asset[p] = new_multi_asset.get(p, Asset()) + other[p] return new_multi_asset def __iadd__(self, other): @@ -148,11 +139,7 @@ def __iadd__(self, other): def __sub__(self, other: MultiAsset) -> MultiAsset: new_multi_asset = deepcopy(self) for p in other: - if p not in new_multi_asset: - raise InvalidOperationException( - f"MultiAsset: {new_multi_asset} doesn't have policy: {p}" - ) - new_multi_asset[p] -= other[p] + new_multi_asset[p] = new_multi_asset.get(p, Asset()) - other[p] return new_multi_asset def __eq__(self, other): From c22134780fe63cb52da0c007df8558c2d51d93fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=BCndler?= Date: Wed, 21 Feb 2024 12:58:50 +0100 Subject: [PATCH 3/3] Fix testcases --- test/pycardano/test_transaction.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/test/pycardano/test_transaction.py b/test/pycardano/test_transaction.py index b9f2e48b..8d2b7539 100644 --- a/test/pycardano/test_transaction.py +++ b/test/pycardano/test_transaction.py @@ -311,8 +311,12 @@ def test_multi_asset_subtraction(): } ) - with pytest.raises(InvalidOperationException): - a - b + assert a - b == MultiAsset.from_primitive( + { + b"1" * SCRIPT_HASH_SIZE: {b"Token1": -9, b"Token2": -18}, + b"2" * SCRIPT_HASH_SIZE: {b"Token1": -1, b"Token2": -2}, + } + ) def test_asset_comparison(): @@ -427,11 +431,25 @@ def test_values(): [101, {b"1" * SCRIPT_HASH_SIZE: {b"Token1": 1, b"Token2": 2}}] ) - with pytest.raises(InvalidOperationException): - a - c + assert a - c == Value.from_primitive( + [ + -10, + { + b"1" * SCRIPT_HASH_SIZE: {b"Token1": -10, b"Token2": -20}, + b"2" * SCRIPT_HASH_SIZE: {b"Token1": -11, b"Token2": -22}, + }, + ] + ) - with pytest.raises(InvalidOperationException): - b - c + assert b - c == Value.from_primitive( + [ + 0, + { + b"1" * SCRIPT_HASH_SIZE: {b"Token1": 0, b"Token2": 0}, + b"2" * SCRIPT_HASH_SIZE: {b"Token1": -11, b"Token2": -22}, + }, + ] + ) def test_inline_datum_serdes():