Skip to content

Commit

Permalink
Merge branch 'feat/multi_asset_math' into running_fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nielstron committed Feb 21, 2024
2 parents bcf3f3c + c221347 commit c666156
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
21 changes: 4 additions & 17 deletions pycardano/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
30 changes: 24 additions & 6 deletions test/pycardano/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit c666156

Please sign in to comment.