|
| 1 | +""" |
| 2 | +Specifies an amount in an issued currency, but without a value field. |
| 3 | +This format is used for some book order requests. |
| 4 | +
|
| 5 | +See https://xrpl.org/currency-formats.html#specifying-currency-amounts |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from dataclasses import dataclass |
| 11 | +from typing import Dict, Union |
| 12 | + |
| 13 | +from typing_extensions import Self |
| 14 | + |
| 15 | +import xrpl.models.amounts # not a direct import, to get around circular imports |
| 16 | +from xrpl.constants import HEX_MPTID_REGEX |
| 17 | +from xrpl.models.base_model import BaseModel |
| 18 | +from xrpl.models.required import REQUIRED |
| 19 | +from xrpl.models.utils import KW_ONLY_DATACLASS, require_kwargs_on_init |
| 20 | + |
| 21 | + |
| 22 | +def _is_valid_mptid(candidate: str) -> bool: |
| 23 | + return bool(HEX_MPTID_REGEX.fullmatch(candidate)) |
| 24 | + |
| 25 | + |
| 26 | +@require_kwargs_on_init |
| 27 | +@dataclass(frozen=True, **KW_ONLY_DATACLASS) |
| 28 | +class MPTCurrency(BaseModel): |
| 29 | + """ |
| 30 | + Specifies an amount in an MPT, but without a value field. |
| 31 | + This format is used for some book order requests. |
| 32 | +
|
| 33 | + See https://xrpl.org/currency-formats.html#specifying-currency-amounts |
| 34 | + """ |
| 35 | + |
| 36 | + mpt_issuance_id: str = REQUIRED # type: ignore |
| 37 | + """ |
| 38 | + This field is required. |
| 39 | +
|
| 40 | + :meta hide-value: |
| 41 | + """ |
| 42 | + |
| 43 | + def _get_errors(self: Self) -> Dict[str, str]: |
| 44 | + errors = super()._get_errors() |
| 45 | + if not _is_valid_mptid(self.mpt_issuance_id): |
| 46 | + errors["mpt_issuance_id"] = ( |
| 47 | + f"Invalid mpt_issuance_id {self.mpt_issuance_id}" |
| 48 | + ) |
| 49 | + return errors |
| 50 | + |
| 51 | + def to_amount(self: Self, value: Union[str, int]) -> xrpl.models.amounts.MPTAmount: |
| 52 | + """ |
| 53 | + Converts an MPTCurrency to an MPTAmount. |
| 54 | +
|
| 55 | + Args: |
| 56 | + value: The amount of MPTs in the MPTAmount. |
| 57 | +
|
| 58 | + Returns: |
| 59 | + An MPTAmount that represents the MPT and the provided value. |
| 60 | + """ |
| 61 | + return xrpl.models.amounts.MPTAmount( |
| 62 | + mpt_issuance_id=self.mpt_issuance_id, value=str(value) |
| 63 | + ) |
0 commit comments