-
Notifications
You must be signed in to change notification settings - Fork 85
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
incorporate DeliverMax in Payment transactions #684
Changes from 14 commits
d83c94c
11279cc
e990232
eeafc40
f713c07
6637014
e43c974
1926c42
7de639c
56b8f32
c47fac1
d47260c
3f417a0
b8bd5a2
211e378
79fd8fd
e3d0b7b
59c77d7
051e13e
f5fae35
47c6b07
052ba00
57aa59a
eeaf241
6af9177
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 |
---|---|---|
|
@@ -73,6 +73,28 @@ def _value_to_json(value: XRPL_VALUE_TYPE) -> XRPL_VALUE_TYPE: | |
return value | ||
|
||
|
||
def process_xrpl_json(value: Union[str, Dict[str, Any]]) -> Dict[str, Any]: | ||
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'd recommend making this a helper class method, instead of a separate function - it saves you some imports. Similar to 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. okay, I have done this. But I dislike the fact that the 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 think the tradeoff is worth it given that it makes it easier to use, but if someone strongly objects I'll withdraw 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. yeah, I think it's a reasonable tradeoff 👍 |
||
""" | ||
Creates a dictionary object based on a JSON or dictionary in the standard XRPL | ||
format. | ||
|
||
Args: | ||
value: The dictionary or JSON string to be processed. | ||
|
||
Returns: | ||
A formatted dictionary instantiated from the input. | ||
""" | ||
if isinstance(value, str): | ||
value = json.loads(value) | ||
|
||
formatted_dict = { | ||
_key_to_json(k): _value_to_json(v) | ||
for (k, v) in cast(Dict[str, XRPL_VALUE_TYPE], value).items() | ||
} | ||
|
||
return formatted_dict | ||
|
||
|
||
@dataclass(frozen=True) | ||
class BaseModel(ABC): | ||
"""The base class for all model types.""" | ||
|
@@ -232,13 +254,7 @@ def from_xrpl(cls: Type[BM], value: Union[str, Dict[str, Any]]) -> BM: | |
Returns: | ||
A BaseModel object instantiated from the input. | ||
""" | ||
if isinstance(value, str): | ||
value = json.loads(value) | ||
|
||
formatted_dict = { | ||
_key_to_json(k): _value_to_json(v) | ||
for (k, v) in cast(Dict[str, XRPL_VALUE_TYPE], value).items() | ||
} | ||
formatted_dict = process_xrpl_json(value) | ||
|
||
return cls.from_dict(formatted_dict) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
|
||
from xrpl.core.binarycodec import decode, encode | ||
from xrpl.models.amounts import IssuedCurrencyAmount | ||
from xrpl.models.base_model import ABBREVIATIONS, BaseModel | ||
from xrpl.models.base_model import ABBREVIATIONS, BaseModel, process_xrpl_json | ||
from xrpl.models.exceptions import XRPLModelException | ||
from xrpl.models.flags import check_false_flag_definition, interface_to_flag_list | ||
from xrpl.models.nested_model import NestedModel | ||
|
@@ -468,3 +468,43 @@ def from_blob(tx_blob: str) -> Transaction: | |
The formatted transaction. | ||
""" | ||
return Transaction.from_xrpl(decode(tx_blob)) | ||
|
||
@classmethod | ||
def from_xrpl(cls: Type[T], value: Union[str, Dict[str, Any]]) -> T: | ||
""" | ||
Creates a Transaction object based on a JSON or JSON-string representation of | ||
data | ||
|
||
In Payment transactions, the DeliverMax field is renamed to the Amount field. | ||
|
||
Args: | ||
value: The dictionary or JSON string to be instantiated. | ||
|
||
Returns: | ||
A Transaction object instantiated from the input. | ||
|
||
Raises: | ||
XRPLModelException: If Payment transactions have different values for | ||
amount and deliver_max fields | ||
""" | ||
processed_value = process_xrpl_json(value) | ||
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. This may not return successfully so we should likely note that in the docs |
||
|
||
# handle the deliver_max alias in Payment transactions | ||
if ( | ||
"transaction_type" in processed_value | ||
and processed_value["transaction_type"] == "Payment" | ||
): | ||
if "deliver_max" in processed_value: | ||
ckeshava marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if "amount" in processed_value: | ||
if processed_value["amount"] != processed_value["deliver_max"]: | ||
ckeshava marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise XRPLModelException( | ||
"Error: amount and deliver_max fields are not identical" | ||
ckeshava marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
else: | ||
processed_value["amount"] = processed_value["deliver_max"] | ||
|
||
# deliver_max field is not recognised in the Payment Request format, | ||
# nor is it supported in the serialization operations. | ||
del processed_value["deliver_max"] | ||
|
||
return cls.from_dict(processed_value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this function can return exceptions which we should note and/or handle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know which exceptions might be thrown by this code.
_key_to_json
and_value_to_json
do not throw any exceptions themselvesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you suspect that
json.load
could throw ajson.JSONDecodeError
exception?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function will never be directly used by end users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mvadari Yes, I agree. Do you mean to say we don't have to document all possible exceptions?