rippled
Loading...
Searching...
No Matches
mpt.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2024 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_MPT_H_INCLUDED
21#define RIPPLE_TEST_JTX_MPT_H_INCLUDED
22
23#include <test/jtx/Account.h>
24#include <test/jtx/Env.h>
25#include <test/jtx/ter.h>
26#include <test/jtx/txflags.h>
27
28#include <xrpl/protocol/UintTypes.h>
29
30namespace ripple {
31namespace test {
32namespace jtx {
33
34class MPTTester;
35
36// Check flags settings on MPT create
38{
39private:
43
44public:
46 MPTTester& tester,
48 std::optional<Account> const& holder = std::nullopt)
49 : tester_(tester), flags_(flags), holder_(holder)
50 {
51 }
52
53 void
54 operator()(Env& env) const;
55};
56
57// Check mptissuance or mptoken amount balances on payment
59{
60private:
64
65public:
66 mptbalance(MPTTester& tester, Account const& account, std::int64_t amount)
67 : tester_(tester), account_(account), amount_(amount)
68 {
69 }
70
71 void
72 operator()(Env& env) const;
73};
74
76{
77private:
79
80public:
81 requireAny(std::function<bool()> const& cb) : cb_(cb)
82 {
83 }
84
85 void
86 operator()(Env& env) const;
87};
88
89struct MPTInit
90{
92 PrettyAmount const xrp = XRP(10'000);
93 PrettyAmount const xrpHolders = XRP(10'000);
94 bool fund = true;
95 bool close = true;
96};
97static MPTInit const mptInitNoFund{.fund = false};
98
100{
107 bool fund = true;
109 std::optional<TER> err = std::nullopt;
110};
111
113{
115 std::optional<MPTID> id = std::nullopt;
119 std::optional<TER> err = std::nullopt;
120};
121
123{
126 std::optional<MPTID> id = std::nullopt;
130 std::optional<TER> err = std::nullopt;
131};
132
133struct MPTSet
134{
137 std::optional<MPTID> id = std::nullopt;
142 std::optional<TER> err = std::nullopt;
143};
144
146{
151 bool close_;
152
153public:
154 MPTTester(Env& env, Account const& issuer, MPTInit const& constr = {});
155
156 void
157 create(MPTCreate const& arg = MPTCreate{});
158
159 void
160 destroy(MPTDestroy const& arg = MPTDestroy{});
161
162 void
163 authorize(MPTAuthorize const& arg = MPTAuthorize{});
164
165 void
166 set(MPTSet const& set = {});
167
168 [[nodiscard]] bool
169 checkMPTokenAmount(Account const& holder, std::int64_t expectedAmount)
170 const;
171
172 [[nodiscard]] bool
173 checkMPTokenOutstandingAmount(std::int64_t expectedAmount) const;
174
175 [[nodiscard]] bool
177 uint32_t const expectedFlags,
178 std::optional<Account> const& holder = std::nullopt) const;
179
180 Account const&
181 issuer() const
182 {
183 return issuer_;
184 }
185 Account const&
186 holder(std::string const& h) const;
187
188 void
189 pay(Account const& src,
190 Account const& dest,
191 std::int64_t amount,
192 std::optional<TER> err = std::nullopt,
193 std::optional<std::vector<std::string>> credentials = std::nullopt);
194
195 void
196 claw(
197 Account const& issuer,
198 Account const& holder,
199 std::int64_t amount,
200 std::optional<TER> err = std::nullopt);
201
203 mpt(std::int64_t amount) const;
204
205 MPTID const&
207 {
208 if (!env_.test.BEAST_EXPECT(id_))
209 Throw<std::logic_error>("Uninitialized issuanceID");
210 return *id_;
211 }
212
214 getBalance(Account const& account) const;
215
216 MPT
217 operator[](std::string const& name);
218
219private:
221 bool
222 forObject(
223 std::function<bool(SLEP const& sle)> const& cb,
224 std::optional<Account> const& holder = std::nullopt) const;
225
226 template <typename A>
227 TER
228 submit(A const& arg, Json::Value const& jv)
229 {
230 env_(
231 jv,
232 txflags(arg.flags.value_or(0)),
233 ter(arg.err.value_or(tesSUCCESS)));
234 auto const err = env_.ter();
235 if (close_)
236 env_.close();
237 if (arg.ownerCount)
238 env_.require(owners(issuer_, *arg.ownerCount));
239 if (arg.holderCount)
240 {
241 for (auto it : holders_)
242 env_.require(owners(it.second, *arg.holderCount));
243 }
244 return err;
245 }
246
248 makeHolders(std::vector<Account> const& holders);
249
252};
253
254} // namespace jtx
255} // namespace test
256} // namespace ripple
257
258#endif
Represents a JSON value.
Definition: json_value.h:149
Immutable cryptographic account descriptor.
Definition: Account.h:39
A transaction testing environment.
Definition: Env.h:121
void require(Args const &... args)
Check a set of requirements.
Definition: Env.h:544
TER ter() const
Return the TER for the last JTx.
Definition: Env.h:595
beast::unit_test::suite & test
Definition: Env.h:123
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition: Env.cpp:117
bool checkMPTokenOutstandingAmount(std::int64_t expectedAmount) const
Definition: mpt.cpp:286
void authorize(MPTAuthorize const &arg=MPTAuthorize{})
Definition: mpt.cpp:145
void pay(Account const &src, Account const &dest, std::int64_t amount, std::optional< TER > err=std::nullopt, std::optional< std::vector< std::string > > credentials=std::nullopt)
Definition: mpt.cpp:302
static std::unordered_map< std::string, Account > makeHolders(std::vector< Account > const &holders)
Definition: mpt.cpp:47
Account const & issuer_
Definition: mpt.h:148
MPTID const & issuanceID() const
Definition: mpt.h:206
TER submit(A const &arg, Json::Value const &jv)
Definition: mpt.h:228
void claw(Account const &issuer, Account const &holder, std::int64_t amount, std::optional< TER > err=std::nullopt)
Definition: mpt.cpp:353
PrettyAmount mpt(std::int64_t amount) const
Definition: mpt.cpp:376
bool checkFlags(uint32_t const expectedFlags, std::optional< Account > const &holder=std::nullopt) const
Definition: mpt.cpp:294
Account const & holder(std::string const &h) const
Definition: mpt.cpp:136
MPT operator[](std::string const &name)
Definition: mpt.cpp:416
Account const & issuer() const
Definition: mpt.h:181
bool forObject(std::function< bool(SLEP const &sle)> const &cb, std::optional< Account > const &holder=std::nullopt) const
Definition: mpt.cpp:262
std::optional< MPTID > id_
Definition: mpt.h:150
std::int64_t getBalance(Account const &account) const
Definition: mpt.cpp:384
std::unordered_map< std::string, Account > const holders_
Definition: mpt.h:149
std::uint32_t getFlags(std::optional< Account > const &holder) const
Definition: mpt.cpp:402
bool checkMPTokenAmount(Account const &holder, std::int64_t expectedAmount) const
Definition: mpt.cpp:276
void create(MPTCreate const &arg=MPTCreate{})
Definition: mpt.cpp:86
Converts to MPT Issue or STAmount.
Match set account flags.
Definition: flags.h:128
mptbalance(MPTTester &tester, Account const &account, std::int64_t amount)
Definition: mpt.h:66
void operator()(Env &env) const
Definition: mpt.cpp:35
Account const & account_
Definition: mpt.h:62
std::int64_t const amount_
Definition: mpt.h:63
MPTTester const & tester_
Definition: mpt.h:61
mptflags(MPTTester &tester, std::uint32_t flags, std::optional< Account > const &holder=std::nullopt)
Definition: mpt.h:45
std::uint32_t flags_
Definition: mpt.h:41
MPTTester & tester_
Definition: mpt.h:40
void operator()(Env &env) const
Definition: mpt.cpp:29
std::optional< Account > holder_
Definition: mpt.h:42
Match the number of items in the account's owner directory.
Definition: owners.h:73
void operator()(Env &env) const
Definition: mpt.cpp:41
requireAny(std::function< bool()> const &cb)
Definition: mpt.h:81
std::function< bool()> cb_
Definition: mpt.h:78
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition: ter.h:35
Set the flags on a JTx.
Definition: txflags.h:31
static MPTInit const mptInitNoFund
Definition: mpt.h:97
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition: amount.cpp:105
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
@ tesSUCCESS
Definition: TER.h:244
std::optional< TER > err
Definition: mpt.h:130
std::optional< std::uint32_t > ownerCount
Definition: mpt.h:127
std::optional< Account > holder
Definition: mpt.h:125
std::optional< std::uint32_t > holderCount
Definition: mpt.h:128
std::optional< Account > account
Definition: mpt.h:124
std::optional< std::uint8_t > assetScale
Definition: mpt.h:102
std::optional< std::uint16_t > transferFee
Definition: mpt.h:103
std::optional< std::string > metadata
Definition: mpt.h:104
std::optional< std::uint32_t > ownerCount
Definition: mpt.h:105
std::optional< TER > err
Definition: mpt.h:109
std::optional< std::uint64_t > maxAmt
Definition: mpt.h:101
std::optional< std::uint32_t > holderCount
Definition: mpt.h:106
std::optional< Account > issuer
Definition: mpt.h:114
std::optional< std::uint32_t > holderCount
Definition: mpt.h:117
std::optional< TER > err
Definition: mpt.h:119
std::optional< std::uint32_t > ownerCount
Definition: mpt.h:116
std::vector< Account > holders
Definition: mpt.h:91
PrettyAmount const xrpHolders
Definition: mpt.h:93
PrettyAmount const xrp
Definition: mpt.h:92
std::optional< std::uint32_t > ownerCount
Definition: mpt.h:138
std::optional< Account > account
Definition: mpt.h:135
std::optional< std::uint32_t > holderCount
Definition: mpt.h:139
std::optional< TER > err
Definition: mpt.h:142
std::optional< Account > holder
Definition: mpt.h:136
std::optional< Account > delegate
Definition: mpt.h:141
Represents an XRP or IOU quantity This customizes the string conversion and supports XRP conversions ...