Skip to content
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

feat: add input check for OracleSet params AssetPrice and Scale #711

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tests/unit/models/transactions/test_oracle_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,25 @@ def test_valid_last_update_time(self):
],
)
self.assertTrue(tx.is_valid())

def test_invalid_price_data_series(self):
with self.assertRaises(XRPLModelException) as err:
OracleSet(
account=_ACCOUNT,
oracle_document_id=1,
provider=_PROVIDER,
asset_class=_ASSET_CLASS,
last_update_time=EPOCH_OFFSET,
price_data_series=[
PriceData(base_asset="XRP", quote_asset="USD", asset_price=740),
PriceData(
base_asset="BTC", quote_asset="EUR", asset_price=100, scale=2
),
],
)

self.assertEqual(
err.exception.args[0],
"{'price_data_series': "
"'Field must have both `AssetPrice` and `Scale` if any are present'}",
)
30 changes: 19 additions & 11 deletions xrpl/models/transactions/oracle_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,25 @@ def _get_errors(self: OracleSet) -> Dict[str, str]:
errors = super()._get_errors()

# If price_data_series is not set, do not perform further validation
if "price_data_series" not in errors and len(self.price_data_series) == 0:
errors["price_data_series"] = "Field must have a length greater than 0."

if (
"price_data_series" not in errors
and len(self.price_data_series) > MAX_ORACLE_DATA_SERIES
):
errors["price_data_series"] = (
"Field must have a length less than"
f" or equal to {MAX_ORACLE_DATA_SERIES}"
)
if "price_data_series" not in errors:
if len(self.price_data_series) == 0:
errors["price_data_series"] = "Field must have a length greater than 0."

if len(self.price_data_series) > MAX_ORACLE_DATA_SERIES:
errors["price_data_series"] = (
"Field must have a length less than"
f" or equal to {MAX_ORACLE_DATA_SERIES}"
)

# either asset_price and scale are both present or both excluded
for price_data in self.price_data_series:
if (price_data.asset_price is not None) != (
price_data.scale is not None
):
errors["price_data_series"] = (
"Field must have both "
"`AssetPrice` and `Scale` if any are present"
)

if self.asset_class is not None and len(self.asset_class) == 0:
errors["asset_class"] = "Field must have a length greater than 0."
Expand Down
Loading