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#include <date/date.h>
25
26namespace ripple {
27namespace test {
28namespace jtx {
29namespace oracle {
30
34
35// Special string value, which is converted to unquoted string in the string
36// passed to rpc.
37constexpr char const* NoneTag = "%None%";
38constexpr char const* UnquotedNone = "None";
39constexpr char const* NonePattern = "\"%None%\"";
40
42asUInt(AnyValue const& v);
43
44bool
46
47void
48toJson(Json::Value& jv, AnyValue const& v);
49
50void
51toJsonHex(Json::Value& jv, AnyValue const& v);
52
53// base asset, quote asset, price, scale
59
60// Typical defaults for Create
62{
65 DataSeries series = {{"XRP", "USD", 740, 1}};
73 int fee = 10;
74 std::optional<ter> err = std::nullopt;
75 bool close = false;
76};
77
78// Typical defaults for Update
80{
91 int fee = 10;
92 std::optional<ter> err = std::nullopt;
93};
94
96{
97 std::optional<AccountID> const& owner = std::nullopt;
98 std::optional<AnyValue> const& documentID = std::nullopt;
100 std::optional<jtx::msig> const& msig = std::nullopt;
102 int fee = 10;
103 std::optional<ter> const& err = std::nullopt;
104};
105
106// Simulate testStartTime as 10'000s from Ripple epoch time to make
107// LastUpdateTime validation to work and to make unit-test consistent.
108// The value doesn't matter much, it has to be greater
109// than maxLastUpdateTimeDelta in order to pass LastUpdateTime
110// validation {close-maxLastUpdateTimeDelta,close+maxLastUpdateTimeDelta}.
113
119{
120private:
121 // Global fee if not 0
122 static inline std::uint32_t fee = 0;
126
127private:
128 void
129 submit(
130 Json::Value const& jv,
133 std::optional<ter> const& err);
134
135public:
136 Oracle(Env& env, CreateArg const& arg, bool submit = true);
137
138 void
139 remove(RemoveArg const& arg);
140
141 void
142 set(CreateArg const& arg);
143 void
144 set(UpdateArg const& arg);
145
146 static Json::Value
148 Env& env,
149 std::optional<AnyValue> const& baseAsset,
150 std::optional<AnyValue> const& quoteAsset,
151 std::optional<OraclesData> const& oracles = std::nullopt,
152 std::optional<AnyValue> const& trim = std::nullopt,
153 std::optional<AnyValue> const& timeTreshold = std::nullopt);
154
157 {
158 return documentID_;
159 }
160
161 [[nodiscard]] bool
162 exists() const
163 {
164 return exists(env_, owner_, documentID_);
165 }
166
167 [[nodiscard]] static bool
168 exists(Env& env, AccountID const& account, std::uint32_t documentID);
169
170 [[nodiscard]] bool
171 expectPrice(DataSeries const& pricess) const;
172
173 [[nodiscard]] bool
174 expectLastUpdateTime(std::uint32_t lastUpdateTime) const;
175
176 static Json::Value
178 Env& env,
181 std::optional<std::string> const& index = std::nullopt);
182
184 ledgerEntry(std::optional<std::string> const& index = std::nullopt) const
185 {
187 }
188
189 static void
191 {
192 fee = f;
193 }
194
196 operator<<(std::ostream& strm, Oracle const& oracle)
197 {
198 strm << oracle.ledgerEntry().toStyledString();
199 return strm;
200 }
201};
202
203} // namespace oracle
204} // namespace jtx
205} // namespace test
206} // namespace ripple
207
208#endif // RIPPLE_TEST_JTX_ORACLE_H_INCLUDED
Represents a JSON value.
Definition: json_value.h:147
A transaction testing environment.
Definition: Env.h:117
Set the fee on a JTx.
Definition: fee.h:36
Match set account flags.
Definition: flags.h:112
Set a multisignature on a JTx.
Definition: multisign.h:65
Oracle class facilitates unit-testing of the Price Oracle feature.
Definition: Oracle.h:119
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:184
void set(CreateArg const &arg)
Definition: Oracle.cpp:269
friend std::ostream & operator<<(std::ostream &strm, Oracle const &oracle)
Definition: Oracle.h:196
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:156
static void setFee(std::uint32_t f)
Definition: Oracle.h:190
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:488
static constexpr std::chrono::seconds testStartTime
Definition: Oracle.h:111
constexpr char const * UnquotedNone
Definition: Oracle.h:38
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:39
bool validDocumentID(AnyValue const &v)
Definition: Oracle.cpp:361
constexpr char const * NoneTag
Definition: Oracle.h:37
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:56
std::optional< AnyValue > uri
Definition: Oracle.h:68
std::optional< AnyValue > documentID
Definition: Oracle.h:64
std::optional< AnyValue > lastUpdateTime
Definition: Oracle.h:69
std::optional< AnyValue > assetClass
Definition: Oracle.h:66
std::optional< AccountID > owner
Definition: Oracle.h:63
std::optional< ter > err
Definition: Oracle.h:74
std::optional< AnyValue > provider
Definition: Oracle.h:67
std::optional< AccountID > const & owner
Definition: Oracle.h:97
std::optional< AnyValue > const & documentID
Definition: Oracle.h:98
std::optional< ter > const & err
Definition: Oracle.h:103
std::optional< AnyValue > uri
Definition: Oracle.h:86
std::optional< AnyValue > assetClass
Definition: Oracle.h:84
std::optional< AnyValue > documentID
Definition: Oracle.h:82
std::optional< AnyValue > lastUpdateTime
Definition: Oracle.h:87
std::optional< AccountID > owner
Definition: Oracle.h:81
std::optional< AnyValue > provider
Definition: Oracle.h:85
std::optional< ter > err
Definition: Oracle.h:92
Set the sequence number on a JTx.
Definition: seq.h:34