14
14
15
15
from pycardano .address import Address
16
16
from pycardano .certificate import Certificate
17
- from pycardano .exception import InvalidDataException , InvalidOperationException
17
+ from pycardano .exception import InvalidDataException
18
18
from pycardano .hash import (
19
19
TRANSACTION_HASH_SIZE ,
20
20
AuxiliaryDataHash ,
@@ -95,14 +95,7 @@ def __iadd__(self, other: Asset) -> Asset:
95
95
def __sub__ (self , other : Asset ) -> Asset :
96
96
new_asset = deepcopy (self )
97
97
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 ]
106
99
return new_asset
107
100
108
101
def __eq__ (self , other ):
@@ -135,9 +128,7 @@ def union(self, other: MultiAsset) -> MultiAsset:
135
128
def __add__ (self , other ):
136
129
new_multi_asset = deepcopy (self )
137
130
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 ]
141
132
return new_multi_asset
142
133
143
134
def __iadd__ (self , other ):
@@ -148,11 +139,7 @@ def __iadd__(self, other):
148
139
def __sub__ (self , other : MultiAsset ) -> MultiAsset :
149
140
new_multi_asset = deepcopy (self )
150
141
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 ]
156
143
return new_multi_asset
157
144
158
145
def __eq__ (self , other ):
0 commit comments