1414
1515from pycardano .address import Address
1616from pycardano .certificate import Certificate
17- from pycardano .exception import InvalidDataException , InvalidOperationException
17+ from pycardano .exception import InvalidDataException
1818from pycardano .hash import (
1919 TRANSACTION_HASH_SIZE ,
2020 AuxiliaryDataHash ,
@@ -95,14 +95,7 @@ def __iadd__(self, other: Asset) -> Asset:
9595 def __sub__ (self , other : Asset ) -> Asset :
9696 new_asset = deepcopy (self )
9797 for n in other :
98- if n not in new_asset :
99- raise InvalidOperationException (
100- f"Asset: { new_asset } does not have asset with name: { n } "
101- )
102- # According to ledger rule, the value of an asset could be negative, so we don't check the value here and
103- # will leave the check to users when necessary.
104- # https://github.com/input-output-hk/cardano-ledger/blob/master/eras/alonzo/test-suite/cddl-files/alonzo.cddl#L378
105- new_asset [n ] -= other [n ]
98+ new_asset [n ] = new_asset .get (n , 0 ) - other [n ]
10699 return new_asset
107100
108101 def __eq__ (self , other ):
@@ -135,9 +128,7 @@ def union(self, other: MultiAsset) -> MultiAsset:
135128 def __add__ (self , other ):
136129 new_multi_asset = deepcopy (self )
137130 for p in other :
138- if p not in new_multi_asset :
139- new_multi_asset [p ] = Asset ()
140- new_multi_asset [p ] += other [p ]
131+ new_multi_asset [p ] = new_multi_asset .get (p , Asset ()) + other [p ]
141132 return new_multi_asset
142133
143134 def __iadd__ (self , other ):
@@ -148,11 +139,7 @@ def __iadd__(self, other):
148139 def __sub__ (self , other : MultiAsset ) -> MultiAsset :
149140 new_multi_asset = deepcopy (self )
150141 for p in other :
151- if p not in new_multi_asset :
152- raise InvalidOperationException (
153- f"MultiAsset: { new_multi_asset } doesn't have policy: { p } "
154- )
155- new_multi_asset [p ] -= other [p ]
142+ new_multi_asset [p ] = new_multi_asset .get (p , Asset ()) - other [p ]
156143 return new_multi_asset
157144
158145 def __eq__ (self , other ):
0 commit comments