From 437ea7bf98baa2f8242e681606fca85eaae383b8 Mon Sep 17 00:00:00 2001 From: Peter Chen <34582813+PeterChen13579@users.noreply.github.com> Date: Wed, 12 Jun 2024 11:16:11 -0400 Subject: [PATCH] Fix quoteAsset value in getAggregatePrice (#1449) Fixes #1373 --- src/rpc/handlers/GetAggregatePrice.hpp | 13 +++++--- .../rpc/handlers/GetAggregatePriceTests.cpp | 32 +++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/rpc/handlers/GetAggregatePrice.hpp b/src/rpc/handlers/GetAggregatePrice.hpp index 0fc20dba..81914033 100644 --- a/src/rpc/handlers/GetAggregatePrice.hpp +++ b/src/rpc/handlers/GetAggregatePrice.hpp @@ -22,6 +22,7 @@ #include "data/BackendInterface.hpp" #include "rpc/Errors.hpp" #include "rpc/JS.hpp" +#include "rpc/common/MetaProcessors.hpp" #include "rpc/common/Modifiers.hpp" #include "rpc/common/Specs.hpp" #include "rpc/common/Types.hpp" @@ -32,7 +33,6 @@ #include #include #include -#include #include #include #include @@ -45,6 +45,7 @@ #include #include #include + namespace rpc { /** @@ -162,11 +163,13 @@ public: static auto const rpcSpec = RpcSpec{ {JS(ledger_hash), validation::Uint256HexStringValidator}, {JS(ledger_index), validation::LedgerIndexValidator}, - // note: Rippled's base_asset and quote_asset can be non-string. It will eventually return - // "rpcOBJECT_NOT_FOUND". Clio will return "rpcINVALID_PARAMS" if the base_asset or quote_asset is not a - // string. User can clearly know there is a mistake in the input. + // validate quoteAsset in accordance to the currency code found in XRPL doc: + // https://xrpl.org/docs/references/protocol/data-types/currency-formats#currency-codes + // usually Clio returns rpcMALFORMED_CURRENCY , return InvalidParam here just to mimic rippled {JS(base_asset), validation::Required{}, validation::Type{}}, - {JS(quote_asset), validation::Required{}, validation::Type{}}, + {JS(quote_asset), + validation::Required{}, + meta::WithCustomError{validation::CurrencyValidator, Status(RippledError::rpcINVALID_PARAMS)}}, {JS(oracles), validation::Required{}, oraclesValidator}, // note: Unlike `rippled`, Clio only supports UInt as input, no string, no `null`, etc. {JS(time_threshold), validation::Type{}}, diff --git a/tests/unit/rpc/handlers/GetAggregatePriceTests.cpp b/tests/unit/rpc/handlers/GetAggregatePriceTests.cpp index 1e8baafc..078bb244 100644 --- a/tests/unit/rpc/handlers/GetAggregatePriceTests.cpp +++ b/tests/unit/rpc/handlers/GetAggregatePriceTests.cpp @@ -159,6 +159,38 @@ generateTestValuesForParametersTest() "invalidParams", "Required field 'quote_asset' missing" }, + GetAggregatePriceParamTestCaseBundle{ + "invalid_quote_asset", + R"({ + "quote_asset" : "asdf", + "base_asset": "USD", + "oracles": + [ + { + "account": "rGh1VZCRBJY6rJiaFpD4LZtyHiuCkC8aeD", + "oracle_document_id": 2 + } + ] + })", + "invalidParams", + "Invalid parameters." + }, + GetAggregatePriceParamTestCaseBundle{ + "invalid_quote_asset2", + R"({ + "quote_asset" : "+aa", + "base_asset": "USD", + "oracles": + [ + { + "account": "rGh1VZCRBJY6rJiaFpD4LZtyHiuCkC8aeD", + "oracle_document_id": 2 + } + ] + })", + "invalidParams", + "Invalid parameters." + }, GetAggregatePriceParamTestCaseBundle{ "oraclesIsEmpty", R"({