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