xrpld
Loading...
Searching...
No Matches
NFToken_test.cpp
1
2#include <test/jtx/Account.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/TestHelpers.h>
5#include <test/jtx/acctdelete.h>
6#include <test/jtx/amount.h>
7#include <test/jtx/balance.h> // IWYU pragma: keep
8#include <test/jtx/check.h>
9#include <test/jtx/fee.h>
10#include <test/jtx/flags.h>
11#include <test/jtx/noop.h>
12#include <test/jtx/offer.h>
13#include <test/jtx/owners.h> // IWYU pragma: keep
14#include <test/jtx/pay.h>
15#include <test/jtx/rate.h>
16#include <test/jtx/ter.h>
17#include <test/jtx/ticket.h>
18#include <test/jtx/token.h>
19#include <test/jtx/trust.h>
20#include <test/jtx/txflags.h>
21
22#include <xrpld/rpc/Context.h>
23#include <xrpld/rpc/RPCHandler.h>
24#include <xrpld/rpc/Role.h>
25#include <xrpld/rpc/detail/Tuning.h>
26
27#include <xrpl/basics/base_uint.h>
28#include <xrpl/basics/random.h>
29#include <xrpl/basics/strHex.h>
30#include <xrpl/beast/unit_test/suite.h>
31#include <xrpl/beast/utility/Journal.h>
32#include <xrpl/beast/utility/Zero.h>
33#include <xrpl/core/Job.h>
34#include <xrpl/core/ServiceRegistry.h>
35#include <xrpl/json/json_value.h>
36#include <xrpl/json/to_string.h>
37#include <xrpl/ledger/OpenView.h>
38#include <xrpl/protocol/ApiVersion.h>
39#include <xrpl/protocol/ErrorCodes.h>
40#include <xrpl/protocol/Feature.h>
41#include <xrpl/protocol/Indexes.h>
42#include <xrpl/protocol/Protocol.h>
43#include <xrpl/protocol/SField.h>
44#include <xrpl/protocol/SeqProxy.h>
45#include <xrpl/protocol/TER.h>
46#include <xrpl/protocol/TxFlags.h>
47#include <xrpl/protocol/UintTypes.h>
48#include <xrpl/protocol/jss.h>
49#include <xrpl/protocol/nft.h>
50#include <xrpl/resource/Charge.h>
51#include <xrpl/resource/Consumer.h>
52#include <xrpl/resource/Fees.h>
53
54#include <algorithm>
55#include <chrono>
56#include <cstddef>
57#include <cstdint>
58#include <functional>
59#include <initializer_list>
60#include <iterator>
61#include <memory>
62#include <optional>
63#include <set>
64#include <sstream>
65#include <string>
66#include <utility>
67#include <vector>
68
69namespace xrpl {
70
72{
73 // Helper function that returns the number of NFTs minted by an issuer.
74 static std::uint32_t
76 {
77 std::uint32_t ret{0};
78 if (auto const sleIssuer = env.le(issuer))
79 ret = sleIssuer->at(~sfMintedNFTokens).value_or(0);
80 return ret;
81 }
82
83 // Helper function that returns the number of an issuer's burned NFTs.
84 static std::uint32_t
86 {
87 std::uint32_t ret{0};
88 if (auto const sleIssuer = env.le(issuer))
89 ret = sleIssuer->at(~sfBurnedNFTokens).value_or(0);
90 return ret;
91 }
92
93 // Helper function that returns the number of nfts owned by an account.
94 static std::uint32_t
96 {
97 json::Value params;
98 params[jss::account] = acct.human();
99 params[jss::type] = "state";
100 json::Value nfts = env.rpc("json", "account_nfts", to_string(params));
101 return nfts[jss::result][jss::account_nfts].size();
102 };
103
104 // Helper function that returns the number of tickets held by an account.
105 static std::uint32_t
107 {
108 std::uint32_t ret{0};
109 if (auto const sleAcct = env.le(acct))
110 ret = sleAcct->at(~sfTicketCount).value_or(0);
111 return ret;
112 }
113
114 // Helper function returns the close time of the parent ledger.
115 static std::uint32_t
117 {
118 return env.current()->header().parentCloseTime.time_since_epoch().count();
119 }
120
121 void
123 {
124 testcase("Enabled");
125
126 using namespace test::jtx;
127 {
128 // If the NFT amendment is enabled all NFT-related
129 // facilities should be available.
130 Env env{*this, features};
131 Account const& master = env.master;
132
133 BEAST_EXPECT(ownerCount(env, master) == 0);
134 BEAST_EXPECT(mintedCount(env, master) == 0);
135 BEAST_EXPECT(burnedCount(env, master) == 0);
136
137 UInt256 const nftId0{token::getNextID(env, env.master, 0u)};
138 env(token::mint(env.master, 0u));
139 env.close();
140 BEAST_EXPECT(ownerCount(env, master) == 1);
141 BEAST_EXPECT(mintedCount(env, master) == 1);
142 BEAST_EXPECT(burnedCount(env, master) == 0);
143
144 env(token::burn(env.master, nftId0));
145 env.close();
146 BEAST_EXPECT(ownerCount(env, master) == 0);
147 BEAST_EXPECT(mintedCount(env, master) == 1);
148 BEAST_EXPECT(burnedCount(env, master) == 1);
149
150 UInt256 const nftId1{token::getNextID(env, env.master, 0u, tfTransferable)};
151 env(token::mint(env.master, 0u), Txflags(tfTransferable));
152 env.close();
153 BEAST_EXPECT(ownerCount(env, master) == 1);
154 BEAST_EXPECT(mintedCount(env, master) == 2);
155 BEAST_EXPECT(burnedCount(env, master) == 1);
156
157 Account const alice{"alice"};
158 env.fund(XRP(10000), alice);
159 env.close();
160 UInt256 const aliceOfferIndex =
161 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
162 env(token::createOffer(alice, nftId1, XRP(1000)), token::Owner(master));
163 env.close();
164
165 BEAST_EXPECT(ownerCount(env, master) == 1);
166 BEAST_EXPECT(mintedCount(env, master) == 2);
167 BEAST_EXPECT(burnedCount(env, master) == 1);
168
169 BEAST_EXPECT(ownerCount(env, alice) == 1);
170 BEAST_EXPECT(mintedCount(env, alice) == 0);
171 BEAST_EXPECT(burnedCount(env, alice) == 0);
172
173 env(token::acceptBuyOffer(master, aliceOfferIndex));
174 env.close();
175
176 BEAST_EXPECT(ownerCount(env, master) == 0);
177 BEAST_EXPECT(mintedCount(env, master) == 2);
178 BEAST_EXPECT(burnedCount(env, master) == 1);
179
180 BEAST_EXPECT(ownerCount(env, alice) == 1);
181 BEAST_EXPECT(mintedCount(env, alice) == 0);
182 BEAST_EXPECT(burnedCount(env, alice) == 0);
183 }
184 }
185
186 void
188 {
189 // Verify that the reserve behaves as expected for minting.
190 testcase("Mint reserve");
191
192 using namespace test::jtx;
193
194 Env env{*this, features};
195 Account const alice{"alice"};
196 Account const minter{"minter"};
197
198 // Fund alice and minter enough to exist, but not enough to meet
199 // the reserve for creating their first NFT.
200 auto const acctReserve = env.current()->fees().reserve;
201 auto const incReserve = env.current()->fees().increment;
202 auto const baseFee = env.current()->fees().base;
203
204 env.fund(acctReserve, alice, minter);
205 env.close();
206
207 BEAST_EXPECT(env.balance(alice) == acctReserve);
208 BEAST_EXPECT(env.balance(minter) == acctReserve);
209 BEAST_EXPECT(ownerCount(env, alice) == 0);
210 BEAST_EXPECT(ownerCount(env, minter) == 0);
211
212 // alice does not have enough XRP to cover the reserve for an NFT
213 // page.
214 env(token::mint(alice, 0u), Ter(tecINSUFFICIENT_RESERVE));
215 env.close();
216
217 BEAST_EXPECT(ownerCount(env, alice) == 0);
218 BEAST_EXPECT(mintedCount(env, alice) == 0);
219 BEAST_EXPECT(burnedCount(env, alice) == 0);
220
221 // Pay alice almost enough to make the reserve for an NFT page.
222 env(pay(env.master, alice, incReserve + drops(baseFee - 1)));
223 env.close();
224
225 // A lambda that checks alice's ownerCount, mintedCount, and
226 // burnedCount all in one fell swoop.
227 auto const checkAliceOwnerMintedBurned =
228 [&env, this, &alice](
229 std::uint32_t owners, std::uint32_t minted, std::uint32_t burned, int line) {
230 auto const oneCheck =
231 [line, this](char const* type, std::uint32_t found, std::uint32_t exp) {
232 if (found == exp)
233 {
234 pass();
235 }
236 else
237 {
239 ss << "Wrong " << type << " count. Found: " << found
240 << "; Expected: " << exp;
241 fail(ss.str(), __FILE__, line);
242 }
243 };
244 oneCheck("owner", ownerCount(env, alice), owners);
245 oneCheck("minted", mintedCount(env, alice), minted);
246 oneCheck("burned", burnedCount(env, alice), burned);
247 };
248
249 // alice still does not have enough XRP for the reserve of an NFT
250 // page.
251 env(token::mint(alice, 0u), Ter(tecINSUFFICIENT_RESERVE));
252 env.close();
253
254 checkAliceOwnerMintedBurned(0, 0, 0, __LINE__);
255
256 // Pay alice enough to make the reserve for an NFT page.
257 env(pay(env.master, alice, drops(baseFee + 1)));
258 env.close();
259
260 // Now alice can mint an NFT.
261 env(token::mint(alice));
262 env.close();
263
264 checkAliceOwnerMintedBurned(1, 1, 0, __LINE__);
265
266 // Alice should be able to mint an additional 31 NFTs without
267 // any additional reserve requirements.
268 for (int i = 1; i < 32; ++i)
269 {
270 env(token::mint(alice));
271 checkAliceOwnerMintedBurned(1, i + 1, 0, __LINE__);
272 }
273
274 // That NFT page is full. Creating an additional NFT page requires
275 // additional reserve.
276 env(token::mint(alice), Ter(tecINSUFFICIENT_RESERVE));
277 env.close();
278 checkAliceOwnerMintedBurned(1, 32, 0, __LINE__);
279
280 // Pay alice almost enough to make the reserve for an NFT page.
281 env(pay(env.master, alice, incReserve + drops(baseFee * 33 - 1)));
282 env.close();
283
284 // alice still does not have enough XRP for the reserve of an NFT
285 // page.
286 env(token::mint(alice), Ter(tecINSUFFICIENT_RESERVE));
287 env.close();
288 checkAliceOwnerMintedBurned(1, 32, 0, __LINE__);
289
290 // Pay alice enough to make the reserve for an NFT page.
291 env(pay(env.master, alice, drops(baseFee + 1)));
292 env.close();
293
294 // Now alice can mint an NFT.
295 env(token::mint(alice));
296 env.close();
297 checkAliceOwnerMintedBurned(2, 33, 0, __LINE__);
298
299 // alice burns the NFTs she created: check that pages consolidate
300 std::uint32_t seq = 0;
301
302 while (seq < 33)
303 {
304 env(token::burn(alice, token::getID(env, alice, 0, seq++)));
305 env.close();
306 checkAliceOwnerMintedBurned(((33 - seq) != 0u) ? 1 : 0, 33, seq, __LINE__);
307 }
308
309 // alice burns a non-existent NFT.
310 env(token::burn(alice, token::getID(env, alice, 197, 5)), Ter(tecNO_ENTRY));
311 env.close();
312 checkAliceOwnerMintedBurned(0, 33, 33, __LINE__);
313
314 // That was fun! Now let's see what happens when we let someone
315 // else mint NFTs on alice's behalf. alice gives permission to
316 // minter.
317 env(token::setMinter(alice, minter));
318 env.close();
319 BEAST_EXPECT(env.le(alice)->getAccountID(sfNFTokenMinter) == minter.id());
320
321 // A lambda that checks minter's and alice's ownerCount,
322 // mintedCount, and burnedCount all in one fell swoop.
323 auto const checkMintersOwnerMintedBurned = [&env, this, &alice, &minter](
324 std::uint32_t aliceOwners,
325 std::uint32_t aliceMinted,
326 std::uint32_t aliceBurned,
327 std::uint32_t minterOwners,
328 std::uint32_t minterMinted,
329 std::uint32_t minterBurned,
330 int line) {
331 auto const oneCheck =
332 [this](char const* type, std::uint32_t found, std::uint32_t exp, int line) {
333 if (found == exp)
334 {
335 pass();
336 }
337 else
338 {
340 ss << "Wrong " << type << " count. Found: " << found
341 << "; Expected: " << exp;
342 fail(ss.str(), __FILE__, line);
343 }
344 };
345 oneCheck("alice owner", ownerCount(env, alice), aliceOwners, line);
346 oneCheck("alice minted", mintedCount(env, alice), aliceMinted, line);
347 oneCheck("alice burned", burnedCount(env, alice), aliceBurned, line);
348 oneCheck("minter owner", ownerCount(env, minter), minterOwners, line);
349 oneCheck("minter minted", mintedCount(env, minter), minterMinted, line);
350 oneCheck("minter burned", burnedCount(env, minter), minterBurned, line);
351 };
352
353 std::uint32_t nftSeq = 33;
354
355 // Pay minter almost enough to make the reserve for an NFT page.
356 env(pay(env.master, minter, incReserve - drops(1)));
357 env.close();
358 checkMintersOwnerMintedBurned(0, 33, nftSeq, 0, 0, 0, __LINE__);
359
360 // minter still does not have enough XRP for the reserve of an NFT
361 // page. Just for grins (and code coverage), minter mints NFTs that
362 // include a URI.
363 env(token::mint(minter),
364 token::Issuer(alice),
365 token::Uri("uri"),
367 env.close();
368 checkMintersOwnerMintedBurned(0, 33, nftSeq, 0, 0, 0, __LINE__);
369
370 // Pay minter enough to make the reserve for an NFT page.
371 env(pay(env.master, minter, drops(baseFee + 1)));
372 env.close();
373
374 // Now minter can mint an NFT for alice.
375 env(token::mint(minter), token::Issuer(alice), token::Uri("uri"));
376 env.close();
377 checkMintersOwnerMintedBurned(0, 34, nftSeq, 1, 0, 0, __LINE__);
378
379 // Minter should be able to mint an additional 31 NFTs for alice
380 // without any additional reserve requirements.
381 for (int i = 1; i < 32; ++i)
382 {
383 env(token::mint(minter), token::Issuer(alice), token::Uri("uri"));
384 checkMintersOwnerMintedBurned(0, i + 34, nftSeq, 1, 0, 0, __LINE__);
385 }
386
387 // Pay minter almost enough for the reserve of an additional NFT
388 // page.
389 env(pay(env.master, minter, incReserve + drops(baseFee * 32 - 1)));
390 env.close();
391
392 // That NFT page is full. Creating an additional NFT page requires
393 // additional reserve.
394 env(token::mint(minter),
395 token::Issuer(alice),
396 token::Uri("uri"),
398 env.close();
399 checkMintersOwnerMintedBurned(0, 65, nftSeq, 1, 0, 0, __LINE__);
400
401 // Pay minter enough for the reserve of an additional NFT page.
402 env(pay(env.master, minter, drops(baseFee + 1)));
403 env.close();
404
405 // Now minter can mint an NFT.
406 env(token::mint(minter), token::Issuer(alice), token::Uri("uri"));
407 env.close();
408 checkMintersOwnerMintedBurned(0, 66, nftSeq, 2, 0, 0, __LINE__);
409
410 // minter burns the NFTs she created.
411 while (nftSeq < 65)
412 {
413 env(token::burn(minter, token::getID(env, alice, 0, nftSeq++)));
414 env.close();
415 checkMintersOwnerMintedBurned(
416 0, 66, nftSeq, ((65 - seq) != 0u) ? 1 : 0, 0, 0, __LINE__);
417 }
418
419 // minter has one more NFT to burn. Should take her owner count to
420 // 0.
421 env(token::burn(minter, token::getID(env, alice, 0, nftSeq++)));
422 env.close();
423 checkMintersOwnerMintedBurned(0, 66, nftSeq, 0, 0, 0, __LINE__);
424
425 // minter burns a non-existent NFT.
426 env(token::burn(minter, token::getID(env, alice, 2009, 3)), Ter(tecNO_ENTRY));
427 env.close();
428 checkMintersOwnerMintedBurned(0, 66, nftSeq, 0, 0, 0, __LINE__);
429 }
430
431 void
433 {
434 // Make sure that an account cannot cause the sfMintedNFTokens
435 // field to wrap by minting more than 0xFFFF'FFFF tokens.
436 testcase("Mint max tokens");
437
438 using namespace test::jtx;
439
440 Account const alice{"alice"};
441 Env env{*this, features};
442 env.fund(XRP(1000), alice);
443 env.close();
444
445 // We're going to hack the ledger in order to avoid generating
446 // 4 billion or so NFTs. Because we're hacking the ledger we
447 // need alice's account to have non-zero sfMintedNFTokens and
448 // sfBurnedNFTokens fields. This prevents an exception when the
449 // AccountRoot template is applied.
450 {
451 UInt256 const nftId0{token::getNextID(env, alice, 0u)};
452 env(token::mint(alice, 0u));
453 env.close();
454
455 env(token::burn(alice, nftId0));
456 env.close();
457 }
458
459 // Note that we're bypassing almost all of the ledger's safety
460 // checks with this modify() call. If you call close() between
461 // here and the end of the test all the effort will be lost.
462 env.app().getOpenLedger().modify([&alice](OpenView& view, beast::Journal j) {
463 // Get the account root we want to hijack.
464 auto const sle = view.read(keylet::account(alice.id()));
465 if (!sle)
466 return false; // This would be really surprising!
467
468 // Just for sanity's sake we'll check that the current value
469 // of sfMintedNFTokens matches what we expect.
470 auto const replacement = std::make_shared<SLE>(*sle);
471 if (replacement->getFieldU32(sfMintedNFTokens) != 1)
472 return false; // Unexpected test conditions.
473
474 // Sequence number is generated by sfFirstNFTokenSequence +
475 // sfMintedNFTokens. We can replace the two fields with any
476 // numbers as long as they add up to the largest valid number.
477 // In our case, sfFirstNFTokenSequence is set to the largest
478 // valid number, and sfMintedNFTokens is set to zero.
479 (*replacement)[sfFirstNFTokenSequence] = 0xFFFF'FFFE;
480 (*replacement)[sfMintedNFTokens] = 0x0000'0000;
481 view.rawReplace(replacement);
482 return true;
483 });
484
485 // See whether alice is at the boundary that causes an error.
486 env(token::mint(alice, 0u), Ter(tesSUCCESS));
487 env(token::mint(alice, 0u), Ter(tecMAX_SEQUENCE_REACHED));
488 }
489
490 void
492 {
493 // Explore many of the invalid ways to mint an NFT.
494 testcase("Mint invalid");
495
496 using namespace test::jtx;
497
498 Env env{*this, features};
499 Account const alice{"alice"};
500 Account const minter{"minter"};
501
502 // Fund alice and minter enough to exist, but not enough to meet
503 // the reserve for creating their first NFT. Account reserve for unit
504 // tests is 200 XRP, not 20.
505 env.fund(XRP(200), alice, minter);
506 env.close();
507
508 env(token::mint(alice, 0u), Ter(tecINSUFFICIENT_RESERVE));
509 env.close();
510
511 // Fund alice enough to start minting NFTs.
512 env(pay(env.master, alice, XRP(1000)));
513 env.close();
514
515 //----------------------------------------------------------------------
516 // preflight
517
518 // Set a negative fee.
519 env(token::mint(alice, 0u), Fee(STAmount(10ull, true)), Ter(temBAD_FEE));
520
521 // Set an invalid flag.
522 env(token::mint(alice, 0u), Txflags(0x00008000), Ter(temINVALID_FLAG));
523
524 // Can't set a transfer fee if the NFT does not have the tfTRANSFERABLE
525 // flag set.
526 env(token::mint(alice, 0u), token::XferFee(kMaxTransferFee), Ter(temMALFORMED));
527
528 // Set a bad transfer fee.
529 env(token::mint(alice, 0u),
530 token::XferFee(kMaxTransferFee + 1),
531 Txflags(tfTransferable),
533
534 // Account can't also be issuer.
535 env(token::mint(alice, 0u), token::Issuer(alice), Ter(temMALFORMED));
536
537 // Invalid URI: zero length.
538 env(token::mint(alice, 0u), token::Uri(""), Ter(temMALFORMED));
539
540 // Invalid URI: too long.
541 env(token::mint(alice, 0u),
542 token::Uri(std::string(kMaxTokenUriLength + 1, 'q')),
543 Ter(temMALFORMED));
544
545 //----------------------------------------------------------------------
546 // preclaim
547
548 // Non-existent issuer.
549 env(token::mint(alice, 0u), token::Issuer(Account("demon")), Ter(tecNO_ISSUER));
550
551 //----------------------------------------------------------------------
552 // doApply
553
554 // Existent issuer, but not given minting permission
555 env(token::mint(minter, 0u), token::Issuer(alice), Ter(tecNO_PERMISSION));
556 }
557
558 void
560 {
561 // Explore many of the invalid ways to burn an NFT.
562 testcase("Burn invalid");
563
564 using namespace test::jtx;
565
566 Env env{*this, features};
567 Account const alice{"alice"};
568 Account const buyer{"buyer"};
569 Account const minter{"minter"};
570 Account const gw("gw");
571 IOU const gwAUD(gw["AUD"]);
572
573 // Fund alice and minter enough to exist and create an NFT, but not
574 // enough to meet the reserve for creating their first NFTOffer.
575 // Account reserve for unit tests is 200 XRP, not 20.
576 env.fund(XRP(250), alice, buyer, minter, gw);
577 env.close();
578 BEAST_EXPECT(ownerCount(env, alice) == 0);
579
580 UInt256 const nftAlice0ID = token::getNextID(env, alice, 0, tfTransferable);
581 env(token::mint(alice, 0u), Txflags(tfTransferable));
582 env.close();
583 BEAST_EXPECT(ownerCount(env, alice) == 1);
584
585 //----------------------------------------------------------------------
586 // preflight
587
588 // Set a negative fee.
589 env(token::burn(alice, nftAlice0ID), Fee(STAmount(10ull, true)), Ter(temBAD_FEE));
590 env.close();
591 BEAST_EXPECT(ownerCount(env, alice) == 1);
592
593 // Set an invalid flag.
594 env(token::burn(alice, nftAlice0ID), Txflags(0x00008000), Ter(temINVALID_FLAG));
595 env.close();
596 BEAST_EXPECT(ownerCount(env, buyer) == 0);
597
598 //----------------------------------------------------------------------
599 // preclaim
600
601 // Try to burn a token that doesn't exist.
602 env(token::burn(alice, token::getID(env, alice, 0, 1)), Ter(tecNO_ENTRY));
603 env.close();
604 BEAST_EXPECT(ownerCount(env, buyer) == 0);
605
606 // Can't burn a token with many buy or sell offers. But that is
607 // verified in testManyNftOffers().
608
609 //----------------------------------------------------------------------
610 // doApply
611 }
612
613 void
615 {
616 testcase("Invalid NFT offer create");
617
618 using namespace test::jtx;
619
620 Env env{*this, features};
621 Account const alice{"alice"};
622 Account const buyer{"buyer"};
623 Account const gw("gw");
624 IOU const gwAUD(gw["AUD"]);
625
626 // Fund alice enough to exist and create an NFT, but not
627 // enough to meet the reserve for creating their first NFTOffer.
628 // Account reserve for unit tests is 200 XRP, not 20.
629 env.fund(XRP(250), alice, buyer, gw);
630 env.close();
631 BEAST_EXPECT(ownerCount(env, alice) == 0);
632
633 UInt256 const nftAlice0ID = token::getNextID(env, alice, 0, tfTransferable, 10);
634 env(token::mint(alice, 0u), Txflags(tfTransferable), token::XferFee(10));
635 env.close();
636 BEAST_EXPECT(ownerCount(env, alice) == 1);
637
638 UInt256 const nftXrpOnlyID = token::getNextID(env, alice, 0, tfOnlyXRP | tfTransferable);
639 env(token::mint(alice, 0), Txflags(tfOnlyXRP | tfTransferable));
640 env.close();
641 BEAST_EXPECT(ownerCount(env, alice) == 1);
642
643 UInt256 const nftNoXferID = token::getNextID(env, alice, 0);
644 env(token::mint(alice, 0));
645 env.close();
646 BEAST_EXPECT(ownerCount(env, alice) == 1);
647
648 //----------------------------------------------------------------------
649 // preflight
650
651 // buyer burns a fee, so they no longer have enough XRP to cover the
652 // reserve for a token offer.
653 env(noop(buyer));
654 env.close();
655
656 // buyer tries to create an NFTokenOffer, but doesn't have the reserve.
657 env(token::createOffer(buyer, nftAlice0ID, XRP(1000)),
658 token::Owner(alice),
660 env.close();
661 BEAST_EXPECT(ownerCount(env, buyer) == 0);
662
663 // Set a negative fee.
664 env(token::createOffer(buyer, nftAlice0ID, XRP(1000)),
665 Fee(STAmount(10ull, true)),
666 Ter(temBAD_FEE));
667 env.close();
668 BEAST_EXPECT(ownerCount(env, buyer) == 0);
669
670 // Set an invalid flag.
671 env(token::createOffer(buyer, nftAlice0ID, XRP(1000)),
672 Txflags(0x00008000),
673 Ter(temINVALID_FLAG));
674 env.close();
675 BEAST_EXPECT(ownerCount(env, buyer) == 0);
676
677 // Set an invalid amount.
678 env(token::createOffer(buyer, nftXrpOnlyID, buyer["USD"](1)), Ter(temBAD_AMOUNT));
679 env(token::createOffer(buyer, nftAlice0ID, buyer["USD"](0)), Ter(temBAD_AMOUNT));
680 env(token::createOffer(buyer, nftXrpOnlyID, drops(0)), Ter(temBAD_AMOUNT));
681 env.close();
682 BEAST_EXPECT(ownerCount(env, buyer) == 0);
683
684 // Set a bad expiration.
685 env(token::createOffer(buyer, nftAlice0ID, buyer["USD"](1)),
686 token::Expiration(0),
687 Ter(temBAD_EXPIRATION));
688 env.close();
689 BEAST_EXPECT(ownerCount(env, buyer) == 0);
690
691 // Invalid Owner field and tfSellToken flag relationships.
692 // A buy offer must specify the owner.
693 env(token::createOffer(buyer, nftXrpOnlyID, XRP(1000)), Ter(temMALFORMED));
694 env.close();
695 BEAST_EXPECT(ownerCount(env, buyer) == 0);
696
697 // A sell offer must not specify the owner; the owner is implicit.
698 env(token::createOffer(alice, nftXrpOnlyID, XRP(1000)),
699 token::Owner(alice),
700 Txflags(tfSellNFToken),
701 Ter(temMALFORMED));
702 env.close();
703 BEAST_EXPECT(ownerCount(env, alice) == 1);
704
705 // An owner may not offer to buy their own token.
706 env(token::createOffer(alice, nftXrpOnlyID, XRP(1000)),
707 token::Owner(alice),
708 Ter(temMALFORMED));
709 env.close();
710 BEAST_EXPECT(ownerCount(env, alice) == 1);
711
712 // The destination may not be the account submitting the transaction.
713 env(token::createOffer(alice, nftXrpOnlyID, XRP(1000)),
714 token::Destination(alice),
715 Txflags(tfSellNFToken),
716 Ter(temMALFORMED));
717 env.close();
718 BEAST_EXPECT(ownerCount(env, alice) == 1);
719
720 // The destination must be an account already established in the ledger.
721 env(token::createOffer(alice, nftXrpOnlyID, XRP(1000)),
722 token::Destination(Account("demon")),
723 Txflags(tfSellNFToken),
724 Ter(tecNO_DST));
725 env.close();
726 BEAST_EXPECT(ownerCount(env, alice) == 1);
727
728 //----------------------------------------------------------------------
729 // preclaim
730
731 // The new NFTokenOffer may not have passed its expiration time.
732 env(token::createOffer(buyer, nftXrpOnlyID, XRP(1000)),
733 token::Owner(alice),
734 token::Expiration(lastClose(env)),
735 Ter(tecEXPIRED));
736 env.close();
737 BEAST_EXPECT(ownerCount(env, buyer) == 0);
738
739 // The nftID must be present in the ledger.
740 env(token::createOffer(buyer, token::getID(env, alice, 0, 1), XRP(1000)),
741 token::Owner(alice),
742 Ter(tecNO_ENTRY));
743 env.close();
744 BEAST_EXPECT(ownerCount(env, buyer) == 0);
745
746 // The nftID must be present in the ledger of a sell offer too.
747 env(token::createOffer(alice, token::getID(env, alice, 0, 1), XRP(1000)),
748 Txflags(tfSellNFToken),
749 Ter(tecNO_ENTRY));
750 env.close();
751 BEAST_EXPECT(ownerCount(env, buyer) == 0);
752
753 // buyer must have the funds to pay for their offer.
754 env(token::createOffer(buyer, nftAlice0ID, gwAUD(1000)),
755 token::Owner(alice),
756 Ter(tecNO_LINE));
757 env.close();
758 BEAST_EXPECT(ownerCount(env, buyer) == 0);
759
760 env(trust(buyer, gwAUD(1000)));
761 env.close();
762 BEAST_EXPECT(ownerCount(env, buyer) == 1);
763 env.close();
764
765 // Issuer (alice) must have a trust line for the offered funds.
766 env(token::createOffer(buyer, nftAlice0ID, gwAUD(1000)),
767 token::Owner(alice),
768 Ter(tecNO_LINE));
769 env.close();
770 BEAST_EXPECT(ownerCount(env, buyer) == 1);
771
772 // Give alice the needed trust line, but freeze it.
773 env(trust(gw, alice["AUD"](999), tfSetFreeze));
774 env.close();
775
776 // Issuer (alice) must have a trust line for the offered funds and
777 // the trust line may not be frozen.
778 env(token::createOffer(buyer, nftAlice0ID, gwAUD(1000)),
779 token::Owner(alice),
780 Ter(tecFROZEN));
781 env.close();
782 BEAST_EXPECT(ownerCount(env, buyer) == 1);
783
784 // Unfreeze alice's trustline.
785 env(trust(gw, alice["AUD"](999), tfClearFreeze));
786 env.close();
787
788 // Can't transfer the NFT if the transferable flag is not set.
789 env(token::createOffer(buyer, nftNoXferID, gwAUD(1000)),
790 token::Owner(alice),
792 env.close();
793 BEAST_EXPECT(ownerCount(env, buyer) == 1);
794
795 // Give buyer the needed trust line, but freeze it.
796 env(trust(gw, buyer["AUD"](999), tfSetFreeze));
797 env.close();
798
799 env(token::createOffer(buyer, nftAlice0ID, gwAUD(1000)),
800 token::Owner(alice),
801 Ter(tecFROZEN));
802 env.close();
803 BEAST_EXPECT(ownerCount(env, buyer) == 1);
804
805 // Unfreeze buyer's trust line, but buyer has no actual gwAUD.
806 // to cover the offer.
807 env(trust(gw, buyer["AUD"](999), tfClearFreeze));
808 env(trust(buyer, gwAUD(1000)));
809 env.close();
810
811 env(token::createOffer(buyer, nftAlice0ID, gwAUD(1000)),
812 token::Owner(alice),
813 Ter(tecUNFUNDED_OFFER));
814 env.close();
815 BEAST_EXPECT(ownerCount(env, buyer) == 1); // the trust line.
816
817 //----------------------------------------------------------------------
818 // doApply
819
820 // Give buyer almost enough AUD to cover the offer...
821 env(pay(gw, buyer, gwAUD(999)));
822 env.close();
823
824 // However buyer doesn't have enough XRP to cover the reserve for
825 // an NFT offer.
826 env(token::createOffer(buyer, nftAlice0ID, gwAUD(1000)),
827 token::Owner(alice),
829 env.close();
830 BEAST_EXPECT(ownerCount(env, buyer) == 1);
831
832 // Give buyer almost enough XRP to cover the reserve.
833 auto const baseFee = env.current()->fees().base;
834 env(pay(env.master, buyer, XRP(50) + drops(baseFee * 12 - 1)));
835 env.close();
836
837 env(token::createOffer(buyer, nftAlice0ID, gwAUD(1000)),
838 token::Owner(alice),
840 env.close();
841 BEAST_EXPECT(ownerCount(env, buyer) == 1);
842
843 // Give buyer just enough XRP to cover the reserve for the offer.
844 env(pay(env.master, buyer, drops(baseFee + 1)));
845 env.close();
846
847 // We don't care whether the offer is fully funded until the offer is
848 // accepted. Success at last!
849 env(token::createOffer(buyer, nftAlice0ID, gwAUD(1000)),
850 token::Owner(alice),
851 Ter(tesSUCCESS));
852 env.close();
853 BEAST_EXPECT(ownerCount(env, buyer) == 2);
854 }
855
856 void
858 {
859 testcase("Invalid NFT offer cancel");
860
861 using namespace test::jtx;
862
863 Env env{*this, features};
864 Account const alice{"alice"};
865 Account const buyer{"buyer"};
866 Account const gw("gw");
867 IOU const gwAUD(gw["AUD"]);
868
869 env.fund(XRP(1000), alice, buyer, gw);
870 env.close();
871 BEAST_EXPECT(ownerCount(env, alice) == 0);
872
873 UInt256 const nftAlice0ID = token::getNextID(env, alice, 0, tfTransferable);
874 env(token::mint(alice, 0u), Txflags(tfTransferable));
875 env.close();
876 BEAST_EXPECT(ownerCount(env, alice) == 1);
877
878 // This is the offer we'll try to cancel.
879 UInt256 const buyerOfferIndex =
880 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
881 env(token::createOffer(buyer, nftAlice0ID, XRP(1)), token::Owner(alice), Ter(tesSUCCESS));
882 env.close();
883 BEAST_EXPECT(ownerCount(env, buyer) == 1);
884
885 //----------------------------------------------------------------------
886 // preflight
887
888 // Set a negative fee.
889 env(token::cancelOffer(buyer, {buyerOfferIndex}),
890 Fee(STAmount(10ull, true)),
891 Ter(temBAD_FEE));
892 env.close();
893 BEAST_EXPECT(ownerCount(env, buyer) == 1);
894
895 // Set an invalid flag.
896 env(token::cancelOffer(buyer, {buyerOfferIndex}),
897 Txflags(0x00008000),
898 Ter(temINVALID_FLAG));
899 env.close();
900 BEAST_EXPECT(ownerCount(env, buyer) == 1);
901
902 // Empty list of tokens to delete.
903 {
904 json::Value jv = token::cancelOffer(buyer);
905 jv[sfNFTokenOffers.jsonName] = json::ValueType::Array;
906 env(jv, Ter(temMALFORMED));
907 env.close();
908 BEAST_EXPECT(ownerCount(env, buyer) == 1);
909 }
910
911 // Only test this with fixCleanup3_2_0 enabled. Without the fix,
912 // an assert-enabled build can crash when Ledger::read() receives
913 // a zero-key offer ID.
914 if (features[fixCleanup3_2_0])
915 {
916 // Zero is not a valid offer ID.
917 env(token::cancelOffer(buyer, {UInt256{}}), Ter(temMALFORMED));
918 env.close();
919 BEAST_EXPECT(ownerCount(env, buyer) == 1);
920
921 // List of offer IDs containing zero is invalid.
922 // craftedIndex is not a valid offer index but it is not zero.
923 auto const craftedIndex =
925 env(token::cancelOffer(buyer, {buyerOfferIndex, UInt256{}, craftedIndex}),
926 Ter(temMALFORMED));
927 env.close();
928 BEAST_EXPECT(ownerCount(env, buyer) == 1);
929 }
930
931 // List of tokens to delete is too long.
932 {
933 std::vector<UInt256> const offers(kMaxTokenOfferCancelCount + 1, buyerOfferIndex);
934
935 env(token::cancelOffer(buyer, offers), Ter(temMALFORMED));
936 env.close();
937 BEAST_EXPECT(ownerCount(env, buyer) == 1);
938 }
939
940 // Duplicate entries are not allowed in the list of offers to cancel.
941 env(token::cancelOffer(buyer, {buyerOfferIndex, buyerOfferIndex}), Ter(temMALFORMED));
942 env.close();
943 BEAST_EXPECT(ownerCount(env, buyer) == 1);
944
945 // Provide neither offers to cancel nor a root index.
946 env(token::cancelOffer(buyer), Ter(temMALFORMED));
947 env.close();
948 BEAST_EXPECT(ownerCount(env, buyer) == 1);
949
950 //----------------------------------------------------------------------
951 // preclaim
952
953 // Make a non-root directory that we can pass as a root index.
954 env(pay(env.master, gw, XRP(5000)));
955 env.close();
956 for (std::uint32_t i = 1; i < 34; ++i)
957 {
958 env(offer(gw, XRP(i), gwAUD(1)));
959 env.close();
960 }
961
962 {
963 // gw attempts to cancel a Check as through it is an NFTokenOffer.
964 auto const gwCheckId = keylet::check(gw, SeqProxy::rawSequence(env.seq(gw))).key;
965 env(check::create(gw, env.master, XRP(300)));
966 env.close();
967
968 env(token::cancelOffer(gw, {gwCheckId}), Ter(tecNO_PERMISSION));
969 env.close();
970
971 // Cancel the check so it doesn't mess up later tests.
972 env(check::cancel(gw, gwCheckId));
973 env.close();
974 }
975
976 // gw attempts to cancel an offer they don't have permission to cancel.
977 env(token::cancelOffer(gw, {buyerOfferIndex}), Ter(tecNO_PERMISSION));
978 env.close();
979 BEAST_EXPECT(ownerCount(env, buyer) == 1);
980
981 //----------------------------------------------------------------------
982 // doApply
983 //
984 // The tefBAD_LEDGER conditions are too hard to test.
985 // But let's see a successful offer cancel.
986 env(token::cancelOffer(buyer, {buyerOfferIndex}));
987 env.close();
988 BEAST_EXPECT(ownerCount(env, buyer) == 0);
989 }
990
991 void
993 {
994 testcase("Invalid NFT offer accept");
995
996 using namespace test::jtx;
997
998 Env env{*this, features};
999 Account const alice{"alice"};
1000 Account const buyer{"buyer"};
1001 Account const gw("gw");
1002 IOU const gwAUD(gw["AUD"]);
1003
1004 env.fund(XRP(1000), alice, buyer, gw);
1005 env.close();
1006 BEAST_EXPECT(ownerCount(env, alice) == 0);
1007 BEAST_EXPECT(ownerCount(env, buyer) == 0);
1008
1009 UInt256 const nftAlice0ID = token::getNextID(env, alice, 0, tfTransferable);
1010 env(token::mint(alice, 0u), Txflags(tfTransferable));
1011 env.close();
1012 uint8_t aliceCount = 1;
1013 BEAST_EXPECT(ownerCount(env, alice) == aliceCount);
1014
1015 UInt256 const nftXrpOnlyID = token::getNextID(env, alice, 0, tfOnlyXRP | tfTransferable);
1016 env(token::mint(alice, 0), Txflags(tfOnlyXRP | tfTransferable));
1017 env.close();
1018 BEAST_EXPECT(ownerCount(env, alice) == aliceCount);
1019
1020 UInt256 const nftNoXferID = token::getNextID(env, alice, 0);
1021 env(token::mint(alice, 0));
1022 env.close();
1023 BEAST_EXPECT(ownerCount(env, alice) == aliceCount);
1024
1025 // alice creates sell offers for her nfts.
1026 UInt256 const plainOfferIndex =
1027 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
1028 env(token::createOffer(alice, nftAlice0ID, XRP(10)), Txflags(tfSellNFToken));
1029 env.close();
1030 aliceCount++;
1031 BEAST_EXPECT(ownerCount(env, alice) == aliceCount);
1032
1033 UInt256 const audOfferIndex =
1034 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
1035 env(token::createOffer(alice, nftAlice0ID, gwAUD(30)), Txflags(tfSellNFToken));
1036 env.close();
1037 aliceCount++;
1038 BEAST_EXPECT(ownerCount(env, alice) == aliceCount);
1039
1040 UInt256 const xrpOnlyOfferIndex =
1041 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
1042 env(token::createOffer(alice, nftXrpOnlyID, XRP(20)), Txflags(tfSellNFToken));
1043 env.close();
1044 aliceCount++;
1045 BEAST_EXPECT(ownerCount(env, alice) == aliceCount);
1046
1047 UInt256 const noXferOfferIndex =
1048 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
1049 env(token::createOffer(alice, nftNoXferID, XRP(30)), Txflags(tfSellNFToken));
1050 env.close();
1051 aliceCount++;
1052 BEAST_EXPECT(ownerCount(env, alice) == aliceCount);
1053
1054 // alice creates a sell offer that will expire soon.
1055 UInt256 const aliceExpOfferIndex =
1056 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
1057 env(token::createOffer(alice, nftNoXferID, XRP(40)),
1058 Txflags(tfSellNFToken),
1059 token::Expiration(lastClose(env) + 5));
1060 env.close();
1061 aliceCount++;
1062 BEAST_EXPECT(ownerCount(env, alice) == aliceCount);
1063
1064 // buyer creates a Buy offer that will expire soon.
1065 UInt256 const buyerExpOfferIndex =
1066 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
1067 env(token::createOffer(buyer, nftAlice0ID, XRP(40)),
1068 token::Owner(alice),
1069 token::Expiration(lastClose(env) + 5));
1070 env.close();
1071 uint8_t buyerCount = 1;
1072 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1073
1074 //----------------------------------------------------------------------
1075 // preflight
1076
1077 // Set a negative fee.
1078 env(token::acceptSellOffer(buyer, noXferOfferIndex),
1079 Fee(STAmount(10ull, true)),
1080 Ter(temBAD_FEE));
1081 env.close();
1082 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1083
1084 // Set an invalid flag.
1085 env(token::acceptSellOffer(buyer, noXferOfferIndex),
1086 Txflags(0x00008000),
1087 Ter(temINVALID_FLAG));
1088 env.close();
1089 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1090
1091 // Supply nether an sfNFTokenBuyOffer nor an sfNFTokenSellOffer field.
1092 {
1093 json::Value jv = token::acceptSellOffer(buyer, noXferOfferIndex);
1094 jv.removeMember(sfNFTokenSellOffer.jsonName);
1095 env(jv, Ter(temMALFORMED));
1096 env.close();
1097 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1098 }
1099
1100 // A buy offer may not contain a sfNFTokenBrokerFee field.
1101 {
1102 json::Value jv = token::acceptBuyOffer(buyer, noXferOfferIndex);
1103 jv[sfNFTokenBrokerFee.jsonName] = STAmount(500000).getJson(JsonOptions::Values::None);
1104 env(jv, Ter(temMALFORMED));
1105 env.close();
1106 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1107 }
1108
1109 // A sell offer may not contain a sfNFTokenBrokerFee field.
1110 {
1111 json::Value jv = token::acceptSellOffer(buyer, noXferOfferIndex);
1112 jv[sfNFTokenBrokerFee.jsonName] = STAmount(500000).getJson(JsonOptions::Values::None);
1113 env(jv, Ter(temMALFORMED));
1114 env.close();
1115 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1116 }
1117
1118 // A brokered offer may not contain a negative or zero brokerFee.
1119 env(token::brokerOffers(buyer, noXferOfferIndex, xrpOnlyOfferIndex),
1120 token::BrokerFee(gwAUD(0)),
1121 Ter(temMALFORMED));
1122 env.close();
1123 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1124
1125 //----------------------------------------------------------------------
1126 // preclaim
1127
1128 // The buy offer must be non-zero.
1129 env(token::acceptBuyOffer(buyer, beast::kZero), Ter(tecOBJECT_NOT_FOUND));
1130 env.close();
1131 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1132
1133 // The buy offer must be present in the ledger.
1134 UInt256 const missingOfferIndex = keylet::nftokenOffer(alice, SeqProxy::rawSequence(1)).key;
1135 env(token::acceptBuyOffer(buyer, missingOfferIndex), Ter(tecOBJECT_NOT_FOUND));
1136 env.close();
1137 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1138
1139 // The buy offer must not have expired.
1140 // NOTE: this is only a preclaim check with the fixCleanup3_1_3 amendment disabled.
1141 env(token::acceptBuyOffer(alice, buyerExpOfferIndex), Ter(tecEXPIRED));
1142 env.close();
1143 if (features[fixCleanup3_1_3])
1144 {
1145 buyerCount--;
1146 BEAST_EXPECT(!env.closed()->exists(keylet::nftokenOffer(buyerExpOfferIndex)));
1147 }
1148 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1149
1150 // The sell offer must be non-zero.
1151 env(token::acceptSellOffer(buyer, beast::kZero), Ter(tecOBJECT_NOT_FOUND));
1152 env.close();
1153 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1154
1155 // The sell offer must be present in the ledger.
1156 env(token::acceptSellOffer(buyer, missingOfferIndex), Ter(tecOBJECT_NOT_FOUND));
1157 env.close();
1158 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1159
1160 // The sell offer must not have expired.
1161 // NOTE: this is only a preclaim check with the fixCleanup3_1_3 amendment disabled.
1162 env(token::acceptSellOffer(buyer, aliceExpOfferIndex), Ter(tecEXPIRED));
1163
1164 env.close();
1165 // Alice's count is decremented by one when the expired offer is
1166 // removed.
1167 if (features[fixCleanup3_1_3])
1168 {
1169 aliceCount--;
1170 BEAST_EXPECT(!env.closed()->exists(keylet::nftokenOffer(aliceExpOfferIndex)));
1171 }
1172 BEAST_EXPECT(ownerCount(env, alice) == aliceCount);
1173 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1174
1175 //----------------------------------------------------------------------
1176 // preclaim brokered
1177
1178 // alice and buyer need trustlines before buyer can to create an
1179 // offer for gwAUD.
1180 env(trust(alice, gwAUD(1000)));
1181 env(trust(buyer, gwAUD(1000)));
1182 env.close();
1183 env(pay(gw, buyer, gwAUD(30)));
1184 env.close();
1185 aliceCount++;
1186 buyerCount++;
1187 BEAST_EXPECT(ownerCount(env, alice) == aliceCount);
1188 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1189
1190 BEAST_EXPECT(ownerCount(env, alice) == aliceCount);
1191 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1192
1193 // We're about to exercise offer brokering, so we need
1194 // corresponding buy and sell offers.
1195 {
1196 // buyer creates a buy offer for one of alice's nfts.
1197 UInt256 const buyerOfferIndex =
1198 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
1199 env(token::createOffer(buyer, nftAlice0ID, gwAUD(29)), token::Owner(alice));
1200 env.close();
1201 buyerCount++;
1202 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1203
1204 // gw attempts to broker offers that are not for the same token.
1205 env(token::brokerOffers(gw, buyerOfferIndex, xrpOnlyOfferIndex),
1207 env.close();
1208 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1209
1210 // gw attempts to broker offers that are not for the same currency.
1211 env(token::brokerOffers(gw, buyerOfferIndex, plainOfferIndex),
1213 env.close();
1214 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1215
1216 // In a brokered offer, the buyer must offer greater than or
1217 // equal to the selling price.
1218 env(token::brokerOffers(gw, buyerOfferIndex, audOfferIndex),
1220 env.close();
1221 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1222
1223 // Remove buyer's offer.
1224 env(token::cancelOffer(buyer, {buyerOfferIndex}));
1225 env.close();
1226 buyerCount--;
1227 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1228 }
1229 {
1230 // buyer creates a buy offer for one of alice's nfts.
1231 UInt256 const buyerOfferIndex =
1232 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
1233 env(token::createOffer(buyer, nftAlice0ID, gwAUD(31)), token::Owner(alice));
1234 env.close();
1235 buyerCount++;
1236 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1237
1238 // Broker sets their fee in a denomination other than the one
1239 // used by the offers
1240 env(token::brokerOffers(gw, buyerOfferIndex, audOfferIndex),
1241 token::BrokerFee(XRP(40)),
1243 env.close();
1244 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1245
1246 // Broker fee way too big.
1247 env(token::brokerOffers(gw, buyerOfferIndex, audOfferIndex),
1248 token::BrokerFee(gwAUD(31)),
1250 env.close();
1251 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1252
1253 // Broker fee is smaller, but still too big once the offer
1254 // seller's minimum is taken into account.
1255 env(token::brokerOffers(gw, buyerOfferIndex, audOfferIndex),
1256 token::BrokerFee(gwAUD(1.5)),
1258 env.close();
1259 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1260
1261 // Remove buyer's offer.
1262 env(token::cancelOffer(buyer, {buyerOfferIndex}));
1263 env.close();
1264 buyerCount--;
1265 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1266 }
1267 //----------------------------------------------------------------------
1268 // preclaim buy
1269 {
1270 // buyer creates a buy offer for one of alice's nfts.
1271 UInt256 const buyerOfferIndex =
1272 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
1273 env(token::createOffer(buyer, nftAlice0ID, gwAUD(30)), token::Owner(alice));
1274 env.close();
1275 buyerCount++;
1276 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1277
1278 // Don't accept a buy offer if the sell flag is set.
1279 env(token::acceptBuyOffer(buyer, plainOfferIndex), Ter(tecNFTOKEN_OFFER_TYPE_MISMATCH));
1280 env.close();
1281 BEAST_EXPECT(ownerCount(env, alice) == aliceCount);
1282
1283 // An account can't accept its own offer.
1284 env(token::acceptBuyOffer(buyer, buyerOfferIndex),
1286 env.close();
1287 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1288
1289 // An offer acceptor must have enough funds to pay for the offer.
1290 env(pay(buyer, gw, gwAUD(30)));
1291 env.close();
1292 BEAST_EXPECT(env.balance(buyer, gwAUD) == gwAUD(0));
1293 env(token::acceptBuyOffer(alice, buyerOfferIndex), Ter(tecINSUFFICIENT_FUNDS));
1294 env.close();
1295 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1296
1297 // alice gives her NFT to gw, so alice no longer owns nftAlice0.
1298 {
1299 UInt256 const offerIndex =
1300 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
1301 env(token::createOffer(alice, nftAlice0ID, XRP(0)), Txflags(tfSellNFToken));
1302 env.close();
1303 env(token::acceptSellOffer(gw, offerIndex));
1304 env.close();
1305 BEAST_EXPECT(ownerCount(env, alice) == aliceCount);
1306 }
1307 env(pay(gw, buyer, gwAUD(30)));
1308 env.close();
1309
1310 // alice can't accept a buy offer for an NFT she no longer owns.
1311 env(token::acceptBuyOffer(alice, buyerOfferIndex), Ter(tecNO_PERMISSION));
1312 env.close();
1313 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1314
1315 // Remove buyer's offer.
1316 env(token::cancelOffer(buyer, {buyerOfferIndex}));
1317 env.close();
1318 buyerCount--;
1319 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1320 }
1321 //----------------------------------------------------------------------
1322 // preclaim sell
1323 {
1324 // buyer creates a buy offer for one of alice's nfts.
1325 UInt256 const buyerOfferIndex =
1326 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
1327 env(token::createOffer(buyer, nftXrpOnlyID, XRP(30)), token::Owner(alice));
1328 env.close();
1329 buyerCount++;
1330 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1331
1332 // Don't accept a sell offer without the sell flag set.
1333 env(token::acceptSellOffer(alice, buyerOfferIndex),
1335 env.close();
1336 BEAST_EXPECT(ownerCount(env, alice) == aliceCount);
1337
1338 // An account can't accept its own offer.
1339 env(token::acceptSellOffer(alice, plainOfferIndex),
1341 env.close();
1342 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1343
1344 // The seller must currently be in possession of the token they
1345 // are selling. alice gave nftAlice0ID to gw.
1346 env(token::acceptSellOffer(buyer, plainOfferIndex), Ter(tecNO_PERMISSION));
1347 env.close();
1348 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1349
1350 // gw gives nftAlice0ID back to alice. That allows us to check
1351 // buyer attempting to accept one of alice's offers with
1352 // insufficient funds.
1353 {
1354 UInt256 const offerIndex =
1356 env(token::createOffer(gw, nftAlice0ID, XRP(0)), Txflags(tfSellNFToken));
1357 env.close();
1358 env(token::acceptSellOffer(alice, offerIndex));
1359 env.close();
1360 BEAST_EXPECT(ownerCount(env, alice) == aliceCount);
1361 }
1362 env(pay(buyer, gw, gwAUD(30)));
1363 env.close();
1364 BEAST_EXPECT(env.balance(buyer, gwAUD) == gwAUD(0));
1365 env(token::acceptSellOffer(buyer, audOfferIndex), Ter(tecINSUFFICIENT_FUNDS));
1366 env.close();
1367 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
1368 }
1369
1370 //----------------------------------------------------------------------
1371 // doApply
1372 //
1373 // As far as I can see kNone of the failure modes are accessible as
1374 // long as the preflight and preclaim conditions are met.
1375 }
1376
1377 void
1379 {
1380 // Exercise NFTs with flagBurnable set and not set.
1381 testcase("Mint flagBurnable");
1382
1383 using namespace test::jtx;
1384
1385 Env env{*this, features};
1386 Account const alice{"alice"};
1387 Account const buyer{"buyer"};
1388 Account const minter1{"minter1"};
1389 Account const minter2{"minter2"};
1390
1391 env.fund(XRP(1000), alice, buyer, minter1, minter2);
1392 env.close();
1393 BEAST_EXPECT(ownerCount(env, alice) == 0);
1394
1395 // alice selects minter as her minter.
1396 env(token::setMinter(alice, minter1));
1397 env.close();
1398
1399 // A lambda that...
1400 // 1. creates an alice nft
1401 // 2. minted by minter and
1402 // 3. transfers that nft to buyer.
1403 auto const nftToBuyer = [&env, &alice, &minter1, &buyer](std::uint32_t flags) {
1404 UInt256 const nftID{token::getNextID(env, alice, 0u, flags)};
1405 env(token::mint(minter1, 0u), token::Issuer(alice), Txflags(flags));
1406 env.close();
1407
1408 UInt256 const offerIndex =
1409 keylet::nftokenOffer(minter1, SeqProxy::rawSequence(env.seq(minter1))).key;
1410 env(token::createOffer(minter1, nftID, XRP(0)), Txflags(tfSellNFToken));
1411 env.close();
1412
1413 env(token::acceptSellOffer(buyer, offerIndex));
1414 env.close();
1415
1416 return nftID;
1417 };
1418
1419 // An NFT without flagBurnable can only be burned by its owner.
1420 {
1421 UInt256 const noBurnID = nftToBuyer(0);
1422 env(token::burn(alice, noBurnID), token::Owner(buyer), Ter(tecNO_PERMISSION));
1423 env.close();
1424 env(token::burn(minter1, noBurnID), token::Owner(buyer), Ter(tecNO_PERMISSION));
1425 env.close();
1426 env(token::burn(minter2, noBurnID), token::Owner(buyer), Ter(tecNO_PERMISSION));
1427 env.close();
1428
1429 BEAST_EXPECT(ownerCount(env, buyer) == 1);
1430 env(token::burn(buyer, noBurnID), token::Owner(buyer));
1431 env.close();
1432 BEAST_EXPECT(ownerCount(env, buyer) == 0);
1433 }
1434 // An NFT with flagBurnable can be burned by the issuer.
1435 {
1436 UInt256 const burnableID = nftToBuyer(tfBurnable);
1437 env(token::burn(minter2, burnableID), token::Owner(buyer), Ter(tecNO_PERMISSION));
1438 env.close();
1439
1440 BEAST_EXPECT(ownerCount(env, buyer) == 1);
1441 env(token::burn(alice, burnableID), token::Owner(buyer));
1442 env.close();
1443 BEAST_EXPECT(ownerCount(env, buyer) == 0);
1444 }
1445 // An NFT with flagBurnable can be burned by the owner.
1446 {
1447 UInt256 const burnableID = nftToBuyer(tfBurnable);
1448 BEAST_EXPECT(ownerCount(env, buyer) == 1);
1449 env(token::burn(buyer, burnableID));
1450 env.close();
1451 BEAST_EXPECT(ownerCount(env, buyer) == 0);
1452 }
1453 // An NFT with flagBurnable can be burned by the minter.
1454 {
1455 UInt256 const burnableID = nftToBuyer(tfBurnable);
1456 BEAST_EXPECT(ownerCount(env, buyer) == 1);
1457 env(token::burn(buyer, burnableID), token::Owner(buyer));
1458 env.close();
1459 BEAST_EXPECT(ownerCount(env, buyer) == 0);
1460 }
1461 // An nft with flagBurnable may be burned by the issuers' minter,
1462 // who may not be the original minter.
1463 {
1464 UInt256 const burnableID = nftToBuyer(tfBurnable);
1465 BEAST_EXPECT(ownerCount(env, buyer) == 1);
1466
1467 env(token::setMinter(alice, minter2));
1468 env.close();
1469
1470 // minter1 is no longer alice's minter, so no longer has
1471 // permission to burn alice's nfts.
1472 env(token::burn(minter1, burnableID), token::Owner(buyer), Ter(tecNO_PERMISSION));
1473 env.close();
1474 BEAST_EXPECT(ownerCount(env, buyer) == 1);
1475
1476 // minter2, however, can burn alice's nfts.
1477 env(token::burn(minter2, burnableID), token::Owner(buyer));
1478 env.close();
1479 BEAST_EXPECT(ownerCount(env, buyer) == 0);
1480 }
1481 }
1482
1483 void
1485 {
1486 // Exercise NFTs with flagOnlyXRP set and not set.
1487 testcase("Mint flagOnlyXRP");
1488
1489 using namespace test::jtx;
1490
1491 Env env{*this, features};
1492 Account const alice{"alice"};
1493 Account const buyer{"buyer"};
1494 Account const gw("gw");
1495 IOU const gwAUD(gw["AUD"]);
1496
1497 // Set trust lines so alice and buyer can use gwAUD.
1498 env.fund(XRP(1000), alice, buyer, gw);
1499 env.close();
1500 env(trust(alice, gwAUD(1000)));
1501 env(trust(buyer, gwAUD(1000)));
1502 env.close();
1503 env(pay(gw, buyer, gwAUD(100)));
1504
1505 // Don't set flagOnlyXRP and offers can be made with IOUs.
1506 {
1507 UInt256 const nftIOUsOkayID{token::getNextID(env, alice, 0u, tfTransferable)};
1508 env(token::mint(alice, 0u), Txflags(tfTransferable));
1509 env.close();
1510
1511 BEAST_EXPECT(ownerCount(env, alice) == 2);
1512 UInt256 const aliceOfferIndex =
1513 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
1514 env(token::createOffer(alice, nftIOUsOkayID, gwAUD(50)), Txflags(tfSellNFToken));
1515 env.close();
1516 BEAST_EXPECT(ownerCount(env, alice) == 3);
1517
1518 BEAST_EXPECT(ownerCount(env, buyer) == 1);
1519 UInt256 const buyerOfferIndex =
1520 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
1521 env(token::createOffer(buyer, nftIOUsOkayID, gwAUD(50)), token::Owner(alice));
1522 env.close();
1523 BEAST_EXPECT(ownerCount(env, buyer) == 2);
1524
1525 // Cancel the two offers just to be tidy.
1526 env(token::cancelOffer(alice, {aliceOfferIndex}));
1527 env(token::cancelOffer(buyer, {buyerOfferIndex}));
1528 env.close();
1529 BEAST_EXPECT(ownerCount(env, alice) == 2);
1530 BEAST_EXPECT(ownerCount(env, buyer) == 1);
1531
1532 // Also burn alice's nft.
1533 env(token::burn(alice, nftIOUsOkayID));
1534 env.close();
1535 BEAST_EXPECT(ownerCount(env, alice) == 1);
1536 }
1537
1538 // Set flagOnlyXRP and offers using IOUs are rejected.
1539 {
1540 UInt256 const nftOnlyXrpID{
1541 token::getNextID(env, alice, 0u, tfOnlyXRP | tfTransferable),
1542 };
1543 env(token::mint(alice, 0u), Txflags(tfOnlyXRP | tfTransferable));
1544 env.close();
1545
1546 BEAST_EXPECT(ownerCount(env, alice) == 2);
1547 env(token::createOffer(alice, nftOnlyXrpID, gwAUD(50)),
1548 Txflags(tfSellNFToken),
1549 Ter(temBAD_AMOUNT));
1550 env.close();
1551 BEAST_EXPECT(ownerCount(env, alice) == 2);
1552
1553 BEAST_EXPECT(ownerCount(env, buyer) == 1);
1554 env(token::createOffer(buyer, nftOnlyXrpID, gwAUD(50)),
1555 token::Owner(alice),
1556 Ter(temBAD_AMOUNT));
1557 env.close();
1558 BEAST_EXPECT(ownerCount(env, buyer) == 1);
1559
1560 // However offers for XRP are okay.
1561 BEAST_EXPECT(ownerCount(env, alice) == 2);
1562 env(token::createOffer(alice, nftOnlyXrpID, XRP(60)), Txflags(tfSellNFToken));
1563 env.close();
1564 BEAST_EXPECT(ownerCount(env, alice) == 3);
1565
1566 BEAST_EXPECT(ownerCount(env, buyer) == 1);
1567 env(token::createOffer(buyer, nftOnlyXrpID, XRP(60)), token::Owner(alice));
1568 env.close();
1569 BEAST_EXPECT(ownerCount(env, buyer) == 2);
1570 }
1571 }
1572
1573 void
1575 {
1576 // Exercise NFTs with flagCreateTrustLines set and not set.
1577 testcase("Mint flagCreateTrustLines");
1578
1579 using namespace test::jtx;
1580
1581 Account const alice{"alice"};
1582 Account const becky{"becky"};
1583 Account const cheri{"cheri"};
1584 Account const gw("gw");
1585 IOU const gwAUD(gw["AUD"]);
1586 IOU const gwCAD(gw["CAD"]);
1587 IOU const gwEUR(gw["EUR"]);
1588
1589 // The behavior of this test changes dramatically based on the
1590 // presence (or absence) of the fixRemoveNFTokenAutoTrustLine
1591 // amendment. So we test both cases here.
1592 for (auto const& tweakedFeatures :
1593 {features - fixRemoveNFTokenAutoTrustLine, features | fixRemoveNFTokenAutoTrustLine})
1594 {
1595 Env env{*this, tweakedFeatures};
1596 env.fund(XRP(1000), alice, becky, cheri, gw);
1597 env.close();
1598
1599 // Set trust lines so becky and cheri can use gw's currency.
1600 env(trust(becky, gwAUD(1000)));
1601 env(trust(cheri, gwAUD(1000)));
1602 env(trust(becky, gwCAD(1000)));
1603 env(trust(cheri, gwCAD(1000)));
1604 env(trust(becky, gwEUR(1000)));
1605 env(trust(cheri, gwEUR(1000)));
1606 env.close();
1607 env(pay(gw, becky, gwAUD(500)));
1608 env(pay(gw, becky, gwCAD(500)));
1609 env(pay(gw, becky, gwEUR(500)));
1610 env(pay(gw, cheri, gwAUD(500)));
1611 env(pay(gw, cheri, gwCAD(500)));
1612 env.close();
1613
1614 // An nft without flagCreateTrustLines but with a non-zero transfer
1615 // fee will not allow creating offers that use IOUs for payment.
1616 for (std::uint32_t const xferFee : {0, 1})
1617 {
1618 UInt256 const nftNoAutoTrustID{
1619 token::getNextID(env, alice, 0u, tfTransferable, xferFee),
1620 };
1621 env(token::mint(alice, 0u), token::XferFee(xferFee), Txflags(tfTransferable));
1622 env.close();
1623
1624 // becky buys the nft for 1 drop.
1625 UInt256 const beckyBuyOfferIndex =
1626 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
1627 env(token::createOffer(becky, nftNoAutoTrustID, drops(1)), token::Owner(alice));
1628 env.close();
1629 env(token::acceptBuyOffer(alice, beckyBuyOfferIndex));
1630 env.close();
1631
1632 // becky attempts to sell the nft for AUD.
1633 TER const createOfferTER = (xferFee != 0u) ? TER(tecNO_LINE) : TER(tesSUCCESS);
1634 UInt256 const beckyOfferIndex =
1635 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
1636 env(token::createOffer(becky, nftNoAutoTrustID, gwAUD(100)),
1637 Txflags(tfSellNFToken),
1638 Ter(createOfferTER));
1639 env.close();
1640
1641 // cheri offers to buy the nft for CAD.
1642 UInt256 const cheriOfferIndex =
1643 keylet::nftokenOffer(cheri, SeqProxy::rawSequence(env.seq(cheri))).key;
1644 env(token::createOffer(cheri, nftNoAutoTrustID, gwCAD(100)),
1645 token::Owner(becky),
1646 Ter(createOfferTER));
1647 env.close();
1648
1649 // To keep things tidy, cancel the offers.
1650 env(token::cancelOffer(becky, {beckyOfferIndex}));
1651 env(token::cancelOffer(cheri, {cheriOfferIndex}));
1652 env.close();
1653 }
1654 // An nft with flagCreateTrustLines but with a non-zero transfer
1655 // fee allows transfers using IOUs for payment.
1656 {
1657 std::uint16_t const transferFee = 10000; // 10%
1658
1659 UInt256 const nftAutoTrustID{
1660 token::getNextID(env, alice, 0u, tfTransferable | tfTrustLine, transferFee),
1661 };
1662
1663 // If the fixRemoveNFTokenAutoTrustLine amendment is active
1664 // then this transaction fails.
1665 {
1666 TER const mintTER = tweakedFeatures[fixRemoveNFTokenAutoTrustLine]
1667 ? static_cast<TER>(temINVALID_FLAG)
1668 : static_cast<TER>(tesSUCCESS);
1669
1670 env(token::mint(alice, 0u),
1671 token::XferFee(transferFee),
1672 Txflags(tfTransferable | tfTrustLine),
1673 Ter(mintTER));
1674 env.close();
1675
1676 // If fixRemoveNFTokenAutoTrustLine is active the rest
1677 // of this test falls on its face.
1678 if (tweakedFeatures[fixRemoveNFTokenAutoTrustLine])
1679 break;
1680 }
1681 // becky buys the nft for 1 drop.
1682 UInt256 const beckyBuyOfferIndex =
1683 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
1684 env(token::createOffer(becky, nftAutoTrustID, drops(1)), token::Owner(alice));
1685 env.close();
1686 env(token::acceptBuyOffer(alice, beckyBuyOfferIndex));
1687 env.close();
1688
1689 // becky sells the nft for AUD.
1690 UInt256 const beckySellOfferIndex =
1691 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
1692 env(token::createOffer(becky, nftAutoTrustID, gwAUD(100)), Txflags(tfSellNFToken));
1693 env.close();
1694 env(token::acceptSellOffer(cheri, beckySellOfferIndex));
1695 env.close();
1696
1697 // alice should now have a trust line for gwAUD.
1698 BEAST_EXPECT(env.balance(alice, gwAUD) == gwAUD(10));
1699
1700 // becky buys the nft back for CAD.
1701 UInt256 const beckyBuyBackOfferIndex =
1702 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
1703 env(token::createOffer(becky, nftAutoTrustID, gwCAD(50)), token::Owner(cheri));
1704 env.close();
1705 env(token::acceptBuyOffer(cheri, beckyBuyBackOfferIndex));
1706 env.close();
1707
1708 // alice should now have a trust line for gwAUD and gwCAD.
1709 BEAST_EXPECT(env.balance(alice, gwAUD) == gwAUD(10));
1710 BEAST_EXPECT(env.balance(alice, gwCAD) == gwCAD(5));
1711 }
1712 // Now that alice has trust lines preestablished, an nft without
1713 // flagCreateTrustLines will work for preestablished trust lines.
1714 {
1715 std::uint16_t const transferFee = 5000; // 5%
1716 UInt256 const nftNoAutoTrustID{
1717 token::getNextID(env, alice, 0u, tfTransferable, transferFee),
1718 };
1719 env(token::mint(alice, 0u), token::XferFee(transferFee), Txflags(tfTransferable));
1720 env.close();
1721
1722 // alice sells the nft using AUD.
1723 UInt256 const aliceSellOfferIndex =
1724 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
1725 env(token::createOffer(alice, nftNoAutoTrustID, gwAUD(200)),
1726 Txflags(tfSellNFToken));
1727 env.close();
1728 env(token::acceptSellOffer(cheri, aliceSellOfferIndex));
1729 env.close();
1730
1731 // alice should now have AUD(210):
1732 // o 200 for this sale and
1733 // o 10 for the previous sale's fee.
1734 BEAST_EXPECT(env.balance(alice, gwAUD) == gwAUD(210));
1735
1736 // cheri can't sell the NFT for EUR, but can for CAD.
1737 env(token::createOffer(cheri, nftNoAutoTrustID, gwEUR(50)),
1738 Txflags(tfSellNFToken),
1739 Ter(tecNO_LINE));
1740 env.close();
1741 UInt256 const cheriSellOfferIndex =
1742 keylet::nftokenOffer(cheri, SeqProxy::rawSequence(env.seq(cheri))).key;
1743 env(token::createOffer(cheri, nftNoAutoTrustID, gwCAD(100)),
1744 Txflags(tfSellNFToken));
1745 env.close();
1746 env(token::acceptSellOffer(becky, cheriSellOfferIndex));
1747 env.close();
1748
1749 // alice should now have CAD(10):
1750 // o 5 from this sale's fee and
1751 // o 5 for the previous sale's fee.
1752 BEAST_EXPECT(env.balance(alice, gwCAD) == gwCAD(10));
1753 }
1754 }
1755 }
1756
1757 void
1759 {
1760 // Exercise NFTs with flagTransferable set and not set.
1761 testcase("Mint flagTransferable");
1762
1763 using namespace test::jtx;
1764
1765 Env env{*this, features};
1766
1767 Account const alice{"alice"};
1768 Account const becky{"becky"};
1769 Account const minter{"minter"};
1770
1771 env.fund(XRP(1000), alice, becky, minter);
1772 env.close();
1773
1774 // First try an nft made by alice without flagTransferable set.
1775 {
1776 BEAST_EXPECT(ownerCount(env, alice) == 0);
1777 UInt256 const nftAliceNoTransferID{token::getNextID(env, alice, 0u)};
1778 env(token::mint(alice, 0u), token::XferFee(0));
1779 env.close();
1780 BEAST_EXPECT(ownerCount(env, alice) == 1);
1781
1782 // becky tries to offer to buy alice's nft.
1783 BEAST_EXPECT(ownerCount(env, becky) == 0);
1784 env(token::createOffer(becky, nftAliceNoTransferID, XRP(20)),
1785 token::Owner(alice),
1787
1788 // alice offers to sell the nft and becky accepts the offer.
1789 UInt256 const aliceSellOfferIndex =
1790 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
1791 env(token::createOffer(alice, nftAliceNoTransferID, XRP(20)), Txflags(tfSellNFToken));
1792 env.close();
1793 env(token::acceptSellOffer(becky, aliceSellOfferIndex));
1794 env.close();
1795 BEAST_EXPECT(ownerCount(env, alice) == 0);
1796 BEAST_EXPECT(ownerCount(env, becky) == 1);
1797
1798 // becky tries to offer the nft for sale.
1799 env(token::createOffer(becky, nftAliceNoTransferID, XRP(21)),
1800 Txflags(tfSellNFToken),
1802 env.close();
1803 BEAST_EXPECT(ownerCount(env, alice) == 0);
1804 BEAST_EXPECT(ownerCount(env, becky) == 1);
1805
1806 // becky tries to offer the nft for sale with alice as the
1807 // destination. That also doesn't work.
1808 env(token::createOffer(becky, nftAliceNoTransferID, XRP(21)),
1809 Txflags(tfSellNFToken),
1810 token::Destination(alice),
1812 env.close();
1813 BEAST_EXPECT(ownerCount(env, alice) == 0);
1814 BEAST_EXPECT(ownerCount(env, becky) == 1);
1815
1816 // alice offers to buy the nft back from becky. becky accepts
1817 // the offer.
1818 UInt256 const aliceBuyOfferIndex =
1819 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
1820 env(token::createOffer(alice, nftAliceNoTransferID, XRP(22)), token::Owner(becky));
1821 env.close();
1822 env(token::acceptBuyOffer(becky, aliceBuyOfferIndex));
1823 env.close();
1824 BEAST_EXPECT(ownerCount(env, alice) == 1);
1825 BEAST_EXPECT(ownerCount(env, becky) == 0);
1826
1827 // alice burns her nft so accounting is simpler below.
1828 env(token::burn(alice, nftAliceNoTransferID));
1829 env.close();
1830 BEAST_EXPECT(ownerCount(env, alice) == 0);
1831 BEAST_EXPECT(ownerCount(env, becky) == 0);
1832 }
1833 // Try an nft minted by minter for alice without flagTransferable set.
1834 {
1835 env(token::setMinter(alice, minter));
1836 env.close();
1837
1838 BEAST_EXPECT(ownerCount(env, minter) == 0);
1839 UInt256 const nftMinterNoTransferID{token::getNextID(env, alice, 0u)};
1840 env(token::mint(minter), token::Issuer(alice));
1841 env.close();
1842 BEAST_EXPECT(ownerCount(env, minter) == 1);
1843
1844 // becky tries to offer to buy minter's nft.
1845 BEAST_EXPECT(ownerCount(env, becky) == 0);
1846 env(token::createOffer(becky, nftMinterNoTransferID, XRP(20)),
1847 token::Owner(minter),
1849 env.close();
1850 BEAST_EXPECT(ownerCount(env, becky) == 0);
1851
1852 // alice removes authorization of minter.
1853 env(token::clearMinter(alice));
1854 env.close();
1855
1856 // minter tries to offer their nft for sale.
1857 BEAST_EXPECT(ownerCount(env, minter) == 1);
1858 env(token::createOffer(minter, nftMinterNoTransferID, XRP(21)),
1859 Txflags(tfSellNFToken),
1861 env.close();
1862 BEAST_EXPECT(ownerCount(env, minter) == 1);
1863
1864 // Let enough ledgers pass that old transactions are no longer
1865 // retried, then alice gives authorization back to minter.
1866 for (int i = 0; i < 10; ++i)
1867 env.close();
1868
1869 env(token::setMinter(alice, minter));
1870 env.close();
1871 BEAST_EXPECT(ownerCount(env, minter) == 1);
1872
1873 // minter successfully offers their nft for sale.
1874 BEAST_EXPECT(ownerCount(env, minter) == 1);
1875 UInt256 const minterSellOfferIndex =
1876 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
1877 env(token::createOffer(minter, nftMinterNoTransferID, XRP(22)), Txflags(tfSellNFToken));
1878 env.close();
1879 BEAST_EXPECT(ownerCount(env, minter) == 2);
1880
1881 // alice removes authorization of minter so we can see whether
1882 // minter's pre-existing offer still works.
1883 env(token::clearMinter(alice));
1884 env.close();
1885
1886 // becky buys minter's nft even though minter is no longer alice's
1887 // official minter.
1888 BEAST_EXPECT(ownerCount(env, becky) == 0);
1889 env(token::acceptSellOffer(becky, minterSellOfferIndex));
1890 env.close();
1891 BEAST_EXPECT(ownerCount(env, becky) == 1);
1892 BEAST_EXPECT(ownerCount(env, minter) == 0);
1893
1894 // becky attempts to sell the nft.
1895 env(token::createOffer(becky, nftMinterNoTransferID, XRP(23)),
1896 Txflags(tfSellNFToken),
1898 env.close();
1899
1900 // Since minter is not, at the moment, alice's official minter
1901 // they cannot create an offer to buy the nft they minted.
1902 BEAST_EXPECT(ownerCount(env, minter) == 0);
1903 env(token::createOffer(minter, nftMinterNoTransferID, XRP(24)),
1904 token::Owner(becky),
1906 env.close();
1907 BEAST_EXPECT(ownerCount(env, minter) == 0);
1908
1909 // alice can create an offer to buy the nft.
1910 BEAST_EXPECT(ownerCount(env, alice) == 0);
1911 UInt256 const aliceBuyOfferIndex =
1912 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
1913 env(token::createOffer(alice, nftMinterNoTransferID, XRP(25)), token::Owner(becky));
1914 env.close();
1915 BEAST_EXPECT(ownerCount(env, alice) == 1);
1916
1917 // Let enough ledgers pass that old transactions are no longer
1918 // retried, then alice gives authorization back to minter.
1919 for (int i = 0; i < 10; ++i)
1920 env.close();
1921
1922 env(token::setMinter(alice, minter));
1923 env.close();
1924
1925 // Now minter can create an offer to buy the nft.
1926 BEAST_EXPECT(ownerCount(env, minter) == 0);
1927 UInt256 const minterBuyOfferIndex =
1928 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
1929 env(token::createOffer(minter, nftMinterNoTransferID, XRP(26)), token::Owner(becky));
1930 env.close();
1931 BEAST_EXPECT(ownerCount(env, minter) == 1);
1932
1933 // alice removes authorization of minter so we can see whether
1934 // minter's pre-existing buy offer still works.
1935 env(token::clearMinter(alice));
1936 env.close();
1937
1938 // becky accepts minter's sell offer.
1939 BEAST_EXPECT(ownerCount(env, minter) == 1);
1940 BEAST_EXPECT(ownerCount(env, becky) == 1);
1941 env(token::acceptBuyOffer(becky, minterBuyOfferIndex));
1942 env.close();
1943 BEAST_EXPECT(ownerCount(env, minter) == 1);
1944 BEAST_EXPECT(ownerCount(env, becky) == 0);
1945 BEAST_EXPECT(ownerCount(env, alice) == 1);
1946
1947 // minter burns their nft and alice cancels her offer so the
1948 // next tests can start with a clean slate.
1949 env(token::burn(minter, nftMinterNoTransferID), Ter(tesSUCCESS));
1950 env.close();
1951 env(token::cancelOffer(alice, {aliceBuyOfferIndex}));
1952 env.close();
1953 BEAST_EXPECT(ownerCount(env, alice) == 0);
1954 BEAST_EXPECT(ownerCount(env, becky) == 0);
1955 BEAST_EXPECT(ownerCount(env, minter) == 0);
1956 }
1957 // nfts with flagTransferable set should be buyable and salable
1958 // by anybody.
1959 {
1960 BEAST_EXPECT(ownerCount(env, alice) == 0);
1961 UInt256 const nftAliceID{token::getNextID(env, alice, 0u, tfTransferable)};
1962 env(token::mint(alice, 0u), Txflags(tfTransferable));
1963 env.close();
1964 BEAST_EXPECT(ownerCount(env, alice) == 1);
1965
1966 // Both alice and becky can make offers for alice's nft.
1967 UInt256 const aliceSellOfferIndex =
1968 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
1969 env(token::createOffer(alice, nftAliceID, XRP(20)), Txflags(tfSellNFToken));
1970 env.close();
1971 BEAST_EXPECT(ownerCount(env, alice) == 2);
1972
1973 UInt256 const beckyBuyOfferIndex =
1974 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
1975 env(token::createOffer(becky, nftAliceID, XRP(21)), token::Owner(alice));
1976 env.close();
1977 BEAST_EXPECT(ownerCount(env, alice) == 2);
1978
1979 // becky accepts alice's sell offer.
1980 env(token::acceptSellOffer(becky, aliceSellOfferIndex));
1981 env.close();
1982 BEAST_EXPECT(ownerCount(env, alice) == 0);
1983 BEAST_EXPECT(ownerCount(env, becky) == 2);
1984
1985 // becky offers to sell the nft.
1986 UInt256 const beckySellOfferIndex =
1987 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
1988 env(token::createOffer(becky, nftAliceID, XRP(22)), Txflags(tfSellNFToken));
1989 env.close();
1990 BEAST_EXPECT(ownerCount(env, alice) == 0);
1991 BEAST_EXPECT(ownerCount(env, becky) == 3);
1992
1993 // minter buys the nft (even though minter is not currently
1994 // alice's minter).
1995 env(token::acceptSellOffer(minter, beckySellOfferIndex));
1996 env.close();
1997 BEAST_EXPECT(ownerCount(env, alice) == 0);
1998 BEAST_EXPECT(ownerCount(env, becky) == 1);
1999 BEAST_EXPECT(ownerCount(env, minter) == 1);
2000
2001 // minter offers to sell the nft.
2002 UInt256 const minterSellOfferIndex =
2003 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2004 env(token::createOffer(minter, nftAliceID, XRP(23)), Txflags(tfSellNFToken));
2005 env.close();
2006 BEAST_EXPECT(ownerCount(env, alice) == 0);
2007 BEAST_EXPECT(ownerCount(env, becky) == 1);
2008 BEAST_EXPECT(ownerCount(env, minter) == 2);
2009
2010 // alice buys back the nft.
2011 env(token::acceptSellOffer(alice, minterSellOfferIndex));
2012 env.close();
2013 BEAST_EXPECT(ownerCount(env, alice) == 1);
2014 BEAST_EXPECT(ownerCount(env, becky) == 1);
2015 BEAST_EXPECT(ownerCount(env, minter) == 0);
2016
2017 // Remember the buy offer that becky made for alice's token way
2018 // back when? It's still in the ledger, and alice accepts it.
2019 env(token::acceptBuyOffer(alice, beckyBuyOfferIndex));
2020 env.close();
2021 BEAST_EXPECT(ownerCount(env, alice) == 0);
2022 BEAST_EXPECT(ownerCount(env, becky) == 1);
2023 BEAST_EXPECT(ownerCount(env, minter) == 0);
2024
2025 // Just for tidiness, becky burns the token before shutting
2026 // things down.
2027 env(token::burn(becky, nftAliceID));
2028 env.close();
2029 BEAST_EXPECT(ownerCount(env, alice) == 0);
2030 BEAST_EXPECT(ownerCount(env, becky) == 0);
2031 BEAST_EXPECT(ownerCount(env, minter) == 0);
2032 }
2033 }
2034
2035 void
2037 {
2038 // Exercise NFTs with and without a transferFee.
2039 testcase("Mint transferFee");
2040
2041 using namespace test::jtx;
2042
2043 Env env{*this, features};
2044 auto const baseFee = env.current()->fees().base;
2045
2046 Account const alice{"alice"};
2047 Account const becky{"becky"};
2048 Account const carol{"carol"};
2049 Account const minter{"minter"};
2050 Account const gw{"gw"};
2051 IOU const gwXAU(gw["XAU"]);
2052
2053 env.fund(XRP(1000), alice, becky, carol, minter, gw);
2054 env.close();
2055
2056 env(trust(alice, gwXAU(2000)));
2057 env(trust(becky, gwXAU(2000)));
2058 env(trust(carol, gwXAU(2000)));
2059 env(trust(minter, gwXAU(2000)));
2060 env.close();
2061 env(pay(gw, alice, gwXAU(1000)));
2062 env(pay(gw, becky, gwXAU(1000)));
2063 env(pay(gw, carol, gwXAU(1000)));
2064 env(pay(gw, minter, gwXAU(1000)));
2065 env.close();
2066
2067 // Giving alice a minter helps us see if transfer rates are affected
2068 // by that.
2069 env(token::setMinter(alice, minter));
2070 env.close();
2071
2072 // If there is no transferFee, then alice gets nothing for the
2073 // transfer.
2074 {
2075 BEAST_EXPECT(ownerCount(env, alice) == 1);
2076 BEAST_EXPECT(ownerCount(env, becky) == 1);
2077 BEAST_EXPECT(ownerCount(env, carol) == 1);
2078 BEAST_EXPECT(ownerCount(env, minter) == 1);
2079
2080 UInt256 const nftID = token::getNextID(env, alice, 0u, tfTransferable);
2081 env(token::mint(alice), Txflags(tfTransferable));
2082 env.close();
2083
2084 // Becky buys the nft for XAU(10). Check balances.
2085 UInt256 const beckyBuyOfferIndex =
2086 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
2087 env(token::createOffer(becky, nftID, gwXAU(10)), token::Owner(alice));
2088 env.close();
2089 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1000));
2090 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(1000));
2091
2092 env(token::acceptBuyOffer(alice, beckyBuyOfferIndex));
2093 env.close();
2094 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1010));
2095 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(990));
2096
2097 // becky sells nft to carol. alice's balance should not change.
2098 UInt256 const beckySellOfferIndex =
2099 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
2100 env(token::createOffer(becky, nftID, gwXAU(10)), Txflags(tfSellNFToken));
2101 env.close();
2102 env(token::acceptSellOffer(carol, beckySellOfferIndex));
2103 env.close();
2104 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1010));
2105 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(1000));
2106 BEAST_EXPECT(env.balance(carol, gwXAU) == gwXAU(990));
2107
2108 // minter buys nft from carol. alice's balance should not change.
2109 UInt256 const minterBuyOfferIndex =
2110 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2111 env(token::createOffer(minter, nftID, gwXAU(10)), token::Owner(carol));
2112 env.close();
2113 env(token::acceptBuyOffer(carol, minterBuyOfferIndex));
2114 env.close();
2115 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1010));
2116 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(1000));
2117 BEAST_EXPECT(env.balance(carol, gwXAU) == gwXAU(1000));
2118 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(990));
2119
2120 // minter sells the nft to alice. gwXAU balances should finish
2121 // where they started.
2122 UInt256 const minterSellOfferIndex =
2123 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2124 env(token::createOffer(minter, nftID, gwXAU(10)), Txflags(tfSellNFToken));
2125 env.close();
2126 env(token::acceptSellOffer(alice, minterSellOfferIndex));
2127 env.close();
2128 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1000));
2129 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(1000));
2130 BEAST_EXPECT(env.balance(carol, gwXAU) == gwXAU(1000));
2131 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(1000));
2132
2133 // alice burns the nft to make later tests easier to think about.
2134 env(token::burn(alice, nftID));
2135 env.close();
2136 BEAST_EXPECT(ownerCount(env, alice) == 1);
2137 BEAST_EXPECT(ownerCount(env, becky) == 1);
2138 BEAST_EXPECT(ownerCount(env, carol) == 1);
2139 BEAST_EXPECT(ownerCount(env, minter) == 1);
2140 }
2141
2142 // Set the smallest possible transfer fee.
2143 {
2144 // An nft with a transfer fee of 1 basis point.
2145 UInt256 const nftID = token::getNextID(env, alice, 0u, tfTransferable, 1);
2146 env(token::mint(alice), Txflags(tfTransferable), token::XferFee(1));
2147 env.close();
2148
2149 // Becky buys the nft for XAU(10). Check balances.
2150 UInt256 const beckyBuyOfferIndex =
2151 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
2152 env(token::createOffer(becky, nftID, gwXAU(10)), token::Owner(alice));
2153 env.close();
2154 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1000));
2155 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(1000));
2156
2157 env(token::acceptBuyOffer(alice, beckyBuyOfferIndex));
2158 env.close();
2159 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1010));
2160 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(990));
2161
2162 // becky sells nft to carol. alice's balance goes up.
2163 UInt256 const beckySellOfferIndex =
2164 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
2165 env(token::createOffer(becky, nftID, gwXAU(10)), Txflags(tfSellNFToken));
2166 env.close();
2167 env(token::acceptSellOffer(carol, beckySellOfferIndex));
2168 env.close();
2169
2170 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1010.0001));
2171 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(999.9999));
2172 BEAST_EXPECT(env.balance(carol, gwXAU) == gwXAU(990));
2173
2174 // minter buys nft from carol. alice's balance goes up.
2175 UInt256 const minterBuyOfferIndex =
2176 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2177 env(token::createOffer(minter, nftID, gwXAU(10)), token::Owner(carol));
2178 env.close();
2179 env(token::acceptBuyOffer(carol, minterBuyOfferIndex));
2180 env.close();
2181
2182 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1010.0002));
2183 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(999.9999));
2184 BEAST_EXPECT(env.balance(carol, gwXAU) == gwXAU(999.9999));
2185 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(990));
2186
2187 // minter sells the nft to alice. Because alice is part of the
2188 // transaction no transfer fee is removed.
2189 UInt256 const minterSellOfferIndex =
2190 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2191 env(token::createOffer(minter, nftID, gwXAU(10)), Txflags(tfSellNFToken));
2192 env.close();
2193 env(token::acceptSellOffer(alice, minterSellOfferIndex));
2194 env.close();
2195 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1000.0002));
2196 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(999.9999));
2197 BEAST_EXPECT(env.balance(carol, gwXAU) == gwXAU(999.9999));
2198 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(1000));
2199
2200 // alice pays to becky and carol so subsequent tests are easier
2201 // to think about.
2202 env(pay(alice, becky, gwXAU(0.0001)));
2203 env(pay(alice, carol, gwXAU(0.0001)));
2204 env.close();
2205
2206 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1000));
2207 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(1000));
2208 BEAST_EXPECT(env.balance(carol, gwXAU) == gwXAU(1000));
2209 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(1000));
2210
2211 // alice burns the nft to make later tests easier to think about.
2212 env(token::burn(alice, nftID));
2213 env.close();
2214 BEAST_EXPECT(ownerCount(env, alice) == 1);
2215 BEAST_EXPECT(ownerCount(env, becky) == 1);
2216 BEAST_EXPECT(ownerCount(env, carol) == 1);
2217 BEAST_EXPECT(ownerCount(env, minter) == 1);
2218 }
2219
2220 // Set the largest allowed transfer fee.
2221 {
2222 // A transfer fee greater than 50% is not allowed.
2223 env(token::mint(alice),
2224 Txflags(tfTransferable),
2225 token::XferFee(kMaxTransferFee + 1),
2227 env.close();
2228
2229 // Make an nft with a transfer fee of 50%.
2230 UInt256 const nftID = token::getNextID(env, alice, 0u, tfTransferable, kMaxTransferFee);
2231 env(token::mint(alice), Txflags(tfTransferable), token::XferFee(kMaxTransferFee));
2232 env.close();
2233
2234 // Becky buys the nft for XAU(10). Check balances.
2235 UInt256 const beckyBuyOfferIndex =
2236 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
2237 env(token::createOffer(becky, nftID, gwXAU(10)), token::Owner(alice));
2238 env.close();
2239 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1000));
2240 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(1000));
2241
2242 env(token::acceptBuyOffer(alice, beckyBuyOfferIndex));
2243 env.close();
2244 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1010));
2245 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(990));
2246
2247 // becky sells nft to minter. alice's balance goes up.
2248 UInt256 const beckySellOfferIndex =
2249 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
2250 env(token::createOffer(becky, nftID, gwXAU(100)), Txflags(tfSellNFToken));
2251 env.close();
2252 env(token::acceptSellOffer(minter, beckySellOfferIndex));
2253 env.close();
2254
2255 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1060));
2256 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(1040));
2257 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(900));
2258
2259 // carol buys nft from minter. alice's balance goes up.
2260 UInt256 const carolBuyOfferIndex =
2261 keylet::nftokenOffer(carol, SeqProxy::rawSequence(env.seq(carol))).key;
2262 env(token::createOffer(carol, nftID, gwXAU(10)), token::Owner(minter));
2263 env.close();
2264 env(token::acceptBuyOffer(minter, carolBuyOfferIndex));
2265 env.close();
2266
2267 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1065));
2268 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(1040));
2269 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(905));
2270 BEAST_EXPECT(env.balance(carol, gwXAU) == gwXAU(990));
2271
2272 // carol sells the nft to alice. Because alice is part of the
2273 // transaction no transfer fee is removed.
2274 UInt256 const carolSellOfferIndex =
2275 keylet::nftokenOffer(carol, SeqProxy::rawSequence(env.seq(carol))).key;
2276 env(token::createOffer(carol, nftID, gwXAU(10)), Txflags(tfSellNFToken));
2277 env.close();
2278 env(token::acceptSellOffer(alice, carolSellOfferIndex));
2279 env.close();
2280
2281 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1055));
2282 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(1040));
2283 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(905));
2284 BEAST_EXPECT(env.balance(carol, gwXAU) == gwXAU(1000));
2285
2286 // rebalance so subsequent tests are easier to think about.
2287 env(pay(alice, minter, gwXAU(55)));
2288 env(pay(becky, minter, gwXAU(40)));
2289 env.close();
2290 BEAST_EXPECT(env.balance(alice, gwXAU) == gwXAU(1000));
2291 BEAST_EXPECT(env.balance(becky, gwXAU) == gwXAU(1000));
2292 BEAST_EXPECT(env.balance(carol, gwXAU) == gwXAU(1000));
2293 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(1000));
2294
2295 // alice burns the nft to make later tests easier to think about.
2296 env(token::burn(alice, nftID));
2297 env.close();
2298 BEAST_EXPECT(ownerCount(env, alice) == 1);
2299 BEAST_EXPECT(ownerCount(env, becky) == 1);
2300 BEAST_EXPECT(ownerCount(env, carol) == 1);
2301 BEAST_EXPECT(ownerCount(env, minter) == 1);
2302 }
2303
2304 // See the impact of rounding when the nft is sold for small amounts
2305 // of drops.
2306 {
2307 // An nft with a transfer fee of 1 basis point.
2308 UInt256 const nftID = token::getNextID(env, alice, 0u, tfTransferable, 1);
2309 env(token::mint(alice), Txflags(tfTransferable), token::XferFee(1));
2310 env.close();
2311
2312 // minter buys the nft for XRP(1). Since the transfer involves
2313 // alice there should be no transfer fee.
2314 STAmount aliceBalance = env.balance(alice);
2315 STAmount minterBalance = env.balance(minter);
2316 UInt256 const minterBuyOfferIndex =
2317 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2318 env(token::createOffer(minter, nftID, XRP(1)), token::Owner(alice));
2319 env.close();
2320 env(token::acceptBuyOffer(alice, minterBuyOfferIndex));
2321 env.close();
2322 aliceBalance += XRP(1) - baseFee;
2323 minterBalance -= XRP(1) + baseFee;
2324 BEAST_EXPECT(env.balance(alice) == aliceBalance);
2325 BEAST_EXPECT(env.balance(minter) == minterBalance);
2326
2327 // minter sells to carol. The payment is just small enough that
2328 // alice does not get any transfer fee.
2329 auto pmt = drops(50000);
2330 STAmount carolBalance = env.balance(carol);
2331 UInt256 const minterSellOfferIndex =
2332 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2333 env(token::createOffer(minter, nftID, pmt), Txflags(tfSellNFToken));
2334 env.close();
2335 env(token::acceptSellOffer(carol, minterSellOfferIndex));
2336 env.close();
2337 minterBalance += pmt - baseFee;
2338 carolBalance -= pmt + baseFee;
2339 BEAST_EXPECT(env.balance(alice) == aliceBalance);
2340 BEAST_EXPECT(env.balance(minter) == minterBalance);
2341 BEAST_EXPECT(env.balance(carol) == carolBalance);
2342
2343 // carol sells to becky. This is the smallest amount to pay for a
2344 // transfer that enables a transfer fee of 1 basis point.
2345 STAmount beckyBalance = env.balance(becky);
2346 UInt256 const beckyBuyOfferIndex =
2347 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
2348 pmt = drops(50001);
2349 env(token::createOffer(becky, nftID, pmt), token::Owner(carol));
2350 env.close();
2351 env(token::acceptBuyOffer(carol, beckyBuyOfferIndex));
2352 env.close();
2353 carolBalance += pmt - drops(1) - baseFee;
2354 beckyBalance -= pmt + baseFee;
2355 aliceBalance += drops(1);
2356
2357 BEAST_EXPECT(env.balance(alice) == aliceBalance);
2358 BEAST_EXPECT(env.balance(minter) == minterBalance);
2359 BEAST_EXPECT(env.balance(carol) == carolBalance);
2360 BEAST_EXPECT(env.balance(becky) == beckyBalance);
2361 }
2362
2363 // See the impact of rounding when the nft is sold for small amounts
2364 // of an IOU.
2365 {
2366 // An nft with a transfer fee of 1 basis point.
2367 UInt256 const nftID = token::getNextID(env, alice, 0u, tfTransferable, 1);
2368 env(token::mint(alice), Txflags(tfTransferable), token::XferFee(1));
2369 env.close();
2370
2371 // Due to the floating point nature of IOUs we need to
2372 // significantly reduce the gwXAU balances of our accounts prior
2373 // to the iou transfer. Otherwise no transfers will happen.
2374 env(pay(alice, gw, env.balance(alice, gwXAU)));
2375 env(pay(minter, gw, env.balance(minter, gwXAU)));
2376 env(pay(becky, gw, env.balance(becky, gwXAU)));
2377 env.close();
2378
2379 STAmount const startXAUBalance(gwXAU, STAmount::kMinValue, STAmount::kMinOffset + 5);
2380 env(pay(gw, alice, startXAUBalance));
2381 env(pay(gw, minter, startXAUBalance));
2382 env(pay(gw, becky, startXAUBalance));
2383 env.close();
2384
2385 // Here is the smallest expressible gwXAU amount.
2386 STAmount const tinyXAU(gwXAU, STAmount::kMinValue, STAmount::kMinOffset);
2387
2388 // minter buys the nft for tinyXAU. Since the transfer involves
2389 // alice there should be no transfer fee.
2390 STAmount aliceBalance = env.balance(alice, gwXAU);
2391 STAmount minterBalance = env.balance(minter, gwXAU);
2392 UInt256 const minterBuyOfferIndex =
2393 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2394 env(token::createOffer(minter, nftID, tinyXAU), token::Owner(alice));
2395 env.close();
2396 env(token::acceptBuyOffer(alice, minterBuyOfferIndex));
2397 env.close();
2398 aliceBalance += tinyXAU;
2399 minterBalance -= tinyXAU;
2400 BEAST_EXPECT(env.balance(alice, gwXAU) == aliceBalance);
2401 BEAST_EXPECT(env.balance(minter, gwXAU) == minterBalance);
2402
2403 // minter sells to carol.
2404 STAmount carolBalance = env.balance(carol, gwXAU);
2405 UInt256 const minterSellOfferIndex =
2406 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2407 env(token::createOffer(minter, nftID, tinyXAU), Txflags(tfSellNFToken));
2408 env.close();
2409 env(token::acceptSellOffer(carol, minterSellOfferIndex));
2410 env.close();
2411
2412 minterBalance += tinyXAU;
2413 carolBalance -= tinyXAU;
2414 // tiny XAU is so small that alice does not get a transfer fee.
2415 BEAST_EXPECT(env.balance(alice, gwXAU) == aliceBalance);
2416 BEAST_EXPECT(env.balance(minter, gwXAU) == minterBalance);
2417 BEAST_EXPECT(env.balance(carol, gwXAU) == carolBalance);
2418
2419 // carol sells to becky. This is the smallest gwXAU amount
2420 // to pay for a transfer that enables a transfer fee of 1.
2421 STAmount const cheapNFT(gwXAU, STAmount::kMinValue, STAmount::kMinOffset + 5);
2422
2423 STAmount beckyBalance = env.balance(becky, gwXAU);
2424 UInt256 const beckyBuyOfferIndex =
2425 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
2426 env(token::createOffer(becky, nftID, cheapNFT), token::Owner(carol));
2427 env.close();
2428 env(token::acceptBuyOffer(carol, beckyBuyOfferIndex));
2429 env.close();
2430
2431 aliceBalance += tinyXAU;
2432 beckyBalance -= cheapNFT;
2433 carolBalance += cheapNFT - tinyXAU;
2434 BEAST_EXPECT(env.balance(alice, gwXAU) == aliceBalance);
2435 BEAST_EXPECT(env.balance(minter, gwXAU) == minterBalance);
2436 BEAST_EXPECT(env.balance(carol, gwXAU) == carolBalance);
2437 BEAST_EXPECT(env.balance(becky, gwXAU) == beckyBalance);
2438 }
2439 }
2440
2441 void
2443 {
2444 // Exercise the NFT taxon field.
2445 testcase("Mint taxon");
2446
2447 using namespace test::jtx;
2448
2449 Env env{*this, features};
2450
2451 Account const alice{"alice"};
2452 Account const becky{"becky"};
2453
2454 env.fund(XRP(1000), alice, becky);
2455 env.close();
2456
2457 // The taxon field is incorporated straight into the NFT ID. So
2458 // tests only need to operate on NFT IDs; we don't need to generate
2459 // any transactions.
2460
2461 // The taxon value should be recoverable from the NFT ID.
2462 {
2463 UInt256 const nftID = token::getNextID(env, alice, 0u);
2464 BEAST_EXPECT(nft::getTaxon(nftID) == nft::toTaxon(0));
2465 }
2466
2467 // Make sure the full range of taxon values work. We just tried
2468 // the minimum. Now try the largest.
2469 {
2470 UInt256 const nftID = token::getNextID(env, alice, 0xFFFFFFFFu);
2471 BEAST_EXPECT(nft::getTaxon(nftID) == nft::toTaxon((0xFFFFFFFF)));
2472 }
2473
2474 // Do some touch testing to show that the taxon is recoverable no
2475 // matter what else changes around it in the nft ID.
2476 {
2477 auto const taxon = randInt<std::uint32_t>();
2478 for (int i = 0; i < 10; ++i)
2479 {
2480 // lambda to produce a useful message on error.
2481 auto const check = [this](std::uint32_t taxon, UInt256 const& nftID) {
2482 nft::Taxon const gotTaxon = nft::getTaxon(nftID);
2483 if (nft::toTaxon(taxon) == gotTaxon)
2484 {
2485 pass();
2486 }
2487 else
2488 {
2490 ss << "Taxon recovery failed from nftID " << to_string(nftID)
2491 << ". Expected: " << taxon << "; got: " << gotTaxon;
2492 fail(ss.str());
2493 }
2494 };
2495
2496 UInt256 const nftAliceID = token::getID(
2497 env,
2498 alice,
2499 taxon,
2503 check(taxon, nftAliceID);
2504
2505 UInt256 const nftBeckyID = token::getID(
2506 env,
2507 becky,
2508 taxon,
2512 check(taxon, nftBeckyID);
2513 }
2514 }
2515 }
2516
2517 void
2519 {
2520 // Exercise the NFT URI field.
2521 // 1. Create a number of NFTs with and without URIs.
2522 // 2. Retrieve the NFTs from the server.
2523 // 3. Make sure the right URI is attached to each NFT.
2524 testcase("Mint URI");
2525
2526 using namespace test::jtx;
2527
2528 Env env{*this, features};
2529
2530 Account const alice{"alice"};
2531 Account const becky{"becky"};
2532
2533 env.fund(XRP(10000), alice, becky);
2534 env.close();
2535
2536 // lambda that returns a randomly generated string which fits
2537 // the constraints of a URI. Empty strings may be returned.
2538 // In the empty string case do not add the URI to the nft.
2539 auto const randURI = [] {
2540 std::string ret;
2541
2542 // About 20% of the returned strings should be empty
2543 if (randInt(4) == 0)
2544 return ret;
2545
2546 std::size_t const strLen = randInt(256);
2547 ret.reserve(strLen);
2548 for (std::size_t i = 0; i < strLen; ++i)
2549 ret.push_back(randByte());
2550
2551 return ret;
2552 };
2553
2554 // Make a list of URIs that we'll put in nfts.
2555 struct Entry
2556 {
2557 std::string uri;
2558 std::uint32_t taxon;
2559
2560 Entry(std::string uri, std::uint32_t taxon) : uri(std::move(uri)), taxon(taxon)
2561 {
2562 }
2563 };
2564
2565 std::vector<Entry> entries;
2566 entries.reserve(100);
2567 for (std::size_t i = 0; i < 100; ++i)
2568 entries.emplace_back(randURI(), randInt<std::uint32_t>());
2569
2570 // alice creates nfts using entries.
2571 for (Entry const& entry : entries)
2572 {
2573 if (entry.uri.empty())
2574 {
2575 env(token::mint(alice, entry.taxon));
2576 }
2577 else
2578 {
2579 env(token::mint(alice, entry.taxon), token::Uri(entry.uri));
2580 }
2581 env.close();
2582 }
2583
2584 // Recover alice's nfts from the ledger.
2585 json::Value aliceNFTs = [&env, &alice] {
2586 json::Value params;
2587 params[jss::account] = alice.human();
2588 params[jss::type] = "state";
2589 return env.rpc("json", "account_nfts", to_string(params));
2590 }();
2591
2592 // Verify that the returned NFTs match what we sent.
2593 json::Value& nfts = aliceNFTs[jss::result][jss::account_nfts];
2594 if (!BEAST_EXPECT(nfts.size() == entries.size()))
2595 return;
2596
2597 // Sort the returned NFTs by nft_serial so the are in the same order
2598 // as entries.
2599 std::vector<json::Value> sortedNFTs;
2600 sortedNFTs.reserve(nfts.size());
2601 for (std::size_t i = 0; i < nfts.size(); ++i)
2602 sortedNFTs.push_back(nfts[i]);
2604 sortedNFTs,
2605
2606 [](json::Value const& lhs, json::Value const& rhs) {
2607 return lhs[jss::nft_serial] < rhs[jss::nft_serial];
2608 });
2609
2610 for (std::size_t i = 0; i < entries.size(); ++i)
2611 {
2612 Entry const& entry = entries[i];
2613 json::Value const& ret = sortedNFTs[i];
2614 BEAST_EXPECT(entry.taxon == ret[sfNFTokenTaxon.jsonName]);
2615 if (entry.uri.empty())
2616 {
2617 BEAST_EXPECT(!ret.isMember(sfURI.jsonName));
2618 }
2619 else
2620 {
2621 BEAST_EXPECT(strHex(entry.uri) == ret[sfURI.jsonName]);
2622 }
2623 }
2624 }
2625
2626 void
2628 {
2629 // Explore the OfferCreate Destination field.
2630 testcase("Create offer destination");
2631
2632 using namespace test::jtx;
2633
2634 Env env{*this, features};
2635
2636 Account const issuer{"issuer"};
2637 Account const minter{"minter"};
2638 Account const buyer{"buyer"};
2639 Account const broker{"broker"};
2640
2641 env.fund(XRP(1000), issuer, minter, buyer, broker);
2642
2643 // We want to explore how issuers vs minters fits into the permission
2644 // scheme. So issuer issues and minter mints.
2645 env(token::setMinter(issuer, minter));
2646 env.close();
2647
2648 UInt256 const nftokenID = token::getNextID(env, issuer, 0, tfTransferable);
2649 env(token::mint(minter, 0), token::Issuer(issuer), Txflags(tfTransferable));
2650 env.close();
2651
2652 // Test how adding a Destination field to an offer affects permissions
2653 // for canceling offers.
2654 {
2655 UInt256 const offerMinterToIssuer =
2656 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2657 env(token::createOffer(minter, nftokenID, drops(1)),
2658 token::Destination(issuer),
2659 Txflags(tfSellNFToken));
2660
2661 UInt256 const offerMinterToBuyer =
2662 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2663 env(token::createOffer(minter, nftokenID, drops(1)),
2664 token::Destination(buyer),
2665 Txflags(tfSellNFToken));
2666
2667 UInt256 const offerIssuerToMinter =
2668 keylet::nftokenOffer(issuer, SeqProxy::rawSequence(env.seq(issuer))).key;
2669 env(token::createOffer(issuer, nftokenID, drops(1)),
2670 token::Owner(minter),
2671 token::Destination(minter));
2672
2673 UInt256 const offerIssuerToBuyer =
2674 keylet::nftokenOffer(issuer, SeqProxy::rawSequence(env.seq(issuer))).key;
2675 env(token::createOffer(issuer, nftokenID, drops(1)),
2676 token::Owner(minter),
2677 token::Destination(buyer));
2678
2679 env.close();
2680 BEAST_EXPECT(ownerCount(env, issuer) == 2);
2681 BEAST_EXPECT(ownerCount(env, minter) == 3);
2682 BEAST_EXPECT(ownerCount(env, buyer) == 0);
2683
2684 // Test who gets to cancel the offers. Anyone outside of the
2685 // offer-owner/destination pair should not be able to cancel the
2686 // offers.
2687 //
2688 // Note that issuer does not have any special permissions regarding
2689 // offer cancellation. issuer cannot cancel an offer for an
2690 // NFToken they issued.
2691 env(token::cancelOffer(issuer, {offerMinterToBuyer}), Ter(tecNO_PERMISSION));
2692 env(token::cancelOffer(buyer, {offerMinterToIssuer}), Ter(tecNO_PERMISSION));
2693 env(token::cancelOffer(buyer, {offerIssuerToMinter}), Ter(tecNO_PERMISSION));
2694 env(token::cancelOffer(minter, {offerIssuerToBuyer}), Ter(tecNO_PERMISSION));
2695 env.close();
2696 BEAST_EXPECT(ownerCount(env, issuer) == 2);
2697 BEAST_EXPECT(ownerCount(env, minter) == 3);
2698 BEAST_EXPECT(ownerCount(env, buyer) == 0);
2699
2700 // Both the offer creator and and destination should be able to
2701 // cancel the offers.
2702 env(token::cancelOffer(buyer, {offerMinterToBuyer}));
2703 env(token::cancelOffer(minter, {offerMinterToIssuer}));
2704 env(token::cancelOffer(buyer, {offerIssuerToBuyer}));
2705 env(token::cancelOffer(issuer, {offerIssuerToMinter}));
2706 env.close();
2707 BEAST_EXPECT(ownerCount(env, issuer) == 0);
2708 BEAST_EXPECT(ownerCount(env, minter) == 1);
2709 BEAST_EXPECT(ownerCount(env, buyer) == 0);
2710 }
2711
2712 // Test how adding a Destination field to a sell offer affects
2713 // accepting that offer.
2714 {
2715 UInt256 const offerMinterSellsToBuyer =
2716 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2717 env(token::createOffer(minter, nftokenID, drops(1)),
2718 token::Destination(buyer),
2719 Txflags(tfSellNFToken));
2720 env.close();
2721 BEAST_EXPECT(ownerCount(env, issuer) == 0);
2722 BEAST_EXPECT(ownerCount(env, minter) == 2);
2723 BEAST_EXPECT(ownerCount(env, buyer) == 0);
2724
2725 // issuer cannot accept a sell offer where they are not the
2726 // destination.
2727 env(token::acceptSellOffer(issuer, offerMinterSellsToBuyer), Ter(tecNO_PERMISSION));
2728 env.close();
2729 BEAST_EXPECT(ownerCount(env, issuer) == 0);
2730 BEAST_EXPECT(ownerCount(env, minter) == 2);
2731 BEAST_EXPECT(ownerCount(env, buyer) == 0);
2732
2733 // However buyer can accept the sell offer.
2734 env(token::acceptSellOffer(buyer, offerMinterSellsToBuyer));
2735 env.close();
2736 BEAST_EXPECT(ownerCount(env, issuer) == 0);
2737 BEAST_EXPECT(ownerCount(env, minter) == 0);
2738 BEAST_EXPECT(ownerCount(env, buyer) == 1);
2739 }
2740
2741 // Test how adding a Destination field to a buy offer affects
2742 // accepting that offer.
2743 {
2744 UInt256 const offerMinterBuysFromBuyer =
2745 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2746 env(token::createOffer(minter, nftokenID, drops(1)),
2747 token::Owner(buyer),
2748 token::Destination(buyer));
2749 env.close();
2750 BEAST_EXPECT(ownerCount(env, issuer) == 0);
2751 BEAST_EXPECT(ownerCount(env, minter) == 1);
2752 BEAST_EXPECT(ownerCount(env, buyer) == 1);
2753
2754 // issuer cannot accept a buy offer where they are the
2755 // destination.
2756 env(token::acceptBuyOffer(issuer, offerMinterBuysFromBuyer), Ter(tecNO_PERMISSION));
2757 env.close();
2758 BEAST_EXPECT(ownerCount(env, issuer) == 0);
2759 BEAST_EXPECT(ownerCount(env, minter) == 1);
2760 BEAST_EXPECT(ownerCount(env, buyer) == 1);
2761
2762 // Buyer accepts minter's offer.
2763 env(token::acceptBuyOffer(buyer, offerMinterBuysFromBuyer));
2764 env.close();
2765 BEAST_EXPECT(ownerCount(env, issuer) == 0);
2766 BEAST_EXPECT(ownerCount(env, minter) == 1);
2767 BEAST_EXPECT(ownerCount(env, buyer) == 0);
2768
2769 // If a destination other than the NFToken owner is set, that
2770 // destination must act as a broker. The NFToken owner may not
2771 // simply accept the offer.
2772 UInt256 const offerBuyerBuysFromMinter =
2773 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
2774 env(token::createOffer(buyer, nftokenID, drops(1)),
2775 token::Owner(minter),
2776 token::Destination(broker));
2777 env.close();
2778 BEAST_EXPECT(ownerCount(env, issuer) == 0);
2779 BEAST_EXPECT(ownerCount(env, minter) == 1);
2780 BEAST_EXPECT(ownerCount(env, buyer) == 1);
2781
2782 env(token::acceptBuyOffer(minter, offerBuyerBuysFromMinter), Ter(tecNO_PERMISSION));
2783 env.close();
2784
2785 // Clean up the unused offer.
2786 env(token::cancelOffer(buyer, {offerBuyerBuysFromMinter}));
2787 env.close();
2788 BEAST_EXPECT(ownerCount(env, issuer) == 0);
2789 BEAST_EXPECT(ownerCount(env, minter) == 1);
2790 BEAST_EXPECT(ownerCount(env, buyer) == 0);
2791 }
2792
2793 // Show that a sell offer's Destination can broker that sell offer
2794 // to another account.
2795 {
2796 UInt256 const offerMinterToBroker =
2797 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2798 env(token::createOffer(minter, nftokenID, drops(1)),
2799 token::Destination(broker),
2800 Txflags(tfSellNFToken));
2801
2802 UInt256 const offerBuyerToMinter =
2803 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
2804 env(token::createOffer(buyer, nftokenID, drops(1)), token::Owner(minter));
2805
2806 env.close();
2807 BEAST_EXPECT(ownerCount(env, issuer) == 0);
2808 BEAST_EXPECT(ownerCount(env, minter) == 2);
2809 BEAST_EXPECT(ownerCount(env, buyer) == 1);
2810
2811 {
2812 // issuer cannot broker the offers, because they are not the
2813 // Destination.
2814 env(token::brokerOffers(issuer, offerBuyerToMinter, offerMinterToBroker),
2815 Ter(tecNO_PERMISSION));
2816 env.close();
2817 BEAST_EXPECT(ownerCount(env, issuer) == 0);
2818 BEAST_EXPECT(ownerCount(env, minter) == 2);
2819 BEAST_EXPECT(ownerCount(env, buyer) == 1);
2820 }
2821
2822 // Since broker is the sell offer's destination, they can broker
2823 // the two offers.
2824 env(token::brokerOffers(broker, offerBuyerToMinter, offerMinterToBroker));
2825 env.close();
2826 BEAST_EXPECT(ownerCount(env, issuer) == 0);
2827 BEAST_EXPECT(ownerCount(env, minter) == 0);
2828 BEAST_EXPECT(ownerCount(env, buyer) == 1);
2829 }
2830
2831 // Show that brokered mode cannot complete a transfer where the
2832 // Destination doesn't match, but can complete if the Destination
2833 // does match.
2834 {
2835 UInt256 const offerBuyerToMinter =
2836 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
2837 env(token::createOffer(buyer, nftokenID, drops(1)),
2838 token::Destination(minter),
2839 Txflags(tfSellNFToken));
2840
2841 UInt256 const offerMinterToBuyer =
2842 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2843 env(token::createOffer(minter, nftokenID, drops(1)), token::Owner(buyer));
2844
2845 UInt256 const offerIssuerToBuyer =
2846 keylet::nftokenOffer(issuer, SeqProxy::rawSequence(env.seq(issuer))).key;
2847 env(token::createOffer(issuer, nftokenID, drops(1)), token::Owner(buyer));
2848
2849 env.close();
2850 BEAST_EXPECT(ownerCount(env, issuer) == 1);
2851 BEAST_EXPECT(ownerCount(env, minter) == 1);
2852 BEAST_EXPECT(ownerCount(env, buyer) == 2);
2853
2854 {
2855 // Cannot broker offers when the sell destination is not the
2856 // buyer.
2857 env(token::brokerOffers(broker, offerIssuerToBuyer, offerBuyerToMinter),
2858 Ter(tecNO_PERMISSION));
2859 env.close();
2860
2861 BEAST_EXPECT(ownerCount(env, issuer) == 1);
2862 BEAST_EXPECT(ownerCount(env, minter) == 1);
2863 BEAST_EXPECT(ownerCount(env, buyer) == 2);
2864
2865 env(token::brokerOffers(broker, offerMinterToBuyer, offerBuyerToMinter),
2866 Ter(tecNO_PERMISSION));
2867 env.close();
2868
2869 // Buyer is successful with acceptOffer.
2870 env(token::acceptBuyOffer(buyer, offerMinterToBuyer));
2871 env.close();
2872
2873 // Clean out the unconsumed offer.
2874 env(token::cancelOffer(buyer, {offerBuyerToMinter}));
2875 env.close();
2876
2877 BEAST_EXPECT(ownerCount(env, issuer) == 1);
2878 BEAST_EXPECT(ownerCount(env, minter) == 1);
2879 BEAST_EXPECT(ownerCount(env, buyer) == 0);
2880
2881 // Clean out the unconsumed offer.
2882 env(token::cancelOffer(issuer, {offerIssuerToBuyer}));
2883 env.close();
2884 BEAST_EXPECT(ownerCount(env, issuer) == 0);
2885 BEAST_EXPECT(ownerCount(env, minter) == 1);
2886 BEAST_EXPECT(ownerCount(env, buyer) == 0);
2887 return;
2888 }
2889 }
2890
2891 // Show that if a buy and a sell offer both have the same destination,
2892 // then that destination can broker the offers.
2893 {
2894 UInt256 const offerMinterToBroker =
2895 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2896 env(token::createOffer(minter, nftokenID, drops(1)),
2897 token::Destination(broker),
2898 Txflags(tfSellNFToken));
2899
2900 UInt256 const offerBuyerToBroker =
2901 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
2902 env(token::createOffer(buyer, nftokenID, drops(1)),
2903 token::Owner(minter),
2904 token::Destination(broker));
2905
2906 {
2907 // Cannot broker offers when the sell destination is not the
2908 // buyer or the broker.
2909 env(token::brokerOffers(issuer, offerBuyerToBroker, offerMinterToBroker),
2910 Ter(tecNO_PERMISSION));
2911 env.close();
2912 BEAST_EXPECT(ownerCount(env, issuer) == 0);
2913 BEAST_EXPECT(ownerCount(env, minter) == 2);
2914 BEAST_EXPECT(ownerCount(env, buyer) == 1);
2915 }
2916
2917 // Broker is successful if they are the destination of both offers.
2918 env(token::brokerOffers(broker, offerBuyerToBroker, offerMinterToBroker));
2919 env.close();
2920 BEAST_EXPECT(ownerCount(env, issuer) == 0);
2921 BEAST_EXPECT(ownerCount(env, minter) == 0);
2922 BEAST_EXPECT(ownerCount(env, buyer) == 1);
2923 }
2924 }
2925
2926 void
2928 {
2929 testcase("Create offer destination disallow incoming");
2930
2931 using namespace test::jtx;
2932
2933 Env env{*this, features};
2934
2935 Account const issuer{"issuer"};
2936 Account const minter{"minter"};
2937 Account const buyer{"buyer"};
2938 Account const alice{"alice"};
2939
2940 env.fund(XRP(1000), issuer, minter, buyer, alice);
2941
2942 env(token::setMinter(issuer, minter));
2943 env.close();
2944
2945 UInt256 const nftokenID = token::getNextID(env, issuer, 0, tfTransferable);
2946 env(token::mint(minter, 0), token::Issuer(issuer), Txflags(tfTransferable));
2947 env.close();
2948
2949 // enable flag
2950 env(fset(buyer, asfDisallowIncomingNFTokenOffer));
2951 env.close();
2952
2953 // a sell offer from the minter to the buyer should be rejected
2954 {
2955 env(token::createOffer(minter, nftokenID, drops(1)),
2956 token::Destination(buyer),
2957 Txflags(tfSellNFToken),
2958 Ter(tecNO_PERMISSION));
2959 env.close();
2960 BEAST_EXPECT(ownerCount(env, issuer) == 0);
2961 BEAST_EXPECT(ownerCount(env, minter) == 1);
2962 BEAST_EXPECT(ownerCount(env, buyer) == 0);
2963 }
2964
2965 // disable the flag
2966 env(fclear(buyer, asfDisallowIncomingNFTokenOffer));
2967 env.close();
2968
2969 // create offer (allowed now) then cancel
2970 {
2971 UInt256 const offerIndex =
2972 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2973
2974 env(token::createOffer(minter, nftokenID, drops(1)),
2975 token::Destination(buyer),
2976 Txflags(tfSellNFToken));
2977 env.close();
2978
2979 env(token::cancelOffer(minter, {offerIndex}));
2980 env.close();
2981 }
2982
2983 // create offer, enable flag, then cancel
2984 {
2985 UInt256 const offerIndex =
2986 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
2987
2988 env(token::createOffer(minter, nftokenID, drops(1)),
2989 token::Destination(buyer),
2990 Txflags(tfSellNFToken));
2991 env.close();
2992
2993 env(fset(buyer, asfDisallowIncomingNFTokenOffer));
2994 env.close();
2995
2996 env(token::cancelOffer(minter, {offerIndex}));
2997 env.close();
2998
2999 env(fclear(buyer, asfDisallowIncomingNFTokenOffer));
3000 env.close();
3001 }
3002
3003 // create offer then transfer
3004 {
3005 UInt256 const offerIndex =
3006 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
3007
3008 env(token::createOffer(minter, nftokenID, drops(1)),
3009 token::Destination(buyer),
3010 Txflags(tfSellNFToken));
3011 env.close();
3012
3013 env(token::acceptSellOffer(buyer, offerIndex));
3014 env.close();
3015 }
3016
3017 // buyer now owns the token
3018
3019 // enable flag again
3020 env(fset(buyer, asfDisallowIncomingNFTokenOffer));
3021 env.close();
3022
3023 // a random offer to buy the token
3024 {
3025 env(token::createOffer(alice, nftokenID, drops(1)),
3026 token::Owner(buyer),
3027 Ter(tecNO_PERMISSION));
3028 env.close();
3029 }
3030
3031 // minter offer to buy the token
3032 {
3033 env(token::createOffer(minter, nftokenID, drops(1)),
3034 token::Owner(buyer),
3035 Ter(tecNO_PERMISSION));
3036 env.close();
3037 }
3038
3039 // minter mint and offer to buyer
3040 if (features[featureNFTokenMintOffer])
3041 {
3042 // enable flag
3043 env(fset(buyer, asfDisallowIncomingNFTokenOffer));
3044 // a sell offer from the minter to the buyer should be rejected
3045 env(token::mint(minter),
3046 token::Amount(drops(1)),
3047 token::Destination(buyer),
3048 Ter(tecNO_PERMISSION));
3049 env.close();
3050
3051 // disable flag
3052 env(fclear(buyer, asfDisallowIncomingNFTokenOffer));
3053 env(token::mint(minter), token::Amount(drops(1)), token::Destination(buyer));
3054 env.close();
3055 }
3056 }
3057
3058 void
3060 {
3061 // Explore the OfferCreate Expiration field.
3062 testcase("Create offer expiration");
3063
3064 using namespace test::jtx;
3065
3066 Env env{*this, features};
3067
3068 Account const issuer{"issuer"};
3069 Account const minter{"minter"};
3070 Account const buyer{"buyer"};
3071
3072 env.fund(XRP(1000), issuer, minter, buyer);
3073
3074 // We want to explore how issuers vs minters fits into the permission
3075 // scheme. So issuer issues and minter mints.
3076 env(token::setMinter(issuer, minter));
3077 env.close();
3078
3079 UInt256 const nftokenID0 = token::getNextID(env, issuer, 0, tfTransferable);
3080 env(token::mint(minter, 0), token::Issuer(issuer), Txflags(tfTransferable));
3081 env.close();
3082
3083 UInt256 const nftokenID1 = token::getNextID(env, issuer, 0, tfTransferable);
3084 env(token::mint(minter, 0), token::Issuer(issuer), Txflags(tfTransferable));
3085 env.close();
3086 uint8_t issuerCount = 0, minterCount = 0, buyerCount = 0;
3087
3088 // Test how adding an Expiration field to an offer affects permissions
3089 // for cancelling offers.
3090 {
3091 std::uint32_t const expiration = lastClose(env) + 25;
3092
3093 UInt256 const offerMinterToIssuer =
3094 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
3095 env(token::createOffer(minter, nftokenID0, drops(1)),
3096 token::Destination(issuer),
3097 token::Expiration(expiration),
3098 Txflags(tfSellNFToken));
3099
3100 UInt256 const offerMinterToAnyone =
3101 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
3102 env(token::createOffer(minter, nftokenID0, drops(1)),
3103 token::Expiration(expiration),
3104 Txflags(tfSellNFToken));
3105
3106 UInt256 const offerIssuerToMinter =
3107 keylet::nftokenOffer(issuer, SeqProxy::rawSequence(env.seq(issuer))).key;
3108 env(token::createOffer(issuer, nftokenID0, drops(1)),
3109 token::Owner(minter),
3110 token::Expiration(expiration));
3111
3112 UInt256 const offerBuyerToMinter =
3113 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
3114 env(token::createOffer(buyer, nftokenID0, drops(1)),
3115 token::Owner(minter),
3116 token::Expiration(expiration));
3117 env.close();
3118 issuerCount = 1;
3119 minterCount = 3;
3120 buyerCount = 1;
3121 BEAST_EXPECT(ownerCount(env, issuer) == issuerCount);
3122 BEAST_EXPECT(ownerCount(env, minter) == minterCount);
3123 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
3124
3125 // Test who gets to cancel the offers. Anyone outside of the
3126 // offer-owner/destination pair should not be able to cancel
3127 // unexpired offers.
3128 //
3129 // Note that these are tec responses, so these transactions will
3130 // not be retried by the ledger.
3131 env(token::cancelOffer(issuer, {offerMinterToAnyone}), Ter(tecNO_PERMISSION));
3132 env(token::cancelOffer(buyer, {offerIssuerToMinter}), Ter(tecNO_PERMISSION));
3133 env.close();
3134 BEAST_EXPECT(lastClose(env) < expiration);
3135 BEAST_EXPECT(ownerCount(env, issuer) == issuerCount);
3136 BEAST_EXPECT(ownerCount(env, minter) == minterCount);
3137 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
3138
3139 // The offer creator can cancel their own unexpired offer.
3140 env(token::cancelOffer(minter, {offerMinterToAnyone}));
3141 minterCount--;
3142
3143 // The destination of a sell offer can cancel the NFT owner's
3144 // unexpired offer.
3145 env(token::cancelOffer(issuer, {offerMinterToIssuer}));
3146 minterCount--;
3147
3148 // Close enough ledgers to get past the expiration.
3149 while (lastClose(env) < expiration)
3150 env.close();
3151
3152 BEAST_EXPECT(ownerCount(env, issuer) == issuerCount);
3153 BEAST_EXPECT(ownerCount(env, minter) == minterCount);
3154 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
3155
3156 // Anyone can cancel expired offers.
3157 env(token::cancelOffer(issuer, {offerBuyerToMinter}));
3158 buyerCount--;
3159 env(token::cancelOffer(buyer, {offerIssuerToMinter}));
3160 issuerCount--;
3161 env.close();
3162 BEAST_EXPECT(ownerCount(env, issuer) == issuerCount);
3163 BEAST_EXPECT(ownerCount(env, minter) == minterCount);
3164 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
3165 }
3166 // Show that:
3167 // 1. An unexpired sell offer with an expiration can be accepted.
3168 // 2. An expired sell offer cannot be accepted and remains
3169 // in ledger after the accept fails.
3170 {
3171 std::uint32_t const expiration = lastClose(env) + 25;
3172
3173 UInt256 const offer0 =
3174 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
3175 env(token::createOffer(minter, nftokenID0, drops(1)),
3176 token::Expiration(expiration),
3177 Txflags(tfSellNFToken));
3178 minterCount++;
3179
3180 UInt256 const offer1 =
3181 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
3182 env(token::createOffer(minter, nftokenID1, drops(1)),
3183 token::Expiration(expiration),
3184 Txflags(tfSellNFToken));
3185 minterCount++;
3186 env.close();
3187 BEAST_EXPECT(lastClose(env) < expiration);
3188 BEAST_EXPECT(ownerCount(env, issuer) == issuerCount);
3189 BEAST_EXPECT(ownerCount(env, minter) == minterCount);
3190 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
3191
3192 // Anyone can accept an unexpired sell offer.
3193 env(token::acceptSellOffer(buyer, offer0));
3194 minterCount--;
3195 buyerCount++;
3196
3197 // Close enough ledgers to get past the expiration.
3198 while (lastClose(env) < expiration)
3199 env.close();
3200
3201 BEAST_EXPECT(ownerCount(env, issuer) == issuerCount);
3202 BEAST_EXPECT(ownerCount(env, minter) == minterCount);
3203 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
3204
3205 // No one can accept an expired sell offer.
3206 env(token::acceptSellOffer(buyer, offer1), Ter(tecEXPIRED));
3207
3208 // With fixCleanup3_1_3 amendment, the first accept
3209 // attempt deletes the expired offer. Without the amendment,
3210 // the offer remains and we can try to accept it again.
3211 if (features[fixCleanup3_1_3])
3212 {
3213 // After amendment: offer was deleted by first accept attempt
3214 minterCount--;
3215 env(token::acceptSellOffer(issuer, offer1), Ter(tecOBJECT_NOT_FOUND));
3216 }
3217 else
3218 {
3219 // Before amendment: offer still exists, second accept also
3220 // fails
3221 env(token::acceptSellOffer(issuer, offer1), Ter(tecEXPIRED));
3222 }
3223 env.close();
3224
3225 // Check if the expired sell offer behavior matches amendment status
3226 BEAST_EXPECT(ownerCount(env, issuer) == issuerCount);
3227 BEAST_EXPECT(ownerCount(env, minter) == minterCount);
3228 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
3229
3230 if (!features[fixCleanup3_1_3])
3231 {
3232 // Before amendment: expired offer still exists and needs to be
3233 // cancelled
3234 env(token::cancelOffer(issuer, {offer1}));
3235 env.close();
3236 minterCount--;
3237 }
3238 // Ensure that owner counts are correct with and without the
3239 // amendment
3240 BEAST_EXPECT(ownerCount(env, issuer) == 0 && issuerCount == 0);
3241 BEAST_EXPECT(ownerCount(env, minter) == 1 && minterCount == 1);
3242 BEAST_EXPECT(ownerCount(env, buyer) == 1 && buyerCount == 1);
3243
3244 // Transfer nftokenID0 back to minter so we start the next test in
3245 // a simple place.
3246 UInt256 const offerSellBack =
3247 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
3248 env(token::createOffer(buyer, nftokenID0, XRP(0)),
3249 Txflags(tfSellNFToken),
3250 token::Destination(minter));
3251 env.close();
3252 env(token::acceptSellOffer(minter, offerSellBack));
3253 buyerCount--;
3254 env.close();
3255 BEAST_EXPECT(ownerCount(env, issuer) == issuerCount);
3256 BEAST_EXPECT(ownerCount(env, minter) == minterCount);
3257 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
3258 }
3259 // Show that:
3260 // 1. An unexpired buy offer with an expiration can be accepted.
3261 // 2. An expired buy offer cannot be accepted and remains
3262 // in ledger after the accept fails.
3263 {
3264 std::uint32_t const expiration = lastClose(env) + 25;
3265
3266 UInt256 const offer0 =
3267 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
3268 env(token::createOffer(buyer, nftokenID0, drops(1)),
3269 token::Owner(minter),
3270 token::Expiration(expiration));
3271 buyerCount++;
3272
3273 UInt256 const offer1 =
3274 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
3275 env(token::createOffer(buyer, nftokenID1, drops(1)),
3276 token::Owner(minter),
3277 token::Expiration(expiration));
3278 buyerCount++;
3279 env.close();
3280 BEAST_EXPECT(lastClose(env) < expiration);
3281 BEAST_EXPECT(ownerCount(env, issuer) == issuerCount);
3282 BEAST_EXPECT(ownerCount(env, minter) == minterCount);
3283 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
3284
3285 // An unexpired buy offer can be accepted.
3286 env(token::acceptBuyOffer(minter, offer0));
3287
3288 // Close enough ledgers to get past the expiration.
3289 while (lastClose(env) < expiration)
3290 env.close();
3291
3292 BEAST_EXPECT(ownerCount(env, issuer) == issuerCount);
3293 BEAST_EXPECT(ownerCount(env, minter) == minterCount);
3294 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
3295
3296 // An expired buy offer cannot be accepted.
3297 env(token::acceptBuyOffer(minter, offer1), Ter(tecEXPIRED));
3298
3299 // With fixCleanup3_1_3 amendment, the first accept
3300 // attempt deletes the expired offer. Without the amendment,
3301 // the offer remains and we can try to accept it again.
3302 if (features[fixCleanup3_1_3])
3303 {
3304 // After amendment: offer was deleted by first accept attempt
3305 buyerCount--;
3306 env(token::acceptBuyOffer(issuer, offer1), Ter(tecOBJECT_NOT_FOUND));
3307 }
3308 else
3309 {
3310 // Before amendment: offer still exists, second accept also
3311 // fails
3312 env(token::acceptBuyOffer(issuer, offer1), Ter(tecEXPIRED));
3313 }
3314 env.close();
3315
3316 // Check if the expired buy offer behavior matches amendment status
3317 BEAST_EXPECT(ownerCount(env, issuer) == issuerCount);
3318 BEAST_EXPECT(ownerCount(env, minter) == minterCount);
3319 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
3320
3321 if (!features[fixCleanup3_1_3])
3322 {
3323 // Before amendment: expired offer still exists and can be
3324 // cancelled
3325 env(token::cancelOffer(issuer, {offer1}));
3326 env.close();
3327 buyerCount--;
3328 }
3329 // Ensure that owner counts are the same with and without the
3330 // amendment
3331 BEAST_EXPECT(ownerCount(env, issuer) == 0 && issuerCount == 0);
3332 BEAST_EXPECT(ownerCount(env, minter) == 1 && minterCount == 1);
3333 BEAST_EXPECT(ownerCount(env, buyer) == 1 && buyerCount == 1);
3334
3335 // Transfer nftokenID0 back to minter so we start the next test in
3336 // a simple place.
3337 UInt256 const offerSellBack =
3338 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
3339 env(token::createOffer(buyer, nftokenID0, XRP(0)),
3340 Txflags(tfSellNFToken),
3341 token::Destination(minter));
3342 env.close();
3343 env(token::acceptSellOffer(minter, offerSellBack));
3344 env.close();
3345 buyerCount--;
3346 BEAST_EXPECT(ownerCount(env, issuer) == issuerCount);
3347 BEAST_EXPECT(ownerCount(env, minter) == minterCount);
3348 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
3349 }
3350 // Show that in brokered mode:
3351 // 1. An unexpired sell offer with an expiration can be accepted.
3352 // 2. An expired sell offer cannot be accepted and remains
3353 // in ledger after the accept fails.
3354 {
3355 std::uint32_t const expiration = lastClose(env) + 25;
3356
3357 UInt256 const sellOffer0 =
3358 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
3359 env(token::createOffer(minter, nftokenID0, drops(1)),
3360 token::Expiration(expiration),
3361 Txflags(tfSellNFToken));
3362 minterCount++;
3363
3364 UInt256 const sellOffer1 =
3365 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
3366 env(token::createOffer(minter, nftokenID1, drops(1)),
3367 token::Expiration(expiration),
3368 Txflags(tfSellNFToken));
3369 minterCount++;
3370
3371 UInt256 const buyOffer0 =
3372 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
3373 env(token::createOffer(buyer, nftokenID0, drops(1)), token::Owner(minter));
3374 buyerCount++;
3375
3376 UInt256 const buyOffer1 =
3377 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
3378 env(token::createOffer(buyer, nftokenID1, drops(1)), token::Owner(minter));
3379 buyerCount++;
3380
3381 env.close();
3382 BEAST_EXPECT(lastClose(env) < expiration);
3383 BEAST_EXPECT(ownerCount(env, issuer) == issuerCount);
3384 BEAST_EXPECT(ownerCount(env, minter) == minterCount);
3385 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
3386
3387 // An unexpired offer can be brokered.
3388 env(token::brokerOffers(issuer, buyOffer0, sellOffer0));
3389 minterCount--;
3390
3391 // Close enough ledgers to get past the expiration.
3392 while (lastClose(env) < expiration)
3393 env.close();
3394
3395 BEAST_EXPECT(ownerCount(env, issuer) == issuerCount);
3396 BEAST_EXPECT(ownerCount(env, minter) == minterCount);
3397 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
3398
3399 // If the sell offer is expired it cannot be brokered.
3400 env(token::brokerOffers(issuer, buyOffer1, sellOffer1), Ter(tecEXPIRED));
3401 env.close();
3402
3403 if (features[fixCleanup3_1_3])
3404 {
3405 // With amendment: expired offers are deleted
3406 minterCount--;
3407 }
3408
3409 BEAST_EXPECT(ownerCount(env, issuer) == issuerCount);
3410 BEAST_EXPECT(ownerCount(env, minter) == minterCount);
3411 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
3412
3413 if (features[fixCleanup3_1_3])
3414 {
3415 // The buy offer was deleted, so no need to cancel it
3416 // The sell offer still exists, so we can cancel it
3417 env(token::cancelOffer(buyer, {buyOffer1}));
3418 buyerCount--;
3419 }
3420 else
3421 {
3422 // Anyone can cancel the expired offers
3423 env(token::cancelOffer(buyer, {buyOffer1, sellOffer1}));
3424 minterCount--;
3425 buyerCount--;
3426 }
3427 env.close();
3428 // Ensure that owner counts are the same with and without the
3429 // amendment
3430 BEAST_EXPECT(ownerCount(env, issuer) == 0 && issuerCount == 0);
3431 BEAST_EXPECT(ownerCount(env, minter) == 1 && minterCount == 1);
3432 BEAST_EXPECT(ownerCount(env, buyer) == 1 && buyerCount == 1);
3433
3434 // Transfer nftokenID0 back to minter so we start the next test in
3435 // a simple place.
3436 UInt256 const offerSellBack =
3437 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
3438 env(token::createOffer(buyer, nftokenID0, XRP(0)),
3439 Txflags(tfSellNFToken),
3440 token::Destination(minter));
3441 env.close();
3442 env(token::acceptSellOffer(minter, offerSellBack));
3443 env.close();
3444 buyerCount--;
3445 BEAST_EXPECT(ownerCount(env, issuer) == issuerCount);
3446 BEAST_EXPECT(ownerCount(env, minter) == minterCount);
3447 BEAST_EXPECT(ownerCount(env, buyer) == buyerCount);
3448 }
3449 // Show that in brokered mode:
3450 // 1. An unexpired buy offer with an expiration can be accepted.
3451 // 2. An expired buy offer cannot be accepted and remains
3452 // in ledger after the accept fails.
3453 {
3454 std::uint32_t const expiration = lastClose(env) + 25;
3455
3456 UInt256 const sellOffer0 =
3457 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
3458 env(token::createOffer(minter, nftokenID0, drops(1)), Txflags(tfSellNFToken));
3459
3460 UInt256 const sellOffer1 =
3461 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
3462 env(token::createOffer(minter, nftokenID1, drops(1)), Txflags(tfSellNFToken));
3463
3464 UInt256 const buyOffer0 =
3465 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
3466 env(token::createOffer(buyer, nftokenID0, drops(1)),
3467 token::Expiration(expiration),
3468 token::Owner(minter));
3469
3470 UInt256 const buyOffer1 =
3471 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
3472 env(token::createOffer(buyer, nftokenID1, drops(1)),
3473 token::Expiration(expiration),
3474 token::Owner(minter));
3475
3476 env.close();
3477 BEAST_EXPECT(lastClose(env) < expiration);
3478 BEAST_EXPECT(ownerCount(env, issuer) == 0);
3479 BEAST_EXPECT(ownerCount(env, minter) == 3);
3480 BEAST_EXPECT(ownerCount(env, buyer) == 2);
3481
3482 // An unexpired offer can be brokered.
3483 env(token::brokerOffers(issuer, buyOffer0, sellOffer0));
3484
3485 // Close enough ledgers to get past the expiration.
3486 while (lastClose(env) < expiration)
3487 env.close();
3488
3489 BEAST_EXPECT(ownerCount(env, issuer) == 0);
3490 BEAST_EXPECT(ownerCount(env, minter) == 2);
3491 BEAST_EXPECT(ownerCount(env, buyer) == 2);
3492
3493 env(token::brokerOffers(issuer, buyOffer1, sellOffer1), Ter(tecEXPIRED));
3494 env.close();
3495
3496 BEAST_EXPECT(ownerCount(env, issuer) == 0);
3497 if (features[fixCleanup3_1_3])
3498 {
3499 // After amendment: expired offers were deleted during broker
3500 // attempt
3501 BEAST_EXPECT(ownerCount(env, minter) == 2);
3502 BEAST_EXPECT(ownerCount(env, buyer) == 1);
3503 // The buy offer was deleted, so no need to cancel it
3504 // The sell offer still exists, so we can cancel it
3505 env(token::cancelOffer(minter, {sellOffer1}));
3506 }
3507 else
3508 {
3509 // Before amendment: expired offers still exist in ledger
3510 BEAST_EXPECT(ownerCount(env, minter) == 2);
3511 BEAST_EXPECT(ownerCount(env, buyer) == 2);
3512 // Anyone can cancel the expired offers
3513 env(token::cancelOffer(minter, {buyOffer1, sellOffer1}));
3514 }
3515 env.close();
3516 BEAST_EXPECT(ownerCount(env, issuer) == 0);
3517 BEAST_EXPECT(ownerCount(env, minter) == 1);
3518 BEAST_EXPECT(ownerCount(env, buyer) == 1);
3519
3520 // Transfer nftokenID0 back to minter so we start the next test in
3521 // a simple place.
3522 UInt256 const offerSellBack =
3523 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
3524 env(token::createOffer(buyer, nftokenID0, XRP(0)),
3525 Txflags(tfSellNFToken),
3526 token::Destination(minter));
3527 env.close();
3528 env(token::acceptSellOffer(minter, offerSellBack));
3529 env.close();
3530 BEAST_EXPECT(ownerCount(env, issuer) == 0);
3531 BEAST_EXPECT(ownerCount(env, minter) == 1);
3532 BEAST_EXPECT(ownerCount(env, buyer) == 0);
3533 }
3534 // Show that in brokered mode:
3535 // 1. An unexpired buy/sell offer pair with an expiration can be
3536 // accepted.
3537 // 2. An expired buy/sell offer pair cannot be accepted and they
3538 // remain in ledger after the accept fails.
3539 {
3540 std::uint32_t const expiration = lastClose(env) + 25;
3541
3542 UInt256 const sellOffer0 =
3543 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
3544 env(token::createOffer(minter, nftokenID0, drops(1)),
3545 token::Expiration(expiration),
3546 Txflags(tfSellNFToken));
3547
3548 UInt256 const sellOffer1 =
3549 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
3550 env(token::createOffer(minter, nftokenID1, drops(1)),
3551 token::Expiration(expiration),
3552 Txflags(tfSellNFToken));
3553
3554 UInt256 const buyOffer0 =
3555 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
3556 env(token::createOffer(buyer, nftokenID0, drops(1)),
3557 token::Expiration(expiration),
3558 token::Owner(minter));
3559
3560 UInt256 const buyOffer1 =
3561 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
3562 env(token::createOffer(buyer, nftokenID1, drops(1)),
3563 token::Expiration(expiration),
3564 token::Owner(minter));
3565
3566 env.close();
3567 BEAST_EXPECT(lastClose(env) < expiration);
3568 BEAST_EXPECT(ownerCount(env, issuer) == 0);
3569 BEAST_EXPECT(ownerCount(env, minter) == 3);
3570 BEAST_EXPECT(ownerCount(env, buyer) == 2);
3571
3572 // Unexpired offers can be brokered.
3573 env(token::brokerOffers(issuer, buyOffer0, sellOffer0));
3574
3575 // Close enough ledgers to get past the expiration.
3576 while (lastClose(env) < expiration)
3577 env.close();
3578
3579 BEAST_EXPECT(ownerCount(env, issuer) == 0);
3580 BEAST_EXPECT(ownerCount(env, minter) == 2);
3581 BEAST_EXPECT(ownerCount(env, buyer) == 2);
3582
3583 env(token::brokerOffers(issuer, buyOffer1, sellOffer1), Ter(tecEXPIRED));
3584 env.close();
3585
3586 // The expired offers are still in the ledger.
3587 BEAST_EXPECT(ownerCount(env, issuer) == 0);
3588 if (!features[fixCleanup3_1_3])
3589 {
3590 // Before amendment: expired offers still exist in ledger
3591 BEAST_EXPECT(ownerCount(env, minter) == 2);
3592 BEAST_EXPECT(ownerCount(env, buyer) == 2);
3593 // Anyone can cancel the expired offers
3594 env(token::cancelOffer(issuer, {buyOffer1, sellOffer1}));
3595 }
3596 env.close();
3597 BEAST_EXPECT(ownerCount(env, issuer) == 0);
3598 BEAST_EXPECT(ownerCount(env, minter) == 1);
3599 BEAST_EXPECT(ownerCount(env, buyer) == 1);
3600
3601 // Transfer nftokenID0 back to minter so we start the next test in
3602 // a simple place.
3603 UInt256 const offerSellBack =
3604 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
3605 env(token::createOffer(buyer, nftokenID0, XRP(0)),
3606 Txflags(tfSellNFToken),
3607 token::Destination(minter));
3608 env.close();
3609 env(token::acceptSellOffer(minter, offerSellBack));
3610 env.close();
3611 BEAST_EXPECT(ownerCount(env, issuer) == 0);
3612 BEAST_EXPECT(ownerCount(env, minter) == 1);
3613 BEAST_EXPECT(ownerCount(env, buyer) == 0);
3614 }
3615 }
3616
3617 void
3619 {
3620 // Look at offer canceling.
3621 testcase("Cancel offers");
3622
3623 using namespace test::jtx;
3624
3625 Env env{*this, features};
3626
3627 Account const alice("alice");
3628 Account const becky("becky");
3629 Account const minter("minter");
3630 env.fund(XRP(50000), alice, becky, minter);
3631 env.close();
3632
3633 // alice has a minter to see if minters have offer canceling permission.
3634 env(token::setMinter(alice, minter));
3635 env.close();
3636
3637 UInt256 const nftokenID = token::getNextID(env, alice, 0, tfTransferable);
3638 env(token::mint(alice, 0), Txflags(tfTransferable));
3639 env.close();
3640
3641 // Anyone can cancel an expired offer.
3642 UInt256 const expiredOfferIndex =
3643 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
3644
3645 env(token::createOffer(alice, nftokenID, XRP(1000)),
3646 Txflags(tfSellNFToken),
3647 token::Expiration(lastClose(env) + 13));
3648 env.close();
3649
3650 // The offer has not expired yet, so becky can't cancel it now.
3651 BEAST_EXPECT(ownerCount(env, alice) == 2);
3652 env(token::cancelOffer(becky, {expiredOfferIndex}), Ter(tecNO_PERMISSION));
3653 env.close();
3654
3655 // Close a couple of ledgers and advance the time. Then becky
3656 // should be able to cancel the (now) expired offer.
3657 env.close();
3658 env.close();
3659 env(token::cancelOffer(becky, {expiredOfferIndex}));
3660 env.close();
3661 BEAST_EXPECT(ownerCount(env, alice) == 1);
3662
3663 // Create a couple of offers with a destination. Those offers
3664 // should be cancellable by the creator and the destination.
3665 UInt256 const dest1OfferIndex =
3666 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
3667
3668 env(token::createOffer(alice, nftokenID, XRP(1000)),
3669 token::Destination(becky),
3670 Txflags(tfSellNFToken));
3671 env.close();
3672 BEAST_EXPECT(ownerCount(env, alice) == 2);
3673
3674 // Minter can't cancel that offer, but becky (the destination) can.
3675 env(token::cancelOffer(minter, {dest1OfferIndex}), Ter(tecNO_PERMISSION));
3676 env.close();
3677 BEAST_EXPECT(ownerCount(env, alice) == 2);
3678
3679 env(token::cancelOffer(becky, {dest1OfferIndex}));
3680 env.close();
3681 BEAST_EXPECT(ownerCount(env, alice) == 1);
3682
3683 // alice can cancel her own offer, even if becky is the destination.
3684 UInt256 const dest2OfferIndex =
3685 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
3686
3687 env(token::createOffer(alice, nftokenID, XRP(1000)),
3688 token::Destination(becky),
3689 Txflags(tfSellNFToken));
3690 env.close();
3691 BEAST_EXPECT(ownerCount(env, alice) == 2);
3692
3693 env(token::cancelOffer(alice, {dest2OfferIndex}));
3694 env.close();
3695 BEAST_EXPECT(ownerCount(env, alice) == 1);
3696
3697 // The issuer has no special permissions regarding offer cancellation.
3698 // Minter creates a token with alice as issuer. alice cannot cancel
3699 // minter's offer.
3700 UInt256 const mintersNFTokenID = token::getNextID(env, alice, 0, tfTransferable);
3701 env(token::mint(minter, 0), token::Issuer(alice), Txflags(tfTransferable));
3702 env.close();
3703
3704 UInt256 const minterOfferIndex =
3705 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
3706
3707 env(token::createOffer(minter, mintersNFTokenID, XRP(1000)), Txflags(tfSellNFToken));
3708 env.close();
3709 BEAST_EXPECT(ownerCount(env, minter) == 2);
3710
3711 // Nobody other than minter should be able to cancel minter's offer.
3712 env(token::cancelOffer(alice, {minterOfferIndex}), Ter(tecNO_PERMISSION));
3713 env(token::cancelOffer(becky, {minterOfferIndex}), Ter(tecNO_PERMISSION));
3714 env.close();
3715 BEAST_EXPECT(ownerCount(env, minter) == 2);
3716
3717 env(token::cancelOffer(minter, {minterOfferIndex}));
3718 env.close();
3719 BEAST_EXPECT(ownerCount(env, minter) == 1);
3720 }
3721
3722 void
3724 {
3725 // Look at the case where too many offers are passed in a cancel.
3726 testcase("Cancel too many offers");
3727
3728 using namespace test::jtx;
3729
3730 Env env{*this, features};
3731
3732 // We want to maximize the metadata from a cancel offer transaction to
3733 // make sure we don't hit metadata limits. The way we'll do that is:
3734 //
3735 // 1. Generate twice as many separate funded accounts as we have
3736 // offers.
3737 // 2.
3738 // a. One of these accounts mints an NFT with a full URL.
3739 // b. The other account makes an offer that will expire soon.
3740 // 3. After all of these offers have expired, cancel all of the
3741 // expired offers in a single transaction.
3742 //
3743 // I can't think of any way to increase the metadata beyond this,
3744 // but I'm open to ideas.
3745 Account const alice("alice");
3746 env.fund(XRP(1000), alice);
3747 env.close();
3748
3749 std::string const uri(kMaxTokenUriLength, '?');
3750 std::vector<UInt256> offerIndexes;
3751 offerIndexes.reserve(kMaxTokenOfferCancelCount + 1);
3752 for (uint32_t i = 0; i < kMaxTokenOfferCancelCount + 1; ++i)
3753 {
3754 Account const nftAcct(std::string("nftAcct") + std::to_string(i));
3755 Account const offerAcct(std::string("offerAcct") + std::to_string(i));
3756 env.fund(XRP(1000), nftAcct, offerAcct);
3757 env.close();
3758
3759 UInt256 const nftokenID = token::getNextID(env, nftAcct, 0, tfTransferable);
3760 env(token::mint(nftAcct, 0), token::Uri(uri), Txflags(tfTransferable));
3761 env.close();
3762
3763 offerIndexes.push_back(
3764 keylet::nftokenOffer(offerAcct, SeqProxy::rawSequence(env.seq(offerAcct))).key);
3765 env(token::createOffer(offerAcct, nftokenID, drops(1)),
3766 token::Owner(nftAcct),
3767 token::Expiration(lastClose(env) + 5));
3768 env.close();
3769 }
3770
3771 // Close the ledger so the last of the offers expire.
3772 env.close();
3773
3774 // All offers should be in the ledger.
3775 for (UInt256 const& offerIndex : offerIndexes)
3776 {
3777 BEAST_EXPECT(env.le(keylet::nftokenOffer(offerIndex)));
3778 }
3779
3780 // alice attempts to cancel all of the expired offers. There is one
3781 // too many so the request fails.
3782 env(token::cancelOffer(alice, offerIndexes), Ter(temMALFORMED));
3783 env.close();
3784
3785 // However alice can cancel just one of the offers.
3786 env(token::cancelOffer(alice, {offerIndexes.back()}));
3787 env.close();
3788
3789 // Verify that offer is gone from the ledger.
3790 BEAST_EXPECT(!env.le(keylet::nftokenOffer(offerIndexes.back())));
3791 offerIndexes.pop_back();
3792
3793 // But alice adds a sell offer to the list...
3794 {
3795 UInt256 const nftokenID = token::getNextID(env, alice, 0, tfTransferable);
3796 env(token::mint(alice, 0), token::Uri(uri), Txflags(tfTransferable));
3797 env.close();
3798
3799 offerIndexes.push_back(
3800 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key);
3801 env(token::createOffer(alice, nftokenID, drops(1)), Txflags(tfSellNFToken));
3802 env.close();
3803
3804 // alice's owner count should now to 2 for the nft and the offer.
3805 BEAST_EXPECT(ownerCount(env, alice) == 2);
3806
3807 // Because alice added the sell offer there are still too many
3808 // offers in the list to cancel.
3809 env(token::cancelOffer(alice, offerIndexes), Ter(temMALFORMED));
3810 env.close();
3811
3812 // alice burns her nft which removes the nft and the offer.
3813 env(token::burn(alice, nftokenID));
3814 env.close();
3815
3816 // If alice's owner count is zero we can see that the offer
3817 // and nft are both gone.
3818 BEAST_EXPECT(ownerCount(env, alice) == 0);
3819 offerIndexes.pop_back();
3820 }
3821
3822 // Now there are few enough offers in the list that they can all
3823 // be cancelled in a single transaction.
3824 env(token::cancelOffer(alice, offerIndexes));
3825 env.close();
3826
3827 // Verify that remaining offers are gone from the ledger.
3828 for (UInt256 const& offerIndex : offerIndexes)
3829 {
3830 BEAST_EXPECT(!env.le(keylet::nftokenOffer(offerIndex)));
3831 }
3832 }
3833
3834 void
3836 {
3837 // Look at the case where too many offers are passed in a cancel.
3838 testcase("Brokered NFT offer accept");
3839
3840 using namespace test::jtx;
3841
3842 {
3843 Env env{*this, features};
3844 auto const baseFee = env.current()->fees().base;
3845
3846 // The most important thing to explore here is the way funds are
3847 // assigned from the buyer to...
3848 // o the Seller,
3849 // o the Broker, and
3850 // o the Issuer (in the case of a transfer fee).
3851
3852 Account const issuer{"issuer"};
3853 Account const minter{"minter"};
3854 Account const buyer{"buyer"};
3855 Account const broker{"broker"};
3856 Account const gw{"gw"};
3857 IOU const gwXAU(gw["XAU"]);
3858
3859 env.fund(XRP(1000), issuer, minter, buyer, broker, gw);
3860 env.close();
3861
3862 env(trust(issuer, gwXAU(2000)));
3863 env(trust(minter, gwXAU(2000)));
3864 env(trust(buyer, gwXAU(2000)));
3865 env(trust(broker, gwXAU(2000)));
3866 env.close();
3867
3868 env(token::setMinter(issuer, minter));
3869 env.close();
3870
3871 // Lambda to check owner count of all accounts is one.
3872 auto const checkOwnerCountIsOne =
3873 [this, &env](
3875 int line) {
3876 for (Account const& acct : accounts)
3877 {
3878 if (std::uint32_t const ownerCount = test::jtx::ownerCount(env, acct);
3879 ownerCount != 1)
3880 {
3882 ss << "Account " << acct.human() << " expected ownerCount == 1. Got "
3883 << ownerCount;
3884 fail(ss.str(), __FILE__, line);
3885 }
3886 }
3887 };
3888
3889 // Lambda that mints an NFT and returns the nftID.
3890 auto const mintNFT = [&env, &issuer, &minter](std::uint16_t xferFee = 0) {
3891 UInt256 const nftID = token::getNextID(env, issuer, 0, tfTransferable, xferFee);
3892 env(token::mint(minter, 0),
3893 token::Issuer(issuer),
3894 token::XferFee(xferFee),
3895 Txflags(tfTransferable));
3896 env.close();
3897 return nftID;
3898 };
3899
3900 // o Seller is selling for zero XRP.
3901 // o Broker charges no fee.
3902 // o No transfer fee.
3903 //
3904 // Since minter is selling for zero the currency must be XRP.
3905 {
3906 checkOwnerCountIsOne({issuer, minter, buyer, broker}, __LINE__);
3907
3908 UInt256 const nftID = mintNFT();
3909
3910 // minter creates their offer.
3911 UInt256 const minterOfferIndex =
3912 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
3913 env(token::createOffer(minter, nftID, XRP(0)), Txflags(tfSellNFToken));
3914 env.close();
3915
3916 // buyer creates their offer. Note: a buy offer can never
3917 // offer zero.
3918 UInt256 const buyOfferIndex =
3919 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
3920 env(token::createOffer(buyer, nftID, XRP(1)), token::Owner(minter));
3921 env.close();
3922
3923 auto const minterBalance = env.balance(minter);
3924 auto const buyerBalance = env.balance(buyer);
3925 auto const brokerBalance = env.balance(broker);
3926 auto const issuerBalance = env.balance(issuer);
3927
3928 // Broker charges no brokerFee.
3929 env(token::brokerOffers(broker, buyOfferIndex, minterOfferIndex));
3930 env.close();
3931
3932 // Note that minter's XRP balance goes up even though they
3933 // requested XRP(0).
3934 BEAST_EXPECT(env.balance(minter) == minterBalance + XRP(1));
3935 BEAST_EXPECT(env.balance(buyer) == buyerBalance - XRP(1));
3936 BEAST_EXPECT(env.balance(broker) == brokerBalance - baseFee);
3937 BEAST_EXPECT(env.balance(issuer) == issuerBalance);
3938
3939 // Burn the NFT so the next test starts with a clean state.
3940 env(token::burn(buyer, nftID));
3941 env.close();
3942 }
3943
3944 // o Seller is selling for zero XRP.
3945 // o Broker charges a fee.
3946 // o No transfer fee.
3947 //
3948 // Since minter is selling for zero the currency must be XRP.
3949 {
3950 checkOwnerCountIsOne({issuer, minter, buyer, broker}, __LINE__);
3951
3952 UInt256 const nftID = mintNFT();
3953
3954 // minter creates their offer.
3955 UInt256 const minterOfferIndex =
3956 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
3957 env(token::createOffer(minter, nftID, XRP(0)), Txflags(tfSellNFToken));
3958 env.close();
3959
3960 // buyer creates their offer. Note: a buy offer can never
3961 // offer zero.
3962 UInt256 const buyOfferIndex =
3963 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
3964 env(token::createOffer(buyer, nftID, XRP(1)), token::Owner(minter));
3965 env.close();
3966
3967 // Broker attempts to charge a 1.1 XRP brokerFee and fails.
3968 env(token::brokerOffers(broker, buyOfferIndex, minterOfferIndex),
3969 token::BrokerFee(XRP(1.1)),
3971 env.close();
3972
3973 auto const minterBalance = env.balance(minter);
3974 auto const buyerBalance = env.balance(buyer);
3975 auto const brokerBalance = env.balance(broker);
3976 auto const issuerBalance = env.balance(issuer);
3977
3978 // Broker charges a 0.5 XRP brokerFee.
3979 env(token::brokerOffers(broker, buyOfferIndex, minterOfferIndex),
3980 token::BrokerFee(XRP(0.5)));
3981 env.close();
3982
3983 // Note that minter's XRP balance goes up even though they
3984 // requested XRP(0).
3985 BEAST_EXPECT(env.balance(minter) == minterBalance + XRP(0.5));
3986 BEAST_EXPECT(env.balance(buyer) == buyerBalance - XRP(1));
3987 BEAST_EXPECT(env.balance(broker) == brokerBalance + XRP(0.5) - baseFee);
3988 BEAST_EXPECT(env.balance(issuer) == issuerBalance);
3989
3990 // Burn the NFT so the next test starts with a clean state.
3991 env(token::burn(buyer, nftID));
3992 env.close();
3993 }
3994
3995 // o Seller is selling for zero XRP.
3996 // o Broker charges no fee.
3997 // o 50% transfer fee.
3998 //
3999 // Since minter is selling for zero the currency must be XRP.
4000 {
4001 checkOwnerCountIsOne({issuer, minter, buyer, broker}, __LINE__);
4002
4003 UInt256 const nftID = mintNFT(kMaxTransferFee);
4004
4005 // minter creates their offer.
4006 UInt256 const minterOfferIndex =
4007 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
4008 env(token::createOffer(minter, nftID, XRP(0)), Txflags(tfSellNFToken));
4009 env.close();
4010
4011 // buyer creates their offer. Note: a buy offer can never
4012 // offer zero.
4013 UInt256 const buyOfferIndex =
4014 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
4015 env(token::createOffer(buyer, nftID, XRP(1)), token::Owner(minter));
4016 env.close();
4017
4018 auto const minterBalance = env.balance(minter);
4019 auto const buyerBalance = env.balance(buyer);
4020 auto const brokerBalance = env.balance(broker);
4021 auto const issuerBalance = env.balance(issuer);
4022
4023 // Broker charges no brokerFee.
4024 env(token::brokerOffers(broker, buyOfferIndex, minterOfferIndex));
4025 env.close();
4026
4027 // Note that minter's XRP balance goes up even though they
4028 // requested XRP(0).
4029 BEAST_EXPECT(env.balance(minter) == minterBalance + XRP(0.5));
4030 BEAST_EXPECT(env.balance(buyer) == buyerBalance - XRP(1));
4031 BEAST_EXPECT(env.balance(broker) == brokerBalance - baseFee);
4032 BEAST_EXPECT(env.balance(issuer) == issuerBalance + XRP(0.5));
4033
4034 // Burn the NFT so the next test starts with a clean state.
4035 env(token::burn(buyer, nftID));
4036 env.close();
4037 }
4038
4039 // o Seller is selling for zero XRP.
4040 // o Broker charges 0.5 XRP.
4041 // o 50% transfer fee.
4042 //
4043 // Since minter is selling for zero the currency must be XRP.
4044 {
4045 checkOwnerCountIsOne({issuer, minter, buyer, broker}, __LINE__);
4046
4047 UInt256 const nftID = mintNFT(kMaxTransferFee);
4048
4049 // minter creates their offer.
4050 UInt256 const minterOfferIndex =
4051 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
4052 env(token::createOffer(minter, nftID, XRP(0)), Txflags(tfSellNFToken));
4053 env.close();
4054
4055 // buyer creates their offer. Note: a buy offer can never
4056 // offer zero.
4057 UInt256 const buyOfferIndex =
4058 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
4059 env(token::createOffer(buyer, nftID, XRP(1)), token::Owner(minter));
4060 env.close();
4061
4062 auto const minterBalance = env.balance(minter);
4063 auto const buyerBalance = env.balance(buyer);
4064 auto const brokerBalance = env.balance(broker);
4065 auto const issuerBalance = env.balance(issuer);
4066
4067 // Broker charges a 0.75 XRP brokerFee.
4068 env(token::brokerOffers(broker, buyOfferIndex, minterOfferIndex),
4069 token::BrokerFee(XRP(0.75)));
4070 env.close();
4071
4072 // Note that, with a 50% transfer fee, issuer gets 1/2 of what's
4073 // left _after_ broker takes their fee. minter gets the
4074 // remainder after both broker and minter take their cuts
4075 BEAST_EXPECT(env.balance(minter) == minterBalance + XRP(0.125));
4076 BEAST_EXPECT(env.balance(buyer) == buyerBalance - XRP(1));
4077 BEAST_EXPECT(env.balance(broker) == brokerBalance + XRP(0.75) - baseFee);
4078 BEAST_EXPECT(env.balance(issuer) == issuerBalance + XRP(0.125));
4079
4080 // Burn the NFT so the next test starts with a clean state.
4081 env(token::burn(buyer, nftID));
4082 env.close();
4083 }
4084
4085 // Lambda to set the balance of all passed in accounts to
4086 // gwXAU(amount).
4087 auto const setXAUBalance =
4088 [this, &gw, &gwXAU, &env](
4090 int amount,
4091 int line) {
4092 for (Account const& acct : accounts)
4093 {
4094 auto const xauAmt = gwXAU(amount);
4095 auto const balance = env.balance(acct, gwXAU);
4096 if (balance < xauAmt)
4097 {
4098 env(pay(gw, acct, xauAmt - balance));
4099 env.close();
4100 }
4101 else if (balance > xauAmt)
4102 {
4103 env(pay(acct, gw, balance - xauAmt));
4104 env.close();
4105 }
4106 if (env.balance(acct, gwXAU) != xauAmt)
4107 {
4109 ss << "Unable to set " << acct.human() << " account balance to gwXAU("
4110 << amount << ")";
4111 this->fail(ss.str(), __FILE__, line);
4112 }
4113 }
4114 };
4115
4116 // The buyer and seller have identical amounts and there is no
4117 // transfer fee.
4118 {
4119 checkOwnerCountIsOne({issuer, minter, buyer, broker}, __LINE__);
4120 setXAUBalance({issuer, minter, buyer, broker}, 1000, __LINE__);
4121
4122 UInt256 const nftID = mintNFT();
4123
4124 // minter creates their offer.
4125 UInt256 const minterOfferIndex =
4126 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
4127 env(token::createOffer(minter, nftID, gwXAU(1000)), Txflags(tfSellNFToken));
4128 env.close();
4129
4130 {
4131 // buyer creates an offer for more XAU than they currently
4132 // own.
4133 UInt256 const buyOfferIndex =
4134 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
4135 env(token::createOffer(buyer, nftID, gwXAU(1001)), token::Owner(minter));
4136 env.close();
4137
4138 // broker attempts to broker the offers but cannot.
4139 env(token::brokerOffers(broker, buyOfferIndex, minterOfferIndex),
4141 env.close();
4142
4143 // Cancel buyer's bad offer so the next test starts in a
4144 // clean state.
4145 env(token::cancelOffer(buyer, {buyOfferIndex}));
4146 env.close();
4147 }
4148 {
4149 // buyer creates an offer for less that what minter is
4150 // asking.
4151 UInt256 const buyOfferIndex =
4152 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
4153 env(token::createOffer(buyer, nftID, gwXAU(999)), token::Owner(minter));
4154 env.close();
4155
4156 // broker attempts to broker the offers but cannot.
4157 env(token::brokerOffers(broker, buyOfferIndex, minterOfferIndex),
4159 env.close();
4160
4161 // Cancel buyer's bad offer so the next test starts in a
4162 // clean state.
4163 env(token::cancelOffer(buyer, {buyOfferIndex}));
4164 env.close();
4165 }
4166
4167 // buyer creates a large enough offer.
4168 UInt256 const buyOfferIndex =
4169 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
4170 env(token::createOffer(buyer, nftID, gwXAU(1000)), token::Owner(minter));
4171 env.close();
4172
4173 // Broker attempts to charge a brokerFee but cannot.
4174 env(token::brokerOffers(broker, buyOfferIndex, minterOfferIndex),
4175 token::BrokerFee(gwXAU(0.1)),
4177 env.close();
4178
4179 // broker charges no brokerFee and succeeds.
4180 env(token::brokerOffers(broker, buyOfferIndex, minterOfferIndex));
4181 env.close();
4182
4183 BEAST_EXPECT(ownerCount(env, issuer) == 1);
4184 BEAST_EXPECT(ownerCount(env, minter) == 1);
4185 BEAST_EXPECT(ownerCount(env, buyer) == 2);
4186 BEAST_EXPECT(ownerCount(env, broker) == 1);
4187 BEAST_EXPECT(env.balance(issuer, gwXAU) == gwXAU(1000));
4188 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(2000));
4189 BEAST_EXPECT(env.balance(buyer, gwXAU) == gwXAU(0));
4190 BEAST_EXPECT(env.balance(broker, gwXAU) == gwXAU(1000));
4191
4192 // Burn the NFT so the next test starts with a clean state.
4193 env(token::burn(buyer, nftID));
4194 env.close();
4195 }
4196
4197 // seller offers more than buyer is asking.
4198 // There are both transfer and broker fees.
4199 {
4200 checkOwnerCountIsOne({issuer, minter, buyer, broker}, __LINE__);
4201 setXAUBalance({issuer, minter, buyer, broker}, 1000, __LINE__);
4202
4203 UInt256 const nftID = mintNFT(kMaxTransferFee);
4204
4205 // minter creates their offer.
4206 UInt256 const minterOfferIndex =
4207 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
4208 env(token::createOffer(minter, nftID, gwXAU(900)), Txflags(tfSellNFToken));
4209 env.close();
4210 {
4211 // buyer creates an offer for more XAU than they currently
4212 // own.
4213 UInt256 const buyOfferIndex =
4214 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
4215 env(token::createOffer(buyer, nftID, gwXAU(1001)), token::Owner(minter));
4216 env.close();
4217
4218 // broker attempts to broker the offers but cannot.
4219 env(token::brokerOffers(broker, buyOfferIndex, minterOfferIndex),
4221 env.close();
4222
4223 // Cancel buyer's bad offer so the next test starts in a
4224 // clean state.
4225 env(token::cancelOffer(buyer, {buyOfferIndex}));
4226 env.close();
4227 }
4228 {
4229 // buyer creates an offer for less that what minter is
4230 // asking.
4231 UInt256 const buyOfferIndex =
4232 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
4233 env(token::createOffer(buyer, nftID, gwXAU(899)), token::Owner(minter));
4234 env.close();
4235
4236 // broker attempts to broker the offers but cannot.
4237 env(token::brokerOffers(broker, buyOfferIndex, minterOfferIndex),
4239 env.close();
4240
4241 // Cancel buyer's bad offer so the next test starts in a
4242 // clean state.
4243 env(token::cancelOffer(buyer, {buyOfferIndex}));
4244 env.close();
4245 }
4246 // buyer creates a large enough offer.
4247 UInt256 const buyOfferIndex =
4248 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
4249 env(token::createOffer(buyer, nftID, gwXAU(1000)), token::Owner(minter));
4250 env.close();
4251
4252 // Broker attempts to charge a brokerFee larger than the
4253 // difference between the two offers but cannot.
4254 env(token::brokerOffers(broker, buyOfferIndex, minterOfferIndex),
4255 token::BrokerFee(gwXAU(101)),
4257 env.close();
4258
4259 // broker charges the full difference between the two offers and
4260 // succeeds.
4261 env(token::brokerOffers(broker, buyOfferIndex, minterOfferIndex),
4262 token::BrokerFee(gwXAU(100)));
4263 env.close();
4264
4265 BEAST_EXPECT(ownerCount(env, issuer) == 1);
4266 BEAST_EXPECT(ownerCount(env, minter) == 1);
4267 BEAST_EXPECT(ownerCount(env, buyer) == 2);
4268 BEAST_EXPECT(ownerCount(env, broker) == 1);
4269 BEAST_EXPECT(env.balance(issuer, gwXAU) == gwXAU(1450));
4270 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(1450));
4271 BEAST_EXPECT(env.balance(buyer, gwXAU) == gwXAU(0));
4272 BEAST_EXPECT(env.balance(broker, gwXAU) == gwXAU(1100));
4273
4274 // Burn the NFT so the next test starts with a clean state.
4275 env(token::burn(buyer, nftID));
4276 env.close();
4277 }
4278 // seller offers more than buyer is asking.
4279 // There are both transfer and broker fees, but broker takes less
4280 // than the maximum.
4281 {
4282 checkOwnerCountIsOne({issuer, minter, buyer, broker}, __LINE__);
4283 setXAUBalance({issuer, minter, buyer, broker}, 1000, __LINE__);
4284
4285 UInt256 const nftID = mintNFT(kMaxTransferFee / 2); // 25%
4286
4287 // minter creates their offer.
4288 UInt256 const minterOfferIndex =
4289 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
4290 env(token::createOffer(minter, nftID, gwXAU(900)), Txflags(tfSellNFToken));
4291 env.close();
4292
4293 // buyer creates a large enough offer.
4294 UInt256 const buyOfferIndex =
4295 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
4296 env(token::createOffer(buyer, nftID, gwXAU(1000)), token::Owner(minter));
4297 env.close();
4298
4299 // broker charges half difference between the two offers and
4300 // succeeds. 25% of the remaining difference goes to issuer.
4301 // The rest goes to minter.
4302 env(token::brokerOffers(broker, buyOfferIndex, minterOfferIndex),
4303 token::BrokerFee(gwXAU(50)));
4304 env.close();
4305
4306 BEAST_EXPECT(ownerCount(env, issuer) == 1);
4307 BEAST_EXPECT(ownerCount(env, minter) == 1);
4308 BEAST_EXPECT(ownerCount(env, buyer) == 2);
4309 BEAST_EXPECT(ownerCount(env, broker) == 1);
4310 BEAST_EXPECT(env.balance(issuer, gwXAU) == gwXAU(1237.5));
4311 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(1712.5));
4312 BEAST_EXPECT(env.balance(buyer, gwXAU) == gwXAU(0));
4313 BEAST_EXPECT(env.balance(broker, gwXAU) == gwXAU(1050));
4314
4315 // Burn the NFT so the next test starts with a clean state.
4316 env(token::burn(buyer, nftID));
4317 env.close();
4318 }
4319 // Broker has a balance less than the seller offer
4320 {
4321 checkOwnerCountIsOne({issuer, minter, buyer, broker}, __LINE__);
4322 setXAUBalance({issuer, minter, buyer}, 1000, __LINE__);
4323 setXAUBalance({broker}, 500, __LINE__);
4324 UInt256 const nftID = mintNFT(kMaxTransferFee / 2); // 25%
4325
4326 // minter creates their offer.
4327 UInt256 const minterOfferIndex =
4328 keylet::nftokenOffer(minter, SeqProxy::rawSequence(env.seq(minter))).key;
4329 env(token::createOffer(minter, nftID, gwXAU(900)), Txflags(tfSellNFToken));
4330 env.close();
4331
4332 // buyer creates a large enough offer.
4333 UInt256 const buyOfferIndex =
4334 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
4335 env(token::createOffer(buyer, nftID, gwXAU(1000)), token::Owner(minter));
4336 env.close();
4337
4338 env(token::brokerOffers(broker, buyOfferIndex, minterOfferIndex),
4339 token::BrokerFee(gwXAU(50)));
4340 env.close();
4341 BEAST_EXPECT(ownerCount(env, issuer) == 1);
4342 BEAST_EXPECT(ownerCount(env, minter) == 1);
4343 BEAST_EXPECT(ownerCount(env, buyer) == 2);
4344 BEAST_EXPECT(ownerCount(env, broker) == 1);
4345 BEAST_EXPECT(env.balance(issuer, gwXAU) == gwXAU(1237.5));
4346 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(1712.5));
4347 BEAST_EXPECT(env.balance(buyer, gwXAU) == gwXAU(0));
4348 BEAST_EXPECT(env.balance(broker, gwXAU) == gwXAU(550));
4349
4350 // Burn the NFT so the next test starts with a clean state.
4351 env(token::burn(buyer, nftID));
4352 env.close();
4353 }
4354 }
4355 }
4356
4357 void
4359 {
4360 // Verify the Owner field of an offer behaves as expected.
4361 testcase("NFToken offer owner");
4362
4363 using namespace test::jtx;
4364
4365 Env env{*this, features};
4366
4367 Account const issuer{"issuer"};
4368 Account const buyer1{"buyer1"};
4369 Account const buyer2{"buyer2"};
4370 env.fund(XRP(10000), issuer, buyer1, buyer2);
4371 env.close();
4372
4373 // issuer creates an NFT.
4374 UInt256 const nftId{token::getNextID(env, issuer, 0u, tfTransferable)};
4375 env(token::mint(issuer, 0u), Txflags(tfTransferable));
4376 env.close();
4377
4378 // Prove that issuer now owns nftId.
4379 BEAST_EXPECT(nftCount(env, issuer) == 1);
4380 BEAST_EXPECT(nftCount(env, buyer1) == 0);
4381 BEAST_EXPECT(nftCount(env, buyer2) == 0);
4382
4383 // Both buyer1 and buyer2 create buy offers for nftId.
4384 UInt256 const buyer1OfferIndex =
4385 keylet::nftokenOffer(buyer1, SeqProxy::rawSequence(env.seq(buyer1))).key;
4386 env(token::createOffer(buyer1, nftId, XRP(100)), token::Owner(issuer));
4387 UInt256 const buyer2OfferIndex =
4388 keylet::nftokenOffer(buyer2, SeqProxy::rawSequence(env.seq(buyer2))).key;
4389 env(token::createOffer(buyer2, nftId, XRP(100)), token::Owner(issuer));
4390 env.close();
4391
4392 // Lambda that counts the number of buy offers for a given NFT.
4393 auto const nftBuyOfferCount = [&env](UInt256 const& nftId) -> std::size_t {
4394 // We know that in this case not very many offers will be
4395 // returned, so we skip the marker stuff.
4396 json::Value params;
4397 params[jss::nft_id] = to_string(nftId);
4398 json::Value buyOffers = env.rpc("json", "nft_buy_offers", to_string(params));
4399
4400 if (buyOffers.isMember(jss::result) && buyOffers[jss::result].isMember(jss::offers))
4401 return buyOffers[jss::result][jss::offers].size();
4402
4403 return 0;
4404 };
4405
4406 // Show there are two buy offers for nftId.
4407 BEAST_EXPECT(nftBuyOfferCount(nftId) == 2);
4408
4409 // issuer accepts buyer1's offer.
4410 env(token::acceptBuyOffer(issuer, buyer1OfferIndex));
4411 env.close();
4412
4413 // Prove that buyer1 now owns nftId.
4414 BEAST_EXPECT(nftCount(env, issuer) == 0);
4415 BEAST_EXPECT(nftCount(env, buyer1) == 1);
4416 BEAST_EXPECT(nftCount(env, buyer2) == 0);
4417
4418 // buyer1's offer was consumed, but buyer2's offer is still in the
4419 // ledger.
4420 BEAST_EXPECT(nftBuyOfferCount(nftId) == 1);
4421
4422 // buyer1 can now accept buyer2's offer, even though buyer2's
4423 // NFTokenCreateOffer transaction specified the NFT Owner as issuer.
4424 env(token::acceptBuyOffer(buyer1, buyer2OfferIndex));
4425 env.close();
4426
4427 // Prove that buyer2 now owns nftId.
4428 BEAST_EXPECT(nftCount(env, issuer) == 0);
4429 BEAST_EXPECT(nftCount(env, buyer1) == 0);
4430 BEAST_EXPECT(nftCount(env, buyer2) == 1);
4431
4432 // All of the NFTokenOffers are now consumed.
4433 BEAST_EXPECT(nftBuyOfferCount(nftId) == 0);
4434 }
4435
4436 void
4438 {
4439 // Make sure all NFToken transactions work with tickets.
4440 testcase("NFToken transactions with tickets");
4441
4442 using namespace test::jtx;
4443
4444 Env env{*this, features};
4445
4446 Account const issuer{"issuer"};
4447 Account const buyer{"buyer"};
4448 env.fund(XRP(10000), issuer, buyer);
4449 env.close();
4450
4451 // issuer and buyer grab enough tickets for all of the following
4452 // transactions. Note that once the tickets are acquired issuer's
4453 // and buyer's account sequence numbers should not advance.
4454 std::uint32_t issuerTicketSeq{env.seq(issuer) + 1};
4455 env(ticket::create(issuer, 10));
4456 env.close();
4457 std::uint32_t const issuerSeq{env.seq(issuer)};
4458 BEAST_EXPECT(ticketCount(env, issuer) == 10);
4459
4460 std::uint32_t buyerTicketSeq{env.seq(buyer) + 1};
4461 env(ticket::create(buyer, 10));
4462 env.close();
4463 std::uint32_t const buyerSeq{env.seq(buyer)};
4464 BEAST_EXPECT(ticketCount(env, buyer) == 10);
4465
4466 // NFTokenMint
4467 BEAST_EXPECT(ownerCount(env, issuer) == 10);
4468 UInt256 const nftId{token::getNextID(env, issuer, 0u, tfTransferable)};
4469 env(token::mint(issuer, 0u), Txflags(tfTransferable), ticket::Use(issuerTicketSeq++));
4470 env.close();
4471 BEAST_EXPECT(ownerCount(env, issuer) == 10);
4472 BEAST_EXPECT(ticketCount(env, issuer) == 9);
4473
4474 // NFTokenCreateOffer
4475 BEAST_EXPECT(ownerCount(env, buyer) == 10);
4476 UInt256 const offerIndex0 =
4477 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(buyerTicketSeq)).key;
4478 env(token::createOffer(buyer, nftId, XRP(1)),
4479 token::Owner(issuer),
4480 ticket::Use(buyerTicketSeq++));
4481 env.close();
4482 BEAST_EXPECT(ownerCount(env, buyer) == 10);
4483 BEAST_EXPECT(ticketCount(env, buyer) == 9);
4484
4485 // NFTokenCancelOffer
4486 env(token::cancelOffer(buyer, {offerIndex0}), ticket::Use(buyerTicketSeq++));
4487 env.close();
4488 BEAST_EXPECT(ownerCount(env, buyer) == 8);
4489 BEAST_EXPECT(ticketCount(env, buyer) == 8);
4490
4491 // NFTokenCreateOffer. buyer tries again.
4492 UInt256 const offerIndex1 =
4493 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(buyerTicketSeq)).key;
4494 env(token::createOffer(buyer, nftId, XRP(2)),
4495 token::Owner(issuer),
4496 ticket::Use(buyerTicketSeq++));
4497 env.close();
4498 BEAST_EXPECT(ownerCount(env, buyer) == 8);
4499 BEAST_EXPECT(ticketCount(env, buyer) == 7);
4500
4501 // NFTokenAcceptOffer. issuer accepts buyer's offer.
4502 env(token::acceptBuyOffer(issuer, offerIndex1), ticket::Use(issuerTicketSeq++));
4503 env.close();
4504 BEAST_EXPECT(ownerCount(env, issuer) == 8);
4505 BEAST_EXPECT(ownerCount(env, buyer) == 8);
4506 BEAST_EXPECT(ticketCount(env, issuer) == 8);
4507
4508 // NFTokenBurn. buyer burns the token they just bought.
4509 env(token::burn(buyer, nftId), ticket::Use(buyerTicketSeq++));
4510 env.close();
4511 BEAST_EXPECT(ownerCount(env, issuer) == 8);
4512 BEAST_EXPECT(ownerCount(env, buyer) == 6);
4513 BEAST_EXPECT(ticketCount(env, buyer) == 6);
4514
4515 // Verify that the account sequence numbers did not advance.
4516 BEAST_EXPECT(env.seq(issuer) == issuerSeq);
4517 BEAST_EXPECT(env.seq(buyer) == buyerSeq);
4518 }
4519
4520 void
4522 {
4523 // Account deletion rules with NFTs:
4524 // 1. An account holding one or more NFT offers may be deleted.
4525 // 2. An NFT issuer with any NFTs they have issued still in the
4526 // ledger may not be deleted.
4527 // 3. An account holding one or more NFTs may not be deleted.
4528 testcase("NFToken delete account");
4529
4530 using namespace test::jtx;
4531
4532 Env env{*this, features};
4533
4534 Account const issuer{"issuer"};
4535 Account const minter{"minter"};
4536 Account const becky{"becky"};
4537 Account const carla{"carla"};
4538 Account const daria{"daria"};
4539
4540 env.fund(XRP(10000), issuer, minter, becky, carla, daria);
4541 env.close();
4542
4543 // Allow enough ledgers to pass so any of these accounts can be deleted.
4544 for (int i = 0; i < 300; ++i)
4545 env.close();
4546
4547 env(token::setMinter(issuer, minter));
4548 env.close();
4549
4550 UInt256 const nftId{token::getNextID(env, issuer, 0u, tfTransferable)};
4551 env(token::mint(minter, 0u), token::Issuer(issuer), Txflags(tfTransferable));
4552 env.close();
4553
4554 // At the moment issuer and minter cannot delete themselves.
4555 // o issuer has an issued NFT in the ledger.
4556 // o minter owns an NFT.
4557 env(acctdelete(issuer, daria), Fee(XRP(50)), Ter(tecHAS_OBLIGATIONS));
4558 env(acctdelete(minter, daria), Fee(XRP(50)), Ter(tecHAS_OBLIGATIONS));
4559 env.close();
4560
4561 // Let enough ledgers pass so the account delete transactions are
4562 // not retried.
4563 for (int i = 0; i < 15; ++i)
4564 env.close();
4565
4566 // becky and carla create offers for minter's NFT.
4567 env(token::createOffer(becky, nftId, XRP(2)), token::Owner(minter));
4568 env.close();
4569
4570 UInt256 const carlaOfferIndex =
4571 keylet::nftokenOffer(carla, SeqProxy::rawSequence(env.seq(carla))).key;
4572 env(token::createOffer(carla, nftId, XRP(3)), token::Owner(minter));
4573 env.close();
4574
4575 // It should be possible for becky to delete herself, even though
4576 // becky has an active NFT offer.
4577 env(acctdelete(becky, daria), Fee(XRP(50)));
4578 env.close();
4579
4580 // minter accepts carla's offer.
4581 env(token::acceptBuyOffer(minter, carlaOfferIndex));
4582 env.close();
4583
4584 // Now it should be possible for minter to delete themselves since
4585 // they no longer own an NFT.
4586 env(acctdelete(minter, daria), Fee(XRP(50)));
4587 env.close();
4588
4589 // 1. issuer cannot delete themselves because they issued an NFT that
4590 // is still in the ledger.
4591 // 2. carla owns an NFT, so she cannot delete herself.
4592 env(acctdelete(issuer, daria), Fee(XRP(50)), Ter(tecHAS_OBLIGATIONS));
4593 env(acctdelete(carla, daria), Fee(XRP(50)), Ter(tecHAS_OBLIGATIONS));
4594 env.close();
4595
4596 // Let enough ledgers pass so the account delete transactions are
4597 // not retried.
4598 for (int i = 0; i < 15; ++i)
4599 env.close();
4600
4601 // carla burns her NFT. Since issuer's NFT is no longer in the
4602 // ledger, both issuer and carla can delete themselves.
4603 env(token::burn(carla, nftId));
4604 env.close();
4605
4606 env(acctdelete(issuer, daria), Fee(XRP(50)));
4607 env(acctdelete(carla, daria), Fee(XRP(50)));
4608 env.close();
4609 }
4610
4611 void
4613 {
4614 testcase("nft_buy_offers and nft_sell_offers");
4615
4616 // The default limit on returned NFToken offers is 250, so we need
4617 // to produce more than 250 offers of each kind in order to exercise
4618 // the marker.
4619
4620 // Fortunately there's nothing in the rules that says an account
4621 // can't hold more than one offer for the same NFT. So we only
4622 // need two accounts to generate the necessary offers.
4623 using namespace test::jtx;
4624
4625 Env env{*this, features};
4626
4627 Account const issuer{"issuer"};
4628 Account const buyer{"buyer"};
4629
4630 // A lot of offers requires a lot for reserve.
4631 env.fund(XRP(1000000), issuer, buyer);
4632 env.close();
4633
4634 // Create an NFT that we'll make offers for.
4635 UInt256 const nftID{token::getNextID(env, issuer, 0u, tfTransferable)};
4636 env(token::mint(issuer, 0), Txflags(tfTransferable));
4637 env.close();
4638
4639 // A lambda that validates nft_XXX_offers query responses.
4640 auto const checkOffers = [this, &env, &nftID](
4641 char const* request,
4642 int expectCount,
4643 int expectMarkerCount,
4644 int line) {
4645 int markerCount = 0;
4647 std::string marker;
4648
4649 // The do/while collects results until no marker is returned.
4650 do
4651 {
4652 json::Value nftOffers = [&env, &nftID, &request, &marker] {
4653 json::Value params;
4654 params[jss::nft_id] = to_string(nftID);
4655
4656 if (!marker.empty())
4657 params[jss::marker] = marker;
4658 return env.rpc("json", request, to_string(params));
4659 }();
4660
4661 // If there are no offers for the NFT we get an error
4662 if (expectCount == 0)
4663 {
4664 if (expect(
4665 nftOffers.isMember(jss::result), "expected \"result\"", __FILE__, line))
4666 {
4667 json::Value const& result = nftOffers[jss::result];
4668 expect(
4669 result[jss::error].asString() == "objectNotFound",
4670 "expected \"objectNotFound\"",
4671 __FILE__,
4672 line);
4673 expect(
4674 result[jss::error_code] == RpcObjectNotFound,
4675 "expected RpcObjectNotFound",
4676 __FILE__,
4677 line);
4678 expect(
4679 result[jss::error_message] == "The requested object was not found.",
4680 "expected \"The requested object was not found.\"",
4681 __FILE__,
4682 line);
4683 }
4684 break;
4685 }
4686
4687 marker.clear();
4688 if (expect(nftOffers.isMember(jss::result), "expected \"result\"", __FILE__, line))
4689 {
4690 json::Value& result = nftOffers[jss::result];
4691
4692 if (result.isMember(jss::marker))
4693 {
4694 ++markerCount;
4695 marker = result[jss::marker].asString();
4696 }
4697
4698 if (expect(result.isMember(jss::offers), "expected \"offers\"", __FILE__, line))
4699 {
4700 json::Value& someOffers = result[jss::offers];
4701 for (std::size_t i = 0; i < someOffers.size(); ++i)
4702 allOffers.append(someOffers[i]);
4703 }
4704 }
4705 } while (!marker.empty());
4706
4707 // Verify the contents of allOffers makes sense.
4708 expect(
4709 allOffers.size() == expectCount, "Unexpected returned offer count", __FILE__, line);
4710 expect(markerCount == expectMarkerCount, "Unexpected marker count", __FILE__, line);
4711 std::optional<int> globalFlags;
4712 std::set<std::string> offerIndexes;
4713 std::set<std::string> amounts;
4714 for (json::Value const& offer : allOffers)
4715 {
4716 // The flags on all found offers should be the same.
4717 if (!globalFlags)
4718 globalFlags = offer[jss::flags].asInt();
4719
4720 expect(
4721 *globalFlags == offer[jss::flags].asInt(),
4722 "Inconsistent flags returned",
4723 __FILE__,
4724 line);
4725
4726 // The test conditions should produce unique indexes and
4727 // amounts for all offers.
4728 offerIndexes.insert(offer[jss::nft_offer_index].asString());
4729 amounts.insert(offer[jss::amount].asString());
4730 }
4731
4732 expect(
4733 offerIndexes.size() == expectCount, "Duplicate indexes returned?", __FILE__, line);
4734 expect(amounts.size() == expectCount, "Duplicate amounts returned?", __FILE__, line);
4735 };
4736
4737 // There are no sell offers.
4738 checkOffers("nft_sell_offers", 0, 0, __LINE__);
4739
4740 // A lambda that generates sell offers.
4741 STAmount sellPrice = XRP(0);
4742 auto const makeSellOffers = [&env, &issuer, &nftID, &sellPrice](STAmount const& limit) {
4743 // Save a little test time by not closing too often.
4744 int offerCount = 0;
4745 while (sellPrice < limit)
4746 {
4747 sellPrice += XRP(1);
4748 env(token::createOffer(issuer, nftID, sellPrice), Txflags(tfSellNFToken));
4749 if (++offerCount % 10 == 0)
4750 env.close();
4751 }
4752 env.close();
4753 };
4754
4755 // There is one sell offer.
4756 makeSellOffers(XRP(1));
4757 checkOffers("nft_sell_offers", 1, 0, __LINE__);
4758
4759 // There are 250 sell offers.
4760 makeSellOffers(XRP(250));
4761 checkOffers("nft_sell_offers", 250, 0, __LINE__);
4762
4763 // There are 251 sell offers.
4764 makeSellOffers(XRP(251));
4765 checkOffers("nft_sell_offers", 251, 1, __LINE__);
4766
4767 // There are 500 sell offers.
4768 makeSellOffers(XRP(500));
4769 checkOffers("nft_sell_offers", 500, 1, __LINE__);
4770
4771 // There are 501 sell offers.
4772 makeSellOffers(XRP(501));
4773 checkOffers("nft_sell_offers", 501, 2, __LINE__);
4774
4775 // There are no buy offers.
4776 checkOffers("nft_buy_offers", 0, 0, __LINE__);
4777
4778 // A lambda that generates buy offers.
4779 STAmount buyPrice = XRP(0);
4780 auto const makeBuyOffers =
4781 [&env, &buyer, &issuer, &nftID, &buyPrice](STAmount const& limit) {
4782 // Save a little test time by not closing too often.
4783 int offerCount = 0;
4784 while (buyPrice < limit)
4785 {
4786 buyPrice += XRP(1);
4787 env(token::createOffer(buyer, nftID, buyPrice), token::Owner(issuer));
4788 if (++offerCount % 10 == 0)
4789 env.close();
4790 }
4791 env.close();
4792 };
4793
4794 // There is one buy offer;
4795 makeBuyOffers(XRP(1));
4796 checkOffers("nft_buy_offers", 1, 0, __LINE__);
4797
4798 // There are 250 buy offers.
4799 makeBuyOffers(XRP(250));
4800 checkOffers("nft_buy_offers", 250, 0, __LINE__);
4801
4802 // There are 251 buy offers.
4803 makeBuyOffers(XRP(251));
4804 checkOffers("nft_buy_offers", 251, 1, __LINE__);
4805
4806 // There are 500 buy offers.
4807 makeBuyOffers(XRP(500));
4808 checkOffers("nft_buy_offers", 500, 1, __LINE__);
4809
4810 // There are 501 buy offers.
4811 makeBuyOffers(XRP(501));
4812 checkOffers("nft_buy_offers", 501, 2, __LINE__);
4813 }
4814
4815 void
4817 {
4818 // A pagination marker passed to nft_buy_offers / nft_sell_offers must
4819 // reference an offer on the same side (buy vs. sell) as the directory
4820 // being enumerated. A wrong-side marker is rejected with invalidParams.
4821 //
4822 // Note: the pre-fix code also returned invalidParams for a wrong-side
4823 // marker, but only after scanning the entire target directory (an
4824 // O(directory size) walk usable to burn CPU). The fix short-circuits
4825 // that scan. The scan-avoidance is not observable from the RPC
4826 // response, so this test locks the rejection contract (wrong-side ->
4827 // error, same-side -> success) rather than the performance property.
4828 testcase("nft_buy_offers and nft_sell_offers wrong-side marker");
4829
4830 using namespace test::jtx;
4831
4832 Env env{*this, features};
4833
4834 Account const issuer{"issuer"};
4835 Account const buyer{"buyer"};
4836
4837 env.fund(XRP(10000), issuer, buyer);
4838 env.close();
4839
4840 // Mint a transferable NFT.
4841 UInt256 const nftID{token::getNextID(env, issuer, 0u, tfTransferable)};
4842 env(token::mint(issuer, 0), Txflags(tfTransferable));
4843 env.close();
4844
4845 // Create one sell offer (from the issuer, who owns the NFT) and one
4846 // buy offer (from the buyer) for the same NFT.
4847 env(token::createOffer(issuer, nftID, XRP(100)), Txflags(tfSellNFToken));
4848 env(token::createOffer(buyer, nftID, XRP(50)), token::Owner(issuer));
4849 env.close();
4850
4851 // Grab the index of the single offer on each side from the RPC
4852 // response so we can use it as a marker.
4853 auto const firstOfferIndex = [this, &env, &nftID](char const* request) {
4854 json::Value params;
4855 params[jss::nft_id] = to_string(nftID);
4856 json::Value const result = env.rpc("json", request, to_string(params))[jss::result];
4857 BEAST_EXPECT(result.isMember(jss::offers) && result[jss::offers].size() == 1);
4858 return result[jss::offers][0u][jss::nft_offer_index].asString();
4859 };
4860
4861 std::string const sellOfferIndex = firstOfferIndex("nft_sell_offers");
4862 std::string const buyOfferIndex = firstOfferIndex("nft_buy_offers");
4863
4864 auto const queryWithMarker = [&env, &nftID](
4865 char const* request, std::string const& marker) {
4866 json::Value params;
4867 params[jss::nft_id] = to_string(nftID);
4868 params[jss::marker] = marker;
4869 return env.rpc("json", request, to_string(params))[jss::result];
4870 };
4871
4872 // A marker referencing an offer on the wrong side is rejected with
4873 // invalidParams.
4874 {
4875 // Sell-side marker passed to nft_buy_offers.
4876 json::Value const result = queryWithMarker("nft_buy_offers", sellOfferIndex);
4877 BEAST_EXPECT(result[jss::error].asString() == "invalidParams");
4878 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
4879 BEAST_EXPECT(result[jss::error_message] == "Invalid parameters.");
4880 }
4881 {
4882 // Buy-side marker passed to nft_sell_offers.
4883 json::Value const result = queryWithMarker("nft_sell_offers", buyOfferIndex);
4884 BEAST_EXPECT(result[jss::error].asString() == "invalidParams");
4885 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
4886 BEAST_EXPECT(result[jss::error_message] == "Invalid parameters.");
4887 }
4888
4889 // A same-side marker is still accepted. With a single offer on each
4890 // side, resuming after it simply yields no further offers.
4891 {
4892 json::Value const result = queryWithMarker("nft_buy_offers", buyOfferIndex);
4893 BEAST_EXPECT(!result.isMember(jss::error));
4894 }
4895 {
4896 json::Value const result = queryWithMarker("nft_sell_offers", sellOfferIndex);
4897 BEAST_EXPECT(!result.isMember(jss::error));
4898 }
4899 }
4900
4901 void
4903 {
4904 testcase("nft_buy_offers and nft_sell_offers invalid params");
4905
4906 using namespace test::jtx;
4907
4908 Env env{*this, features};
4909
4910 Account const issuer{"issuer"};
4911 Account const buyer{"buyer"};
4912
4913 env.fund(XRP(10000), issuer, buyer);
4914 env.close();
4915
4916 UInt256 const nftID{token::getNextID(env, issuer, 0u, tfTransferable)};
4917 env(token::mint(issuer, 0), Txflags(tfTransferable));
4918 env.close();
4919
4920 env(token::createOffer(issuer, nftID, XRP(100)), Txflags(tfSellNFToken));
4921 env(token::createOffer(issuer, nftID, XRP(200)), Txflags(tfSellNFToken));
4922 env(token::createOffer(buyer, nftID, XRP(50)), token::Owner(issuer));
4923 env(token::createOffer(buyer, nftID, XRP(60)), token::Owner(issuer));
4924 env.close();
4925
4926 for (auto const* request : {"nft_buy_offers", "nft_sell_offers"})
4927 {
4928 auto const query = [&env, request](json::Value const& params) {
4929 return env.rpc("json", request, to_string(params))[jss::result];
4930 };
4931 auto const withField = [&nftID](json::StaticString field, json::Value value) {
4932 json::Value params;
4933 params[jss::nft_id] = to_string(nftID);
4934 params[field] = std::move(value);
4935 return params;
4936 };
4937 auto const expectError = [this](json::Value const& result, std::string const& message) {
4938 BEAST_EXPECT(result[jss::error] == "invalidParams");
4939 BEAST_EXPECT(result[jss::error_message] == message);
4940 };
4941
4942 expectError(query(json::ValueType::Object), "Missing field 'nft_id'.");
4943 for (json::Value const& nftId : {
4944 json::Value{"NOTHEX"},
4947 })
4948 {
4949 json::Value params;
4950 params[jss::nft_id] = nftId;
4951 expectError(query(params), "Invalid field 'nft_id'.");
4952 }
4953
4954 expectError(
4955 query(withField(jss::limit, "1")), "Invalid field 'limit', not unsigned integer.");
4956 expectError(
4957 query(withField(jss::limit, -1)), "Invalid field 'limit', not unsigned integer.");
4958 expectError(query(withField(jss::limit, 0)), "Invalid field 'limit'.");
4959 {
4960 json::Value const result = query(withField(jss::limit, json::ValueType::Null));
4961 BEAST_EXPECT(!result.isMember(jss::error));
4962 BEAST_EXPECT(result[jss::offers].size() == 2);
4963 }
4964 {
4965 json::Value const result = query(withField(jss::limit, 1));
4966 BEAST_EXPECT(!result.isMember(jss::error));
4967 BEAST_EXPECT(result[jss::offers].size() == 1);
4968 BEAST_EXPECT(result[jss::limit] == 1);
4969 BEAST_EXPECT(result.isMember(jss::marker));
4970 }
4971
4972 expectError(query(withField(jss::marker, 5)), "Invalid field 'marker', not string.");
4973 expectError(query(withField(jss::marker, "NOTHEX")), "Invalid parameters.");
4974 }
4975 }
4976
4977 void
4979 {
4980 testcase("nft_buy_offers and nft_sell_offers non-admin limit");
4981
4982 using namespace test::jtx;
4983
4984 Env env{*this, features};
4985
4986 Account const issuer{"issuer"};
4987 Account const buyer{"buyer"};
4988
4989 env.fund(XRP(1000000), issuer, buyer);
4990 env.close();
4991
4992 UInt256 const nftID{token::getNextID(env, issuer, 0u, tfTransferable)};
4993 env(token::mint(issuer, 0), Txflags(tfTransferable));
4994 env.close();
4995
4996 auto const& range = rpc::tuning::kNftOffers;
4997 for (auto i = 1u; i <= range.rmax + 1; ++i)
4998 {
4999 env(token::createOffer(issuer, nftID, XRP(i)), Txflags(tfSellNFToken));
5000 env(token::createOffer(buyer, nftID, XRP(i)), token::Owner(issuer));
5001 if (i % 5 == 0)
5002 env.close();
5003 }
5004 env.close();
5005
5006 auto const userQuery = [this, &env, &nftID](char const* request, unsigned int limit) {
5007 auto& app = env.app();
5010 rpc::JsonContext context{
5011 {
5012 .j = env.journal,
5013 .app = app,
5014 .loadType = loadType,
5015 .netOps = app.getOPs(),
5016 .ledgerMaster = app.getLedgerMaster(),
5017 .consumer = c,
5018 .role = Role::USER,
5019 .coro = {},
5020 .infoSub = {},
5021 .apiVersion = rpc::kApiVersionIfUnspecified,
5022 },
5023 {},
5024 // NOLINTNEXTLINE(readability-trailing-comma) fix-it drops comma after `{}`
5025 {},
5026 };
5027
5028 json::Value params;
5029 params[jss::command] = request;
5030 params[jss::nft_id] = to_string(nftID);
5031 params[jss::limit] = limit;
5032
5033 json::Value result;
5034 Gate g;
5035 auto const job =
5036 app.getJobQueue().postCoro(JtClient, "RPC-Client", [&](auto const& coro) {
5037 context.params = std::move(params);
5038 context.coro = coro;
5039 rpc::doCommand(context, result);
5040 g.signal();
5041 });
5042 if (!BEAST_EXPECT(job))
5043 return result;
5044
5045 using namespace std::chrono_literals;
5046 BEAST_EXPECT(g.waitFor(5s));
5047 job->join();
5048
5049 return result;
5050 };
5051
5052 for (auto const* request : {"nft_buy_offers", "nft_sell_offers"})
5053 {
5054 {
5055 json::Value const result = userQuery(request, 1);
5056 BEAST_EXPECT(!result.isMember(jss::error));
5057 BEAST_EXPECT(result[jss::offers].size() == range.rmin);
5058 BEAST_EXPECT(result[jss::limit] == range.rmin);
5059 BEAST_EXPECT(result.isMember(jss::marker));
5060 }
5061 {
5062 json::Value const result = userQuery(request, range.rmax * 2);
5063 BEAST_EXPECT(!result.isMember(jss::error));
5064 BEAST_EXPECT(result[jss::offers].size() == range.rmax);
5065 BEAST_EXPECT(result[jss::limit] == range.rmax);
5066 BEAST_EXPECT(result.isMember(jss::marker));
5067 }
5068 }
5069 }
5070
5071 void
5073 {
5074 using namespace test::jtx;
5075
5076 testcase("NFTokenNegOffer");
5077
5078 Account const issuer{"issuer"};
5079 Account const buyer{"buyer"};
5080 Account const gw{"gw"};
5081 IOU const gwXAU(gw["XAU"]);
5082
5083 {
5084 Env env{*this, features};
5085
5086 env.fund(XRP(1000000), issuer, buyer, gw);
5087 env.close();
5088
5089 env(trust(issuer, gwXAU(2000)));
5090 env(trust(buyer, gwXAU(2000)));
5091 env.close();
5092
5093 env(pay(gw, issuer, gwXAU(1000)));
5094 env(pay(gw, buyer, gwXAU(1000)));
5095 env.close();
5096
5097 // Create an NFT that we'll make XRP offers for.
5098 UInt256 const nftID0{token::getNextID(env, issuer, 0u, tfTransferable)};
5099 env(token::mint(issuer, 0), Txflags(tfTransferable));
5100 env.close();
5101
5102 // Create an NFT that we'll make IOU offers for.
5103 UInt256 const nftID1{token::getNextID(env, issuer, 1u, tfTransferable)};
5104 env(token::mint(issuer, 1), Txflags(tfTransferable));
5105 env.close();
5106
5107 TER const offerCreateTER = temBAD_AMOUNT;
5108
5109 // Make offers with negative amounts for the NFTs
5110 UInt256 const sellNegXrpOfferIndex =
5111 keylet::nftokenOffer(issuer, SeqProxy::rawSequence(env.seq(issuer))).key;
5112 env(token::createOffer(issuer, nftID0, XRP(-2)),
5113 Txflags(tfSellNFToken),
5114 Ter(offerCreateTER));
5115 env.close();
5116
5117 UInt256 const sellNegIouOfferIndex =
5118 keylet::nftokenOffer(issuer, SeqProxy::rawSequence(env.seq(issuer))).key;
5119 env(token::createOffer(issuer, nftID1, gwXAU(-2)),
5120 Txflags(tfSellNFToken),
5121 Ter(offerCreateTER));
5122 env.close();
5123
5124 UInt256 const buyNegXrpOfferIndex =
5125 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
5126 env(token::createOffer(buyer, nftID0, XRP(-1)),
5127 token::Owner(issuer),
5128 Ter(offerCreateTER));
5129 env.close();
5130
5131 UInt256 const buyNegIouOfferIndex =
5132 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
5133 env(token::createOffer(buyer, nftID1, gwXAU(-1)),
5134 token::Owner(issuer),
5135 Ter(offerCreateTER));
5136 env.close();
5137
5138 {
5139 // Now try to accept the offers.
5140 TER const offerAcceptTER = tecOBJECT_NOT_FOUND;
5141
5142 // Sell offers.
5143 env(token::acceptSellOffer(buyer, sellNegXrpOfferIndex), Ter(offerAcceptTER));
5144 env.close();
5145 env(token::acceptSellOffer(buyer, sellNegIouOfferIndex), Ter(offerAcceptTER));
5146 env.close();
5147
5148 // Buy offers.
5149 env(token::acceptBuyOffer(issuer, buyNegXrpOfferIndex), Ter(offerAcceptTER));
5150 env.close();
5151 env(token::acceptBuyOffer(issuer, buyNegIouOfferIndex), Ter(offerAcceptTER));
5152 env.close();
5153 }
5154 {
5155 TER const offerAcceptTER = tecOBJECT_NOT_FOUND;
5156
5157 // Brokered offers.
5158 env(token::brokerOffers(gw, buyNegXrpOfferIndex, sellNegXrpOfferIndex),
5159 Ter(offerAcceptTER));
5160 env.close();
5161 env(token::brokerOffers(gw, buyNegIouOfferIndex, sellNegIouOfferIndex),
5162 Ter(offerAcceptTER));
5163 env.close();
5164 }
5165 }
5166
5167 {
5168 // Test buy offers with a destination.
5169 Env env{*this, features};
5170
5171 env.fund(XRP(1000000), issuer, buyer);
5172
5173 // Create an NFT that we'll make offers for.
5174 UInt256 const nftID{token::getNextID(env, issuer, 0u, tfTransferable)};
5175 env(token::mint(issuer, 0), Txflags(tfTransferable));
5176 env.close();
5177
5178 TER const offerCreateTER = tesSUCCESS;
5179
5180 env(token::createOffer(buyer, nftID, drops(1)),
5181 token::Owner(issuer),
5182 token::Destination(issuer),
5183 Ter(offerCreateTER));
5184 env.close();
5185 }
5186 }
5187
5188 void
5190 {
5191 using namespace test::jtx;
5192
5193 testcase("Payments with IOU transfer fees");
5194
5195 {
5196 Env env{*this, features};
5197
5198 Account const minter{"minter"};
5199 Account const secondarySeller{"seller"};
5200 Account const buyer{"buyer"};
5201 Account const gw{"gateway"};
5202 Account const broker{"broker"};
5203 IOU const gwXAU(gw["XAU"]);
5204 IOU const gwXPB(gw["XPB"]);
5205
5206 env.fund(XRP(1000), gw, minter, secondarySeller, buyer, broker);
5207 env.close();
5208
5209 env(trust(minter, gwXAU(2000)));
5210 env(trust(secondarySeller, gwXAU(2000)));
5211 env(trust(broker, gwXAU(10000)));
5212 env(trust(buyer, gwXAU(2000)));
5213 env(trust(buyer, gwXPB(2000)));
5214 env.close();
5215
5216 // The IOU issuer has a 2% transfer rate
5217 env(rate(gw, 1.02));
5218 env.close();
5219
5220 auto expectInitialState =
5221 [this, &env, &buyer, &minter, &secondarySeller, &broker, &gw, &gwXAU, &gwXPB] {
5222 // Buyer should have XAU 1000, XPB 0
5223 // Minter should have XAU 0, XPB 0
5224 // Secondary seller should have XAU 0, XPB 0
5225 // Broker should have XAU 5000, XPB 0
5226 BEAST_EXPECT(env.balance(buyer, gwXAU) == gwXAU(1000));
5227 BEAST_EXPECT(env.balance(buyer, gwXPB) == gwXPB(0));
5228 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(0));
5229 BEAST_EXPECT(env.balance(minter, gwXPB) == gwXPB(0));
5230 BEAST_EXPECT(env.balance(secondarySeller, gwXAU) == gwXAU(0));
5231 BEAST_EXPECT(env.balance(secondarySeller, gwXPB) == gwXPB(0));
5232 BEAST_EXPECT(env.balance(broker, gwXAU) == gwXAU(5000));
5233 BEAST_EXPECT(env.balance(broker, gwXPB) == gwXPB(0));
5234 BEAST_EXPECT(env.balance(gw, buyer["XAU"]) == gwXAU(-1000));
5235 BEAST_EXPECT(env.balance(gw, buyer["XPB"]) == gwXPB(0));
5236 BEAST_EXPECT(env.balance(gw, minter["XAU"]) == gwXAU(0));
5237 BEAST_EXPECT(env.balance(gw, minter["XPB"]) == gwXPB(0));
5238 BEAST_EXPECT(env.balance(gw, secondarySeller["XAU"]) == gwXAU(0));
5239 BEAST_EXPECT(env.balance(gw, secondarySeller["XPB"]) == gwXPB(0));
5240 BEAST_EXPECT(env.balance(gw, broker["XAU"]) == gwXAU(-5000));
5241 BEAST_EXPECT(env.balance(gw, broker["XPB"]) == gwXPB(0));
5242 };
5243
5244 auto const reinitializeTrustLineBalances = [&expectInitialState,
5245 &env,
5246 &buyer,
5247 &minter,
5248 &secondarySeller,
5249 &broker,
5250 &gw,
5251 &gwXAU,
5252 &gwXPB] {
5253 if (auto const difference = gwXAU(1000) - env.balance(buyer, gwXAU);
5254 difference > gwXAU(0))
5255 env(pay(gw, buyer, difference));
5256 if (env.balance(buyer, gwXPB) > gwXPB(0))
5257 env(pay(buyer, gw, env.balance(buyer, gwXPB)));
5258 if (env.balance(minter, gwXAU) > gwXAU(0))
5259 env(pay(minter, gw, env.balance(minter, gwXAU)));
5260 if (env.balance(minter, gwXPB) > gwXPB(0))
5261 env(pay(minter, gw, env.balance(minter, gwXPB)));
5262 if (env.balance(secondarySeller, gwXAU) > gwXAU(0))
5263 env(pay(secondarySeller, gw, env.balance(secondarySeller, gwXAU)));
5264 if (env.balance(secondarySeller, gwXPB) > gwXPB(0))
5265 env(pay(secondarySeller, gw, env.balance(secondarySeller, gwXPB)));
5266 auto brokerDiff = gwXAU(5000) - env.balance(broker, gwXAU);
5267 if (brokerDiff > gwXAU(0))
5268 {
5269 env(pay(gw, broker, brokerDiff));
5270 }
5271 else if (brokerDiff < gwXAU(0))
5272 {
5273 brokerDiff.negate();
5274 env(pay(broker, gw, brokerDiff));
5275 }
5276 if (env.balance(broker, gwXPB) > gwXPB(0))
5277 env(pay(broker, gw, env.balance(broker, gwXPB)));
5278 env.close();
5279 expectInitialState();
5280 };
5281
5282 auto const mintNFT = [&env](Account const& minter, int transferFee = 0) {
5283 UInt256 const nftID = token::getNextID(env, minter, 0, tfTransferable, transferFee);
5284 env(token::mint(minter), token::XferFee(transferFee), Txflags(tfTransferable));
5285 env.close();
5286 return nftID;
5287 };
5288
5289 auto const createBuyOffer = [&env](
5290 Account const& offerer,
5291 Account const& owner,
5292 UInt256 const& nftID,
5293 STAmount const& amount,
5294 std::optional<TER const> const terCode = {}) {
5295 UInt256 const offerID =
5296 keylet::nftokenOffer(offerer, SeqProxy::rawSequence(env.seq(offerer))).key;
5297 env(token::createOffer(offerer, nftID, amount),
5298 token::Owner(owner),
5299 terCode ? Ter(*terCode) : Ter(static_cast<TER>(tesSUCCESS)));
5300 env.close();
5301 return offerID;
5302 };
5303
5304 auto const createSellOffer = [&env](
5305 Account const& offerer,
5306 UInt256 const& nftID,
5307 STAmount const& amount,
5308 std::optional<TER const> const terCode = {}) {
5309 UInt256 const offerID =
5310 keylet::nftokenOffer(offerer, SeqProxy::rawSequence(env.seq(offerer))).key;
5311 env(token::createOffer(offerer, nftID, amount),
5312 Txflags(tfSellNFToken),
5313 terCode ? Ter(*terCode) : Ter(static_cast<TER>(tesSUCCESS)));
5314 env.close();
5315 return offerID;
5316 };
5317
5318 {
5319 // Buyer attempts to send 100% of their balance of an IOU
5320 // (sellside)
5321 reinitializeTrustLineBalances();
5322 auto const nftID = mintNFT(minter);
5323 auto const offerID = createSellOffer(minter, nftID, gwXAU(1000));
5324 TER const sellTER = tecINSUFFICIENT_FUNDS;
5325 env(token::acceptSellOffer(buyer, offerID), Ter(sellTER));
5326 env.close();
5327
5328 expectInitialState();
5329 }
5330 {
5331 // Buyer attempts to send 100% of their balance of an IOU
5332 // (buyside)
5333 reinitializeTrustLineBalances();
5334 auto const nftID = mintNFT(minter);
5335 auto const offerID = createBuyOffer(buyer, minter, nftID, gwXAU(1000));
5336 TER const sellTER = tecINSUFFICIENT_FUNDS;
5337 env(token::acceptBuyOffer(minter, offerID), Ter(sellTER));
5338 env.close();
5339
5340 expectInitialState();
5341 }
5342 {
5343 // Buyer attempts to send an amount less than 100% of their
5344 // balance of an IOU, but such that the addition of the transfer
5345 // fee would be greater than the buyer's balance (sellside)
5346 reinitializeTrustLineBalances();
5347 auto const nftID = mintNFT(minter);
5348 auto const offerID = createSellOffer(minter, nftID, gwXAU(995));
5349 TER const sellTER = tecINSUFFICIENT_FUNDS;
5350 env(token::acceptSellOffer(buyer, offerID), Ter(sellTER));
5351 env.close();
5352
5353 expectInitialState();
5354 }
5355 {
5356 // Buyer attempts to send an amount less than 100% of their
5357 // balance of an IOU, but such that the addition of the transfer
5358 // fee would be greater than the buyer's balance (buyside)
5359 reinitializeTrustLineBalances();
5360 auto const nftID = mintNFT(minter);
5361 auto const offerID = createBuyOffer(buyer, minter, nftID, gwXAU(995));
5362 TER const sellTER = tecINSUFFICIENT_FUNDS;
5363 env(token::acceptBuyOffer(minter, offerID), Ter(sellTER));
5364 env.close();
5365
5366 expectInitialState();
5367 }
5368 {
5369 // Buyer attempts to send an amount less than 100% of their
5370 // balance of an IOU with a transfer fee, and such that the
5371 // addition of the transfer fee is still less than their balance
5372 // (sellside)
5373 reinitializeTrustLineBalances();
5374 auto const nftID = mintNFT(minter);
5375 auto const offerID = createSellOffer(minter, nftID, gwXAU(900));
5376 env(token::acceptSellOffer(buyer, offerID));
5377 env.close();
5378
5379 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(900));
5380 BEAST_EXPECT(env.balance(buyer, gwXAU) == gwXAU(82));
5381 BEAST_EXPECT(env.balance(gw, minter["XAU"]) == gwXAU(-900));
5382 BEAST_EXPECT(env.balance(gw, buyer["XAU"]) == gwXAU(-82));
5383 }
5384 {
5385 // Buyer attempts to send an amount less than 100% of their
5386 // balance of an IOU with a transfer fee, and such that the
5387 // addition of the transfer fee is still less than their balance
5388 // (buyside)
5389 reinitializeTrustLineBalances();
5390 auto const nftID = mintNFT(minter);
5391 auto const offerID = createBuyOffer(buyer, minter, nftID, gwXAU(900));
5392 env(token::acceptBuyOffer(minter, offerID));
5393 env.close();
5394
5395 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(900));
5396 BEAST_EXPECT(env.balance(buyer, gwXAU) == gwXAU(82));
5397 BEAST_EXPECT(env.balance(gw, minter["XAU"]) == gwXAU(-900));
5398 BEAST_EXPECT(env.balance(gw, buyer["XAU"]) == gwXAU(-82));
5399 }
5400 {
5401 // Buyer attempts to send an amount less than 100% of their
5402 // balance of an IOU with a transfer fee, and such that the
5403 // addition of the transfer fee is equal than their balance
5404 // (sellside)
5405 reinitializeTrustLineBalances();
5406
5407 // pay them an additional XAU 20 to cover transfer rate
5408 env(pay(gw, buyer, gwXAU(20)));
5409 env.close();
5410
5411 auto const nftID = mintNFT(minter);
5412 auto const offerID = createSellOffer(minter, nftID, gwXAU(1000));
5413 env(token::acceptSellOffer(buyer, offerID));
5414 env.close();
5415
5416 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(1000));
5417 BEAST_EXPECT(env.balance(buyer, gwXAU) == gwXAU(0));
5418 BEAST_EXPECT(env.balance(gw, minter["XAU"]) == gwXAU(-1000));
5419 BEAST_EXPECT(env.balance(gw, buyer["XAU"]) == gwXAU(0));
5420 }
5421 {
5422 // Buyer attempts to send an amount less than 100% of their
5423 // balance of an IOU with a transfer fee, and such that the
5424 // addition of the transfer fee is equal than their balance
5425 // (buyside)
5426 reinitializeTrustLineBalances();
5427
5428 // pay them an additional XAU 20 to cover transfer rate
5429 env(pay(gw, buyer, gwXAU(20)));
5430 env.close();
5431
5432 auto const nftID = mintNFT(minter);
5433 auto const offerID = createBuyOffer(buyer, minter, nftID, gwXAU(1000));
5434 env(token::acceptBuyOffer(minter, offerID));
5435 env.close();
5436
5437 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(1000));
5438 BEAST_EXPECT(env.balance(buyer, gwXAU) == gwXAU(0));
5439 BEAST_EXPECT(env.balance(gw, minter["XAU"]) == gwXAU(-1000));
5440 BEAST_EXPECT(env.balance(gw, buyer["XAU"]) == gwXAU(0));
5441 }
5442 {
5443 // Gateway attempts to buy NFT with their own IOU - no
5444 // transfer fee is calculated here (sellside)
5445 reinitializeTrustLineBalances();
5446
5447 auto const nftID = mintNFT(minter);
5448 auto const offerID = createSellOffer(minter, nftID, gwXAU(1000));
5449 TER const sellTER = tesSUCCESS;
5450 env(token::acceptSellOffer(gw, offerID), Ter(sellTER));
5451 env.close();
5452
5453 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(1000));
5454 BEAST_EXPECT(env.balance(gw, minter["XAU"]) == gwXAU(-1000));
5455 }
5456 {
5457 // Gateway attempts to buy NFT with their own IOU - no
5458 // transfer fee is calculated here (buyside)
5459 reinitializeTrustLineBalances();
5460
5461 auto const nftID = mintNFT(minter);
5462 TER const offerTER = tesSUCCESS;
5463 auto const offerID = createBuyOffer(gw, minter, nftID, gwXAU(1000), {offerTER});
5464 TER const sellTER = tesSUCCESS;
5465 env(token::acceptBuyOffer(minter, offerID), Ter(sellTER));
5466 env.close();
5467
5468 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(1000));
5469 BEAST_EXPECT(env.balance(gw, minter["XAU"]) == gwXAU(-1000));
5470 }
5471 {
5472 // Gateway attempts to buy NFT with their own IOU for more
5473 // than minter trusts (sellside)
5474 reinitializeTrustLineBalances();
5475 auto const nftID = mintNFT(minter);
5476 auto const offerID = createSellOffer(minter, nftID, gwXAU(5000));
5477 TER const sellTER = tesSUCCESS;
5478 env(token::acceptSellOffer(gw, offerID), Ter(sellTER));
5479 env.close();
5480
5481 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(5000));
5482 BEAST_EXPECT(env.balance(gw, minter["XAU"]) == gwXAU(-5000));
5483 }
5484 {
5485 // Gateway attempts to buy NFT with their own IOU for more
5486 // than minter trusts (buyside)
5487 reinitializeTrustLineBalances();
5488
5489 auto const nftID = mintNFT(minter);
5490 TER const offerTER = tesSUCCESS;
5491 auto const offerID = createBuyOffer(gw, minter, nftID, gwXAU(5000), {offerTER});
5492 TER const sellTER = tesSUCCESS;
5493 env(token::acceptBuyOffer(minter, offerID), Ter(sellTER));
5494 env.close();
5495
5496 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(5000));
5497 BEAST_EXPECT(env.balance(gw, minter["XAU"]) == gwXAU(-5000));
5498 }
5499 {
5500 // Gateway is the NFT minter and attempts to sell NFT for an
5501 // amount that would be greater than a balance if there were a
5502 // transfer fee calculated in this transaction. (sellside)
5503 reinitializeTrustLineBalances();
5504 auto const nftID = mintNFT(gw);
5505 auto const offerID = createSellOffer(gw, nftID, gwXAU(1000));
5506 env(token::acceptSellOffer(buyer, offerID));
5507 env.close();
5508
5509 BEAST_EXPECT(env.balance(buyer, gwXAU) == gwXAU(0));
5510 BEAST_EXPECT(env.balance(gw, buyer["XAU"]) == gwXAU(0));
5511 }
5512 {
5513 // Gateway is the NFT minter and attempts to sell NFT for an
5514 // amount that would be greater than a balance if there were a
5515 // transfer fee calculated in this transaction. (buyside)
5516 reinitializeTrustLineBalances();
5517
5518 auto const nftID = mintNFT(gw);
5519 auto const offerID = createBuyOffer(buyer, gw, nftID, gwXAU(1000));
5520 env(token::acceptBuyOffer(gw, offerID));
5521 env.close();
5522
5523 BEAST_EXPECT(env.balance(buyer, gwXAU) == gwXAU(0));
5524 BEAST_EXPECT(env.balance(gw, buyer["XAU"]) == gwXAU(0));
5525 }
5526 {
5527 // Gateway is the NFT minter and attempts to sell NFT for an
5528 // amount that is greater than a balance before transfer fees.
5529 // (sellside)
5530 reinitializeTrustLineBalances();
5531 auto const nftID = mintNFT(gw);
5532 auto const offerID = createSellOffer(gw, nftID, gwXAU(2000));
5533 env(token::acceptSellOffer(buyer, offerID),
5534 Ter(static_cast<TER>(tecINSUFFICIENT_FUNDS)));
5535 env.close();
5536 expectInitialState();
5537 }
5538 {
5539 // Gateway is the NFT minter and attempts to sell NFT for an
5540 // amount that is greater than a balance before transfer fees.
5541 // (buyside)
5542 reinitializeTrustLineBalances();
5543 auto const nftID = mintNFT(gw);
5544 auto const offerID = createBuyOffer(buyer, gw, nftID, gwXAU(2000));
5545 env(token::acceptBuyOffer(gw, offerID),
5546 Ter(static_cast<TER>(tecINSUFFICIENT_FUNDS)));
5547 env.close();
5548 expectInitialState();
5549 }
5550 {
5551 // Minter attempts to sell the token for XPB 10, which they
5552 // have no trust line for and buyer has kNone of (sellside).
5553 reinitializeTrustLineBalances();
5554 auto const nftID = mintNFT(minter);
5555 auto const offerID = createSellOffer(minter, nftID, gwXPB(10));
5556 env(token::acceptSellOffer(buyer, offerID),
5557 Ter(static_cast<TER>(tecINSUFFICIENT_FUNDS)));
5558 env.close();
5559 expectInitialState();
5560 }
5561 {
5562 // Minter attempts to sell the token for XPB 10, which they
5563 // have no trust line for and buyer has kNone of (buyside).
5564 reinitializeTrustLineBalances();
5565 auto const nftID = mintNFT(minter);
5566 auto const offerID = createBuyOffer(
5567 buyer, minter, nftID, gwXPB(10), {static_cast<TER>(tecUNFUNDED_OFFER)});
5568 env(token::acceptBuyOffer(minter, offerID),
5569 Ter(static_cast<TER>(tecOBJECT_NOT_FOUND)));
5570 env.close();
5571 expectInitialState();
5572 }
5573 {
5574 // Minter attempts to sell the token for XPB 10 and the buyer
5575 // has it but the minter has no trust line. Trust line is
5576 // created as a result of the tx (sellside).
5577 reinitializeTrustLineBalances();
5578 env(pay(gw, buyer, gwXPB(100)));
5579 env.close();
5580
5581 auto const nftID = mintNFT(minter);
5582 auto const offerID = createSellOffer(minter, nftID, gwXPB(10));
5583 env(token::acceptSellOffer(buyer, offerID));
5584 env.close();
5585
5586 BEAST_EXPECT(env.balance(minter, gwXPB) == gwXPB(10));
5587 BEAST_EXPECT(env.balance(buyer, gwXPB) == gwXPB(89.8));
5588 BEAST_EXPECT(env.balance(gw, minter["XPB"]) == gwXPB(-10));
5589 BEAST_EXPECT(env.balance(gw, buyer["XPB"]) == gwXPB(-89.8));
5590 }
5591 {
5592 // Minter attempts to sell the token for XPB 10 and the buyer
5593 // has it but the minter has no trust line. Trust line is
5594 // created as a result of the tx (buyside).
5595 reinitializeTrustLineBalances();
5596 env(pay(gw, buyer, gwXPB(100)));
5597 env.close();
5598
5599 auto const nftID = mintNFT(minter);
5600 auto const offerID = createBuyOffer(buyer, minter, nftID, gwXPB(10));
5601 env(token::acceptBuyOffer(minter, offerID));
5602 env.close();
5603
5604 BEAST_EXPECT(env.balance(minter, gwXPB) == gwXPB(10));
5605 BEAST_EXPECT(env.balance(buyer, gwXPB) == gwXPB(89.8));
5606 BEAST_EXPECT(env.balance(gw, minter["XPB"]) == gwXPB(-10));
5607 BEAST_EXPECT(env.balance(gw, buyer["XPB"]) == gwXPB(-89.8));
5608 }
5609 {
5610 // There is a transfer fee on the NFT and buyer has exact
5611 // amount (sellside)
5612 reinitializeTrustLineBalances();
5613
5614 // secondarySeller has to sell it because transfer fees only
5615 // happen on secondary sales
5616 auto const nftID = mintNFT(minter, 3000); // 3%
5617 auto const primaryOfferID = createSellOffer(minter, nftID, XRP(0));
5618 env(token::acceptSellOffer(secondarySeller, primaryOfferID));
5619 env.close();
5620
5621 // now we can do a secondary sale
5622 auto const offerID = createSellOffer(secondarySeller, nftID, gwXAU(1000));
5623 TER const sellTER = tecINSUFFICIENT_FUNDS;
5624 env(token::acceptSellOffer(buyer, offerID), Ter(sellTER));
5625 env.close();
5626
5627 expectInitialState();
5628 }
5629 {
5630 // There is a transfer fee on the NFT and buyer has exact
5631 // amount (buyside)
5632 reinitializeTrustLineBalances();
5633
5634 // secondarySeller has to sell it because transfer fees only
5635 // happen on secondary sales
5636 auto const nftID = mintNFT(minter, 3000); // 3%
5637 auto const primaryOfferID = createSellOffer(minter, nftID, XRP(0));
5638 env(token::acceptSellOffer(secondarySeller, primaryOfferID));
5639 env.close();
5640
5641 // now we can do a secondary sale
5642 auto const offerID = createBuyOffer(buyer, secondarySeller, nftID, gwXAU(1000));
5643 TER const sellTER = tecINSUFFICIENT_FUNDS;
5644 env(token::acceptBuyOffer(secondarySeller, offerID), Ter(sellTER));
5645 env.close();
5646
5647 expectInitialState();
5648 }
5649 {
5650 // There is a transfer fee on the NFT and buyer has enough
5651 // (sellside)
5652 reinitializeTrustLineBalances();
5653
5654 // secondarySeller has to sell it because transfer fees only
5655 // happen on secondary sales
5656 auto const nftID = mintNFT(minter, 3000); // 3%
5657 auto const primaryOfferID = createSellOffer(minter, nftID, XRP(0));
5658 env(token::acceptSellOffer(secondarySeller, primaryOfferID));
5659 env.close();
5660
5661 // now we can do a secondary sale
5662 auto const offerID = createSellOffer(secondarySeller, nftID, gwXAU(900));
5663 env(token::acceptSellOffer(buyer, offerID));
5664 env.close();
5665
5666 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(27));
5667 BEAST_EXPECT(env.balance(secondarySeller, gwXAU) == gwXAU(873));
5668 BEAST_EXPECT(env.balance(buyer, gwXAU) == gwXAU(82));
5669 BEAST_EXPECT(env.balance(gw, minter["XAU"]) == gwXAU(-27));
5670 BEAST_EXPECT(env.balance(gw, secondarySeller["XAU"]) == gwXAU(-873));
5671 BEAST_EXPECT(env.balance(gw, buyer["XAU"]) == gwXAU(-82));
5672 }
5673 {
5674 // There is a transfer fee on the NFT and buyer has enough
5675 // (buyside)
5676 reinitializeTrustLineBalances();
5677
5678 // secondarySeller has to sell it because transfer fees only
5679 // happen on secondary sales
5680 auto const nftID = mintNFT(minter, 3000); // 3%
5681 auto const primaryOfferID = createSellOffer(minter, nftID, XRP(0));
5682 env(token::acceptSellOffer(secondarySeller, primaryOfferID));
5683 env.close();
5684
5685 // now we can do a secondary sale
5686 auto const offerID = createBuyOffer(buyer, secondarySeller, nftID, gwXAU(900));
5687 env(token::acceptBuyOffer(secondarySeller, offerID));
5688 env.close();
5689
5690 // receives 3% of 900 - 27
5691 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(27));
5692 // receives 97% of 900 - 873
5693 BEAST_EXPECT(env.balance(secondarySeller, gwXAU) == gwXAU(873));
5694 // pays 900 plus 2% transfer fee on XAU - 918
5695 BEAST_EXPECT(env.balance(buyer, gwXAU) == gwXAU(82));
5696 BEAST_EXPECT(env.balance(gw, minter["XAU"]) == gwXAU(-27));
5697 BEAST_EXPECT(env.balance(gw, secondarySeller["XAU"]) == gwXAU(-873));
5698 BEAST_EXPECT(env.balance(gw, buyer["XAU"]) == gwXAU(-82));
5699 }
5700 {
5701 // There is a broker fee on the NFT. XAU transfer fee is only
5702 // calculated from the buyer's output, not deducted from
5703 // broker fee.
5704 //
5705 // For a payment of 500 with a 2% IOU transfee fee and 100
5706 // broker fee:
5707 //
5708 // A) Total sale amount + IOU transfer fee is paid by buyer
5709 // (Buyer pays (1.02 * 500) = 510)
5710 // B) GW receives the additional IOU transfer fee
5711 // (GW receives 10 from buyer calculated above)
5712 // C) Broker receives broker fee (no IOU transfer fee)
5713 // (Broker receives 100 from buyer)
5714 // D) Seller receives balance (no IOU transfer fee)
5715 // (Seller receives (510 - 10 - 100) = 400)
5716 reinitializeTrustLineBalances();
5717
5718 auto const nftID = mintNFT(minter);
5719 auto const sellOffer = createSellOffer(minter, nftID, gwXAU(300));
5720 auto const buyOffer = createBuyOffer(buyer, minter, nftID, gwXAU(500));
5721 env(token::brokerOffers(broker, buyOffer, sellOffer), token::BrokerFee(gwXAU(100)));
5722 env.close();
5723
5724 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(400));
5725 BEAST_EXPECT(env.balance(buyer, gwXAU) == gwXAU(490));
5726 BEAST_EXPECT(env.balance(broker, gwXAU) == gwXAU(5100));
5727 BEAST_EXPECT(env.balance(gw, minter["XAU"]) == gwXAU(-400));
5728 BEAST_EXPECT(env.balance(gw, buyer["XAU"]) == gwXAU(-490));
5729 BEAST_EXPECT(env.balance(gw, broker["XAU"]) == gwXAU(-5100));
5730 }
5731 {
5732 // There is broker and transfer fee on the NFT
5733 //
5734 // For a payment of 500 with a 2% IOU transfer fee, 3% NFT
5735 // transfer fee, and 100 broker fee:
5736 //
5737 // A) Total sale amount + IOU transfer fee is paid by buyer
5738 // (Buyer pays (1.02 * 500) = 510)
5739 // B) GW receives the additional IOU transfer fee
5740 // (GW receives 10 from buyer calculated above)
5741 // C) Broker receives broker fee (no IOU transfer fee)
5742 // (Broker receives 100 from buyer)
5743 // D) Minter receives transfer fee (no IOU transfer fee)
5744 // (Minter receives 0.03 * (510 - 10 - 100) = 12)
5745 // E) Seller receives balance (no IOU transfer fee)
5746 // (Seller receives (510 - 10 - 100 - 12) = 388)
5747 reinitializeTrustLineBalances();
5748
5749 // secondarySeller has to sell it because transfer fees only
5750 // happen on secondary sales
5751 auto const nftID = mintNFT(minter, 3000); // 3%
5752 auto const primaryOfferID = createSellOffer(minter, nftID, XRP(0));
5753 env(token::acceptSellOffer(secondarySeller, primaryOfferID));
5754 env.close();
5755
5756 // now we can do a secondary sale
5757 auto const sellOffer = createSellOffer(secondarySeller, nftID, gwXAU(300));
5758 auto const buyOffer = createBuyOffer(buyer, secondarySeller, nftID, gwXAU(500));
5759 env(token::brokerOffers(broker, buyOffer, sellOffer), token::BrokerFee(gwXAU(100)));
5760 env.close();
5761
5762 BEAST_EXPECT(env.balance(minter, gwXAU) == gwXAU(12));
5763 BEAST_EXPECT(env.balance(buyer, gwXAU) == gwXAU(490));
5764 BEAST_EXPECT(env.balance(secondarySeller, gwXAU) == gwXAU(388));
5765 BEAST_EXPECT(env.balance(broker, gwXAU) == gwXAU(5100));
5766 BEAST_EXPECT(env.balance(gw, minter["XAU"]) == gwXAU(-12));
5767 BEAST_EXPECT(env.balance(gw, buyer["XAU"]) == gwXAU(-490));
5768 BEAST_EXPECT(env.balance(gw, secondarySeller["XAU"]) == gwXAU(-388));
5769 BEAST_EXPECT(env.balance(gw, broker["XAU"]) == gwXAU(-5100));
5770 }
5771 }
5772 }
5773
5774 void
5776 {
5777 // There was a bug that if an account had...
5778 //
5779 // 1. An NFToken, and
5780 // 2. An offer on the ledger to buy that same token, and
5781 // 3. Also an offer of the ledger to sell that same token,
5782 //
5783 // Then someone could broker the two offers. This would result in
5784 // the NFToken being bought and returned to the original owner and
5785 // the broker pocketing the profit.
5786 //
5787 testcase("Brokered sale to self");
5788
5789 using namespace test::jtx;
5790
5791 Account const alice{"alice"};
5792 Account const bob{"bob"};
5793 Account const broker{"broker"};
5794
5795 Env env{*this, features};
5796 auto const baseFee = env.current()->fees().base;
5797 env.fund(XRP(10000), alice, bob, broker);
5798 env.close();
5799
5800 // For this scenario to occur we need the following steps:
5801 //
5802 // 1. alice mints NFT.
5803 // 2. bob creates a buy offer for it for 5 XRP.
5804 // 3. alice decides to gift the NFT to bob for 0.
5805 // creating a sell offer (hopefully using a destination too)
5806 // 4. Bob accepts the sell offer, because it is better than
5807 // paying 5 XRP.
5808 // 5. At this point, bob has the NFT and still has their buy
5809 // offer from when they did not have the NFT! This is because
5810 // the order book is not cleared when an NFT changes hands.
5811 // 6. Now that Bob owns the NFT, he cannot create new buy offers.
5812 // However he still has one left over from when he did not own
5813 // it. He can create new sell offers and does.
5814 // 7. Now that bob has both a buy and a sell offer for the same NFT,
5815 // a broker can sell the NFT that bob owns to bob and pocket the
5816 // difference.
5817 UInt256 const nftId{token::getNextID(env, alice, 0u, tfTransferable)};
5818 env(token::mint(alice, 0u), Txflags(tfTransferable));
5819 env.close();
5820
5821 // Bob creates a buy offer for 5 XRP. Alice creates a sell offer
5822 // for 0 XRP.
5823 UInt256 const bobBuyOfferIndex =
5825 env(token::createOffer(bob, nftId, XRP(5)), token::Owner(alice));
5826
5827 UInt256 const aliceSellOfferIndex =
5828 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
5829 env(token::createOffer(alice, nftId, XRP(0)),
5830 token::Destination(bob),
5831 Txflags(tfSellNFToken));
5832 env.close();
5833
5834 // bob accepts alice's offer but forgets to remove the old buy offer.
5835 env(token::acceptSellOffer(bob, aliceSellOfferIndex));
5836 env.close();
5837
5838 // Note that bob still has a buy offer on the books.
5839 BEAST_EXPECT(env.le(keylet::nftokenOffer(bobBuyOfferIndex)));
5840
5841 // Bob creates a sell offer for the gift NFT from alice.
5842 UInt256 const bobSellOfferIndex =
5844 env(token::createOffer(bob, nftId, XRP(4)), Txflags(tfSellNFToken));
5845 env.close();
5846
5847 // bob now has a buy offer and a sell offer on the books. A broker
5848 // spots this and swoops in to make a profit.
5849 BEAST_EXPECT(nftCount(env, bob) == 1);
5850 auto const bobPriorBalance = env.balance(bob);
5851 auto const brokerPriorBalance = env.balance(broker);
5852 env(token::brokerOffers(broker, bobBuyOfferIndex, bobSellOfferIndex),
5853 token::BrokerFee(XRP(1)),
5855 env.close();
5856
5857 // A tec result was returned, so no state should change other
5858 // than the broker burning their transaction fee.
5859 BEAST_EXPECT(nftCount(env, bob) == 1);
5860 BEAST_EXPECT(env.balance(bob) == bobPriorBalance);
5861 BEAST_EXPECT(env.balance(broker) == brokerPriorBalance - baseFee);
5862 }
5863
5864 void
5866 {
5867 using namespace test::jtx;
5868
5869 testcase("NFTokenRemint");
5870
5871 // Returns the current ledger sequence
5872 auto openLedgerSeq = [](Env& env) { return env.current()->seq(); };
5873
5874 // Close the ledger until the ledger sequence is large enough to delete
5875 // the account (no longer within <Sequence + 256>)
5876 auto const incLgrSeqForAcctDel = [&](Env& env, Account const& acct) {
5877 int const delta = [&] -> int {
5878 if (env.seq(acct) + 255 > openLedgerSeq(env))
5879 return env.seq(acct) - openLedgerSeq(env) + 255;
5880 return 0;
5881 }();
5882 BEAST_EXPECT(delta >= 0);
5883 for (int i = 0; i < delta; ++i)
5884 env.close();
5885 BEAST_EXPECT(openLedgerSeq(env) == env.seq(acct) + 255);
5886 };
5887
5888 // Close the ledger until the ledger sequence is no longer
5889 // within <FirstNFTokenSequence + MintedNFTokens + 256>.
5890 auto const incLgrSeqForFixNftRemint = [&](Env& env, Account const& acct) {
5891 int delta = 0;
5892 auto const deletableLgrSeq = (*env.le(acct))[~sfFirstNFTokenSequence].value_or(0) +
5893 (*env.le(acct))[sfMintedNFTokens] + 255;
5894
5895 if (deletableLgrSeq > openLedgerSeq(env))
5896 delta = deletableLgrSeq - openLedgerSeq(env);
5897
5898 BEAST_EXPECT(delta >= 0);
5899 for (int i = 0; i < delta; ++i)
5900 env.close();
5901 BEAST_EXPECT(openLedgerSeq(env) == deletableLgrSeq);
5902 };
5903
5904 // We check if NFTokenIDs can be duplicated by
5905 // re-creation of an account
5906 {
5907 Env env{*this, features};
5908 Account const alice("alice");
5909 Account const becky("becky");
5910
5911 env.fund(XRP(10000), alice, becky);
5912 env.close();
5913
5914 // alice mint and burn a NFT
5915 UInt256 const prevNFTokenID = token::getNextID(env, alice, 0u);
5916 env(token::mint(alice));
5917 env.close();
5918 env(token::burn(alice, prevNFTokenID));
5919 env.close();
5920
5921 // alice has minted 1 NFToken
5922 BEAST_EXPECT((*env.le(alice))[sfMintedNFTokens] == 1);
5923
5924 // Close enough ledgers to delete alice's account
5925 incLgrSeqForAcctDel(env, alice);
5926
5927 // alice's account is deleted
5928 Keylet const aliceAcctKey{keylet::account(alice.id())};
5929 auto const acctDelFee{drops(env.current()->fees().increment)};
5930 env(acctdelete(alice, becky), Fee(acctDelFee));
5931 env.close();
5932
5933 // alice's account root is gone from the most recently
5934 // closed ledger and the current ledger.
5935 BEAST_EXPECT(!env.closed()->exists(aliceAcctKey));
5936 BEAST_EXPECT(!env.current()->exists(aliceAcctKey));
5937
5938 // Fund alice to re-create her account
5939 env.fund(XRP(10000), alice);
5940 env.close();
5941
5942 // alice's account now exists and has minted 0 NFTokens
5943 BEAST_EXPECT(env.closed()->exists(aliceAcctKey));
5944 BEAST_EXPECT(env.current()->exists(aliceAcctKey));
5945 BEAST_EXPECT((*env.le(alice))[sfMintedNFTokens] == 0);
5946
5947 // alice mints a NFT with same params as prevNFTokenID
5948 UInt256 const remintNFTokenID = token::getNextID(env, alice, 0u);
5949 env(token::mint(alice));
5950 env.close();
5951
5952 // burn the NFT to make sure alice owns remintNFTokenID
5953 env(token::burn(alice, remintNFTokenID));
5954 env.close();
5955
5956 // Check that two NFTs don't have the same ID
5957 BEAST_EXPECT(remintNFTokenID != prevNFTokenID);
5958 }
5959
5960 // Test if the issuer account can be deleted after an authorized
5961 // minter mints and burns a batch of NFTokens.
5962 {
5963 Env env{*this, features};
5964 Account const alice("alice");
5965 Account const becky("becky");
5966 Account const minter{"minter"};
5967
5968 env.fund(XRP(10000), alice, becky, minter);
5969 env.close();
5970
5971 // alice sets minter as her authorized minter
5972 env(token::setMinter(alice, minter));
5973 env.close();
5974
5975 // minter mints 500 NFTs for alice
5976 std::vector<UInt256> nftIDs;
5977 nftIDs.reserve(500);
5978 for (int i = 0; i < 500; i++)
5979 {
5980 UInt256 const nftokenID = token::getNextID(env, alice, 0u);
5981 nftIDs.push_back(nftokenID);
5982 env(token::mint(minter), token::Issuer(alice));
5983 }
5984 env.close();
5985
5986 // minter burns 500 NFTs
5987 for (auto const nftokenID : nftIDs)
5988 {
5989 env(token::burn(minter, nftokenID));
5990 }
5991 env.close();
5992
5993 incLgrSeqForAcctDel(env, alice);
5994
5995 // Verify that alice's account root is present.
5996 Keylet const aliceAcctKey{keylet::account(alice.id())};
5997 BEAST_EXPECT(env.closed()->exists(aliceAcctKey));
5998 BEAST_EXPECT(env.current()->exists(aliceAcctKey));
5999
6000 auto const acctDelFee{drops(env.current()->fees().increment)};
6001
6002 // alice tries to delete her account, but is unsuccessful.
6003 // Due to authorized minting, alice's account sequence does not
6004 // advance while minter mints NFTokens for her.
6005 // The new account deletion restriction <FirstNFTokenSequence +
6006 // MintedNFTokens + 256> enabled by this amendment will enforce
6007 // alice to wait for more ledgers to close before she can
6008 // delete her account, to prevent duplicate NFTokenIDs
6009 env(acctdelete(alice, becky), Fee(acctDelFee), Ter(tecTOO_SOON));
6010 env.close();
6011
6012 // alice's account is still present
6013 BEAST_EXPECT(env.current()->exists(aliceAcctKey));
6014
6015 // Close more ledgers until it is no longer within
6016 // <FirstNFTokenSequence + MintedNFTokens + 256>
6017 // to be able to delete alice's account
6018 incLgrSeqForFixNftRemint(env, alice);
6019
6020 // alice's account is deleted
6021 env(acctdelete(alice, becky), Fee(acctDelFee));
6022 env.close();
6023
6024 // alice's account root is gone from the most recently
6025 // closed ledger and the current ledger.
6026 BEAST_EXPECT(!env.closed()->exists(aliceAcctKey));
6027 BEAST_EXPECT(!env.current()->exists(aliceAcctKey));
6028
6029 // Fund alice to re-create her account
6030 env.fund(XRP(10000), alice);
6031 env.close();
6032
6033 // alice's account now exists and has minted 0 NFTokens
6034 BEAST_EXPECT(env.closed()->exists(aliceAcctKey));
6035 BEAST_EXPECT(env.current()->exists(aliceAcctKey));
6036 BEAST_EXPECT((*env.le(alice))[sfMintedNFTokens] == 0);
6037
6038 // alice mints a NFT with same params as the first one before
6039 // the account delete.
6040 UInt256 const remintNFTokenID = token::getNextID(env, alice, 0u);
6041 env(token::mint(alice));
6042 env.close();
6043
6044 // burn the NFT to make sure alice owns remintNFTokenID
6045 env(token::burn(alice, remintNFTokenID));
6046 env.close();
6047
6048 // The new NFT minted will not have the same ID
6049 // as any of the NFTs authorized minter minted
6050 BEAST_EXPECT(std::ranges::find(nftIDs, remintNFTokenID) == nftIDs.end());
6051 }
6052
6053 // When an account mints and burns a batch of NFTokens using tickets,
6054 // see if the account can be deleted.
6055 {
6056 Env env{*this, features};
6057
6058 Account const alice{"alice"};
6059 Account const becky{"becky"};
6060 env.fund(XRP(10000), alice, becky);
6061 env.close();
6062
6063 // alice grab enough tickets for all of the following
6064 // transactions. Note that once the tickets are acquired alice's
6065 // account sequence number should not advance.
6066 std::uint32_t aliceTicketSeq{env.seq(alice) + 1};
6067 env(ticket::create(alice, 100));
6068 env.close();
6069
6070 BEAST_EXPECT(ticketCount(env, alice) == 100);
6071 BEAST_EXPECT(ownerCount(env, alice) == 100);
6072
6073 // alice mints 50 NFTs using tickets
6074 std::vector<UInt256> nftIDs;
6075 nftIDs.reserve(50);
6076 for (int i = 0; i < 50; i++)
6077 {
6078 nftIDs.push_back(token::getNextID(env, alice, 0u));
6079 env(token::mint(alice, 0u), ticket::Use(aliceTicketSeq++));
6080 env.close();
6081 }
6082
6083 // alice burns 50 NFTs using tickets
6084 for (auto const nftokenID : nftIDs)
6085 {
6086 env(token::burn(alice, nftokenID), ticket::Use(aliceTicketSeq++));
6087 }
6088 env.close();
6089
6090 BEAST_EXPECT(ticketCount(env, alice) == 0);
6091
6092 incLgrSeqForAcctDel(env, alice);
6093
6094 // Verify that alice's account root is present.
6095 Keylet const aliceAcctKey{keylet::account(alice.id())};
6096 BEAST_EXPECT(env.closed()->exists(aliceAcctKey));
6097 BEAST_EXPECT(env.current()->exists(aliceAcctKey));
6098
6099 auto const acctDelFee{drops(env.current()->fees().increment)};
6100
6101 // alice tries to delete her account, but is unsuccessful.
6102 // Due to authorized minting, alice's account sequence does not
6103 // advance while minter mints NFTokens for her using tickets.
6104 // The new account deletion restriction <FirstNFTokenSequence +
6105 // MintedNFTokens + 256> enabled by this amendment will enforce
6106 // alice to wait for more ledgers to close before she can
6107 // delete her account, to prevent duplicate NFTokenIDs
6108 env(acctdelete(alice, becky), Fee(acctDelFee), Ter(tecTOO_SOON));
6109 env.close();
6110
6111 // alice's account is still present
6112 BEAST_EXPECT(env.current()->exists(aliceAcctKey));
6113
6114 // Close more ledgers until it is no longer within
6115 // <FirstNFTokenSequence + MintedNFTokens + 256>
6116 // to be able to delete alice's account
6117 incLgrSeqForFixNftRemint(env, alice);
6118
6119 // alice's account is deleted
6120 env(acctdelete(alice, becky), Fee(acctDelFee));
6121 env.close();
6122
6123 // alice's account root is gone from the most recently
6124 // closed ledger and the current ledger.
6125 BEAST_EXPECT(!env.closed()->exists(aliceAcctKey));
6126 BEAST_EXPECT(!env.current()->exists(aliceAcctKey));
6127
6128 // Fund alice to re-create her account
6129 env.fund(XRP(10000), alice);
6130 env.close();
6131
6132 // alice's account now exists and has minted 0 NFTokens
6133 BEAST_EXPECT(env.closed()->exists(aliceAcctKey));
6134 BEAST_EXPECT(env.current()->exists(aliceAcctKey));
6135 BEAST_EXPECT((*env.le(alice))[sfMintedNFTokens] == 0);
6136
6137 // alice mints a NFT with same params as the first one before
6138 // the account delete.
6139 UInt256 const remintNFTokenID = token::getNextID(env, alice, 0u);
6140 env(token::mint(alice));
6141 env.close();
6142
6143 // burn the NFT to make sure alice owns remintNFTokenID
6144 env(token::burn(alice, remintNFTokenID));
6145 env.close();
6146
6147 // The new NFT minted will not have the same ID
6148 // as any of the NFTs authorized minter minted using tickets
6149 BEAST_EXPECT(std::ranges::find(nftIDs, remintNFTokenID) == nftIDs.end());
6150 }
6151 // When an authorized minter mints and burns a batch of NFTokens using
6152 // tickets, issuer's account needs to wait a longer time before it can
6153 // be deleted.
6154 // After the issuer's account is re-created and mints a NFT, it should
6155 // not have the same NFTokenID as the ones authorized minter minted.
6156 Env env{*this, features};
6157 Account const alice("alice");
6158 Account const becky("becky");
6159 Account const minter{"minter"};
6160
6161 env.fund(XRP(10000), alice, becky, minter);
6162 env.close();
6163
6164 // alice sets minter as her authorized minter
6165 env(token::setMinter(alice, minter));
6166 env.close();
6167
6168 // minter creates 100 tickets
6169 std::uint32_t minterTicketSeq{env.seq(minter) + 1};
6170 env(ticket::create(minter, 100));
6171 env.close();
6172
6173 BEAST_EXPECT(ticketCount(env, minter) == 100);
6174 BEAST_EXPECT(ownerCount(env, minter) == 100);
6175
6176 // minter mints 50 NFTs for alice using tickets
6177 std::vector<UInt256> nftIDs;
6178 nftIDs.reserve(50);
6179 for (int i = 0; i < 50; i++)
6180 {
6181 UInt256 const nftokenID = token::getNextID(env, alice, 0u);
6182 nftIDs.push_back(nftokenID);
6183 env(token::mint(minter), token::Issuer(alice), ticket::Use(minterTicketSeq++));
6184 }
6185 env.close();
6186
6187 // minter burns 50 NFTs using tickets
6188 for (auto const nftokenID : nftIDs)
6189 {
6190 env(token::burn(minter, nftokenID), ticket::Use(minterTicketSeq++));
6191 }
6192 env.close();
6193
6194 BEAST_EXPECT(ticketCount(env, minter) == 0);
6195
6196 incLgrSeqForAcctDel(env, alice);
6197
6198 // Verify that alice's account root is present.
6199 Keylet const aliceAcctKey{keylet::account(alice.id())};
6200 BEAST_EXPECT(env.closed()->exists(aliceAcctKey));
6201 BEAST_EXPECT(env.current()->exists(aliceAcctKey));
6202
6203 // alice tries to delete her account, but is unsuccessful.
6204 // Due to authorized minting, alice's account sequence does not
6205 // advance while minter mints NFTokens for her using tickets.
6206 // The new account deletion restriction <FirstNFTokenSequence +
6207 // MintedNFTokens + 256> enabled by this amendment will enforce
6208 // alice to wait for more ledgers to close before she can delete her
6209 // account, to prevent duplicate NFTokenIDs
6210 auto const acctDelFee{drops(env.current()->fees().increment)};
6211 env(acctdelete(alice, becky), Fee(acctDelFee), Ter(tecTOO_SOON));
6212 env.close();
6213
6214 // alice's account is still present
6215 BEAST_EXPECT(env.current()->exists(aliceAcctKey));
6216
6217 // Close more ledgers until it is no longer within
6218 // <FirstNFTokenSequence + MintedNFTokens + 256>
6219 // to be able to delete alice's account
6220 incLgrSeqForFixNftRemint(env, alice);
6221
6222 // alice's account is deleted
6223 env(acctdelete(alice, becky), Fee(acctDelFee));
6224 env.close();
6225
6226 // alice's account root is gone from the most recently
6227 // closed ledger and the current ledger.
6228 BEAST_EXPECT(!env.closed()->exists(aliceAcctKey));
6229 BEAST_EXPECT(!env.current()->exists(aliceAcctKey));
6230
6231 // Fund alice to re-create her account
6232 env.fund(XRP(10000), alice);
6233 env.close();
6234
6235 // alice's account now exists and has minted 0 NFTokens
6236 BEAST_EXPECT(env.closed()->exists(aliceAcctKey));
6237 BEAST_EXPECT(env.current()->exists(aliceAcctKey));
6238 BEAST_EXPECT((*env.le(alice))[sfMintedNFTokens] == 0);
6239
6240 // The new NFT minted will not have the same ID
6241 // as any of the NFTs authorized minter minted using tickets
6242 UInt256 const remintNFTokenID = token::getNextID(env, alice, 0u);
6243 env(token::mint(alice));
6244 env.close();
6245
6246 // burn the NFT to make sure alice owns remintNFTokenID
6247 env(token::burn(alice, remintNFTokenID));
6248 env.close();
6249
6250 // The new NFT minted will not have the same ID
6251 // as one of NFTs authorized minter minted using tickets
6252 BEAST_EXPECT(std::ranges::find(nftIDs, remintNFTokenID) == nftIDs.end());
6253 }
6254
6255 void
6257 {
6258 testcase("NFTokenMint with Create NFTokenOffer");
6259
6260 using namespace test::jtx;
6261
6262 if (!features[featureNFTokenMintOffer])
6263 {
6264 Env env{*this, features};
6265 Account const alice("alice");
6266 Account const buyer("buyer");
6267
6268 env.fund(XRP(10000), alice, buyer);
6269 env.close();
6270
6271 env(token::mint(alice), token::Amount(XRP(10000)), Ter(temDISABLED));
6272 env.close();
6273
6274 env(token::mint(alice), token::Destination("buyer"), Ter(temDISABLED));
6275 env.close();
6276
6277 env(token::mint(alice), token::Expiration(lastClose(env) + 25), Ter(temDISABLED));
6278 env.close();
6279
6280 return;
6281 }
6282
6283 // The remaining tests assume featureNFTokenMintOffer is enabled.
6284 {
6285 Env env{*this, features};
6286 auto const baseFee = env.current()->fees().base;
6287 Account const alice("alice");
6288 Account const buyer{"buyer"};
6289 Account const gw("gw");
6290 Account const issuer("issuer");
6291 Account const minter("minter");
6292 Account const bob("bob");
6293 IOU const gwAUD(gw["AUD"]);
6294
6295 env.fund(XRP(10000), alice, buyer, gw, issuer, minter);
6296 env.close();
6297
6298 {
6299 // Destination field specified but Amount field not specified
6300 env(token::mint(alice), token::Destination(buyer), Ter(temMALFORMED));
6301 env.close();
6302 BEAST_EXPECT(ownerCount(env, alice) == 0);
6303
6304 // Expiration field specified but Amount field not specified
6305 env(token::mint(alice), token::Expiration(lastClose(env) + 25), Ter(temMALFORMED));
6306 env.close();
6307 BEAST_EXPECT(ownerCount(env, buyer) == 0);
6308 }
6309
6310 {
6311 // The destination may not be the account submitting the
6312 // transaction.
6313 env(token::mint(alice),
6314 token::Amount(XRP(1000)),
6315 token::Destination(alice),
6316 Ter(temMALFORMED));
6317 env.close();
6318 BEAST_EXPECT(ownerCount(env, alice) == 0);
6319
6320 // The destination must be an account already established in the
6321 // ledger.
6322 env(token::mint(alice),
6323 token::Amount(XRP(1000)),
6324 token::Destination(Account("demon")),
6325 Ter(tecNO_DST));
6326 env.close();
6327 BEAST_EXPECT(ownerCount(env, alice) == 0);
6328 }
6329
6330 {
6331 // Set a bad expiration.
6332 env(token::mint(alice),
6333 token::Amount(XRP(1000)),
6334 token::Expiration(0),
6335 Ter(temBAD_EXPIRATION));
6336 env.close();
6337 BEAST_EXPECT(ownerCount(env, alice) == 0);
6338
6339 // The new NFTokenOffer may not have passed its expiration time.
6340 env(token::mint(alice),
6341 token::Amount(XRP(1000)),
6342 token::Expiration(lastClose(env)),
6343 Ter(tecEXPIRED));
6344 env.close();
6345 BEAST_EXPECT(ownerCount(env, alice) == 0);
6346 }
6347
6348 {
6349 // Set an invalid amount.
6350 env(token::mint(alice),
6351 token::Amount(buyer["USD"](1)),
6352 Txflags(tfOnlyXRP),
6353 Ter(temBAD_AMOUNT));
6354 env(token::mint(alice), token::Amount(buyer["USD"](0)), Ter(temBAD_AMOUNT));
6355 env.close();
6356 BEAST_EXPECT(ownerCount(env, alice) == 0);
6357
6358 // Issuer (alice) must have a trust line for the offered funds.
6359 env(token::mint(alice),
6360 token::Amount(gwAUD(1000)),
6361 Txflags(tfTransferable),
6362 token::XferFee(10),
6363 Ter(tecNO_LINE));
6364 env.close();
6365 BEAST_EXPECT(ownerCount(env, alice) == 0);
6366
6367 // If the IOU issuer and the NFToken issuer are the same,
6368 // then that issuer does not need a trust line to accept their
6369 // fee.
6370 env(token::mint(gw),
6371 token::Amount(gwAUD(1000)),
6372 Txflags(tfTransferable),
6373 token::XferFee(10));
6374 env.close();
6375
6376 // Give alice the needed trust line, but freeze it.
6377 env(trust(gw, alice["AUD"](999), tfSetFreeze));
6378 env.close();
6379
6380 // Issuer (alice) must have a trust line for the offered funds
6381 // and the trust line may not be frozen.
6382 env(token::mint(alice),
6383 token::Amount(gwAUD(1000)),
6384 Txflags(tfTransferable),
6385 token::XferFee(10),
6386 Ter(tecFROZEN));
6387 env.close();
6388 BEAST_EXPECT(ownerCount(env, alice) == 0);
6389
6390 // Seller (alice) must have a trust line may not be frozen.
6391 env(token::mint(alice), token::Amount(gwAUD(1000)), Ter(tecFROZEN));
6392 env.close();
6393 BEAST_EXPECT(ownerCount(env, alice) == 0);
6394
6395 // Unfreeze alice's trustline.
6396 env(trust(gw, alice["AUD"](999), tfClearFreeze));
6397 env.close();
6398 }
6399
6400 {
6401 // check reserve
6402 auto const acctReserve = env.current()->fees().reserve;
6403 auto const incReserve = env.current()->fees().increment;
6404
6405 env.fund(acctReserve + incReserve, bob);
6406 env.close();
6407
6408 // doesn't have reserve for 2 objects (NFTokenPage, Offer)
6409 env(token::mint(bob), token::Amount(XRP(0)), Ter(tecINSUFFICIENT_RESERVE));
6410 env.close();
6411
6412 // have reserve for NFTokenPage, Offer
6413 env(pay(env.master, bob, incReserve + drops(baseFee)));
6414 env.close();
6415 env(token::mint(bob), token::Amount(XRP(0)));
6416 env.close();
6417
6418 // doesn't have reserve for Offer
6419 env(pay(env.master, bob, drops(baseFee)));
6420 env.close();
6421 env(token::mint(bob), token::Amount(XRP(0)), Ter(tecINSUFFICIENT_RESERVE));
6422 env.close();
6423
6424 // have reserve for Offer
6425 env(pay(env.master, bob, incReserve + drops(baseFee)));
6426 env.close();
6427 env(token::mint(bob), token::Amount(XRP(0)));
6428 env.close();
6429 }
6430
6431 // Amount field specified
6432 BEAST_EXPECT(ownerCount(env, alice) == 0);
6433 env(token::mint(alice), token::Amount(XRP(10)));
6434 BEAST_EXPECT(ownerCount(env, alice) == 2);
6435 env.close();
6436
6437 // Amount field and Destination field, Expiration field specified
6438 env(token::mint(alice),
6439 token::Amount(XRP(10)),
6440 token::Destination(buyer),
6441 token::Expiration(lastClose(env) + 25));
6442 env.close();
6443
6444 // With TransferFee field
6445 env(trust(alice, gwAUD(1000)));
6446 env.close();
6447 env(token::mint(alice),
6448 token::Amount(gwAUD(1)),
6449 token::Destination(buyer),
6450 token::Expiration(lastClose(env) + 25),
6451 Txflags(tfTransferable),
6452 token::XferFee(10));
6453 env.close();
6454
6455 // Can be canceled by the issuer.
6456 env(token::mint(alice),
6457 token::Amount(XRP(10)),
6458 token::Destination(buyer),
6459 token::Expiration(lastClose(env) + 25));
6460 UInt256 const offerAliceSellsToBuyer =
6461 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
6462 env(token::cancelOffer(alice, {offerAliceSellsToBuyer}));
6463 env.close();
6464
6465 // Can be canceled by the buyer.
6466 env(token::mint(buyer),
6467 token::Amount(XRP(10)),
6468 token::Destination(alice),
6469 token::Expiration(lastClose(env) + 25));
6470 UInt256 const offerBuyerSellsToAlice =
6471 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
6472 env(token::cancelOffer(alice, {offerBuyerSellsToAlice}));
6473 env.close();
6474
6475 env(token::setMinter(issuer, minter));
6476 env.close();
6477
6478 // Minter will have offer not issuer
6479 BEAST_EXPECT(ownerCount(env, minter) == 0);
6480 BEAST_EXPECT(ownerCount(env, issuer) == 0);
6481 env(token::mint(minter), token::Issuer(issuer), token::Amount(drops(1)));
6482 env.close();
6483 BEAST_EXPECT(ownerCount(env, minter) == 2);
6484 BEAST_EXPECT(ownerCount(env, issuer) == 0);
6485 }
6486
6487 Env env{*this, features};
6488 Account const alice("alice");
6489
6490 env.fund(XRP(1000000), alice);
6491
6492 TER const offerCreateTER = temBAD_AMOUNT;
6493
6494 // Make offers with negative amounts for the NFTs
6495 env(token::mint(alice), token::Amount(XRP(-2)), Ter(offerCreateTER));
6496 env.close();
6497 }
6498
6499 void
6501 {
6502 // `nftoken_id` is added in the `tx` response for NFTokenMint and
6503 // NFTokenAcceptOffer.
6504 //
6505 // `nftoken_ids` is added in the `tx` response for NFTokenCancelOffer
6506 //
6507 // `offer_id` is added in the `tx` response for NFTokenCreateOffer
6508 //
6509 // The values of these fields are dependent on the NFTokenID/OfferID
6510 // changed in its corresponding transaction. We want to validate each
6511 // transaction to make sure the synthetic fields hold the right values.
6512
6513 testcase("Test synthetic fields from JSON response");
6514
6515 using namespace test::jtx;
6516
6517 Account const alice{"alice"};
6518 Account const bob{"bob"};
6519 Account const broker{"broker"};
6520
6521 Env env{*this, features};
6522 env.fund(XRP(10000), alice, bob, broker);
6523 env.close();
6524
6525 // Transaction metadata is not always reported under the same field
6526 // name: the `ledger` RPC uses `metaData`, the others use `meta`.
6527 auto const getMeta = [](json::Value const& tx) -> json::Value const* {
6528 if (tx.isMember(jss::meta))
6529 return &tx[jss::meta];
6530 if (tx.isMember(jss::metaData))
6531 return &tx[jss::metaData];
6532 return nullptr;
6533 };
6534
6535 // Neither is the transaction hash: api_version 1 nests the
6536 // transaction under `tx`, later versions use `tx_json`, and some
6537 // responses put the hash on the entry itself.
6538 auto const getHash = [](json::Value const& entry) -> std::string {
6539 if (entry.isMember(jss::tx) && entry[jss::tx].isMember(jss::hash))
6540 return entry[jss::tx][jss::hash].asString();
6541 if (entry.isMember(jss::tx_json) && entry[jss::tx_json].isMember(jss::hash))
6542 return entry[jss::tx_json][jss::hash].asString();
6543 return entry[jss::hash].asString();
6544 };
6545
6546 // Run `verifyMeta` against the metadata of the most recent
6547 // transaction as reported by the `tx`, `ledger` and `account_tx`
6548 // RPCs, so that the synthetic fields are checked in every response
6549 // that carries them. Runs under both api_version 1 (`tx`/`meta`)
6550 // and the latest api_version (`tx_json`/synthetic fields alongside
6551 // it), since the two versions place fields differently.
6552 auto verifyMetaInAllResponses = [&](auto verifyMeta) {
6553 // Get the hash for the most recent transaction.
6554 std::string const txHash{
6555 env.tx()->getJson(JsonOptions::Values::None)[jss::hash].asString(),
6556 };
6557
6558 env.close();
6559
6560 for (unsigned const apiVersion : {
6563 })
6564 {
6565 // Test 1: Check tx RPC response
6566 json::Value const txResult = env.rpc(apiVersion, "tx", txHash)[jss::result];
6567 verifyMeta(txResult[jss::meta]);
6568
6569 // Test 2: Check ledger RPC response with expanded
6570 // transactions
6571 json::Value ledgerParams;
6572 ledgerParams[jss::ledger_index] = txResult[jss::ledger_index].asUInt();
6573 ledgerParams[jss::transactions] = true;
6574 ledgerParams[jss::expand] = true;
6575
6576 auto const ledgerResult =
6577 env.rpc(apiVersion, "json", "ledger", to_string(ledgerParams));
6578 auto const& ledgerTx =
6579 ledgerResult[jss::result][jss::ledger][jss::transactions][0u];
6580
6581 // Verify transaction hash matches
6582 BEAST_EXPECT(getHash(ledgerTx) == txHash);
6583
6584 if (auto const* meta = getMeta(ledgerTx); BEAST_EXPECT(meta != nullptr))
6585 verifyMeta(*meta);
6586
6587 // Test 3: Check account_tx RPC response
6588 // The transaction is not necessarily alice's, so query
6589 // account_tx for the account that actually submitted it.
6590 json::Value accountTxParams;
6591 accountTxParams[jss::account] = txResult.isMember(jss::tx_json)
6592 ? txResult[jss::tx_json][jss::Account].asString()
6593 : txResult[jss::Account].asString();
6594
6595 auto const accountTxResult =
6596 env.rpc(apiVersion, "json", "account_tx", to_string(accountTxParams));
6597
6598 // account_tx ordering is not guaranteed, so find our
6599 // transaction by hash rather than assuming it is the most
6600 // recent one.
6601 json::Value const* accountTx = nullptr;
6602 for (auto const& entry : accountTxResult[jss::result][jss::transactions])
6603 {
6604 if (getHash(entry) == txHash)
6605 {
6606 accountTx = &entry;
6607 break;
6608 }
6609 }
6610
6611 if (!BEAST_EXPECT(accountTx != nullptr))
6612 continue;
6613
6614 if (auto const* meta = getMeta(*accountTx); BEAST_EXPECT(meta != nullptr))
6615 verifyMeta(*meta);
6616 }
6617 };
6618
6619 // Verify `nftoken_id` value equals to the NFTokenID that was
6620 // changed in the most recent NFTokenMint or NFTokenAcceptOffer
6621 // transaction
6622 auto const verifyNFTokenID = [&](UInt256 const& actualNftID) {
6623 verifyMetaInAllResponses([&](json::Value const& meta) {
6624 // Expect nftoken_id field
6625 if (!BEAST_EXPECT(meta.isMember(jss::nftoken_id)))
6626 return;
6627
6628 // Check the value of NFT ID matches
6629 UInt256 nftID;
6630 BEAST_EXPECT(nftID.parseHex(meta[jss::nftoken_id].asString()));
6631 BEAST_EXPECT(nftID == actualNftID);
6632 });
6633 };
6634
6635 // Verify `nftoken_ids` value equals to the NFTokenIDs that were
6636 // changed in the most recent NFTokenCancelOffer transaction
6637 auto const verifyNFTokenIDsInCancelOffer = [&](std::vector<UInt256> actualNftIDs) {
6638 // Sort to prepare for comparison
6639 std::ranges::sort(actualNftIDs);
6640
6641 verifyMetaInAllResponses([&](json::Value const& meta) {
6642 // Expect nftoken_ids field and verify the values
6643 if (!BEAST_EXPECT(meta.isMember(jss::nftoken_ids)))
6644 return;
6645
6646 // Convert NFT IDs from json::Value to UInt256
6647 std::vector<UInt256> metaIDs;
6649 meta[jss::nftoken_ids].begin(),
6650 meta[jss::nftoken_ids].end(),
6651 std::back_inserter(metaIDs),
6652 [this](json::Value id) {
6653 UInt256 nftID;
6654 BEAST_EXPECT(nftID.parseHex(id.asString()));
6655 return nftID;
6656 });
6657
6658 std::ranges::sort(metaIDs);
6659
6660 // Make sure the expect number of NFTs is correct
6661 if (!BEAST_EXPECT(metaIDs.size() == actualNftIDs.size()))
6662 return;
6663
6664 // Check the value of NFT ID in the meta with the
6665 // actual values
6666 for (size_t i = 0; i < metaIDs.size(); ++i)
6667 BEAST_EXPECT(metaIDs[i] == actualNftIDs[i]);
6668 });
6669 };
6670
6671 // Verify `offer_id` value equals to the offerID that was
6672 // changed in the most recent NFTokenCreateOffer tx
6673 auto const verifyNFTokenOfferID = [&](UInt256 const& offerID) {
6674 verifyMetaInAllResponses([&](json::Value const& meta) {
6675 // Expect offer_id field and verify the value
6676 if (!BEAST_EXPECT(meta.isMember(jss::offer_id)))
6677 return;
6678
6679 UInt256 metaOfferID;
6680 BEAST_EXPECT(metaOfferID.parseHex(meta[jss::offer_id].asString()));
6681 BEAST_EXPECT(metaOfferID == offerID);
6682 });
6683 };
6684
6685 // Check new fields in tx meta when for all NFTtransactions
6686 {
6687 // Alice mints 2 NFTs
6688 // Verify the NFTokenIDs are correct in the NFTokenMint tx meta
6689 UInt256 const nftId1{token::getNextID(env, alice, 0u, tfTransferable)};
6690 env(token::mint(alice, 0u), Txflags(tfTransferable));
6691 env.close();
6692 verifyNFTokenID(nftId1);
6693
6694 UInt256 const nftId2{token::getNextID(env, alice, 0u, tfTransferable)};
6695 env(token::mint(alice, 0u), Txflags(tfTransferable));
6696 env.close();
6697 verifyNFTokenID(nftId2);
6698
6699 // Alice creates one sell offer for each NFT
6700 // Verify the offer indexes are correct in the NFTokenCreateOffer tx
6701 // meta
6702 UInt256 const aliceOfferIndex1 =
6703 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
6704 env(token::createOffer(alice, nftId1, drops(1)), Txflags(tfSellNFToken));
6705 env.close();
6706 verifyNFTokenOfferID(aliceOfferIndex1);
6707
6708 UInt256 const aliceOfferIndex2 =
6709 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
6710 env(token::createOffer(alice, nftId2, drops(1)), Txflags(tfSellNFToken));
6711 env.close();
6712 verifyNFTokenOfferID(aliceOfferIndex2);
6713
6714 // Alice cancels two offers she created
6715 // Verify the NFTokenIDs are correct in the NFTokenCancelOffer tx
6716 // meta
6717 env(token::cancelOffer(alice, {aliceOfferIndex1, aliceOfferIndex2}));
6718 env.close();
6719 verifyNFTokenIDsInCancelOffer({nftId1, nftId2});
6720
6721 // Bobs creates a buy offer for nftId1
6722 // Verify the offer id is correct in the NFTokenCreateOffer tx meta
6723 auto const bobBuyOfferIndex =
6725 env(token::createOffer(bob, nftId1, drops(1)), token::Owner(alice));
6726 env.close();
6727 verifyNFTokenOfferID(bobBuyOfferIndex);
6728
6729 // Alice accepts bob's buy offer
6730 // Verify the NFTokenID is correct in the NFTokenAcceptOffer tx meta
6731 env(token::acceptBuyOffer(alice, bobBuyOfferIndex));
6732 env.close();
6733 verifyNFTokenID(nftId1);
6734 }
6735
6736 // Check `nftoken_ids` in brokered mode
6737 {
6738 // Alice mints a NFT
6739 UInt256 const nftId{token::getNextID(env, alice, 0u, tfTransferable)};
6740 env(token::mint(alice, 0u), Txflags(tfTransferable));
6741 env.close();
6742 verifyNFTokenID(nftId);
6743
6744 // Alice creates sell offer and set broker as destination
6745 UInt256 const offerAliceToBroker =
6746 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
6747 env(token::createOffer(alice, nftId, drops(1)),
6748 token::Destination(broker),
6749 Txflags(tfSellNFToken));
6750 env.close();
6751 verifyNFTokenOfferID(offerAliceToBroker);
6752
6753 // Bob creates buy offer
6754 UInt256 const offerBobToBroker =
6756 env(token::createOffer(bob, nftId, drops(1)), token::Owner(alice));
6757 env.close();
6758 verifyNFTokenOfferID(offerBobToBroker);
6759
6760 // Check NFTokenID meta for NFTokenAcceptOffer in brokered mode
6761 env(token::brokerOffers(broker, offerBobToBroker, offerAliceToBroker));
6762 env.close();
6763 verifyNFTokenID(nftId);
6764 }
6765
6766 // Check if there are no duplicate nft id in Cancel transactions where
6767 // multiple offers are cancelled for the same NFT
6768 {
6769 // Alice mints a NFT
6770 UInt256 const nftId{token::getNextID(env, alice, 0u, tfTransferable)};
6771 env(token::mint(alice, 0u), Txflags(tfTransferable));
6772 env.close();
6773 verifyNFTokenID(nftId);
6774
6775 // Alice creates 2 sell offers for the same NFT
6776 UInt256 const aliceOfferIndex1 =
6777 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
6778 env(token::createOffer(alice, nftId, drops(1)), Txflags(tfSellNFToken));
6779 env.close();
6780 verifyNFTokenOfferID(aliceOfferIndex1);
6781
6782 UInt256 const aliceOfferIndex2 =
6783 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
6784 env(token::createOffer(alice, nftId, drops(1)), Txflags(tfSellNFToken));
6785 env.close();
6786 verifyNFTokenOfferID(aliceOfferIndex2);
6787
6788 // Make sure the metadata only has 1 nft id, since both offers are
6789 // for the same nft
6790 env(token::cancelOffer(alice, {aliceOfferIndex1, aliceOfferIndex2}));
6791 env.close();
6792 verifyNFTokenIDsInCancelOffer({nftId});
6793 }
6794
6795 if (features[featureNFTokenMintOffer])
6796 {
6797 UInt256 const aliceMintWithOfferIndex1 =
6798 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
6799 env(token::mint(alice), token::Amount(XRP(0)));
6800 env.close();
6801 verifyNFTokenOfferID(aliceMintWithOfferIndex1);
6802 }
6803 }
6804
6805 void
6807 {
6808 testcase("Test buyer reserve when accepting an offer");
6809
6810 using namespace test::jtx;
6811
6812 // Lambda that mints an NFT and then creates a sell offer
6813 auto const mintAndCreateSellOffer =
6814 [](test::jtx::Env& env, test::jtx::Account const& acct, STAmount const amt) -> UInt256 {
6815 // acct mints a NFT
6816 UInt256 const nftId{token::getNextID(env, acct, 0u, tfTransferable)};
6817 env(token::mint(acct, 0u), Txflags(tfTransferable));
6818 env.close();
6819
6820 // acct makes an sell offer
6821 UInt256 const sellOfferIndex =
6823 env(token::createOffer(acct, nftId, amt), Txflags(tfSellNFToken));
6824 env.close();
6825
6826 return sellOfferIndex;
6827 };
6828
6829 // Test the behaviors when the buyer makes an accept offer, both before
6830 // and after enabling the amendment. Exercises the precise number of
6831 // reserve in drops that's required to accept the offer
6832 {
6833 Account const alice{"alice"};
6834 Account const bob{"bob"};
6835
6836 Env env{*this, features};
6837 auto const acctReserve = env.current()->fees().reserve;
6838 auto const incReserve = env.current()->fees().increment;
6839 auto const baseFee = env.current()->fees().base;
6840
6841 env.fund(XRP(10000), alice);
6842 env.close();
6843
6844 // Bob is funded with minimum XRP reserve
6845 env.fund(acctReserve, bob);
6846 env.close();
6847
6848 // alice mints an NFT and create a sell offer for 0 XRP
6849 auto const sellOfferIndex = mintAndCreateSellOffer(env, alice, XRP(0));
6850
6851 // Bob owns no object
6852 BEAST_EXPECT(ownerCount(env, bob) == 0);
6853
6854 // bob can no longer accept the offer unless
6855 // there is enough reserve. A detail to note is that NFTs(sell
6856 // offer) will not allow one to go below the reserve requirement,
6857 // because buyer's balance is computed after the transaction fee is
6858 // deducted. This means that the reserve requirement will be `base
6859 // fee` drops higher than normal.
6860
6861 // Bob is not able to accept the offer with only the account
6862 // reserve (200,000,000 drops)
6863 env(token::acceptSellOffer(bob, sellOfferIndex), Ter(tecINSUFFICIENT_RESERVE));
6864 env.close();
6865
6866 // after prev transaction, Bob owns `200M - base fee` drops due
6867 // to burnt tx fee
6868
6869 BEAST_EXPECT(ownerCount(env, bob) == 0);
6870
6871 // Send bob an kIncrement reserve and base fee (to make up for
6872 // the transaction fee burnt from the prev failed tx) Bob now
6873 // owns 250,000,000 drops
6874 env(pay(env.master, bob, incReserve + drops(baseFee)));
6875 env.close();
6876
6877 // However, this transaction will still fail because the reserve
6878 // requirement is `base fee` drops higher
6879 env(token::acceptSellOffer(bob, sellOfferIndex), Ter(tecINSUFFICIENT_RESERVE));
6880 env.close();
6881
6882 // Send bob `base fee * 2` drops
6883 // Bob now owns `250M + base fee` drops
6884 env(pay(env.master, bob, drops(baseFee * 2)));
6885 env.close();
6886
6887 // Bob is now able to accept the offer
6888 env(token::acceptSellOffer(bob, sellOfferIndex));
6889 env.close();
6890
6891 BEAST_EXPECT(ownerCount(env, bob) == 1);
6892 }
6893
6894 // Now exercise the scenario when the buyer accepts
6895 // many sell offers
6896 {
6897 Account const alice{"alice"};
6898 Account const bob{"bob"};
6899
6900 Env env{*this, features};
6901 auto const acctReserve = env.current()->fees().reserve;
6902 auto const incReserve = env.current()->fees().increment;
6903
6904 env.fund(XRP(10000), alice);
6905 env.close();
6906
6907 env.fund(acctReserve + XRP(1), bob);
6908 env.close();
6909
6910 // alice mints the first NFT and creates a sell offer for 0 XRP
6911 auto const sellOfferIndex1 = mintAndCreateSellOffer(env, alice, XRP(0));
6912
6913 // Bob cannot accept this offer because he doesn't have the
6914 // reserve for the NFT
6915 env(token::acceptSellOffer(bob, sellOfferIndex1), Ter(tecINSUFFICIENT_RESERVE));
6916 env.close();
6917
6918 // Give bob enough reserve
6919 env(pay(env.master, bob, drops(incReserve)));
6920 env.close();
6921
6922 BEAST_EXPECT(ownerCount(env, bob) == 0);
6923
6924 // Bob now owns his first NFT
6925 env(token::acceptSellOffer(bob, sellOfferIndex1));
6926 env.close();
6927
6928 BEAST_EXPECT(ownerCount(env, bob) == 1);
6929
6930 // alice now mints 31 more NFTs and creates an offer for each
6931 // NFT, then sells to bob
6932 for (size_t i = 0; i < 31; i++)
6933 {
6934 // alice mints an NFT and creates a sell offer for 0 XRP
6935 auto const sellOfferIndex = mintAndCreateSellOffer(env, alice, XRP(0));
6936
6937 // Bob can accept the offer because the new NFT is stored in
6938 // an existing NFTokenPage so no new reserve is required
6939 env(token::acceptSellOffer(bob, sellOfferIndex));
6940 env.close();
6941 }
6942
6943 BEAST_EXPECT(ownerCount(env, bob) == 1);
6944
6945 // alice now mints the 33rd NFT and creates an sell offer for 0
6946 // XRP
6947 auto const sellOfferIndex33 = mintAndCreateSellOffer(env, alice, XRP(0));
6948
6949 // Bob fails to accept this NFT because he does not have enough
6950 // reserve for a new NFTokenPage
6951 env(token::acceptSellOffer(bob, sellOfferIndex33), Ter(tecINSUFFICIENT_RESERVE));
6952 env.close();
6953
6954 // Send bob incremental reserve
6955 env(pay(env.master, bob, drops(incReserve)));
6956 env.close();
6957
6958 // Bob now has enough reserve to accept the offer and now
6959 // owns one more NFTokenPage
6960 env(token::acceptSellOffer(bob, sellOfferIndex33));
6961 env.close();
6962
6963 BEAST_EXPECT(ownerCount(env, bob) == 2);
6964 }
6965
6966 // Test the behavior when the seller accepts a buy offer.
6967 {
6968 Account const alice{"alice"};
6969 Account const bob{"bob"};
6970
6971 Env env{*this, features};
6972 auto const acctReserve = env.current()->fees().reserve;
6973 auto const incReserve = env.current()->fees().increment;
6974 auto const baseFee = env.current()->fees().base;
6975
6976 env.fund(XRP(10000), alice);
6977 env.close();
6978
6979 // Bob is funded with account reserve + kIncrement reserve + 1 XRP
6980 // kIncrement reserve is for the buy offer, and 1 XRP is for offer
6981 // price
6982 env.fund(acctReserve + incReserve + XRP(1), bob);
6983 env.close();
6984
6985 // Alice mints a NFT
6986 UInt256 const nftId{token::getNextID(env, alice, 0u, tfTransferable)};
6987 env(token::mint(alice, 0u), Txflags(tfTransferable));
6988 env.close();
6989
6990 // Bob makes a buy offer for 1 XRP
6991 auto const buyOfferIndex =
6993 env(token::createOffer(bob, nftId, XRP(1)), token::Owner(alice));
6994 env.close();
6995
6996 // accepting the buy offer fails because bob's balance is `base fee`
6997 // drops lower than the required amount, since the previous tx burnt
6998 // drops for tx fee.
6999 env(token::acceptBuyOffer(alice, buyOfferIndex), Ter(tecINSUFFICIENT_FUNDS));
7000 env.close();
7001
7002 // send Bob `base fee` drops
7003 env(pay(env.master, bob, drops(baseFee)));
7004 env.close();
7005
7006 // Now bob can buy the offer
7007 env(token::acceptBuyOffer(alice, buyOfferIndex));
7008 env.close();
7009 }
7010
7011 // Test the reserve behavior in brokered mode.
7012 {
7013 Account const alice{"alice"};
7014 Account const bob{"bob"};
7015 Account const broker{"broker"};
7016
7017 Env env{*this, features};
7018 auto const acctReserve = env.current()->fees().reserve;
7019 auto const incReserve = env.current()->fees().increment;
7020 auto const baseFee = env.current()->fees().base;
7021
7022 env.fund(XRP(10000), alice, broker);
7023 env.close();
7024
7025 // Bob is funded with account reserve + incr reserve + 1 XRP(offer
7026 // price)
7027 env.fund(acctReserve + incReserve + XRP(1), bob);
7028 env.close();
7029
7030 // Alice mints a NFT
7031 UInt256 const nftId{token::getNextID(env, alice, 0u, tfTransferable)};
7032 env(token::mint(alice, 0u), Txflags(tfTransferable));
7033 env.close();
7034
7035 // Alice creates sell offer and set broker as destination
7036 UInt256 const offerAliceToBroker =
7037 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
7038 env(token::createOffer(alice, nftId, XRP(1)),
7039 token::Destination(broker),
7040 Txflags(tfSellNFToken));
7041 env.close();
7042
7043 // Bob creates buy offer
7044 UInt256 const offerBobToBroker =
7046 env(token::createOffer(bob, nftId, XRP(1)), token::Owner(alice));
7047 env.close();
7048
7049 // broker offers.
7050 // Returns insufficient funds, because bob burnt tx fee when he
7051 // created his buy offer, which makes his spendable balance to be
7052 // less than the required amount.
7053 env(token::brokerOffers(broker, offerBobToBroker, offerAliceToBroker),
7055 env.close();
7056
7057 // send Bob `base fee` drops
7058 env(pay(env.master, bob, drops(baseFee)));
7059 env.close();
7060
7061 // broker offers.
7062 env(token::brokerOffers(broker, offerBobToBroker, offerAliceToBroker));
7063 env.close();
7064 }
7065 }
7066
7067 void
7069 {
7070 testcase("Test fix unasked for auto-trustline.");
7071
7072 using namespace test::jtx;
7073
7074 Account const issuer{"issuer"};
7075 Account const becky{"becky"};
7076 Account const cheri{"cheri"};
7077 Account const gw("gw");
7078 IOU const gwAUD(gw["AUD"]);
7079
7080 // This test case covers issue...
7081 // https://github.com/XRPLF/rippled/issues/4925
7082 //
7083 // For an NFToken with a transfer fee, the issuer must be able to
7084 // accept the transfer fee or else a transfer should fail. If the
7085 // NFToken is transferred for a non-XRP asset, then the issuer must
7086 // have a trustline to that asset to receive the fee.
7087 //
7088 // This test looks at a situation where issuer would get a trustline
7089 // for the fee without the issuer's consent. Here are the steps:
7090 // 1. Issuer has a trustline (i.e., USD)
7091 // 2. Issuer mints NFToken with transfer fee.
7092 // 3. Becky acquires the NFToken, paying with XRP.
7093 // 4. Becky creates offer to sell NFToken for USD(100).
7094 // 5. Issuer deletes trustline for USD.
7095 // 6. Carol buys NFToken from Becky for USD(100).
7096 // 7. The transfer fee from Carol's purchase re-establishes issuer's
7097 // USD trustline.
7098 //
7099 // The fixEnforceNFTokenTrustline amendment addresses this oversight.
7100 //
7101 // We run this test case both with and without
7102 // fixEnforceNFTokenTrustline enabled so we can see the change
7103 // in behavior.
7104 //
7105 // In both cases we remove the fixRemoveNFTokenAutoTrustLine amendment.
7106 // Otherwise we can't create NFTokens with tfTrustLine enabled.
7107 FeatureBitset const localFeatures = features - fixRemoveNFTokenAutoTrustLine;
7108 for (FeatureBitset feats : {
7109 localFeatures - fixEnforceNFTokenTrustline,
7110 localFeatures | fixEnforceNFTokenTrustline,
7111 })
7112 {
7113 Env env{*this, feats};
7114 env.fund(XRP(1000), issuer, becky, cheri, gw);
7115 env.close();
7116
7117 // Set trust lines so becky and cheri can use gw's currency.
7118 env(trust(becky, gwAUD(1000)));
7119 env(trust(cheri, gwAUD(1000)));
7120 env.close();
7121 env(pay(gw, cheri, gwAUD(500)));
7122 env.close();
7123
7124 // issuer creates two NFTs: one with and one without AutoTrustLine.
7125 std::uint16_t const xferFee = 5000; // 5%
7126 UInt256 const nftAutoTrustID{
7127 token::getNextID(env, issuer, 0u, tfTransferable | tfTrustLine, xferFee),
7128 };
7129 env(token::mint(issuer, 0u),
7130 token::XferFee(xferFee),
7131 Txflags(tfTransferable | tfTrustLine));
7132 env.close();
7133
7134 UInt256 const nftNoAutoTrustID{
7135 token::getNextID(env, issuer, 0u, tfTransferable, xferFee),
7136 };
7137 env(token::mint(issuer, 0u), token::XferFee(xferFee), Txflags(tfTransferable));
7138 env.close();
7139
7140 // becky buys the nfts for 1 drop each.
7141 {
7142 UInt256 const beckyBuyOfferIndex1 =
7143 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
7144 env(token::createOffer(becky, nftAutoTrustID, drops(1)), token::Owner(issuer));
7145
7146 UInt256 const beckyBuyOfferIndex2 =
7147 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
7148 env(token::createOffer(becky, nftNoAutoTrustID, drops(1)), token::Owner(issuer));
7149
7150 env.close();
7151 env(token::acceptBuyOffer(issuer, beckyBuyOfferIndex1));
7152 env(token::acceptBuyOffer(issuer, beckyBuyOfferIndex2));
7153 env.close();
7154 }
7155
7156 // becky creates offers to sell the nfts for AUD.
7157 UInt256 const beckyAutoTrustOfferIndex =
7158 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
7159 env(token::createOffer(becky, nftAutoTrustID, gwAUD(100)), Txflags(tfSellNFToken));
7160 env.close();
7161
7162 // Creating an offer for the NFToken without tfTrustLine fails
7163 // because issuer does not have a trust line for AUD.
7164 env(token::createOffer(becky, nftNoAutoTrustID, gwAUD(100)),
7165 Txflags(tfSellNFToken),
7166 Ter(tecNO_LINE));
7167 env.close();
7168
7169 // issuer creates a trust line. Now the offer create for the
7170 // NFToken without tfTrustLine succeeds.
7171 BEAST_EXPECT(ownerCount(env, issuer) == 0);
7172 env(trust(issuer, gwAUD(1000)));
7173 env.close();
7174 BEAST_EXPECT(ownerCount(env, issuer) == 1);
7175
7176 UInt256 const beckyNoAutoTrustOfferIndex =
7177 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
7178 env(token::createOffer(becky, nftNoAutoTrustID, gwAUD(100)), Txflags(tfSellNFToken));
7179 env.close();
7180
7181 // Now that the offers are in place, issuer removes the trustline.
7182 BEAST_EXPECT(ownerCount(env, issuer) == 1);
7183 env(trust(issuer, gwAUD(0)));
7184 env.close();
7185 BEAST_EXPECT(ownerCount(env, issuer) == 0);
7186
7187 // cheri attempts to accept becky's offers. Behavior with the
7188 // AutoTrustline NFT is uniform: issuer gets a new trust line.
7189 env(token::acceptSellOffer(cheri, beckyAutoTrustOfferIndex));
7190 env.close();
7191
7192 // Here's evidence that issuer got the new AUD trust line.
7193 BEAST_EXPECT(ownerCount(env, issuer) == 1);
7194 BEAST_EXPECT(env.balance(issuer, gwAUD) == gwAUD(5));
7195
7196 // issuer once again removes the trust line for AUD.
7197 env(pay(issuer, gw, gwAUD(5)));
7198 env.close();
7199 BEAST_EXPECT(ownerCount(env, issuer) == 0);
7200
7201 // cheri attempts to accept the NoAutoTrustLine NFT. Behavior
7202 // depends on whether fixEnforceNFTokenTrustline is enabled.
7203 if (feats[fixEnforceNFTokenTrustline])
7204 {
7205 // With fixEnforceNFTokenTrustline cheri can't accept the
7206 // offer because issuer could not get their transfer fee
7207 // without the appropriate trustline.
7208 env(token::acceptSellOffer(cheri, beckyNoAutoTrustOfferIndex), Ter(tecNO_LINE));
7209 env.close();
7210
7211 // But if issuer re-establishes the trustline then the offer
7212 // can be accepted.
7213 env(trust(issuer, gwAUD(1000)));
7214 env.close();
7215 BEAST_EXPECT(ownerCount(env, issuer) == 1);
7216
7217 env(token::acceptSellOffer(cheri, beckyNoAutoTrustOfferIndex));
7218 env.close();
7219 }
7220 else
7221 {
7222 // Without fixEnforceNFTokenTrustline the offer just works
7223 // and issuer gets a trustline that they did not request.
7224 env(token::acceptSellOffer(cheri, beckyNoAutoTrustOfferIndex));
7225 env.close();
7226 }
7227 BEAST_EXPECT(ownerCount(env, issuer) == 1);
7228 BEAST_EXPECT(env.balance(issuer, gwAUD) == gwAUD(5));
7229 } // for feats
7230 }
7231
7232 void
7234 {
7235 testcase("Test fix NFT issuer is IOU issuer");
7236
7237 using namespace test::jtx;
7238
7239 Account const issuer{"issuer"};
7240 Account const becky{"becky"};
7241 Account const cheri{"cheri"};
7242 IOU const isISU(issuer["ISU"]);
7243
7244 // This test case covers issue...
7245 // https://github.com/XRPLF/rippled/issues/4941
7246 //
7247 // If an NFToken has a transfer fee then, when an offer is accepted,
7248 // a portion of the sale price goes to the issuer.
7249 //
7250 // It is possible for an issuer to issue both an IOU (for remittances)
7251 // and NFTokens. If the issuer's IOU is used to pay for the transfer
7252 // of one of the issuer's NFTokens, then paying the fee for that
7253 // transfer will fail with a tecNO_LINE.
7254 //
7255 // The problem occurs because the NFT code looks for a trust line to
7256 // pay the transfer fee. However the issuer of an IOU does not need
7257 // a trust line to accept their own issuance and, in fact, is not
7258 // allowed to have a trust line to themselves.
7259 //
7260 // This test looks at a situation where transfer of an NFToken is
7261 // prevented by this bug:
7262 // 1. Issuer issues an IOU (e.g, isISU).
7263 // 2. Becky and Cheri get trust lines for, and acquire, some isISU.
7264 // 3. Issuer mints NFToken with transfer fee.
7265 // 4. Becky acquires the NFToken, paying with XRP.
7266 // 5. Becky attempts to create an offer to sell the NFToken for
7267 // isISU(100). The attempt fails with `tecNO_LINE`.
7268 //
7269 // The featureNFTokenMintOffer amendment addresses this oversight.
7270 //
7271 // We remove the fixRemoveNFTokenAutoTrustLine amendment. Otherwise
7272 // we can't create NFTokens with tfTrustLine enabled.
7273 FeatureBitset const localFeatures = features - fixRemoveNFTokenAutoTrustLine;
7274
7275 Env env{*this, localFeatures};
7276 env.fund(XRP(1000), issuer, becky, cheri);
7277 env.close();
7278
7279 // Set trust lines so becky and cheri can use isISU.
7280 env(trust(becky, isISU(1000)));
7281 env(trust(cheri, isISU(1000)));
7282 env.close();
7283 env(pay(issuer, cheri, isISU(500)));
7284 env.close();
7285
7286 // issuer creates two NFTs: one with and one without AutoTrustLine.
7287 std::uint16_t const xferFee = 5000; // 5%
7288 UInt256 const nftAutoTrustID{
7289 token::getNextID(env, issuer, 0u, tfTransferable | tfTrustLine, xferFee),
7290 };
7291 env(token::mint(issuer, 0u),
7292 token::XferFee(xferFee),
7293 Txflags(tfTransferable | tfTrustLine));
7294 env.close();
7295
7296 UInt256 const nftNoAutoTrustID{token::getNextID(env, issuer, 0u, tfTransferable, xferFee)};
7297 env(token::mint(issuer, 0u), token::XferFee(xferFee), Txflags(tfTransferable));
7298 env.close();
7299
7300 // becky buys the nfts for 1 drop each.
7301 {
7302 UInt256 const beckyBuyOfferIndex1 =
7303 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
7304 env(token::createOffer(becky, nftAutoTrustID, drops(1)), token::Owner(issuer));
7305
7306 UInt256 const beckyBuyOfferIndex2 =
7307 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
7308 env(token::createOffer(becky, nftNoAutoTrustID, drops(1)), token::Owner(issuer));
7309
7310 env.close();
7311 env(token::acceptBuyOffer(issuer, beckyBuyOfferIndex1));
7312 env(token::acceptBuyOffer(issuer, beckyBuyOfferIndex2));
7313 env.close();
7314 }
7315
7316 // Behavior from here down diverges significantly based on
7317 // featureNFTokenMintOffer.
7318 if (!localFeatures[featureNFTokenMintOffer])
7319 {
7320 // Without featureNFTokenMintOffer becky simply can't
7321 // create an offer for a non-tfTrustLine NFToken that would
7322 // pay the transfer fee in issuer's own IOU.
7323 env(token::createOffer(becky, nftNoAutoTrustID, isISU(100)),
7324 Txflags(tfSellNFToken),
7325 Ter(tecNO_LINE));
7326 env.close();
7327
7328 // And issuer can't create a trust line to themselves.
7329 env(trust(issuer, isISU(1000)), Ter(temDST_IS_SRC));
7330 env.close();
7331
7332 // However if the NFToken has the tfTrustLine flag set,
7333 // then becky can create the offer.
7334 UInt256 const beckyAutoTrustOfferIndex =
7335 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
7336 env(token::createOffer(becky, nftAutoTrustID, isISU(100)), Txflags(tfSellNFToken));
7337 env.close();
7338
7339 // And cheri can accept the offer.
7340 env(token::acceptSellOffer(cheri, beckyAutoTrustOfferIndex));
7341 env.close();
7342
7343 // We verify that issuer got their transfer fee by seeing that
7344 // ISU(5) has disappeared out of cheri's and becky's balances.
7345 BEAST_EXPECT(env.balance(becky, isISU) == isISU(95));
7346 BEAST_EXPECT(env.balance(cheri, isISU) == isISU(400));
7347 }
7348 else
7349 {
7350 // With featureNFTokenMintOffer things go better.
7351 // becky creates offers to sell the nfts for ISU.
7352 UInt256 const beckyNoAutoTrustOfferIndex =
7353 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
7354 env(token::createOffer(becky, nftNoAutoTrustID, isISU(100)), Txflags(tfSellNFToken));
7355 env.close();
7356 UInt256 const beckyAutoTrustOfferIndex =
7357 keylet::nftokenOffer(becky, SeqProxy::rawSequence(env.seq(becky))).key;
7358 env(token::createOffer(becky, nftAutoTrustID, isISU(100)), Txflags(tfSellNFToken));
7359 env.close();
7360
7361 // cheri accepts becky's offers. Behavior is uniform:
7362 // issuer gets paid.
7363 env(token::acceptSellOffer(cheri, beckyAutoTrustOfferIndex));
7364 env.close();
7365
7366 // We verify that issuer got their transfer fee by seeing that
7367 // ISU(5) has disappeared out of cheri's and becky's balances.
7368 BEAST_EXPECT(env.balance(becky, isISU) == isISU(95));
7369 BEAST_EXPECT(env.balance(cheri, isISU) == isISU(400));
7370
7371 env(token::acceptSellOffer(cheri, beckyNoAutoTrustOfferIndex));
7372 env.close();
7373
7374 // We verify that issuer got their transfer fee by seeing that
7375 // an additional ISU(5) has disappeared out of cheri's and
7376 // becky's balances.
7377 BEAST_EXPECT(env.balance(becky, isISU) == isISU(190));
7378 BEAST_EXPECT(env.balance(cheri, isISU) == isISU(300));
7379 }
7380 }
7381
7382 void
7384 {
7385 testcase("Test NFTokenModify");
7386
7387 using namespace test::jtx;
7388
7389 Account const issuer{"issuer"};
7390 Account const alice("alice");
7391 Account const bob("bob");
7392
7393 bool const modifyEnabled = features[featureDynamicNFT];
7394
7395 {
7396 // Mint with tfMutable
7397 Env env{*this, features};
7398 env.fund(XRP(10000), issuer);
7399 env.close();
7400
7401 auto const expectedTer = modifyEnabled ? TER{tesSUCCESS} : TER{temINVALID_FLAG};
7402 env(token::mint(issuer, 0u), Txflags(tfMutable), Ter(expectedTer));
7403 env.close();
7404 }
7405 {
7406 Env env{*this, features};
7407 env.fund(XRP(10000), issuer);
7408 env.close();
7409
7410 // Modify a nftoken
7411 UInt256 const nftId{token::getNextID(env, issuer, 0u, tfMutable)};
7412 if (modifyEnabled)
7413 {
7414 env(token::mint(issuer, 0u), Txflags(tfMutable));
7415 env.close();
7416 BEAST_EXPECT(ownerCount(env, issuer) == 1);
7417 env(token::modify(issuer, nftId));
7418 BEAST_EXPECT(ownerCount(env, issuer) == 1);
7419 }
7420 else
7421 {
7422 env(token::mint(issuer, 0u));
7423 env.close();
7424 env(token::modify(issuer, nftId), Ter(temDISABLED));
7425 env.close();
7426 }
7427 }
7428 if (!modifyEnabled)
7429 return;
7430
7431 {
7432 Env env{*this, features};
7433 env.fund(XRP(10000), issuer);
7434 env.close();
7435
7436 UInt256 const nftId{token::getNextID(env, issuer, 0u, tfMutable)};
7437 env(token::mint(issuer, 0u), Txflags(tfMutable));
7438 env.close();
7439
7440 // Set a negative fee. Exercises invalid preflight1.
7441 env(token::modify(issuer, nftId), Fee(STAmount(10ull, true)), Ter(temBAD_FEE));
7442 env.close();
7443
7444 // Invalid Flags
7445 env(token::modify(issuer, nftId), Txflags(0x00000001), Ter(temINVALID_FLAG));
7446
7447 // Invalid Owner
7448 env(token::modify(issuer, nftId), token::Owner(issuer), Ter(temMALFORMED));
7449 env.close();
7450
7451 // Invalid URI length = 0
7452 env(token::modify(issuer, nftId), token::Uri(""), Ter(temMALFORMED));
7453 env.close();
7454
7455 // Invalid URI length > 256
7456 env(token::modify(issuer, nftId),
7457 token::Uri(std::string(kMaxTokenUriLength + 1, 'q')),
7458 Ter(temMALFORMED));
7459 env.close();
7460 }
7461 {
7462 Env env{*this, features};
7463 env.fund(XRP(10000), issuer, alice, bob);
7464 env.close();
7465
7466 {
7467 // NFToken not exists
7468 UInt256 const nftIDNotExists{token::getNextID(env, issuer, 0u, tfMutable)};
7469 env.close();
7470
7471 env(token::modify(issuer, nftIDNotExists), Ter(tecNO_ENTRY));
7472 env.close();
7473 }
7474 {
7475 // Invalid NFToken flag
7476 UInt256 const nftIDNotModifiable{token::getNextID(env, issuer, 0u)};
7477 env(token::mint(issuer, 0u));
7478 env.close();
7479
7480 env(token::modify(issuer, nftIDNotModifiable), Ter(tecNO_PERMISSION));
7481 env.close();
7482 }
7483 {
7484 // Unauthorized account
7485 UInt256 const nftId{token::getNextID(env, issuer, 0u, tfMutable)};
7486 env(token::mint(issuer, 0u), Txflags(tfMutable));
7487 env.close();
7488
7489 env(token::modify(bob, nftId), token::Owner(issuer), Ter(tecNO_PERMISSION));
7490 env.close();
7491
7492 env(token::setMinter(issuer, alice));
7493 env.close();
7494
7495 env(token::modify(bob, nftId), token::Owner(issuer), Ter(tecNO_PERMISSION));
7496 env.close();
7497 }
7498 }
7499 {
7500 Env env{*this, features};
7501 env.fund(XRP(10000), issuer, alice, bob);
7502 env.close();
7503
7504 // modify with tfFullyCanonicalSig should success
7505 UInt256 const nftId{token::getNextID(env, issuer, 0u, tfMutable)};
7506 env(token::mint(issuer, 0u), Txflags(tfMutable), token::Uri("uri"));
7507 env.close();
7508
7509 env(token::modify(issuer, nftId), Txflags(tfFullyCanonicalSig));
7510 env.close();
7511 }
7512 {
7513 Env env{*this, features};
7514 env.fund(XRP(10000), issuer, alice, bob);
7515 env.close();
7516
7517 // lambda that returns the JSON form of NFTokens held by acct
7518 auto accountNFTs = [&env](Account const& acct) {
7519 json::Value params;
7520 params[jss::account] = acct.human();
7521 params[jss::type] = "state";
7522 auto response = env.rpc("json", "account_nfts", to_string(params));
7523 return response[jss::result][jss::account_nfts];
7524 };
7525
7526 // lambda that checks for the expected URI value of an NFToken
7527 auto const checkURI = [&accountNFTs, this](
7528 Account const& acct, char const* uri, int line) {
7529 auto const nfts = accountNFTs(acct);
7530 if (nfts.size() == 1)
7531 {
7532 pass();
7533 }
7534 else
7535 {
7536 std::ostringstream text;
7537 text << "checkURI: unexpected NFT count on line " << line;
7538 fail(text.str(), __FILE__, line);
7539 return;
7540 }
7541
7542 if (uri == nullptr)
7543 {
7544 if (!nfts[0u].isMember(sfURI.jsonName))
7545 {
7546 pass();
7547 }
7548 else
7549 {
7550 std::ostringstream text;
7551 text << "checkURI: unexpected URI present on line " << line;
7552 fail(text.str(), __FILE__, line);
7553 }
7554 return;
7555 }
7556
7557 if (nfts[0u][sfURI.jsonName] == strHex(std::string(uri)))
7558 {
7559 pass();
7560 }
7561 else
7562 {
7563 std::ostringstream text;
7564 text << "checkURI: unexpected URI contents on line " << line;
7565 fail(text.str(), __FILE__, line);
7566 }
7567 };
7568
7569 UInt256 const nftId{token::getNextID(env, issuer, 0u, tfMutable)};
7570 env.close();
7571
7572 env(token::mint(issuer, 0u), Txflags(tfMutable), token::Uri("uri"));
7573 env.close();
7574 checkURI(issuer, "uri", __LINE__);
7575
7576 // set URI Field
7577 env(token::modify(issuer, nftId), token::Uri("new_uri"));
7578 env.close();
7579 checkURI(issuer, "new_uri", __LINE__);
7580
7581 // unset URI Field
7582 env(token::modify(issuer, nftId));
7583 env.close();
7584 checkURI(issuer, nullptr, __LINE__);
7585
7586 // set URI Field
7587 env(token::modify(issuer, nftId), token::Uri("uri"));
7588 env.close();
7589 checkURI(issuer, "uri", __LINE__);
7590
7591 // Account != Owner
7592 UInt256 const offerID =
7593 keylet::nftokenOffer(issuer, SeqProxy::rawSequence(env.seq(issuer))).key;
7594 env(token::createOffer(issuer, nftId, XRP(0)), Txflags(tfSellNFToken));
7595 env.close();
7596 env(token::acceptSellOffer(alice, offerID));
7597 env.close();
7598 BEAST_EXPECT(ownerCount(env, issuer) == 0);
7599 BEAST_EXPECT(ownerCount(env, alice) == 1);
7600 checkURI(alice, "uri", __LINE__);
7601
7602 // Modify by owner fails.
7603 env(token::modify(alice, nftId), token::Uri("new_uri"), Ter(tecNO_PERMISSION));
7604 env.close();
7605 BEAST_EXPECT(ownerCount(env, issuer) == 0);
7606 BEAST_EXPECT(ownerCount(env, alice) == 1);
7607 checkURI(alice, "uri", __LINE__);
7608
7609 env(token::modify(issuer, nftId), token::Owner(alice), token::Uri("new_uri"));
7610 env.close();
7611 BEAST_EXPECT(ownerCount(env, issuer) == 0);
7612 BEAST_EXPECT(ownerCount(env, alice) == 1);
7613 checkURI(alice, "new_uri", __LINE__);
7614
7615 env(token::modify(issuer, nftId), token::Owner(alice));
7616 env.close();
7617 checkURI(alice, nullptr, __LINE__);
7618
7619 env(token::modify(issuer, nftId), token::Owner(alice), token::Uri("uri"));
7620 env.close();
7621 checkURI(alice, "uri", __LINE__);
7622
7623 // Modify by authorized minter
7624 env(token::setMinter(issuer, bob));
7625 env.close();
7626 env(token::modify(bob, nftId), token::Owner(alice), token::Uri("new_uri"));
7627 env.close();
7628 checkURI(alice, "new_uri", __LINE__);
7629
7630 env(token::modify(bob, nftId), token::Owner(alice));
7631 env.close();
7632 checkURI(alice, nullptr, __LINE__);
7633
7634 env(token::modify(bob, nftId), token::Owner(alice), token::Uri("uri"));
7635 env.close();
7636 checkURI(alice, "uri", __LINE__);
7637 }
7638 }
7639
7640 void
7642 {
7643 testcase("Invalid NFT offer create amount");
7644
7645 using namespace test::jtx;
7646
7647 // Before fixCleanup3_4_0, a fake-XRP offer amount (an IOU using the
7648 // "XRP" currency code) is not rejected in preflight. With the amendment
7649 // enabled, preflight rejects it with temBAD_CURRENCY.
7650 for (bool const withFix : {false, true})
7651 {
7652 Env env{*this, withFix ? features | fixCleanup3_4_0 : features - fixCleanup3_4_0};
7653
7654 Account const alice{"alice"};
7655 Account const gw{"gw"};
7656
7657 env.fund(XRP(1000), alice, gw);
7658 env.close();
7659
7660 UInt256 const nftID = token::getNextID(env, alice, 0, tfTransferable);
7661 env(token::mint(alice, 0u), Txflags(tfTransferable));
7662 env.close();
7663
7664 // Fake XRP (an IOU using the "XRP" currency code) sell offer
7665 // amount.
7666 auto const bad = IOU(gw, badCurrency());
7667 env(token::createOffer(alice, nftID, bad(1)),
7668 Txflags(tfSellNFToken),
7669 Ter(withFix ? TER{temBAD_CURRENCY} : TER{tesSUCCESS}));
7670 env.close();
7671 }
7672 }
7673
7674 void
7676 {
7677 testcase("Invalid NFT offer accept broker fee");
7678
7679 using namespace test::jtx;
7680
7681 // Before fixCleanup3_4_0, a fake-XRP broker fee (an IOU using the "XRP"
7682 // currency code) is not rejected in preflight and reaches later offer
7683 // validation instead. With the amendment enabled, preflight rejects it
7684 // with temBAD_CURRENCY.
7685 for (bool const withFix : {false, true})
7686 {
7687 Env env{*this, withFix ? features | fixCleanup3_4_0 : features - fixCleanup3_4_0};
7688
7689 Account const alice{"alice"};
7690 Account const buyer{"buyer"};
7691 Account const broker{"broker"};
7692 Account const gw{"gw"};
7693
7694 env.fund(XRP(1000), alice, buyer, broker, gw);
7695 env.close();
7696
7697 UInt256 const nftID = token::getNextID(env, alice, 0, tfTransferable);
7698 env(token::mint(alice, 0u), Txflags(tfTransferable));
7699 env.close();
7700
7701 UInt256 const sellOfferIndex =
7702 keylet::nftokenOffer(alice, SeqProxy::rawSequence(env.seq(alice))).key;
7703 env(token::createOffer(alice, nftID, XRP(10)), Txflags(tfSellNFToken));
7704 env.close();
7705
7706 UInt256 const buyOfferIndex =
7707 keylet::nftokenOffer(buyer, SeqProxy::rawSequence(env.seq(buyer))).key;
7708 env(token::createOffer(buyer, nftID, XRP(40)), token::Owner(alice));
7709 env.close();
7710
7711 // Fake XRP (an IOU using the "XRP" currency code) broker fee.
7712 auto const bad = IOU(gw, badCurrency());
7713 env(token::brokerOffers(broker, buyOfferIndex, sellOfferIndex),
7714 token::BrokerFee(bad(1)),
7716 env.close();
7717 }
7718 }
7719
7720 void
7722 {
7723 testcase("Create NFT offer by IOU issuer under global freeze");
7724
7725 using namespace test::jtx;
7726
7727 // Before fixCleanup3_4_0, an IOU issuer that has set a global freeze on
7728 // their own currency cannot create an NFToken offer denominated in that
7729 // currency; the offer is rejected with tecFROZEN. With the amendment
7730 // enabled, the issuer is not subject to their own global freeze when the
7731 // offer is denominated in their own IOU (e.g. to receive their own
7732 // transfer fees), so the offer succeeds.
7733 for (bool const withFix : {false, true})
7734 {
7735 Env env{*this, withFix ? features | fixCleanup3_4_0 : features - fixCleanup3_4_0};
7736
7737 Account const issuer{"issuer"};
7738 IOU const isISU(issuer["ISU"]);
7739
7740 env.fund(XRP(1000), issuer);
7741 env.close();
7742
7743 // issuer mints a transferable NFToken.
7744 UInt256 const nftID = token::getNextID(env, issuer, 0, tfTransferable);
7745 env(token::mint(issuer, 0u), Txflags(tfTransferable));
7746 env.close();
7747
7748 // issuer sets a global freeze on their own IOU.
7749 env(fset(issuer, asfGlobalFreeze));
7750 env.close();
7751
7752 // issuer creates a sell offer for the NFToken denominated in their
7753 // own (globally frozen) IOU.
7754 env(token::createOffer(issuer, nftID, isISU(100)),
7755 Txflags(tfSellNFToken),
7756 Ter(withFix ? TER{tesSUCCESS} : TER{tecFROZEN}));
7757 env.close();
7758 }
7759 }
7760
7761protected:
7763
7764 void
7766 {
7767 testEnabled(features);
7768 testMintReserve(features);
7769 testMintMaxTokens(features);
7770 testMintInvalid(features);
7771 testBurnInvalid(features);
7772 testCreateOfferInvalid(features);
7773 testCancelOfferInvalid(features);
7774 testAcceptOfferInvalid(features);
7775 testMintFlagBurnable(features);
7776 testMintFlagOnlyXRP(features);
7778 testMintFlagTransferable(features);
7779 testMintTransferFee(features);
7780 testMintTaxon(features);
7781 testMintURI(features);
7784 testCreateOfferExpiration(features);
7785 testCancelOffers(features);
7786 testCancelTooManyOffers(features);
7787 testBrokeredAccept(features);
7788 testNFTokenOfferOwner(features);
7789 testNFTokenWithTickets(features);
7790 testNFTokenDeleteAccount(features);
7791 testNftXxxOffers(features);
7795 testNFTokenNegOffer(features);
7796 testIOUWithTransferFee(features);
7797 testBrokeredSaleToSelf(features);
7798 testNFTokenRemint(features);
7799 testFeatMintWithOffer(features);
7800 testTxJsonMetaFields(features);
7803 testNFTIssuerIsIOUIssuer(features);
7804 testNFTokenModify(features);
7808 }
7809
7810public:
7811 void
7812 run() override
7813 {
7814 testWithFeats(allFeatures_ - featureNFTokenMintOffer - featureDynamicNFT - fixCleanup3_1_3);
7815 }
7816};
7817
7819{
7820 void
7821 run() override
7822 {
7823 testWithFeats(allFeatures_ - featureNFTokenMintOffer - featureDynamicNFT);
7824 }
7825};
7826
7828{
7829 void
7830 run() override
7831 {
7832 testWithFeats(allFeatures_ - featureNFTokenMintOffer - featureDynamicNFT);
7833 }
7834};
7835
7837{
7838 void
7839 run() override
7840 {
7841 testWithFeats(allFeatures_ - featureDynamicNFT);
7842 }
7843};
7844
7846{
7847 void
7848 run() override
7849 {
7850 testWithFeats(allFeatures_ - fixCleanup3_1_3);
7851 }
7852};
7853
7855{
7856 void
7857 run() override
7858 {
7860 }
7861};
7862
7863BEAST_DEFINE_TESTSUITE_PRIO(NFTokenBaseUtil, app, xrpl, 2);
7864BEAST_DEFINE_TESTSUITE_PRIO(NFTokenDisallowIncoming, app, xrpl, 2);
7865BEAST_DEFINE_TESTSUITE_PRIO(NFTokenWOMintOffer, app, xrpl, 2);
7866BEAST_DEFINE_TESTSUITE_PRIO(NFTokenWOModify, app, xrpl, 2);
7867BEAST_DEFINE_TESTSUITE_PRIO(NFTokenAllFeatures, app, xrpl, 2);
7868
7869} // namespace xrpl
T back(T... args)
T back_inserter(T... args)
A generic endpoint for log messages.
Definition Journal.h:44
A testsuite class.
Definition suite.h:52
void pass()
Record a successful test condition.
Definition suite.h:532
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
Definition suite.h:235
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:554
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Lightweight wrapper to tag static string.
Definition json_value.h:52
Represents a JSON value.
Definition json_value.h:121
Value removeMember(char const *key)
Remove and return the named member.
Value & append(Value const &value)
Append value to array at the end.
UInt size() const
Number of values in array or object.
UInt asUInt() const
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:526
void run() override
Runs the suite.
static std::uint32_t nftCount(test::jtx::Env &env, test::jtx::Account const &acct)
void testCreateOfferExpiration(FeatureBitset features)
void testCancelOfferInvalid(FeatureBitset features)
void testBrokeredSaleToSelf(FeatureBitset features)
static std::uint32_t mintedCount(test::jtx::Env const &env, test::jtx::Account const &issuer)
void testFeatMintWithOffer(FeatureBitset features)
void testCreateOfferDestination(FeatureBitset features)
static std::uint32_t ticketCount(test::jtx::Env const &env, test::jtx::Account const &acct)
void testAcceptOfferInvalid(FeatureBitset features)
void testWithFeats(FeatureBitset features)
void testMintFlagBurnable(FeatureBitset features)
void testIOUWithTransferFee(FeatureBitset features)
void testNFTokenNegOffer(FeatureBitset features)
void testFixNFTokenBuyerReserve(FeatureBitset features)
void run() override
Runs the suite.
void testNFTokenModify(FeatureBitset features)
void testMintReserve(FeatureBitset features)
void testCreateOfferIouIssuerGlobalFreeze(FeatureBitset features)
void testNFTokenOfferOwner(FeatureBitset features)
void testNftXxxOffersMarkerWrongSide(FeatureBitset features)
void testMintFlagOnlyXRP(FeatureBitset features)
void testMintTaxon(FeatureBitset features)
void testCancelOffers(FeatureBitset features)
void testMintInvalid(FeatureBitset features)
void testMintTransferFee(FeatureBitset features)
static std::uint32_t burnedCount(test::jtx::Env const &env, test::jtx::Account const &issuer)
void testNFTokenRemint(FeatureBitset features)
void testAcceptOfferInvalidBrokerFee(FeatureBitset features)
void testMintFlagTransferable(FeatureBitset features)
static std::uint32_t lastClose(test::jtx::Env &env)
FeatureBitset const allFeatures_
void testBrokeredAccept(FeatureBitset features)
void testBurnInvalid(FeatureBitset features)
void testMintFlagCreateTrustLine(FeatureBitset features)
void testUnaskedForAutoTrustline(FeatureBitset features)
void testEnabled(FeatureBitset features)
void testTxJsonMetaFields(FeatureBitset features)
void testNFTokenWithTickets(FeatureBitset features)
void testNftXxxOffersNonAdminLimit(FeatureBitset features)
void testCreateOfferInvalidAmount(FeatureBitset features)
void testCreateOfferInvalid(FeatureBitset features)
void testNFTokenDeleteAccount(FeatureBitset features)
void testCreateOfferDestinationDisallowIncoming(FeatureBitset features)
void testCancelTooManyOffers(FeatureBitset features)
void testNFTIssuerIsIOUIssuer(FeatureBitset features)
void testMintMaxTokens(FeatureBitset features)
void testMintURI(FeatureBitset features)
void testNftXxxOffersInvalidParams(FeatureBitset features)
void testNftXxxOffers(FeatureBitset features)
void run() override
Runs the suite.
void run() override
Runs the suite.
void run() override
Runs the suite.
void run() override
Runs the suite.
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:59
void rawReplace(SLE::Ref sle) override
Unconditionally replace a state item.
Definition OpenView.cpp:246
SLE::const_pointer read(Keylet const &k) const override
Return the state item associated with a key.
Definition OpenView.cpp:170
static constexpr int kMinOffset
Definition STAmount.h:61
json::Value getJson(JsonOptions=JsonOptions::Values::None) const override
static constexpr std::uint64_t kMinValue
Definition STAmount.h:65
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
A consumption charge.
Definition Charge.h:13
An endpoint that consumes resources.
Definition Consumer.h:20
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
std::string const & human() const
Returns the human readable public key.
A transaction testing environment.
Definition Env.h:161
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
SLE::const_pointer le(Account const &account) const
Return an account root.
Definition Env.cpp:311
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
Definition Env.cpp:302
json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition Env.h:1058
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:377
T clear(T... args)
T emplace_back(T... args)
T empty(T... args)
T end(T... args)
T find(T... args)
T insert(T... args)
T make_shared(T... args)
constexpr Zero kZero
Definition Zero.h:30
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
@ Null
'null' value
Definition json_value.h:26
Keylet nftokenOffer(AccountID const &owner, SeqProxy const &seq)
An offer from an account to buy or sell an NFT.
Definition Indexes.cpp:465
Keylet check(AccountID const &id, SeqProxy const &seq) noexcept
A Check.
Definition Indexes.cpp:372
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Taxon toTaxon(std::uint32_t i)
Definition nft.h:21
Taxon getTaxon(UInt256 const &id)
Definition nft.h:87
TaggedInteger< std::uint32_t, TaxonTag > Taxon
Definition nft.h:18
Charge const kFeeReferenceRpc
static constexpr LimitRange kNftOffers
Limits for the nft_buy_offers & nft_sell_offers commands.
static constexpr auto kApiMaximumSupportedVersion
Definition ApiVersion.h:33
static constexpr auto kApiVersionIfUnspecified
Definition ApiVersion.h:34
static constexpr auto kApiMinimumSupportedVersion
Definition ApiVersion.h:32
::rpc::Status doCommand(rpc::JsonContext &context, json::Value &result)
Execute an RPC command and store the results in a json::Value.
std::uint32_t ownerCount(Env const &env, Account const &account)
FeatureBitset testableAmendments()
Definition Env.h:92
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcObjectNotFound
Definition ErrorCodes.h:126
@ RpcInvalidParams
Definition ErrorCodes.h:67
std::uint32_t ownerCount(SLE::ConstRef sle, beast::Journal j, std::int32_t ownerCountAdj=0)
Return number of the objects which reserve is covered by the account(sle) (so called "ownercount").
constexpr FlagValue tfTrustLine
Definition TxFlags.h:406
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition RangeSet.h:37
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
constexpr std::size_t kMaxTokenUriLength
The maximum length of a URI inside an NFT.
Definition Protocol.h:246
@ tefNFTOKEN_IS_NOT_TRANSFERABLE
Definition TER.h:180
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
@ USER
Definition Role.h:27
BaseUInt< 256 > UInt256
Definition base_uint.h:581
@ JtClient
Definition Job.h:31
BEAST_DEFINE_TESTSUITE_PRIO(NFTokenBaseUtil, app, xrpl, 2)
constexpr std::size_t kMaxTokenOfferCancelCount
The maximum number of token offers that can be canceled at once.
Definition Protocol.h:80
Integral randInt(Engine &engine, Integral min, Integral max)
Return a uniformly distributed random integer.
constexpr std::uint16_t kMaxTransferFee
The maximum token transfer fee allowed.
Definition Protocol.h:97
@ temBAD_CURRENCY
Definition TER.h:78
@ temBAD_EXPIRATION
Definition TER.h:79
@ temBAD_FEE
Definition TER.h:80
@ temINVALID_FLAG
Definition TER.h:99
@ temDST_IS_SRC
Definition TER.h:96
@ temMALFORMED
Definition TER.h:75
@ temDISABLED
Definition TER.h:102
@ temBAD_AMOUNT
Definition TER.h:77
@ temBAD_NFTOKEN_TRANSFER_FEE
Definition TER.h:115
Byte randByte(Engine &engine)
Return a random byte.
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecCANT_ACCEPT_OWN_NFTOKEN_OFFER
Definition TER.h:335
@ tecNO_ENTRY
Definition TER.h:317
@ tecOBJECT_NOT_FOUND
Definition TER.h:337
@ tecNFTOKEN_BUY_SELL_MISMATCH
Definition TER.h:333
@ tecTOO_SOON
Definition TER.h:329
@ tecFROZEN
Definition TER.h:314
@ tecINSUFFICIENT_FUNDS
Definition TER.h:336
@ tecUNFUNDED_OFFER
Definition TER.h:295
@ tecEXPIRED
Definition TER.h:325
@ tecNO_LINE
Definition TER.h:312
@ tecNFTOKEN_OFFER_TYPE_MISMATCH
Definition TER.h:334
@ tecINSUFFICIENT_RESERVE
Definition TER.h:318
@ tecMAX_SEQUENCE_REACHED
Definition TER.h:331
@ tecNO_PERMISSION
Definition TER.h:316
@ tecNO_ISSUER
Definition TER.h:310
@ tecHAS_OBLIGATIONS
Definition TER.h:328
@ tecNO_DST
Definition TER.h:301
@ tecINSUFFICIENT_PAYMENT
Definition TER.h:338
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
@ tesSUCCESS
Definition TER.h:253
constexpr FlagValue tfFullyCanonicalSig
Definition TxFlags.h:43
T pop_back(T... args)
T push_back(T... args)
T reserve(T... args)
T size(T... args)
T sort(T... args)
T str(T... args)
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
UInt256 key
Definition Keylet.h:21
std::shared_ptr< JobQueue::Coro > coro
Definition Context.h:35
json::Value params
Definition Context.h:51
T to_string(T... args)
T transform(T... args)