20#include <test/jtx/Oracle.h>
22#include <xrpl/protocol/jss.h>
24#include <boost/lexical_cast/try_lexical_convert.hpp>
25#include <boost/regex.hpp>
35 : env_(env), owner_{}, documentID_{}
43 if (now.time_since_epoch().count() == 0 || arg.
close)
57 jv[jss::TransactionType] = jss::OracleDelete;
62 else if (arg.
fee != 0)
67 jv[jss::Flags] = arg.
flags;
103 assert(account.isNonZero());
112 auto const& leSeries = sle->getFieldArray(sfPriceDataSeries);
113 if (leSeries.size() == 0 || leSeries.size() != series.
size())
115 for (
auto const& data : series)
121 auto const& baseAsset = o.getFieldCurrency(sfBaseAsset);
122 auto const& quoteAsset =
123 o.getFieldCurrency(sfQuoteAsset);
124 auto const& price = o.getFieldU64(sfAssetPrice);
125 auto const& scale = o.getFieldU8(sfScale);
126 return baseAsset.getText() == std::get<0>(data) &&
127 quoteAsset.getText() == std::get<1>(data) &&
128 price == std::get<2>(data) &&
129 scale == std::get<3>(data);
130 }) == leSeries.end())
142 return sle && (*sle)[sfLastUpdateTime] == lastUpdateTime;
158 for (
auto const&
id : *oracles)
162 oracle[jss::account] =
to_string((*
id.first).
id());
164 toJson(oracle[jss::oracle_document_id], *
id.second);
167 jv[jss::oracles] = jvOracles;
170 toJson(jv[jss::trim], *trim);
172 toJson(jv[jss::base_asset], *baseAsset);
174 toJson(jv[jss::quote_asset], *quoteAsset);
176 toJson(jv[jss::time_threshold], *timeThreshold);
180 auto jr = env.
rpc(
"json",
"get_aggregate_price", str);
184 if (jr.isMember(jss::result) && jr[jss::result].isMember(jss::status))
185 return jr[jss::result];
186 else if (jr.isMember(jss::error))
200 std::holds_alternative<std::uint32_t>(*arg.
documentID))
209 jv[jss::TransactionType] = jss::OracleSet;
218 jv[jss::Flags] = arg.
flags;
221 else if (arg.
fee != 0)
237 duration_cast<seconds>(
238 env_.
current()->info().closeTime.time_since_epoch())
246 assert(s.size() <= 20);
251 for (
auto const& data : arg.
series)
255 price[jss::BaseAsset] = assetToStr(std::get<0>(data));
256 price[jss::QuoteAsset] = assetToStr(std::get<1>(data));
257 if (std::get<2>(data))
258 price[jss::AssetPrice] = *std::get<2>(data);
259 if (std::get<3>(data))
260 price[jss::Scale] = *std::get<3>(data);
261 priceData[jss::PriceData] = price;
262 dataSeries.
append(priceData);
264 jv[jss::PriceDataSeries] = dataSeries;
296 if (std::holds_alternative<AccountID>(*account))
297 jvParams[jss::oracle][jss::account] =
298 to_string(std::get<AccountID>(*account));
300 jvParams[jss::oracle][jss::account] =
301 std::get<std::string>(*account);
308 if (boost::conversion::try_lexical_convert(*index, i))
309 jvParams[jss::oracle][jss::ledger_index] = i;
311 jvParams[jss::oracle][jss::ledger_index] = *index;
316 auto jr = env.
rpc(
"json",
"ledger_entry", str);
320 if (jr.isMember(jss::result) && jr[jss::result].isMember(jss::status))
321 return jr[jss::result];
322 else if (jr.isMember(jss::error))
338 [&]<
typename T>(T&& arg) {
339 if constexpr (std::is_same_v<T, std::string const&>)
341 if (arg.starts_with(
"##"))
Value & append(Value const &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.