rippled
Loading...
Searching...
No Matches
Oracle.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_TEST_JTX_ORACLE_H_INCLUDED
21#define RIPPLE_TEST_JTX_ORACLE_H_INCLUDED
22
23#include <test/jtx.h>
24
25#include <date/date.h>
26
27namespace ripple {
28namespace test {
29namespace jtx {
30namespace oracle {
31
35
36// Special string value, which is converted to unquoted string in the string
37// passed to rpc.
38constexpr char const* NoneTag = "%None%";
39constexpr char const* UnquotedNone = "None";
40constexpr char const* NonePattern = "\"%None%\"";
41
43asUInt(AnyValue const& v);
44
45bool
47
48void
49toJson(Json::Value& jv, AnyValue const& v);
50
51void
52toJsonHex(Json::Value& jv, AnyValue const& v);
53
54// base asset, quote asset, price, scale
60
61// Typical defaults for Create
63{
66 DataSeries series = {{"XRP", "USD", 740, 1}};
74 int fee = 10;
75 std::optional<ter> err = std::nullopt;
76 bool close = false;
77};
78
79// Typical defaults for Update
81{
92 int fee = 10;
93 std::optional<ter> err = std::nullopt;
94};
95
97{
98 std::optional<AccountID> const& owner = std::nullopt;
99 std::optional<AnyValue> const& documentID = std::nullopt;
101 std::optional<jtx::msig> const& msig = std::nullopt;
103 int fee = 10;
104 std::optional<ter> const& err = std::nullopt;
105};
106
107// Simulate testStartTime as 10'000s from Ripple epoch time to make
108// LastUpdateTime validation to work and to make unit-test consistent.
109// The value doesn't matter much, it has to be greater
110// than maxLastUpdateTimeDelta in order to pass LastUpdateTime
111// validation {close-maxLastUpdateTimeDelta,close+maxLastUpdateTimeDelta}.
114
120{
121private:
122 // Global fee if not 0
123 static inline std::uint32_t fee = 0;
127
128private:
129 void
130 submit(
131 Json::Value const& jv,
134 std::optional<ter> const& err);
135
136public:
137 Oracle(Env& env, CreateArg const& arg, bool submit = true);
138
139 void
140 remove(RemoveArg const& arg);
141
142 void
143 set(CreateArg const& arg);
144 void
145 set(UpdateArg const& arg);
146
147 static Json::Value
149 Env& env,
150 std::optional<AnyValue> const& baseAsset,
151 std::optional<AnyValue> const& quoteAsset,
152 std::optional<OraclesData> const& oracles = std::nullopt,
153 std::optional<AnyValue> const& trim = std::nullopt,
154 std::optional<AnyValue> const& timeTreshold = std::nullopt);
155
158 {
159 return documentID_;
160 }
161
162 [[nodiscard]] bool
163 exists() const
164 {
165 return exists(env_, owner_, documentID_);
166 }
167
168 [[nodiscard]] static bool
169 exists(Env& env, AccountID const& account, std::uint32_t documentID);
170
171 [[nodiscard]] bool
172 expectPrice(DataSeries const& pricess) const;
173
174 [[nodiscard]] bool
175 expectLastUpdateTime(std::uint32_t lastUpdateTime) const;
176
177 static Json::Value
179 Env& env,
182 std::optional<std::string> const& index = std::nullopt);
183
185 ledgerEntry(std::optional<std::string> const& index = std::nullopt) const
186 {
188 }
189
190 static void
192 {
193 fee = f;
194 }
195
197 operator<<(std::ostream& strm, Oracle const& oracle)
198 {
199 strm << oracle.ledgerEntry().toStyledString();
200 return strm;
201 }
202};
203
204} // namespace oracle
205} // namespace jtx
206} // namespace test
207} // namespace ripple
208
209#endif // RIPPLE_TEST_JTX_ORACLE_H_INCLUDED
Represents a JSON value.
Definition: json_value.h:148
A transaction testing environment.
Definition: Env.h:120
Set the fee on a JTx.
Definition: fee.h:37
Match set account flags.
Definition: flags.h:125
Set a multisignature on a JTx.
Definition: multisign.h:66
Oracle class facilitates unit-testing of the Price Oracle feature.
Definition: Oracle.h:120
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 &timeTreshold=std::nullopt)
Definition: Oracle.cpp:146
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:287
Json::Value ledgerEntry(std::optional< std::string > const &index=std::nullopt) const
Definition: Oracle.h:185
void set(CreateArg const &arg)
Definition: Oracle.cpp:269
friend std::ostream & operator<<(std::ostream &strm, Oracle const &oracle)
Definition: Oracle.h:197
void remove(RemoveArg const &arg)
Definition: Oracle.cpp:54
bool expectPrice(DataSeries const &pricess) const
Definition: Oracle.cpp:108
bool expectLastUpdateTime(std::uint32_t lastUpdateTime) const
Definition: Oracle.cpp:139
std::uint32_t documentID() const
Definition: Oracle.h:157
static void setFee(std::uint32_t f)
Definition: Oracle.h:191
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:72
Keylet oracle(AccountID const &account, std::uint32_t const &documentID) noexcept
Definition: Indexes.cpp:512
static constexpr std::chrono::seconds testStartTime
Definition: Oracle.h:112
constexpr char const * UnquotedNone
Definition: Oracle.h:39
std::uint32_t asUInt(AnyValue const &v)
Definition: Oracle.cpp:353
void toJson(Json::Value &jv, AnyValue const &v)
Definition: Oracle.cpp:329
constexpr char const * NonePattern
Definition: Oracle.h:40
bool validDocumentID(AnyValue const &v)
Definition: Oracle.cpp:361
constexpr char const * NoneTag
Definition: Oracle.h:38
void toJsonHex(Json::Value &jv, AnyValue const &v)
Definition: Oracle.cpp:335
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
static constexpr std::chrono::seconds epoch_offset
Clock for measuring the network time.
Definition: chrono.h:55
std::optional< AnyValue > uri
Definition: Oracle.h:69
std::optional< AnyValue > documentID
Definition: Oracle.h:65
std::optional< AnyValue > lastUpdateTime
Definition: Oracle.h:70
std::optional< AnyValue > assetClass
Definition: Oracle.h:67
std::optional< AccountID > owner
Definition: Oracle.h:64
std::optional< ter > err
Definition: Oracle.h:75
std::optional< AnyValue > provider
Definition: Oracle.h:68
std::optional< AccountID > const & owner
Definition: Oracle.h:98
std::optional< AnyValue > const & documentID
Definition: Oracle.h:99
std::optional< ter > const & err
Definition: Oracle.h:104
std::optional< AnyValue > uri
Definition: Oracle.h:87
std::optional< AnyValue > assetClass
Definition: Oracle.h:85
std::optional< AnyValue > documentID
Definition: Oracle.h:83
std::optional< AnyValue > lastUpdateTime
Definition: Oracle.h:88
std::optional< AccountID > owner
Definition: Oracle.h:82
std::optional< AnyValue > provider
Definition: Oracle.h:86
std::optional< ter > err
Definition: Oracle.h:93
Set the sequence number on a JTx.
Definition: seq.h:34