Skip to content

Commit

Permalink
feat: add input check for OracleSet params AssetPrice and Scale (#711)
Browse files Browse the repository at this point in the history
* add input check for AssetPrice and Scale being either both present or excluded plus additional checks for PriceDataSeries

* removed type validation
  • Loading branch information
anissa-ripple authored May 30, 2024
1 parent 407dc26 commit 9c09eef
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
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

0 comments on commit 9c09eef

Please sign in to comment.