xrpld
Loading...
Searching...
No Matches
src/test/jtx/Oracle.h
1#pragma once
2
3#include <test/jtx.h>
4
5#include <date/date.h>
6
8
11
12// Special string value, which is converted to unquoted string in the string
13// passed to rpc.
14constexpr char const* kNoneTag = "%None%";
15constexpr char const* kUnquotedNone = "None";
16constexpr char const* kNonePattern = "\"%None%\"";
17
19asUInt(AnyValue const& v);
20
21bool
23
24void
25toJson(json::Value& jv, AnyValue const& v);
26
27void
28toJsonHex(json::Value& jv, AnyValue const& v);
29
30// base asset, quote asset, price, scale
32 std::
33 tuple<std::string, std::string, std::optional<std::uint32_t>, std::optional<std::uint8_t>>>;
34
35// Typical defaults for Create
52
53// Typical defaults for Update
55{
58 DataSeries series = {}; // NOLINT(readability-redundant-member-init)
66 int fee = 10;
67 std::optional<Ter> err = std::nullopt;
68};
69
71{
72 std::optional<AccountID> const& owner = std::nullopt;
73 std::optional<AnyValue> const& documentID = std::nullopt;
75 std::optional<jtx::Msig> const& msig = std::nullopt;
77 int fee = 10;
78 std::optional<Ter> const& err = std::nullopt;
79};
80
81// Simulate testStartTime as 10'000s from XRPL epoch time to make
82// LastUpdateTime validation to work and to make unit-test consistent.
83// The value doesn't matter much, it has to be greater
84// than maxLastUpdateTimeDelta in order to pass LastUpdateTime
85// validation {close-maxLastUpdateTimeDelta,close+maxLastUpdateTimeDelta}.
87
92class Oracle
93{
94private:
95 // Global fee if not 0
96 static inline std::uint32_t fee = 0;
100
101private:
102 void
103 submit(
104 json::Value const& jv,
105 std::optional<jtx::Msig> const& msig,
106 std::optional<jtx::Seq> const& seq,
107 std::optional<Ter> const& err);
108
109public:
110 Oracle(Env& env, CreateArg const& arg, bool submit = true);
111
112 void
113 remove(RemoveArg const& arg);
114
115 void
116 set(CreateArg const& arg);
117 void
118 set(UpdateArg const& arg);
119
120 static json::Value
122 Env& env,
123 std::optional<AnyValue> const& baseAsset,
124 std::optional<AnyValue> const& quoteAsset,
125 std::optional<OraclesData> const& oracles = std::nullopt,
126 std::optional<AnyValue> const& trim = std::nullopt,
127 std::optional<AnyValue> const& timeThreshold = std::nullopt);
128
129 [[nodiscard]] std::uint32_t
131 {
132 return documentID_;
133 }
134
135 [[nodiscard]] bool
136 exists() const
137 {
138 return exists(env_, owner_, documentID_);
139 }
140
141 [[nodiscard]] static bool
142 exists(Env& env, AccountID const& account, std::uint32_t documentID);
143
144 [[nodiscard]] bool
145 expectPrice(DataSeries const& prices) const;
146
147 [[nodiscard]] bool
148 expectLastUpdateTime(std::uint32_t lastUpdateTime) const;
149
150 static json::Value
152 Env& env,
155 std::optional<std::string> const& index = std::nullopt);
156
157 [[nodiscard]] json::Value
158 ledgerEntry(std::optional<std::string> const& index = std::nullopt) const
159 {
161 }
162
163 static void
165 {
166 fee = f;
167 }
168
170 operator<<(std::ostream& strm, Oracle const& oracle)
171 {
172 strm << oracle.ledgerEntry().toStyledString();
173 return strm;
174 }
175};
176
177} // namespace xrpl::test::jtx::oracle
Represents a JSON value.
Definition json_value.h:130
A transaction testing environment.
Definition Env.h:143
void set(CreateArg const &arg)
Definition Oracle.cpp:302
friend std::ostream & operator<<(std::ostream &strm, Oracle const &oracle)
bool expectLastUpdateTime(std::uint32_t lastUpdateTime) const
Definition Oracle.cpp:154
Oracle(Env &env, CreateArg const &arg, bool submit=true)
Definition Oracle.cpp:33
bool expectPrice(DataSeries const &prices) const
Definition Oracle.cpp:128
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:161
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:320
void remove(RemoveArg const &arg)
Definition Oracle.cpp:52
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:76
json::Value ledgerEntry(std::optional< std::string > const &index=std::nullopt) const
std::uint32_t asUInt(AnyValue const &v)
Definition Oracle.cpp:398
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:374
constexpr char const * kNoneTag
bool validDocumentID(AnyValue const &v)
Definition Oracle.cpp:406
void toJson(json::Value &jv, AnyValue const &v)
Definition Oracle.cpp:368
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:33
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
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