20#include <test/jtx/Oracle.h>
21#include <xrpl/protocol/jss.h>
23#include <boost/lexical_cast/try_lexical_convert.hpp>
24#include <boost/regex.hpp>
34 : env_(env), owner_{}, documentID_{}
42 if (now.time_since_epoch().count() == 0 || arg.
close)
56 jv[jss::TransactionType] = jss::OracleDelete;
61 else if (arg.
fee != 0)
66 jv[jss::Flags] = arg.
flags;
102 assert(account.isNonZero());
111 auto const& leSeries = sle->getFieldArray(sfPriceDataSeries);
112 if (leSeries.size() == 0 || leSeries.size() != series.
size())
114 for (
auto const& data : series)
120 auto const& baseAsset = o.getFieldCurrency(sfBaseAsset);
121 auto const& quoteAsset =
122 o.getFieldCurrency(sfQuoteAsset);
123 auto const& price = o.getFieldU64(sfAssetPrice);
124 auto const& scale = o.getFieldU8(sfScale);
125 return baseAsset.getText() == std::get<0>(data) &&
126 quoteAsset.getText() == std::get<1>(data) &&
127 price == std::get<2>(data) &&
128 scale == std::get<3>(data);
129 }) == leSeries.end())
141 return sle && (*sle)[sfLastUpdateTime] == lastUpdateTime;
157 for (
auto const&
id : *oracles)
161 oracle[jss::account] =
to_string((*
id.first).
id());
163 toJson(oracle[jss::oracle_document_id], *
id.second);
166 jv[jss::oracles] = jvOracles;
169 toJson(jv[jss::trim], *trim);
171 toJson(jv[jss::base_asset], *baseAsset);
173 toJson(jv[jss::quote_asset], *quoteAsset);
175 toJson(jv[jss::time_threshold], *timeThreshold);
179 auto jr = env.
rpc(
"json",
"get_aggregate_price", str);
183 if (jr.isMember(jss::result) && jr[jss::result].isMember(jss::status))
184 return jr[jss::result];
185 else if (jr.isMember(jss::error))
199 std::holds_alternative<std::uint32_t>(*arg.
documentID))
208 jv[jss::TransactionType] = jss::OracleSet;
217 jv[jss::Flags] = arg.
flags;
220 else if (arg.
fee != 0)
236 duration_cast<seconds>(
237 env_.
current()->info().closeTime.time_since_epoch())
245 assert(s.size() <= 20);
250 for (
auto const& data : arg.
series)
254 price[jss::BaseAsset] = assetToStr(std::get<0>(data));
255 price[jss::QuoteAsset] = assetToStr(std::get<1>(data));
256 if (std::get<2>(data))
257 price[jss::AssetPrice] = *std::get<2>(data);
258 if (std::get<3>(data))
259 price[jss::Scale] = *std::get<3>(data);
260 priceData[jss::PriceData] = price;
261 dataSeries.
append(priceData);
263 jv[jss::PriceDataSeries] = dataSeries;
295 if (std::holds_alternative<AccountID>(*account))
296 jvParams[jss::oracle][jss::account] =
297 to_string(std::get<AccountID>(*account));
299 jvParams[jss::oracle][jss::account] =
300 std::get<std::string>(*account);
307 if (boost::conversion::try_lexical_convert(*index, i))
308 jvParams[jss::oracle][jss::ledger_index] = i;
310 jvParams[jss::oracle][jss::ledger_index] = *index;
315 auto jr = env.
rpc(
"json",
"ledger_entry", str);
319 if (jr.isMember(jss::result) && jr[jss::result].isMember(jss::status))
320 return jr[jss::result];
321 else if (jr.isMember(jss::error))
337 [&]<
typename T>(T&& arg) {
338 if constexpr (std::is_same_v<T, std::string const&>)
340 if (arg.starts_with(
"##"))
Value & append(const Value &value)
Append value to array at the end.
time_point now() const override
Returns the current time.
A transaction testing environment.
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
ManualTimeKeeper & timeKeeper()
Json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
std::shared_ptr< SLE const > le(Account const &account) const
Return an account root.
Set a multisignature on a JTx.
static Json::Value aggregatePrice(Env &env, std::optional< AnyValue > const &baseAsset, std::optional< AnyValue > const "eAsset, std::optional< OraclesData > const &oracles=std::nullopt, std::optional< AnyValue > const &trim=std::nullopt, std::optional< AnyValue > const &timeTreshold=std::nullopt)
static Json::Value ledgerEntry(Env &env, std::optional< std::variant< AccountID, std::string > > const &account, std::optional< AnyValue > const &documentID, std::optional< std::string > const &index=std::nullopt)
void set(CreateArg const &arg)
std::uint32_t documentID_
Oracle(Env &env, CreateArg const &arg, bool submit=true)
void remove(RemoveArg const &arg)
bool expectPrice(DataSeries const &pricess) const
bool expectLastUpdateTime(std::uint32_t lastUpdateTime) const
std::uint32_t documentID() const
void submit(Json::Value const &jv, std::optional< jtx::msig > const &msig, std::optional< jtx::seq > const &seq, std::optional< ter > const &err)
@ arrayValue
array value (ordered list)
Keylet oracle(AccountID const &account, std::uint32_t const &documentID) noexcept
static constexpr std::chrono::seconds testStartTime
constexpr char const * UnquotedNone
std::uint32_t asUInt(AnyValue const &v)
void toJson(Json::Value &jv, AnyValue const &v)
constexpr char const * NonePattern
bool validDocumentID(AnyValue const &v)
void toJsonHex(Json::Value &jv, AnyValue const &v)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::string strHex(FwdIt begin, FwdIt end)
static constexpr std::chrono::seconds epoch_offset
Clock for measuring the network time.
std::string to_string(base_uint< Bits, Tag > const &a)
std::optional< AnyValue > uri
std::optional< AnyValue > documentID
std::optional< AnyValue > lastUpdateTime
std::optional< AnyValue > assetClass
std::optional< AccountID > owner
std::optional< jtx::seq > seq
std::optional< jtx::msig > msig
std::optional< AnyValue > provider
std::optional< AccountID > const & owner
std::optional< AnyValue > const & documentID
std::optional< ter > const & err
std::optional< jtx::msig > const & msig
std::optional< jtx::seq > seq
std::optional< AnyValue > uri
std::optional< AnyValue > assetClass
std::optional< AnyValue > documentID
std::optional< AnyValue > lastUpdateTime
std::optional< AccountID > owner
std::optional< AnyValue > provider
std::optional< jtx::msig > msig
std::optional< jtx::seq > seq
Set the sequence number on a JTx.