diff --git a/src/rpc/handlers/AMMInfo.cpp b/src/rpc/handlers/AMMInfo.cpp index f2e65135..bc5ed56b 100644 --- a/src/rpc/handlers/AMMInfo.cpp +++ b/src/rpc/handlers/AMMInfo.cpp @@ -316,13 +316,8 @@ tag_invoke(boost::json::value_to_tag, boost::json::value if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jv.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jv.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jv.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jsonObject.at(JS(ledger_index))); if (jsonObject.contains(JS(asset))) input.issue1 = parseIssue(jsonObject.at(JS(asset)).as_object()); @@ -332,6 +327,7 @@ tag_invoke(boost::json::value_to_tag, boost::json::value if (jsonObject.contains(JS(account))) input.accountID = accountFromStringStrict(boost::json::value_to(jsonObject.at(JS(account)))); + if (jsonObject.contains(JS(amm_account))) input.ammAccount = accountFromStringStrict(boost::json::value_to(jsonObject.at(JS(amm_account)))); diff --git a/src/rpc/handlers/AccountChannels.cpp b/src/rpc/handlers/AccountChannels.cpp index 96891eff..49f9fbe4 100644 --- a/src/rpc/handlers/AccountChannels.cpp +++ b/src/rpc/handlers/AccountChannels.cpp @@ -154,13 +154,8 @@ tag_invoke(boost::json::value_to_tag, boost::json if (jsonObject.contains(JS(destination_account))) input.destinationAccount = boost::json::value_to(jv.at(JS(destination_account))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jv.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jv.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jv.at(JS(ledger_index))); return input; } diff --git a/src/rpc/handlers/AccountCurrencies.cpp b/src/rpc/handlers/AccountCurrencies.cpp index 7700b244..78a2ce20 100644 --- a/src/rpc/handlers/AccountCurrencies.cpp +++ b/src/rpc/handlers/AccountCurrencies.cpp @@ -128,13 +128,8 @@ tag_invoke(boost::json::value_to_tag, boost::js if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jv.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jv.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jv.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jv.at(JS(ledger_index))); return input; } diff --git a/src/rpc/handlers/AccountInfo.cpp b/src/rpc/handlers/AccountInfo.cpp index 6a0a0e97..c07f439a 100644 --- a/src/rpc/handlers/AccountInfo.cpp +++ b/src/rpc/handlers/AccountInfo.cpp @@ -205,13 +205,8 @@ tag_invoke(boost::json::value_to_tag, boost::json::va if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jsonObject.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jsonObject.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jsonObject.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jsonObject.at(JS(ledger_index))); if (jsonObject.contains(JS(signer_lists))) input.signerLists = boost::json::value_to(jsonObject.at(JS(signer_lists))); diff --git a/src/rpc/handlers/AccountLines.cpp b/src/rpc/handlers/AccountLines.cpp index 69c18a4c..8b3772a9 100644 --- a/src/rpc/handlers/AccountLines.cpp +++ b/src/rpc/handlers/AccountLines.cpp @@ -215,13 +215,8 @@ tag_invoke(boost::json::value_to_tag, boost::json::v if (jsonObject.contains(JS(ignore_default))) input.ignoreDefault = jv.at(JS(ignore_default)).as_bool(); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jv.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jv.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jv.at(JS(ledger_index))); return input; } diff --git a/src/rpc/handlers/AccountMPTokenIssuances.cpp b/src/rpc/handlers/AccountMPTokenIssuances.cpp index fce656ad..85c20586 100644 --- a/src/rpc/handlers/AccountMPTokenIssuances.cpp +++ b/src/rpc/handlers/AccountMPTokenIssuances.cpp @@ -164,13 +164,8 @@ tag_invoke(boost::json::value_to_tag, boo if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jv.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jv.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jv.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jv.at(JS(ledger_index))); return input; } diff --git a/src/rpc/handlers/AccountMPTokens.cpp b/src/rpc/handlers/AccountMPTokens.cpp index 762ae0a7..3663eebc 100644 --- a/src/rpc/handlers/AccountMPTokens.cpp +++ b/src/rpc/handlers/AccountMPTokens.cpp @@ -132,17 +132,15 @@ tag_invoke(boost::json::value_to_tag, boost::json if (jsonObject.contains(JS(limit))) input.limit = util::integralValueAs(jv.at(JS(limit))); + if (jsonObject.contains(JS(marker))) input.marker = boost::json::value_to(jv.at(JS(marker))); + if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jv.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jv.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jv.at(JS(ledger_index))); - } else if (boost::json::value_to(jv.at(JS(ledger_index))) != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jv.at(JS(ledger_index)))); - } - } + + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jv.at(JS(ledger_index))); return input; } diff --git a/src/rpc/handlers/AccountNFTs.cpp b/src/rpc/handlers/AccountNFTs.cpp index e910140f..7d2dde8d 100644 --- a/src/rpc/handlers/AccountNFTs.cpp +++ b/src/rpc/handlers/AccountNFTs.cpp @@ -157,13 +157,8 @@ tag_invoke(boost::json::value_to_tag, boost::json::va if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jsonObject.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jsonObject.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jsonObject.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jsonObject.at(JS(ledger_index))); if (jsonObject.contains(JS(limit))) input.limit = util::integralValueAs(jsonObject.at(JS(limit))); diff --git a/src/rpc/handlers/AccountObjects.cpp b/src/rpc/handlers/AccountObjects.cpp index b47c2beb..74d59b44 100644 --- a/src/rpc/handlers/AccountObjects.cpp +++ b/src/rpc/handlers/AccountObjects.cpp @@ -153,13 +153,8 @@ tag_invoke(boost::json::value_to_tag, boost::json: if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jv.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jv.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jv.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jv.at(JS(ledger_index))); if (jsonObject.contains(JS(type))) { input.type = diff --git a/src/rpc/handlers/AccountOffers.cpp b/src/rpc/handlers/AccountOffers.cpp index fb2975e8..7ded2b9f 100644 --- a/src/rpc/handlers/AccountOffers.cpp +++ b/src/rpc/handlers/AccountOffers.cpp @@ -166,16 +166,11 @@ tag_invoke(boost::json::value_to_tag, boost::json:: input.account = boost::json::value_to(jsonObject.at(JS(account))); - if (jsonObject.contains(JS(ledger_hash))) { + if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jsonObject.at(JS(ledger_hash))); - } - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jsonObject.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jsonObject.at(JS(ledger_index)))); - } - } + + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jsonObject.at(JS(ledger_index))); if (jsonObject.contains(JS(limit))) input.limit = util::integralValueAs(jsonObject.at(JS(limit))); diff --git a/src/rpc/handlers/AccountTx.cpp b/src/rpc/handlers/AccountTx.cpp index 2af44774..043ea4fa 100644 --- a/src/rpc/handlers/AccountTx.cpp +++ b/src/rpc/handlers/AccountTx.cpp @@ -258,11 +258,8 @@ tag_invoke(boost::json::value_to_tag, boost::json::valu input.ledgerHash = boost::json::value_to(jsonObject.at(JS(ledger_hash))); if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jsonObject.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jsonObject.at(JS(ledger_index)))); - } else { + input.ledgerIndex = util::getLedgerIndex(jsonObject.at(JS(ledger_index))); + if (not input.ledgerIndex.has_value()) { // could not get the latest validated ledger seq here, using this flag to indicate that input.usingValidatedLedger = true; } diff --git a/src/rpc/handlers/BookChanges.cpp b/src/rpc/handlers/BookChanges.cpp index fd2ffb0a..20c07641 100644 --- a/src/rpc/handlers/BookChanges.cpp +++ b/src/rpc/handlers/BookChanges.cpp @@ -91,13 +91,8 @@ tag_invoke(boost::json::value_to_tag, boost::json::va if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jv.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jv.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jv.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jv.at(JS(ledger_index))); return input; } diff --git a/src/rpc/handlers/BookOffers.cpp b/src/rpc/handlers/BookOffers.cpp index ff5bf431..bca916f9 100644 --- a/src/rpc/handlers/BookOffers.cpp +++ b/src/rpc/handlers/BookOffers.cpp @@ -122,13 +122,8 @@ tag_invoke(boost::json::value_to_tag, boost::json::val if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jv.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jv.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jv.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jv.at(JS(ledger_index))); if (jsonObject.contains(JS(taker))) input.taker = accountFromStringStrict(boost::json::value_to(jv.at(JS(taker)))); diff --git a/src/rpc/handlers/DepositAuthorized.cpp b/src/rpc/handlers/DepositAuthorized.cpp index a18e0922..f89fc1e8 100644 --- a/src/rpc/handlers/DepositAuthorized.cpp +++ b/src/rpc/handlers/DepositAuthorized.cpp @@ -146,17 +146,11 @@ tag_invoke(boost::json::value_to_tag, boost::js if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jv.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jv.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jv.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jv.at(JS(ledger_index))); - if (jsonObject.contains(JS(credentials))) { + if (jsonObject.contains(JS(credentials))) input.credentials = boost::json::value_to(jv.at(JS(credentials))); - } return input; } diff --git a/src/rpc/handlers/Feature.cpp b/src/rpc/handlers/Feature.cpp index ddab0f7d..4216ba38 100644 --- a/src/rpc/handlers/Feature.cpp +++ b/src/rpc/handlers/Feature.cpp @@ -168,13 +168,9 @@ tag_invoke(boost::json::value_to_tag, boost::json::value if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jv.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jv.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jv.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jv.at(JS(ledger_index))); + return input; } diff --git a/src/rpc/handlers/GatewayBalances.cpp b/src/rpc/handlers/GatewayBalances.cpp index 0e94f7df..6bcec93e 100644 --- a/src/rpc/handlers/GatewayBalances.cpp +++ b/src/rpc/handlers/GatewayBalances.cpp @@ -249,13 +249,8 @@ tag_invoke(boost::json::value_to_tag, boost::json if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jv.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jv.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jv.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jv.at(JS(ledger_index))); if (jsonObject.contains(JS(hotwallet))) { if (jsonObject.at(JS(hotwallet)).is_string()) { diff --git a/src/rpc/handlers/GetAggregatePrice.cpp b/src/rpc/handlers/GetAggregatePrice.cpp index 6d513dbc..ade1b9da 100644 --- a/src/rpc/handlers/GetAggregatePrice.cpp +++ b/src/rpc/handlers/GetAggregatePrice.cpp @@ -263,13 +263,8 @@ tag_invoke(boost::json::value_to_tag, boost::js if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jv.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jv.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jv.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jv.at(JS(ledger_index))); for (auto const& oracle : jsonObject.at(JS(oracles)).as_array()) { input.oracles.push_back( diff --git a/src/rpc/handlers/Ledger.cpp b/src/rpc/handlers/Ledger.cpp index 30dbbfce..42c9b122 100644 --- a/src/rpc/handlers/Ledger.cpp +++ b/src/rpc/handlers/Ledger.cpp @@ -209,13 +209,8 @@ tag_invoke(boost::json::value_to_tag, boost::json::value c if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jv.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jv.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jv.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jv.at(JS(ledger_index))); if (jsonObject.contains(JS(transactions))) input.transactions = jv.at(JS(transactions)).as_bool(); diff --git a/src/rpc/handlers/LedgerData.cpp b/src/rpc/handlers/LedgerData.cpp index 6fd6d033..4f9ad5dd 100644 --- a/src/rpc/handlers/LedgerData.cpp +++ b/src/rpc/handlers/LedgerData.cpp @@ -210,13 +210,8 @@ tag_invoke(boost::json::value_to_tag, boost::json::val if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jsonObject.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jsonObject.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jsonObject.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jsonObject.at(JS(ledger_index))); if (jsonObject.contains(JS(type))) input.type = util::LedgerTypes::getLedgerEntryTypeFromStr(boost::json::value_to(jv.at(JS(type)))); diff --git a/src/rpc/handlers/LedgerEntry.cpp b/src/rpc/handlers/LedgerEntry.cpp index 2caf85ff..4cd7fb72 100644 --- a/src/rpc/handlers/LedgerEntry.cpp +++ b/src/rpc/handlers/LedgerEntry.cpp @@ -303,13 +303,8 @@ tag_invoke(boost::json::value_to_tag, boost::json::va if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jv.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jv.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jv.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jv.at(JS(ledger_index))); if (jsonObject.contains(JS(binary))) input.binary = jv.at(JS(binary)).as_bool(); diff --git a/src/rpc/handlers/MPTHolders.cpp b/src/rpc/handlers/MPTHolders.cpp index 32c77fea..97254025 100644 --- a/src/rpc/handlers/MPTHolders.cpp +++ b/src/rpc/handlers/MPTHolders.cpp @@ -124,13 +124,8 @@ tag_invoke(boost::json::value_to_tag, boost::json::val if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = jsonObject.at(JS(ledger_hash)).as_string().c_str(); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jsonObject.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(jsonObject.at(JS(ledger_index)).as_string().c_str()); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jsonObject.at(JS(ledger_index))); if (jsonObject.contains(JS(limit))) input.limit = util::integralValueAs(jsonObject.at(JS(limit))); diff --git a/src/rpc/handlers/NFTHistory.cpp b/src/rpc/handlers/NFTHistory.cpp index 77368873..4b941dac 100644 --- a/src/rpc/handlers/NFTHistory.cpp +++ b/src/rpc/handlers/NFTHistory.cpp @@ -215,13 +215,8 @@ tag_invoke(boost::json::value_to_tag, boost::json::val if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jsonObject.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jsonObject.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jsonObject.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jsonObject.at(JS(ledger_index))); if (jsonObject.contains(JS(binary))) input.binary = jsonObject.at(JS(binary)).as_bool(); diff --git a/src/rpc/handlers/NFTInfo.cpp b/src/rpc/handlers/NFTInfo.cpp index a0ae5ac7..946f5041 100644 --- a/src/rpc/handlers/NFTInfo.cpp +++ b/src/rpc/handlers/NFTInfo.cpp @@ -116,13 +116,8 @@ tag_invoke(boost::json::value_to_tag, boost::json::value if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jsonObject.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jsonObject.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jsonObject.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jsonObject.at(JS(ledger_index))); return input; } diff --git a/src/rpc/handlers/NFTOffersCommon.cpp b/src/rpc/handlers/NFTOffersCommon.cpp index b89acdef..76cb01e6 100644 --- a/src/rpc/handlers/NFTOffersCommon.cpp +++ b/src/rpc/handlers/NFTOffersCommon.cpp @@ -194,13 +194,8 @@ tag_invoke(boost::json::value_to_tag, boost::json:: if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jsonObject.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jsonObject.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jsonObject.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jsonObject.at(JS(ledger_index))); if (jsonObject.contains(JS(marker))) input.marker = boost::json::value_to(jsonObject.at(JS(marker))); diff --git a/src/rpc/handlers/NFTsByIssuer.cpp b/src/rpc/handlers/NFTsByIssuer.cpp index e727ef91..eb17f63e 100644 --- a/src/rpc/handlers/NFTsByIssuer.cpp +++ b/src/rpc/handlers/NFTsByIssuer.cpp @@ -136,13 +136,8 @@ tag_invoke(boost::json::value_to_tag, boost::json::v if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jsonObject.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jsonObject.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jsonObject.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jsonObject.at(JS(ledger_index))); if (jsonObject.contains(JS(limit))) input.limit = util::integralValueAs(jsonObject.at(JS(limit))); diff --git a/src/rpc/handlers/NoRippleCheck.cpp b/src/rpc/handlers/NoRippleCheck.cpp index e44a62be..1c07e88d 100644 --- a/src/rpc/handlers/NoRippleCheck.cpp +++ b/src/rpc/handlers/NoRippleCheck.cpp @@ -196,13 +196,8 @@ tag_invoke(boost::json::value_to_tag, boost::json:: if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jsonObject.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jsonObject.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jsonObject.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jsonObject.at(JS(ledger_index))); return input; } diff --git a/src/rpc/handlers/TransactionEntry.cpp b/src/rpc/handlers/TransactionEntry.cpp index 1d0c6b9b..e3cb6174 100644 --- a/src/rpc/handlers/TransactionEntry.cpp +++ b/src/rpc/handlers/TransactionEntry.cpp @@ -110,13 +110,8 @@ tag_invoke(boost::json::value_to_tag, boost::jso if (jsonObject.contains(JS(ledger_hash))) input.ledgerHash = boost::json::value_to(jv.at(JS(ledger_hash))); - if (jsonObject.contains(JS(ledger_index))) { - if (!jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jv.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(boost::json::value_to(jv.at(JS(ledger_index)))); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jv.at(JS(ledger_index))); return input; } diff --git a/src/rpc/handlers/VaultInfo.cpp b/src/rpc/handlers/VaultInfo.cpp index 24d815ad..277c178d 100644 --- a/src/rpc/handlers/VaultInfo.cpp +++ b/src/rpc/handlers/VaultInfo.cpp @@ -177,13 +177,8 @@ tag_invoke(boost::json::value_to_tag, boost::json::valu if (jsonObject.contains(JS(vault_id))) input.vaultID = jsonObject.at(JS(vault_id)).as_string(); - if (jsonObject.contains(JS(ledger_index))) { - if (not jsonObject.at(JS(ledger_index)).is_string()) { - input.ledgerIndex = util::integralValueAs(jsonObject.at(JS(ledger_index))); - } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { - input.ledgerIndex = std::stoi(jsonObject.at(JS(ledger_index)).as_string().c_str()); - } - } + if (jsonObject.contains(JS(ledger_index))) + input.ledgerIndex = util::getLedgerIndex(jsonObject.at(JS(ledger_index))); return input; } diff --git a/src/util/JsonUtils.hpp b/src/util/JsonUtils.hpp index 87f995e9..9170b677 100644 --- a/src/util/JsonUtils.hpp +++ b/src/util/JsonUtils.hpp @@ -19,6 +19,8 @@ #pragma once +#include "rpc/JS.hpp" + #include #include @@ -110,4 +112,26 @@ integralValueAs(boost::json::value const& value) throw std::logic_error("Value neither uint64 nor int64"); } +/** + * @brief Extracts ledger index from a JSON value which can be either a number or a string. + * + * @param value The JSON value to extract ledger index from + * @return An optional containing the ledger index if it is a number; std::nullopt otherwise + * @throws logic_error comes from integralValueAs if the underlying number is neither int64 nor uint64 + * @throws std::invalid_argument or std::out_of_range if the string cannot be converted to a number + */ +[[nodiscard]] inline std::optional +getLedgerIndex(boost::json::value const& value) +{ + std::optional ledgerIndex; + + if (not value.is_string()) { + ledgerIndex = util::integralValueAs(value); + } else if (value.as_string() != "validated") { + ledgerIndex = std::stoi(value.as_string().c_str()); + } + + return ledgerIndex; +} + } // namespace util diff --git a/tests/unit/JsonUtilTests.cpp b/tests/unit/JsonUtilTests.cpp index 4556c3f7..a1166283 100644 --- a/tests/unit/JsonUtilTests.cpp +++ b/tests/unit/JsonUtilTests.cpp @@ -88,3 +88,29 @@ TEST(JsonUtils, integralValueAs) auto const stringJson = boost::json::value("not a number"); EXPECT_THROW(util::integralValueAs(stringJson), std::logic_error); } + +TEST(JsonUtils, getLedgerIndex) +{ + auto const emptyJson = boost::json::value(); + EXPECT_THROW(std::ignore = util::getLedgerIndex(emptyJson), std::logic_error); + + auto const boolJson = boost::json::value(true); + EXPECT_THROW(std::ignore = util::getLedgerIndex(emptyJson), std::logic_error); + + auto const numberJson = boost::json::value(12345); + auto ledgerIndex = util::getLedgerIndex(numberJson); + EXPECT_TRUE(ledgerIndex.has_value()); + EXPECT_EQ(ledgerIndex.value(), 12345u); + + auto const validStringJson = boost::json::value("12345"); + ledgerIndex = util::getLedgerIndex(validStringJson); + EXPECT_TRUE(ledgerIndex.has_value()); + EXPECT_EQ(ledgerIndex.value(), 12345u); + + auto const invalidStringJson = boost::json::value("invalid123"); + EXPECT_THROW(std::ignore = util::getLedgerIndex(invalidStringJson), std::invalid_argument); + + auto const validatedJson = boost::json::value("validated"); + ledgerIndex = util::getLedgerIndex(validatedJson); + EXPECT_FALSE(ledgerIndex.has_value()); +}