-
Notifications
You must be signed in to change notification settings - Fork 116
fix: add MPTCurrency
type instead of MPTAmount
for Issue
s
#822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
834d630
4df151c
0d616c0
8651933
88b0f36
7b0dccd
a700ab1
6b07894
c4ce852
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from unittest import TestCase | ||
|
||
from xrpl.models.currencies import MPTCurrency | ||
from xrpl.models.exceptions import XRPLModelException | ||
|
||
_MPTID = "00002403C84A0A28E0190E208E982C352BBD5006600555CF" | ||
|
||
|
||
class TestMPTCurrency(TestCase): | ||
def test_correct_mptid_format(self): | ||
obj = MPTCurrency( | ||
mpt_issuance_id=_MPTID, | ||
) | ||
self.assertTrue(obj.is_valid()) | ||
|
||
def test_lower_mptid_format(self): | ||
obj = MPTCurrency( | ||
mpt_issuance_id=_MPTID.lower(), | ||
) | ||
self.assertTrue(obj.is_valid()) | ||
|
||
def test_invalid_length(self): | ||
with self.assertRaises(XRPLModelException): | ||
MPTCurrency(mpt_issuance_id=_MPTID[:40]) | ||
|
||
with self.assertRaises(XRPLModelException): | ||
MPTCurrency(mpt_issuance_id=_MPTID + "AA") | ||
|
||
def test_incorrect_hex_format(self): | ||
# the "+" is not allowed in a currency format" | ||
with self.assertRaises(XRPLModelException): | ||
MPTCurrency( | ||
mpt_issuance_id="ABCD" * 11 + "XXXX", | ||
) | ||
|
||
def test_to_amount(self): | ||
amount = "12" | ||
MPT_currency = MPTCurrency(mpt_issuance_id=_MPTID) | ||
MPT_currency_amount = MPT_currency.to_amount(amount) | ||
|
||
self.assertEqual(MPT_currency_amount.mpt_issuance_id, _MPTID) | ||
self.assertEqual(MPT_currency_amount.value, amount) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,8 @@ class XRPLException(Exception): | |
:meta private: | ||
""" | ||
|
||
HEX_MPTID_REGEX: Final[Pattern[str]] = re.compile(r"^[0-9A-Fa-f]{48}$") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was under the belief that However, the size of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 192 bits is 24 bytes, which is a hex string of length 48.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
As a new feature, if rippled could expose an
Hmm yes, you are right. I had mis-understood the calculation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, I would slightly prefer the name The MPT amendment has been merged into the rippled codebase, I should be able to use the develop branch to test the
Hmm, this will help me with serialization, but not the other way around i.e. I'm trying to understand how I can deserialize a blob of |
||
|
||
# Constants for validating amounts. | ||
MIN_IOU_EXPONENT: Final[int] = -96 | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
""" | ||
The XRP Ledger has two kinds of money: XRP, and issued | ||
currencies. Both types have high precision, although their | ||
formats are different. | ||
The XRP Ledger has three kinds of money: XRP, issued currencies, and MPTs. All types | ||
have high precision, although their formats are different. | ||
""" | ||
|
||
from xrpl.models.currencies.currency import Currency | ||
from xrpl.models.currencies.issued_currency import IssuedCurrency | ||
from xrpl.models.currencies.mpt_currency import MPTCurrency | ||
from xrpl.models.currencies.xrp import XRP | ||
|
||
__all__ = [ | ||
"Currency", | ||
"IssuedCurrency", | ||
"MPTCurrency", | ||
"XRP", | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
Specifies an amount in an issued currency, but without a value field. | ||
This format is used for some book order requests. | ||
|
||
See https://xrpl.org/currency-formats.html#specifying-currency-amounts | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from typing import Dict, Union | ||
|
||
from typing_extensions import Self | ||
|
||
import xrpl.models.amounts # not a direct import, to get around circular imports | ||
from xrpl.constants import HEX_MPTID_REGEX | ||
from xrpl.models.base_model import BaseModel | ||
from xrpl.models.required import REQUIRED | ||
from xrpl.models.utils import KW_ONLY_DATACLASS, require_kwargs_on_init | ||
|
||
|
||
def _is_valid_mptid(candidate: str) -> bool: | ||
return bool(HEX_MPTID_REGEX.fullmatch(candidate)) | ||
|
||
|
||
@require_kwargs_on_init | ||
@dataclass(frozen=True, **KW_ONLY_DATACLASS) | ||
class MPTCurrency(BaseModel): | ||
""" | ||
Specifies an amount in an MPT, but without a value field. | ||
This format is used for some book order requests. | ||
|
||
See https://xrpl.org/currency-formats.html#specifying-currency-amounts | ||
""" | ||
|
||
mpt_issuance_id: str = REQUIRED # type: ignore | ||
""" | ||
This field is required. | ||
|
||
:meta hide-value: | ||
""" | ||
|
||
def _get_errors(self: Self) -> Dict[str, str]: | ||
errors = super()._get_errors() | ||
if not _is_valid_mptid(self.mpt_issuance_id): | ||
errors["mpt_issuance_id"] = ( | ||
f"Invalid mpt_issuance_id {self.mpt_issuance_id}" | ||
) | ||
return errors | ||
|
||
def to_amount(self: Self, value: Union[str, int]) -> xrpl.models.amounts.MPTAmount: | ||
""" | ||
Converts an MPTCurrency to an MPTAmount. | ||
|
||
Args: | ||
value: The amount of MPTs in the MPTAmount. | ||
|
||
Returns: | ||
An MPTAmount that represents the MPT and the provided value. | ||
""" | ||
return xrpl.models.amounts.MPTAmount( | ||
mpt_issuance_id=self.mpt_issuance_id, value=str(value) | ||
) |
Uh oh!
There was an error while loading. Please reload this page.