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.h>
24#include <test/jtx/ter.h>
25#include <test/jtx/txflags.h>
26
27#include <xrpl/protocol/UintTypes.h>
28
29namespace ripple {
30namespace test {
31namespace jtx {
32
33class MPTTester;
34
35// Check flags settings on MPT create
37{
38private:
42
43public:
45 MPTTester& tester,
47 std::optional<Account> const& holder = std::nullopt)
48 : tester_(tester), flags_(flags), holder_(holder)
49 {
50 }
51
52 void
53 operator()(Env& env) const;
54};
55
56// Check mptissuance or mptoken amount balances on payment
58{
59private:
63
64public:
65 mptbalance(MPTTester& tester, Account const& account, std::int64_t amount)
66 : tester_(tester), account_(account), amount_(amount)
67 {
68 }
69
70 void
71 operator()(Env& env) const;
72};
73
75{
76private:
78
79public:
80 requireAny(std::function<bool()> const& cb) : cb_(cb)
81 {
82 }
83
84 void
85 operator()(Env& env) const;
86};
87
88struct MPTInit
89{
91 PrettyAmount const xrp = XRP(10'000);
92 PrettyAmount const xrpHolders = XRP(10'000);
93 bool fund = true;
94 bool close = true;
95};
96static MPTInit const mptInitNoFund{.fund = false};
97
99{
106 bool fund = true;
108 std::optional<TER> err = std::nullopt;
109};
110
112{
114 std::optional<MPTID> id = std::nullopt;
118 std::optional<TER> err = std::nullopt;
119};
120
122{
125 std::optional<MPTID> id = std::nullopt;
129 std::optional<TER> err = std::nullopt;
130};
131
132struct MPTSet
133{
136 std::optional<MPTID> id = std::nullopt;
141 std::optional<TER> err = std::nullopt;
142};
143
145{
150 bool close_;
151
152public:
153 MPTTester(Env& env, Account const& issuer, MPTInit const& constr = {});
154
155 void
156 create(MPTCreate const& arg = MPTCreate{});
157
158 void
159 destroy(MPTDestroy const& arg = MPTDestroy{});
160
161 void
162 authorize(MPTAuthorize const& arg = MPTAuthorize{});
163
164 void
165 set(MPTSet const& set = {});
166
167 [[nodiscard]] bool
168 checkMPTokenAmount(Account const& holder, std::int64_t expectedAmount)
169 const;
170
171 [[nodiscard]] bool
172 checkMPTokenOutstandingAmount(std::int64_t expectedAmount) const;
173
174 [[nodiscard]] bool
176 uint32_t const expectedFlags,
177 std::optional<Account> const& holder = std::nullopt) const;
178
179 Account const&
180 issuer() const
181 {
182 return issuer_;
183 }
184 Account const&
185 holder(std::string const& h) const;
186
187 void
188 pay(Account const& src,
189 Account const& dest,
190 std::int64_t amount,
191 std::optional<TER> err = std::nullopt,
192 std::optional<std::vector<std::string>> credentials = std::nullopt);
193
194 void
195 claw(
196 Account const& issuer,
197 Account const& holder,
198 std::int64_t amount,
199 std::optional<TER> err = std::nullopt);
200
202 mpt(std::int64_t amount) const;
203
204 MPTID const&
206 {
207 if (!env_.test.BEAST_EXPECT(id_))
208 Throw<std::logic_error>("Uninitialized issuanceID");
209 return *id_;
210 }
211
213 getBalance(Account const& account) const;
214
215 MPT
216 operator[](std::string const& name);
217
218private:
220 bool
221 forObject(
222 std::function<bool(SLEP const& sle)> const& cb,
223 std::optional<Account> const& holder = std::nullopt) const;
224
225 template <typename A>
226 TER
227 submit(A const& arg, Json::Value const& jv)
228 {
229 env_(
230 jv,
231 txflags(arg.flags.value_or(0)),
232 ter(arg.err.value_or(tesSUCCESS)));
233 auto const err = env_.ter();
234 if (close_)
235 env_.close();
236 if (arg.ownerCount)
237 env_.require(owners(issuer_, *arg.ownerCount));
238 if (arg.holderCount)
239 {
240 for (auto it : holders_)
241 env_.require(owners(it.second, *arg.holderCount));
242 }
243 return err;
244 }
245
247 makeHolders(std::vector<Account> const& holders);
248
251};
252
253} // namespace jtx
254} // namespace test
255} // namespace ripple
256
257#endif
Represents a JSON value.
Definition: json_value.h:150
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:535
TER ter() const
Return the TER for the last JTx.
Definition: Env.h:586
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:147
MPTID const & issuanceID() const
Definition: mpt.h:205
TER submit(A const &arg, Json::Value const &jv)
Definition: mpt.h:227
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:180
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:149
std::int64_t getBalance(Account const &account) const
Definition: mpt.cpp:384
std::unordered_map< std::string, Account > const holders_
Definition: mpt.h:148
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:125
mptbalance(MPTTester &tester, Account const &account, std::int64_t amount)
Definition: mpt.h:65
void operator()(Env &env) const
Definition: mpt.cpp:35
Account const & account_
Definition: mpt.h:61
std::int64_t const amount_
Definition: mpt.h:62
MPTTester const & tester_
Definition: mpt.h:60
mptflags(MPTTester &tester, std::uint32_t flags, std::optional< Account > const &holder=std::nullopt)
Definition: mpt.h:44
std::uint32_t flags_
Definition: mpt.h:40
MPTTester & tester_
Definition: mpt.h:39
void operator()(Env &env) const
Definition: mpt.cpp:29
std::optional< Account > holder_
Definition: mpt.h:41
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:80
std::function< bool()> cb_
Definition: mpt.h:77
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:96
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:26
@ tesSUCCESS
Definition: TER.h:244
std::optional< TER > err
Definition: mpt.h:129
std::optional< std::uint32_t > ownerCount
Definition: mpt.h:126
std::optional< Account > holder
Definition: mpt.h:124
std::optional< std::uint32_t > holderCount
Definition: mpt.h:127
std::optional< Account > account
Definition: mpt.h:123
std::optional< std::uint8_t > assetScale
Definition: mpt.h:101
std::optional< std::uint16_t > transferFee
Definition: mpt.h:102
std::optional< std::string > metadata
Definition: mpt.h:103
std::optional< std::uint32_t > ownerCount
Definition: mpt.h:104
std::optional< TER > err
Definition: mpt.h:108
std::optional< std::uint64_t > maxAmt
Definition: mpt.h:100
std::optional< std::uint32_t > holderCount
Definition: mpt.h:105
std::optional< Account > issuer
Definition: mpt.h:113
std::optional< std::uint32_t > holderCount
Definition: mpt.h:116
std::optional< TER > err
Definition: mpt.h:118
std::optional< std::uint32_t > ownerCount
Definition: mpt.h:115
std::vector< Account > holders
Definition: mpt.h:90
PrettyAmount const xrpHolders
Definition: mpt.h:92
PrettyAmount const xrp
Definition: mpt.h:91
std::optional< std::uint32_t > ownerCount
Definition: mpt.h:137
std::optional< Account > account
Definition: mpt.h:134
std::optional< std::uint32_t > holderCount
Definition: mpt.h:138
std::optional< TER > err
Definition: mpt.h:141
std::optional< Account > holder
Definition: mpt.h:135
std::optional< Account > delegate
Definition: mpt.h:140
Represents an XRP or IOU quantity This customizes the string conversion and supports XRP conversions ...