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};
96
98{
105 bool fund = true;
107 std::optional<TER> err = std::nullopt;
108};
109
111{
113 std::optional<MPTID> id = std::nullopt;
117 std::optional<TER> err = std::nullopt;
118};
119
121{
124 std::optional<MPTID> id = std::nullopt;
128 std::optional<TER> err = std::nullopt;
129};
130
131struct MPTSet
132{
135 std::optional<MPTID> id = std::nullopt;
139 std::optional<TER> err = std::nullopt;
140};
141
143{
148 bool close_;
149
150public:
151 MPTTester(Env& env, Account const& issuer, MPTInit const& constr = {});
152
153 void
154 create(MPTCreate const& arg = MPTCreate{});
155
156 void
157 destroy(MPTDestroy const& arg = MPTDestroy{});
158
159 void
160 authorize(MPTAuthorize const& arg = MPTAuthorize{});
161
162 void
163 set(MPTSet const& set = {});
164
165 [[nodiscard]] bool
166 checkMPTokenAmount(Account const& holder, std::int64_t expectedAmount)
167 const;
168
169 [[nodiscard]] bool
170 checkMPTokenOutstandingAmount(std::int64_t expectedAmount) const;
171
172 [[nodiscard]] bool
174 uint32_t const expectedFlags,
175 std::optional<Account> const& holder = std::nullopt) const;
176
177 Account const&
178 issuer() const
179 {
180 return issuer_;
181 }
182 Account const&
183 holder(std::string const& h) const;
184
185 void
186 pay(Account const& src,
187 Account const& dest,
188 std::int64_t amount,
189 std::optional<TER> err = std::nullopt,
190 std::optional<std::vector<std::string>> credentials = std::nullopt);
191
192 void
193 claw(
194 Account const& issuer,
195 Account const& holder,
196 std::int64_t amount,
197 std::optional<TER> err = std::nullopt);
198
200 mpt(std::int64_t amount) const;
201
202 MPTID const&
204 {
205 if (!env_.test.BEAST_EXPECT(id_))
206 Throw<std::logic_error>("Uninitialized issuanceID");
207 return *id_;
208 }
209
211 getBalance(Account const& account) const;
212
213 MPT
214 operator[](std::string const& name);
215
216private:
218 bool
219 forObject(
220 std::function<bool(SLEP const& sle)> const& cb,
221 std::optional<Account> const& holder = std::nullopt) const;
222
223 template <typename A>
224 TER
225 submit(A const& arg, Json::Value const& jv)
226 {
227 env_(
228 jv,
229 txflags(arg.flags.value_or(0)),
230 ter(arg.err.value_or(tesSUCCESS)));
231 auto const err = env_.ter();
232 if (close_)
233 env_.close();
234 if (arg.ownerCount)
235 env_.require(owners(issuer_, *arg.ownerCount));
236 if (arg.holderCount)
237 {
238 for (auto it : holders_)
239 env_.require(owners(it.second, *arg.holderCount));
240 }
241 return err;
242 }
243
245 makeHolders(std::vector<Account> const& holders);
246
249};
250
251} // namespace jtx
252} // namespace test
253} // namespace ripple
254
255#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:120
void require(Args const &... args)
Check a set of requirements.
Definition: Env.h:532
TER ter() const
Return the TER for the last JTx.
Definition: Env.h:583
beast::unit_test::suite & test
Definition: Env.h:122
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:284
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:300
static std::unordered_map< std::string, Account > makeHolders(std::vector< Account > const &holders)
Definition: mpt.cpp:47
Account const & issuer_
Definition: mpt.h:145
MPTID const & issuanceID() const
Definition: mpt.h:203
TER submit(A const &arg, Json::Value const &jv)
Definition: mpt.h:225
void claw(Account const &issuer, Account const &holder, std::int64_t amount, std::optional< TER > err=std::nullopt)
Definition: mpt.cpp:351
PrettyAmount mpt(std::int64_t amount) const
Definition: mpt.cpp:374
bool checkFlags(uint32_t const expectedFlags, std::optional< Account > const &holder=std::nullopt) const
Definition: mpt.cpp:292
Account const & holder(std::string const &h) const
Definition: mpt.cpp:136
MPT operator[](std::string const &name)
Definition: mpt.cpp:414
Account const & issuer() const
Definition: mpt.h:178
bool forObject(std::function< bool(SLEP const &sle)> const &cb, std::optional< Account > const &holder=std::nullopt) const
Definition: mpt.cpp:260
std::optional< MPTID > id_
Definition: mpt.h:147
std::int64_t getBalance(Account const &account) const
Definition: mpt.cpp:382
std::unordered_map< std::string, Account > const holders_
Definition: mpt.h:146
std::uint32_t getFlags(std::optional< Account > const &holder) const
Definition: mpt.cpp:400
bool checkMPTokenAmount(Account const &holder, std::int64_t expectedAmount) const
Definition: mpt.cpp:274
void create(MPTCreate const &arg=MPTCreate{})
Definition: mpt.cpp:86
Converts to MPT Issue or STAmount.
Match set account flags.
Definition: flags.h:113
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
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:242
std::optional< TER > err
Definition: mpt.h:128
std::optional< std::uint32_t > ownerCount
Definition: mpt.h:125
std::optional< Account > holder
Definition: mpt.h:123
std::optional< std::uint32_t > holderCount
Definition: mpt.h:126
std::optional< Account > account
Definition: mpt.h:122
std::optional< std::uint8_t > assetScale
Definition: mpt.h:100
std::optional< std::uint16_t > transferFee
Definition: mpt.h:101
std::optional< std::string > metadata
Definition: mpt.h:102
std::optional< std::uint32_t > ownerCount
Definition: mpt.h:103
std::optional< TER > err
Definition: mpt.h:107
std::optional< std::uint64_t > maxAmt
Definition: mpt.h:99
std::optional< std::uint32_t > holderCount
Definition: mpt.h:104
std::optional< Account > issuer
Definition: mpt.h:112
std::optional< std::uint32_t > holderCount
Definition: mpt.h:115
std::optional< TER > err
Definition: mpt.h:117
std::optional< std::uint32_t > ownerCount
Definition: mpt.h:114
std::vector< Account > holders
Definition: mpt.h:90
PrettyAmount const & xrp
Definition: mpt.h:91
PrettyAmount const & xrpHolders
Definition: mpt.h:92
std::optional< std::uint32_t > ownerCount
Definition: mpt.h:136
std::optional< Account > account
Definition: mpt.h:133
std::optional< std::uint32_t > holderCount
Definition: mpt.h:137
std::optional< TER > err
Definition: mpt.h:139
std::optional< Account > holder
Definition: mpt.h:134
Represents an XRP or IOU quantity This customizes the string conversion and supports XRP conversions ...