Skip to content

Commit

Permalink
Add AssetDelta.__mul__ method
Browse files Browse the repository at this point in the history
  • Loading branch information
miohtama committed Jul 3, 2023
1 parent 4270478 commit 5c9a8f9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Current
# 0.22.2

- TODO
- Add `AssetDelta.__mul__` method

# 0.21.1

Expand Down
20 changes: 20 additions & 0 deletions eth_defi/tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ def __post_init__(self):
assert type(self.asset) in (HexAddress, str)
assert self.raw_amount, "Raw amount should be specified"

def __mul__(self, other: float | int) -> "AssetDelta":
"""Adjust asset delta by multiplier.
E.g. multiplied by 0.99 returns :py:class:`AssetDelta` with
raw amount reduced 1%. Uses integer flooring.
Example:
.. code-block:: python
d = AssetDelta(usdc.address, 1*10**6)
d2 = d * 0.99
assert d2.raw_amount == int(10**6 * 0.99)
"""
assert isinstance(other, (float, int))
return AssetDelta(
self.asset,
int(self.raw_amount * other)
)

def is_incoming(self) -> bool:
"""This delta describes incoming assets."""
return self.raw_amount > 0
Expand Down
8 changes: 8 additions & 0 deletions tests/enzyme/test_vault_controlled_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ def deployment(
return deployment


def test_asset_delta_mul(usdc: Contract):
"""Check that the asset delta multiplier works."""

d = AssetDelta(usdc.address, 1*10**6)
d2 = d * 0.99
assert d2.raw_amount == int(10**6 * 0.99)


def test_repr(
web3: Web3,
deployment: EnzymeDeployment,
Expand Down

0 comments on commit 5c9a8f9

Please sign in to comment.