diff --git a/cdp/fiat_amount.py b/cdp/fiat_amount.py new file mode 100644 index 0000000..7454c66 --- /dev/null +++ b/cdp/fiat_amount.py @@ -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__() diff --git a/cdp/fund_operation.py b/cdp/fund_operation.py index e9a71f6..54e61e7 100644 --- a/cdp/fund_operation.py +++ b/cdp/fund_operation.py @@ -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 @@ -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: diff --git a/cdp/fund_quote.py b/cdp/fund_quote.py index 31cc24e..36e318e 100644 --- a/cdp/fund_quote.py +++ b/cdp/fund_quote.py @@ -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 @@ -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: @@ -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}'}})" ) diff --git a/tests/test_fiat_amount.py b/tests/test_fiat_amount.py new file mode 100644 index 0000000..ff802b9 --- /dev/null +++ b/tests/test_fiat_amount.py @@ -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')" diff --git a/tests/test_fund_operation.py b/tests/test_fund_operation.py index 9722174..bbe941f 100644 --- a/tests/test_fund_operation.py +++ b/tests/test_fund_operation.py @@ -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) diff --git a/tests/test_fund_quote.py b/tests/test_fund_quote.py index a38a3f5..23084ab 100644 --- a/tests/test_fund_quote.py +++ b/tests/test_fund_quote.py @@ -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)