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;
140 std::optional<TER> err = std::nullopt;
141};
142
144{
149 bool close_;
150
151public:
152 MPTTester(Env& env, Account const& issuer, MPTInit const& constr = {});
153
154 void
155 create(MPTCreate const& arg = MPTCreate{});
156
157 void
158 destroy(MPTDestroy const& arg = MPTDestroy{});
159
160 void
161 authorize(MPTAuthorize const& arg = MPTAuthorize{});
162
163 void
164 set(MPTSet const& set = {});
165
166 [[nodiscard]] bool
167 checkMPTokenAmount(Account const& holder, std::int64_t expectedAmount)
168 const;
169
170 [[nodiscard]] bool
171 checkMPTokenOutstandingAmount(std::int64_t expectedAmount) const;
172
173 [[nodiscard]] bool
175 uint32_t const expectedFlags,
176 std::optional<Account> const& holder = std::nullopt) const;
177
178 Account const&
179 issuer() const
180 {
181 return issuer_;
182 }
183 Account const&
184 holder(std::string const& h) const;
185
186 void
187 pay(Account const& src,
188 Account const& dest,
189 std::int64_t amount,
190 std::optional<TER> err = std::nullopt,
191 std::optional<std::vector<std::string>> credentials = std::nullopt);
192
193 void
194 claw(
195 Account const& issuer,
196 Account const& holder,
197 std::int64_t amount,
198 std::optional<TER> err = std::nullopt);
199
201 mpt(std::int64_t amount) const;
202
203 MPTID const&
205 {
206 if (!env_.test.BEAST_EXPECT(id_))
207 Throw<std::logic_error>("Uninitialized issuanceID");
208 return *id_;
209 }
210
212 getBalance(Account const& account) const;
213
214 MPT
215 operator[](std::string const& name);
216
217private:
219 bool
220 forObject(
221 std::function<bool(SLEP const& sle)> const& cb,
222 std::optional<Account> const& holder = std::nullopt) const;
223
224 template <typename A>
225 TER
226 submit(A const& arg, Json::Value const& jv)
227 {
228 env_(
229 jv,
230 txflags(arg.flags.value_or(0)),
231 ter(arg.err.value_or(tesSUCCESS)));
232 auto const err = env_.ter();
233 if (close_)
234 env_.close();
235 if (arg.ownerCount)
236 env_.require(owners(issuer_, *arg.ownerCount));
237 if (arg.holderCount)
238 {
239 for (auto it : holders_)
240 env_.require(owners(it.second, *arg.holderCount));
241 }
242 return err;
243 }
244
246 makeHolders(std::vector<Account> const& holders);
247
250};
251
252} // namespace jtx
253} // namespace test
254} // namespace ripple
255
256#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:534
TER ter() const
Return the TER for the last JTx.
Definition: Env.h:585
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: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:146
MPTID const & issuanceID() const
Definition: mpt.h:204
TER submit(A const &arg, Json::Value const &jv)
Definition: mpt.h:226
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:179
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:148
std::int64_t getBalance(Account const &account) const
Definition: mpt.cpp:384
std::unordered_map< std::string, Account > const holders_
Definition: mpt.h:147
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
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:140
std::optional< Account > holder
Definition: mpt.h:134
std::optional< Account > delegate
Definition: mpt.h:139
Represents an XRP or IOU quantity This customizes the string conversion and supports XRP conversions ...