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;
110 std::optional<TER> err = std::nullopt;
111};
112
114{
116 std::optional<MPTID> id = std::nullopt;
120 std::optional<TER> err = std::nullopt;
121};
122
124{
127 std::optional<MPTID> id = std::nullopt;
131 std::optional<TER> err = std::nullopt;
132};
133
134struct MPTSet
135{
138 std::optional<MPTID> id = std::nullopt;
144 std::optional<TER> err = std::nullopt;
145};
146
148{
153 bool close_;
154
155public:
156 MPTTester(Env& env, Account const& issuer, MPTInit const& constr = {});
157
158 void
159 create(MPTCreate const& arg = MPTCreate{});
160
161 void
162 destroy(MPTDestroy const& arg = MPTDestroy{});
163
164 void
165 authorize(MPTAuthorize const& arg = MPTAuthorize{});
166
167 void
168 set(MPTSet const& set = {});
169
170 [[nodiscard]] bool
172
173 [[nodiscard]] bool
174 checkMPTokenAmount(Account const& holder, std::int64_t expectedAmount)
175 const;
176
177 [[nodiscard]] bool
178 checkMPTokenOutstandingAmount(std::int64_t expectedAmount) const;
179
180 [[nodiscard]] bool
182 uint32_t const expectedFlags,
183 std::optional<Account> const& holder = std::nullopt) const;
184
185 Account const&
186 issuer() const
187 {
188 return issuer_;
189 }
190 Account const&
191 holder(std::string const& h) const;
192
193 void
194 pay(Account const& src,
195 Account const& dest,
196 std::int64_t amount,
197 std::optional<TER> err = std::nullopt,
198 std::optional<std::vector<std::string>> credentials = std::nullopt);
199
200 void
201 claw(
202 Account const& issuer,
203 Account const& holder,
204 std::int64_t amount,
205 std::optional<TER> err = std::nullopt);
206
208 mpt(std::int64_t amount) const;
209
210 MPTID const&
212 {
213 if (!env_.test.BEAST_EXPECT(id_))
214 Throw<std::logic_error>("Uninitialized issuanceID");
215 return *id_;
216 }
217
219 getBalance(Account const& account) const;
220
221 MPT
222 operator[](std::string const& name);
223
224private:
226 bool
227 forObject(
228 std::function<bool(SLEP const& sle)> const& cb,
229 std::optional<Account> const& holder = std::nullopt) const;
230
231 template <typename A>
232 TER
233 submit(A const& arg, Json::Value const& jv)
234 {
235 env_(
236 jv,
237 txflags(arg.flags.value_or(0)),
238 ter(arg.err.value_or(tesSUCCESS)));
239 auto const err = env_.ter();
240 if (close_)
241 env_.close();
242 if (arg.ownerCount)
243 env_.require(owners(issuer_, *arg.ownerCount));
244 if (arg.holderCount)
245 {
246 for (auto it : holders_)
247 env_.require(owners(it.second, *arg.holderCount));
248 }
249 return err;
250 }
251
253 makeHolders(std::vector<Account> const& holders);
254
257};
258
259} // namespace jtx
260} // namespace test
261} // namespace ripple
262
263#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:301
void authorize(MPTAuthorize const &arg=MPTAuthorize{})
Definition: mpt.cpp:148
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:317
static std::unordered_map< std::string, Account > makeHolders(std::vector< Account > const &holders)
Definition: mpt.cpp:48
Account const & issuer_
Definition: mpt.h:150
MPTID const & issuanceID() const
Definition: mpt.h:211
TER submit(A const &arg, Json::Value const &jv)
Definition: mpt.h:233
void claw(Account const &issuer, Account const &holder, std::int64_t amount, std::optional< TER > err=std::nullopt)
Definition: mpt.cpp:368
PrettyAmount mpt(std::int64_t amount) const
Definition: mpt.cpp:391
bool checkFlags(uint32_t const expectedFlags, std::optional< Account > const &holder=std::nullopt) const
Definition: mpt.cpp:309
Account const & holder(std::string const &h) const
Definition: mpt.cpp:139
MPT operator[](std::string const &name)
Definition: mpt.cpp:431
Account const & issuer() const
Definition: mpt.h:186
bool forObject(std::function< bool(SLEP const &sle)> const &cb, std::optional< Account > const &holder=std::nullopt) const
Definition: mpt.cpp:267
std::optional< MPTID > id_
Definition: mpt.h:152
std::int64_t getBalance(Account const &account) const
Definition: mpt.cpp:399
std::unordered_map< std::string, Account > const holders_
Definition: mpt.h:151
bool checkDomainID(std::optional< uint256 > expected) const
Definition: mpt.cpp:281
std::uint32_t getFlags(std::optional< Account > const &holder) const
Definition: mpt.cpp:417
bool checkMPTokenAmount(Account const &holder, std::int64_t expectedAmount) const
Definition: mpt.cpp:291
void create(MPTCreate const &arg=MPTCreate{})
Definition: mpt.cpp:87
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:36
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:30
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:42
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:131
std::optional< std::uint32_t > ownerCount
Definition: mpt.h:128
std::optional< Account > holder
Definition: mpt.h:126
std::optional< std::uint32_t > holderCount
Definition: mpt.h:129
std::optional< Account > account
Definition: mpt.h:125
std::optional< std::uint8_t > assetScale
Definition: mpt.h:102
std::optional< std::uint16_t > transferFee
Definition: mpt.h:103
std::optional< uint256 > domainID
Definition: mpt.h:109
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:110
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:115
std::optional< std::uint32_t > holderCount
Definition: mpt.h:118
std::optional< TER > err
Definition: mpt.h:120
std::optional< std::uint32_t > ownerCount
Definition: mpt.h:117
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:139
std::optional< Account > account
Definition: mpt.h:136
std::optional< uint256 > domainID
Definition: mpt.h:143
std::optional< std::uint32_t > holderCount
Definition: mpt.h:140
std::optional< TER > err
Definition: mpt.h:144
std::optional< Account > holder
Definition: mpt.h:137
std::optional< Account > delegate
Definition: mpt.h:142
Represents an XRP or IOU quantity This customizes the string conversion and supports XRP conversions ...