Skip to content

Commit

Permalink
feat(docs): func level docstrings for balance module (#100)
Browse files Browse the repository at this point in the history
Co-authored-by: BobTheBuidler <[email protected]>
  • Loading branch information
nvy-0x and BobTheBuidler authored Nov 9, 2024
1 parent 40ab4cb commit 6a69143
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions eth_portfolio/_loaders/balances.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@

@stuck_coro_debugger
async def load_token_balance(token: y.ERC20, address: Address, block: Block) -> Balance:
"""
Asynchronously fetch the ERC20 token balance and its USD value for a given address at a specific block.
Args:
token: The ERC20 token contract to query.
address: The address holding the ERC20 tokens.
block: The block number for the balance query.
Returns:
:class:`~eth_portfolio.typing.Balance`: A custom object containing:
- balance: The token balance (in token's smallest unit).
- value: The USD value of the balance (18 decimal places).
- token: The ERC20 token which was checked.
- block: The block number where the balance was taken.
Note:
Non-standard ERC20 tokens are handled gracefully, returning a zero balance.
Example:
>>> balance = await load_token_balance(token=dai_contract, address='0x1234...', block=12345678)
>>> print(f"Token: {balance.token}, Value: {balance.balance}, USD: {balance.usd_value}")
"""
try:
balance = await token.balance_of_readable(address, block, sync=False)
except y.NonStandardERC20:
Expand All @@ -28,6 +50,24 @@ async def load_token_balance(token: y.ERC20, address: Address, block: Block) ->


def _calc_value(balance, price) -> Decimal:
"""
Calculate the USD value of a token balance based on its price.
Args:
balance: The token balance.
price: The token price in USD.
Returns:
The total USD value, rounded to 18 decimal places if possible.
If rounding is not possible due to high precision, returns the unrounded value.
Note:
Returns :class:`~decimal.Decimal(0)` if the price is None, handling cases where price data is unavailable.
Example:
>>> value = _calc_value(balance=1000, price=0.50)
>>> print(f"USD Value: {value}")
"""
if price is None:
return Decimal(0)
# NOTE If balance * price returns a Decimal with precision < 18, rounding is both impossible and unnecessary.
Expand Down

0 comments on commit 6a69143

Please sign in to comment.