rippled
Loading...
Searching...
No Matches
NFTokenOfferID.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2023 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/basics/base_uint.h>
21#include <xrpl/json/json_value.h>
22#include <xrpl/protocol/LedgerFormats.h>
23#include <xrpl/protocol/NFTokenOfferID.h>
24#include <xrpl/protocol/SField.h>
25#include <xrpl/protocol/STObject.h>
26#include <xrpl/protocol/STTx.h>
27#include <xrpl/protocol/TER.h>
28#include <xrpl/protocol/TxFormats.h>
29#include <xrpl/protocol/TxMeta.h>
30#include <xrpl/protocol/jss.h>
31
32#include <memory>
33#include <optional>
34
35namespace ripple {
36
37bool
39 std::shared_ptr<STTx const> const& serializedTx,
40 TxMeta const& transactionMeta)
41{
42 if (!serializedTx)
43 return false;
44
45 TxType const tt = serializedTx->getTxnType();
46 if (!(tt == ttNFTOKEN_MINT && serializedTx->isFieldPresent(sfAmount)) &&
47 tt != ttNFTOKEN_CREATE_OFFER)
48 return false;
49
50 // if the transaction failed nothing could have been delivered.
51 if (transactionMeta.getResultTER() != tesSUCCESS)
52 return false;
53
54 return true;
55}
56
58getOfferIDFromCreatedOffer(TxMeta const& transactionMeta)
59{
60 for (STObject const& node : transactionMeta.getNodes())
61 {
62 if (node.getFieldU16(sfLedgerEntryType) != ltNFTOKEN_OFFER ||
63 node.getFName() != sfCreatedNode)
64 continue;
65
66 return node.getFieldH256(sfLedgerIndex);
67 }
68 return std::nullopt;
69}
70
71void
73 Json::Value& response,
74 std::shared_ptr<STTx const> const& transaction,
75 TxMeta const& transactionMeta)
76{
77 if (!canHaveNFTokenOfferID(transaction, transactionMeta))
78 return;
79
80 std::optional<uint256> result = getOfferIDFromCreatedOffer(transactionMeta);
81
82 if (result.has_value())
83 response[jss::offer_id] = to_string(result.value());
84}
85
86} // namespace ripple
Represents a JSON value.
Definition json_value.h:149
STArray & getNodes()
Definition TxMeta.h:89
TER getResultTER() const
Definition TxMeta.h:57
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
bool canHaveNFTokenOfferID(std::shared_ptr< STTx const > const &serializedTx, TxMeta const &transactionMeta)
Add an offer_id field to the meta output parameter.
TxType
Transaction type identifiers.
Definition TxFormats.h:57
std::optional< uint256 > getOfferIDFromCreatedOffer(TxMeta const &transactionMeta)
@ tesSUCCESS
Definition TER.h:245
void insertNFTokenOfferID(Json::Value &response, std::shared_ptr< STTx const > const &transaction, TxMeta const &transactionMeta)
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630
T has_value(T... args)
T value(T... args)