Skip to content

Commit

Permalink
Allow subtracting assets if not existing before
Browse files Browse the repository at this point in the history
Negative values were allowed before as well so it is only consequential
to allow subtracting from 0
  • Loading branch information
nielstron committed Feb 21, 2024
1 parent d4ec506 commit 4b95a56
Showing 1 changed file with 4 additions and 17 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

0 comments on commit 4b95a56

Please sign in to comment.