Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rohan-agarwal-coinbase committed Nov 22, 2024
1 parent c556401 commit 9e0e3be
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 7 deletions.
69 changes: 69 additions & 0 deletions cdp/fiat_amount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from decimal import Decimal

from cdp.client.models.fiat_amount import FiatAmount as FiatAmountModel


class FiatAmount:
"""A representation of a FiatAmount that includes the amount and currency."""

def __init__(self, amount: Decimal, currency: str) -> None:
"""Initialize a new FiatAmount. Do not use this directly, use the from_model method instead.
Args:
amount (Decimal): The amount in the fiat currency
currency (str): The currency code (e.g. 'USD')
"""
self._amount = amount
self._currency = currency

@classmethod
def from_model(cls, fiat_amount_model: FiatAmountModel) -> "FiatAmount":
"""Convert a FiatAmount model to a FiatAmount.
Args:
fiat_amount_model (FiatAmountModel): The fiat amount from the API.
Returns:
FiatAmount: The converted FiatAmount object.
"""
return cls(amount=Decimal(fiat_amount_model.amount), currency=fiat_amount_model.currency)

@property
def amount(self) -> Decimal:
"""Get the amount in the fiat currency.
Returns:
Decimal: The amount in the fiat currency.
"""
return self._amount

@property
def currency(self) -> str:
"""Get the currency code.
Returns:
str: The currency code.
"""
return self._currency

def __str__(self) -> str:
"""Get a string representation of the FiatAmount.
Returns:
str: A string representation of the FiatAmount.
"""
return f"FiatAmount(amount: '{int(self.amount)}', currency: '{self.currency}')"

def __repr__(self) -> str:
"""Get a string representation of the FiatAmount.
Returns:
str: A string representation of the FiatAmount.
"""
return self.__str__()
5 changes: 3 additions & 2 deletions cdp/fund_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from cdp.cdp import Cdp
from cdp.client.models import FundOperation as FundOperationModel
from cdp.crypto_amount import CryptoAmount
from cdp.fiat_amount import FiatAmount
from cdp.fund_quote import FundQuote


Expand Down Expand Up @@ -153,9 +154,9 @@ def amount(self) -> CryptoAmount:
return CryptoAmount.from_model(self._model.crypto_amount)

@property
def fiat_amount(self) -> Decimal:
def fiat_amount(self) -> FiatAmount:
"""Get the fiat amount."""
return Decimal(self._model.fiat_amount.amount)
return FiatAmount.from_model(self._model.fiat_amount)

@property
def fiat_currency(self) -> str:
Expand Down
7 changes: 4 additions & 3 deletions cdp/fund_quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from cdp.asset import Asset
from cdp.cdp import Cdp
from cdp.crypto_amount import CryptoAmount
from cdp.fiat_amount import FiatAmount

if TYPE_CHECKING:
from cdp.fund_operation import FundOperation
Expand Down Expand Up @@ -123,14 +124,14 @@ def amount(self) -> CryptoAmount:
return CryptoAmount.from_model(self._model.crypto_amount)

@property
def fiat_amount(self) -> Decimal:
def fiat_amount(self) -> FiatAmount:
"""Get the fiat amount.
Returns:
Decimal: The fiat amount.
"""
return Decimal(self._model.fiat_amount.amount)
return FiatAmount.from_model(self._model.fiat_amount)

@property
def fiat_currency(self) -> str:
Expand Down Expand Up @@ -188,7 +189,7 @@ def __str__(self) -> str:
return (
f"FundQuote(network_id: {self.network_id}, wallet_id: {self.wallet_id}, "
f"address_id: {self.address_id}, crypto_amount: {self.amount.amount}, "
f"crypto_asset: {self.asset.asset_id}, fiat_amount: {self.fiat_amount}, "
f"crypto_asset: {self.asset.asset_id}, fiat_amount: {self.fiat_amount.amount}, "
f"fiat_currency: {self.fiat_currency}, buy_fee: {{'amount': '{self.buy_fee['amount']}'}}, "
f"transfer_fee: {{'amount': '{self.transfer_fee.amount}'}})"
)
Expand Down
36 changes: 36 additions & 0 deletions tests/test_fiat_amount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from decimal import Decimal

from cdp.client.models.fiat_amount import FiatAmount as FiatAmountModel
from cdp.fiat_amount import FiatAmount


def test_fiat_amount_from_model():
"""Test converting a FiatAmount model to a FiatAmount."""
model = FiatAmountModel(amount="100.50", currency="USD")
fiat_amount = FiatAmount.from_model(model)

assert fiat_amount.amount == Decimal("100.50")
assert fiat_amount.currency == "USD"


def test_fiat_amount_properties():
"""Test FiatAmount properties."""
fiat_amount = FiatAmount(amount=Decimal("50.25"), currency="USD")

assert fiat_amount.amount == Decimal("50.25")
assert fiat_amount.currency == "USD"


def test_fiat_amount_str_representation():
"""Test string representation of FiatAmount."""
fiat_amount = FiatAmount(amount=Decimal("75.00"), currency="USD")
expected_str = "FiatAmount(amount: '75', currency: 'USD')"

assert str(fiat_amount) == expected_str
assert repr(fiat_amount) == expected_str


def test_fiat_amount_repr():
"""Test repr of FiatAmount."""
fiat_amount = FiatAmount(amount=Decimal("75.00"), currency="USD")
assert repr(fiat_amount) == "FiatAmount(amount: '75', currency: 'USD')"
2 changes: 1 addition & 1 deletion tests/test_fund_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_fund_operation_properties(fund_operation_factory):
"""Test the properties of a FundOperation object."""
fund_operation = fund_operation_factory()
assert fund_operation.amount.amount == Decimal("2")
assert fund_operation.fiat_amount == Decimal("100")
assert fund_operation.fiat_amount.amount == Decimal("100")
assert fund_operation.buy_fee["amount"] == "1"
assert fund_operation.transfer_fee.amount == Decimal("0.01")
assert isinstance(fund_operation.asset, Asset)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_fund_quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_fund_quote_properties(fund_quote_factory):
"""Test the properties of a FundQuote object."""
fund_quote = fund_quote_factory()
assert fund_quote.amount.amount == Decimal("2")
assert fund_quote.fiat_amount == Decimal("100")
assert fund_quote.fiat_amount.amount == Decimal("100")
assert fund_quote.buy_fee["amount"] == "1"
assert fund_quote.transfer_fee.amount == Decimal("0.01")
assert isinstance(fund_quote.asset, Asset)
Expand Down

0 comments on commit 9e0e3be

Please sign in to comment.