-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[History] add balance history and transactions function for address (#25
) * [History] add balance history and transactions function for address * update changelog * fix merge typo * fix lint * address comment * minor * minor * address comments * fix merge issue * lint
- Loading branch information
1 parent
59ec96e
commit a09f7bf
Showing
9 changed files
with
469 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
from collections.abc import Iterator | ||
from decimal import Decimal | ||
|
||
from cdp.asset import Asset | ||
from cdp.cdp import Cdp | ||
from cdp.client.models.address_historical_balance_list import AddressHistoricalBalanceList | ||
from cdp.client.models.historical_balance import HistoricalBalance as HistoricalBalanceModel | ||
|
||
|
||
class HistoricalBalance: | ||
"""A class representing a balance.""" | ||
|
||
def __init__(self, amount: Decimal, asset: Asset, block_height: str, block_hash: str): | ||
"""Initialize the Balance class. | ||
Args: | ||
amount (Decimal): The amount. | ||
asset (Asset): The asset. | ||
block_height (str): the block height where the balance is in. | ||
block_hash (str): the block hash where the balance is in. | ||
""" | ||
self._amount = amount | ||
self._asset = asset | ||
self._block_height = block_height | ||
self._block_hash = block_hash | ||
|
||
@classmethod | ||
def from_model(cls, model: HistoricalBalanceModel) -> "HistoricalBalance": | ||
"""Create a Balance instance from a model. | ||
Args: | ||
model (BalanceModel): The model representing the balance. | ||
asset_id (Optional[str]): The asset ID. | ||
Returns: | ||
Balance: The Balance instance. | ||
""" | ||
asset = Asset.from_model(model.asset) | ||
|
||
return cls( | ||
amount=asset.from_atomic_amount(model.amount), | ||
asset=asset, | ||
block_height=model.block_height, | ||
block_hash=model.block_hash | ||
) | ||
|
||
@classmethod | ||
def list(cls, network_id: str, address_id: str, asset_id: str) -> Iterator["HistoricalBalance"]: | ||
"""List historical balances of an address of an asset. | ||
Args: | ||
network_id (str): The ID of the network to list historical balance for. | ||
address_id (str): The ID of the address to list historical balance for. | ||
asset_id(str): The asset ID to list historical balance. | ||
Returns: | ||
Iterator[Transaction]: An iterator of HistoricalBalance objects. | ||
Raises: | ||
Exception: If there's an error listing the historical_balances. | ||
""" | ||
page = None | ||
while True: | ||
response: AddressHistoricalBalanceList = Cdp.api_clients.balance_history.list_address_historical_balance( | ||
network_id=network_id, | ||
address_id=address_id, | ||
asset_id=Asset.primary_denomination(asset_id), | ||
limit=100, | ||
page=page, | ||
) | ||
|
||
for model in response.data: | ||
yield cls.from_model(model) | ||
|
||
if not response.has_more: | ||
break | ||
|
||
page = response.next_page | ||
|
||
@property | ||
def amount(self) -> Decimal: | ||
"""Get the amount. | ||
Returns: | ||
Decimal: The amount. | ||
""" | ||
return self._amount | ||
|
||
@property | ||
def asset(self) -> Asset: | ||
"""Get the asset. | ||
Returns: | ||
Asset: The asset. | ||
""" | ||
return self._asset | ||
|
||
@property | ||
def block_height(self) -> str: | ||
"""Get the block height. | ||
Returns: | ||
str: The block height. | ||
""" | ||
return self._block_height | ||
|
||
@property | ||
def block_hash(self) -> str: | ||
"""Get the block hash. | ||
Returns: | ||
str: The block hash. | ||
""" | ||
return self._block_hash | ||
|
||
def __str__(self) -> str: | ||
"""Return a string representation of the Balance.""" | ||
return f"HistoricalBalance: (amount: {self.amount}, asset: {self.asset}, block_height: {self.block_height}, block_hash: {self.block_hash})" | ||
|
||
def __repr__(self) -> str: | ||
"""Return a string representation of the Balance.""" | ||
return str(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import pytest | ||
|
||
from cdp.client.models.historical_balance import HistoricalBalance as HistoricalBalanceModel | ||
from cdp.historical_balance import HistoricalBalance | ||
|
||
|
||
@pytest.fixture | ||
def historical_balance_model_factory(asset_model_factory): | ||
"""Create and return a factory for creating HistoricalBalance fixtures.""" | ||
|
||
def _create_historical_balance_model( | ||
amount="1000000000000000000", | ||
network_id="base-sepolia", | ||
asset_id="eth", | ||
decimals=18, | ||
block_hash="0xblockhash", | ||
block_height="12345" | ||
): | ||
asset_model = asset_model_factory(network_id, asset_id, decimals) | ||
return HistoricalBalanceModel( | ||
amount=amount, block_hash=block_hash, block_height=block_height, asset=asset_model) | ||
|
||
return _create_historical_balance_model | ||
|
||
|
||
@pytest.fixture | ||
def historical_balance_factory(asset_factory): | ||
"""Create and return a factory for creating HistoricalBalance fixtures.""" | ||
|
||
def _create_historical_balance( | ||
amount="1000000000000000000", | ||
network_id="base-sepolia", | ||
asset_id="eth", | ||
decimals=18, | ||
block_hash="0xblockhash", | ||
block_height="12345" | ||
): | ||
asset = asset_factory(network_id=network_id, asset_id=asset_id, decimals=decimals) | ||
return HistoricalBalance(amount, asset, block_height, block_hash) | ||
|
||
return _create_historical_balance |
Oops, something went wrong.