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,
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
113
123
134
150
152{
157 bool close_;
158
159public:
160 MPTTester(Env& env, Account const& issuer, MPTInit const& constr = {});
161
162 void
163 create(MPTCreate const& arg = MPTCreate{});
164
165 void
166 destroy(MPTDestroy const& arg = MPTDestroy{});
167
168 void
169 authorize(MPTAuthorize const& arg = MPTAuthorize{});
170
171 void
172 set(MPTSet const& set = {});
173
174 [[nodiscard]] bool
176
177 [[nodiscard]] bool
178 checkMPTokenAmount(Account const& holder, std::int64_t expectedAmount)
179 const;
180
181 [[nodiscard]] bool
182 checkMPTokenOutstandingAmount(std::int64_t expectedAmount) const;
183
184 [[nodiscard]] bool
186 uint32_t const expectedFlags,
188
189 [[nodiscard]] bool
190 checkMetadata(std::string const& metadata) const;
191
192 [[nodiscard]] bool
193 isMetadataPresent() const;
194
195 [[nodiscard]] bool
196 checkTransferFee(std::uint16_t transferFee) const;
197
198 [[nodiscard]] bool
199 isTransferFeePresent() const;
200
201 Account const&
202 issuer() const
203 {
204 return issuer_;
205 }
206 Account const&
207 holder(std::string const& h) const;
208
209 void
210 pay(Account const& src,
211 Account const& dest,
212 std::int64_t amount,
215
216 void
217 claw(
218 Account const& issuer,
219 Account const& holder,
220 std::int64_t amount,
222
224 mpt(std::int64_t amount) const;
225
226 MPTID const&
228 {
229 if (!env_.test.BEAST_EXPECT(id_))
230 Throw<std::logic_error>("Uninitialized issuanceID");
231 return *id_;
232 }
233
235 getBalance(Account const& account) const;
236
237 MPT
238 operator[](std::string const& name);
239
240private:
242 bool
243 forObject(
244 std::function<bool(SLEP const& sle)> const& cb,
246
247 template <typename A>
248 TER
249 submit(A const& arg, Json::Value const& jv)
250 {
251 env_(
252 jv,
253 txflags(arg.flags.value_or(0)),
254 ter(arg.err.value_or(tesSUCCESS)));
255 auto const err = env_.ter();
256 if (close_)
257 env_.close();
258 if (arg.ownerCount)
259 env_.require(owners(issuer_, *arg.ownerCount));
260 if (arg.holderCount)
261 {
262 for (auto it : holders_)
263 env_.require(owners(it.second, *arg.holderCount));
264 }
265 return err;
266 }
267
269 makeHolders(std::vector<Account> const& holders);
270
273};
274
275} // namespace jtx
276} // namespace test
277} // namespace ripple
278
279#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:547
TER ter() const
Return the TER for the last JTx.
Definition Env.h:598
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:121
bool checkMPTokenOutstandingAmount(std::int64_t expectedAmount) const
Definition mpt.cpp:343
void authorize(MPTAuthorize const &arg=MPTAuthorize{})
Definition mpt.cpp:150
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:396
static std::unordered_map< std::string, Account > makeHolders(std::vector< Account > const &holders)
Definition mpt.cpp:48
Account const & issuer_
Definition mpt.h:154
bool isMetadataPresent() const
Definition mpt.cpp:370
bool checkTransferFee(std::uint16_t transferFee) const
Definition mpt.cpp:378
MPTID const & issuanceID() const
Definition mpt.h:227
TER submit(A const &arg, Json::Value const &jv)
Definition mpt.h:249
void claw(Account const &issuer, Account const &holder, std::int64_t amount, std::optional< TER > err=std::nullopt)
Definition mpt.cpp:447
PrettyAmount mpt(std::int64_t amount) const
Definition mpt.cpp:470
bool checkFlags(uint32_t const expectedFlags, std::optional< Account > const &holder=std::nullopt) const
Definition mpt.cpp:351
Account const & holder(std::string const &h) const
Definition mpt.cpp:141
MPT operator[](std::string const &name)
Definition mpt.cpp:510
Account const & issuer() const
Definition mpt.h:202
bool checkMetadata(std::string const &metadata) const
Definition mpt.cpp:359
bool forObject(std::function< bool(SLEP const &sle)> const &cb, std::optional< Account > const &holder=std::nullopt) const
Definition mpt.cpp:309
bool isTransferFeePresent() const
Definition mpt.cpp:388
std::optional< MPTID > id_
Definition mpt.h:156
std::int64_t getBalance(Account const &account) const
Definition mpt.cpp:478
std::unordered_map< std::string, Account > const holders_
Definition mpt.h:155
bool checkDomainID(std::optional< uint256 > expected) const
Definition mpt.cpp:323
std::uint32_t getFlags(std::optional< Account > const &holder) const
Definition mpt.cpp:496
bool checkMPTokenAmount(Account const &holder, std::int64_t expectedAmount) const
Definition mpt.cpp:333
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
T is_same_v
static MPTInit const mptInitNoFund
Definition mpt.h:97
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:111
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:132
std::optional< std::uint32_t > ownerCount
Definition mpt.h:129
std::optional< Account > holder
Definition mpt.h:127
std::optional< std::uint32_t > holderCount
Definition mpt.h:130
std::optional< Account > account
Definition mpt.h:126
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:110
std::optional< std::uint32_t > mutableFlags
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:111
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:116
std::optional< std::uint32_t > holderCount
Definition mpt.h:119
std::optional< TER > err
Definition mpt.h:121
std::optional< std::uint32_t > ownerCount
Definition mpt.h:118
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::uint16_t > transferFee
Definition mpt.h:144
std::optional< std::uint32_t > ownerCount
Definition mpt.h:140
std::optional< Account > account
Definition mpt.h:137
std::optional< std::string > metadata
Definition mpt.h:145
std::optional< uint256 > domainID
Definition mpt.h:147
std::optional< std::uint32_t > mutableFlags
Definition mpt.h:143
std::optional< std::uint32_t > holderCount
Definition mpt.h:141
std::optional< TER > err
Definition mpt.h:148
std::optional< Account > holder
Definition mpt.h:138
std::optional< Account > delegate
Definition mpt.h:146
Represents an XRP or IOU quantity This customizes the string conversion and supports XRP conversions ...