xrpld
Loading...
Searching...
No Matches
src/test/jtx/Oracle.h
1#pragma once
2
3#include <test/jtx/Account.h>
4#include <test/jtx/Env.h>
5#include <test/jtx/multisign.h>
6#include <test/jtx/seq.h>
7#include <test/jtx/ter.h>
8
9#include <xrpl/basics/chrono.h>
10#include <xrpl/json/json_forwards.h>
11#include <xrpl/json/json_value.h>
12#include <xrpl/protocol/AccountID.h>
13
14#include <chrono>
15#include <cstdint>
16#include <optional>
17#include <ostream>
18#include <string>
19#include <tuple>
20#include <utility>
21#include <variant>
22#include <vector>
23
25
28
29// Special string value, which is converted to unquoted string in the string
30// passed to rpc.
31constexpr char const* kNoneTag = "%None%";
32constexpr char const* kUnquotedNone = "None";
33constexpr char const* kNonePattern = "\"%None%\"";
34
36asUInt(AnyValue const& v);
37
38bool
40
41void
42toJson(json::Value& jv, AnyValue const& v);
43
44void
45toJsonHex(json::Value& jv, AnyValue const& v);
46
47// base asset, quote asset, price, scale
49 std::
50 tuple<std::string, std::string, std::optional<std::uint32_t>, std::optional<std::uint8_t>>>;
51
52// Typical defaults for Create
69
70// Typical defaults for Update
72{
75 DataSeries series = {}; // NOLINT(readability-redundant-member-init)
83 int fee = 10;
84 std::optional<Ter> err = std::nullopt;
85};
86
88{
89 std::optional<AccountID> const& owner = std::nullopt;
90 std::optional<AnyValue> const& documentID = std::nullopt;
92 std::optional<jtx::Msig> const& msig = std::nullopt;
94 int fee = 10;
95 std::optional<Ter> const& err = std::nullopt;
96};
97
98// Simulate testStartTime as 10'000s from XRPL epoch time to make
99// LastUpdateTime validation to work and to make unit-test consistent.
100// The value doesn't matter much, it has to be greater
101// than maxLastUpdateTimeDelta in order to pass LastUpdateTime
102// validation {close-maxLastUpdateTimeDelta,close+maxLastUpdateTimeDelta}.
104
111{
112private:
113 // Global fee if not 0
114 static inline std::uint32_t fee = 0;
118
119private:
120 void
121 submit(
122 json::Value const& jv,
123 std::optional<jtx::Msig> const& msig,
124 std::optional<jtx::Seq> const& seq,
125 std::optional<Ter> const& err);
126
127public:
128 Oracle(Env& env, CreateArg const& arg, bool submit = true);
129
130 void
131 remove(RemoveArg const& arg);
132
133 void
134 set(CreateArg const& arg);
135 void
136 set(UpdateArg const& arg);
137
138 static json::Value
140 Env& env,
141 std::optional<AnyValue> const& baseAsset,
142 std::optional<AnyValue> const& quoteAsset,
143 std::optional<OraclesData> const& oracles = std::nullopt,
144 std::optional<AnyValue> const& trim = std::nullopt,
145 std::optional<AnyValue> const& timeThreshold = std::nullopt);
146
147 [[nodiscard]] std::uint32_t
149 {
150 return documentID_;
151 }
152
153 [[nodiscard]] bool
154 exists() const
155 {
156 return exists(env_, owner_, documentID_);
157 }
158
159 [[nodiscard]] static bool
160 exists(Env& env, AccountID const& account, std::uint32_t documentID);
161
162 [[nodiscard]] bool
163 expectPrice(DataSeries const& prices) const;
164
165 [[nodiscard]] bool
166 expectLastUpdateTime(std::uint32_t lastUpdateTime) const;
167
168 static json::Value
170 Env& env,
173 std::optional<std::string> const& index = std::nullopt);
174
175 [[nodiscard]] json::Value
176 ledgerEntry(std::optional<std::string> const& index = std::nullopt) const
177 {
179 }
180
181 static void
183 {
184 fee = f;
185 }
186
188 operator<<(std::ostream& strm, Oracle const& oracle)
189 {
190 strm << oracle.ledgerEntry().toStyledString();
191 return strm;
192 }
193};
194
195} // namespace xrpl::test::jtx::oracle
Represents a JSON value.
Definition json_value.h:117
A transaction testing environment.
Definition Env.h:161
void set(CreateArg const &arg)
Definition Oracle.cpp:303
friend std::ostream & operator<<(std::ostream &strm, Oracle const &oracle)
bool expectLastUpdateTime(std::uint32_t lastUpdateTime) const
Definition Oracle.cpp:155
Oracle(Env &env, CreateArg const &arg, bool submit=true)
Definition Oracle.cpp:34
bool expectPrice(DataSeries const &prices) const
Definition Oracle.cpp:129
static void setFee(std::uint32_t f)
static json::Value aggregatePrice(Env &env, std::optional< AnyValue > const &baseAsset, std::optional< AnyValue > const &quoteAsset, std::optional< OraclesData > const &oracles=std::nullopt, std::optional< AnyValue > const &trim=std::nullopt, std::optional< AnyValue > const &timeThreshold=std::nullopt)
Definition Oracle.cpp:162
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)
Definition Oracle.cpp:321
void remove(RemoveArg const &arg)
Definition Oracle.cpp:53
void submit(json::Value const &jv, std::optional< jtx::Msig > const &msig, std::optional< jtx::Seq > const &seq, std::optional< Ter > const &err)
Definition Oracle.cpp:77
json::Value ledgerEntry(std::optional< std::string > const &index=std::nullopt) const
std::uint32_t asUInt(AnyValue const &v)
Definition Oracle.cpp:399
constexpr char const * kUnquotedNone
std::vector< std::pair< std::optional< Account >, std::optional< AnyValue > > > OraclesData
std::variant< std::string, double, json::Int, json::UInt > AnyValue
void toJsonHex(json::Value &jv, AnyValue const &v)
Definition Oracle.cpp:375
constexpr char const * kNoneTag
bool validDocumentID(AnyValue const &v)
Definition Oracle.cpp:407
void toJson(json::Value &jv, AnyValue const &v)
Definition Oracle.cpp:369
constexpr char const * kNonePattern
std::vector< std:: tuple< std::string, std::string, std::optional< std::uint32_t >, std::optional< std::uint8_t > > > DataSeries
static constexpr std::chrono::seconds kTestStartTime
static constexpr std::chrono::seconds kEpochOffset
Clock for measuring the network time.
Definition chrono.h:35
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
std::optional< AnyValue > documentID
std::optional< AnyValue > lastUpdateTime
std::optional< AnyValue > assetClass
std::optional< Ter > const & err
std::optional< AnyValue > const & documentID
std::optional< jtx::Msig > const & msig
std::optional< AccountID > const & owner
std::optional< AnyValue > documentID
std::optional< AnyValue > lastUpdateTime
std::optional< AnyValue > assetClass