Skip to content

Commit

Permalink
Automatically reduce multiassets and assets
Browse files Browse the repository at this point in the history
  • Loading branch information
nielstron committed Sep 2, 2024
1 parent b55dfaf commit 2e2fc08
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion pycardano/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
MapCBORSerializable,
Primitive,
default_encoder,
list_hook,
list_hook, DictBase, limit_primitive_type,
)
from pycardano.types import typechecked
from pycardano.witness import TransactionWitnessSet
Expand Down Expand Up @@ -94,17 +94,29 @@ def __add__(self, other: Asset) -> Asset:
new_asset = deepcopy(self)
for n in other:
new_asset[n] = new_asset.get(n, 0) + other[n]
# pop zero values
for n, v in list(new_asset.items()):
if v == 0:
new_asset.pop(n)
return new_asset

def __iadd__(self, other: Asset) -> Asset:
new_item = self + other
self.update(new_item)
# pop zero values
for n, v in list(self.items()):
if new_item.get(n, 0) == 0:
self.pop(n)
return self

def __sub__(self, other: Asset) -> Asset:
new_asset = deepcopy(self)
for n in other:
new_asset[n] = new_asset.get(n, 0) - other[n]
# pop zero values
for n, v in list(new_asset.items()):
if v == 0:
new_asset.pop(n)
return new_asset

def __eq__(self, other):
Expand All @@ -124,6 +136,17 @@ def __le__(self, other: Asset) -> bool:
return False
return True

@classmethod
@limit_primitive_type(dict)
def from_primitive(cls: Type[DictBase], value: dict) -> DictBase:
res = super().from_primitive(value)
# pop zero values
for n, v in list(res.items()):
if v == 0:
res.pop(n)
return res



@typechecked
class MultiAsset(DictCBORSerializable):
Expand All @@ -138,17 +161,29 @@ def __add__(self, other):
new_multi_asset = deepcopy(self)
for p in other:
new_multi_asset[p] = new_multi_asset.get(p, Asset()) + other[p]
# pop empty assets
for p in list(new_multi_asset.keys()):
if not new_multi_asset[p]:
new_multi_asset.pop(p)
return new_multi_asset

def __iadd__(self, other):
new_item = self + other
self.update(new_item)
# pop empty assets
for p in list(self.keys()):
if not new_item.get(p, Asset()):
self.pop(p)
return self

def __sub__(self, other: MultiAsset) -> MultiAsset:
new_multi_asset = deepcopy(self)
for p in other:
new_multi_asset[p] = new_multi_asset.get(p, Asset()) - other[p]
# pop empty assets
for p in list(new_multi_asset.keys()):
if not new_multi_asset[p]:
new_multi_asset.pop(p)
return new_multi_asset

def __eq__(self, other):
Expand Down Expand Up @@ -209,6 +244,16 @@ def count(self, criteria=Callable[[ScriptHash, AssetName, int], bool]) -> int:

return count

@classmethod
@limit_primitive_type(dict)
def from_primitive(cls: Type[DictBase], value: dict) -> DictBase:
res = super().from_primitive(value)
# pop empty values
for n, v in list(res.items()):
if not v:
res.pop(n)
return res


@typechecked
@dataclass(repr=False)
Expand Down

0 comments on commit 2e2fc08

Please sign in to comment.