rippled
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 <ripple/rpc/NFTokenOfferID.h>
21 
22 #include <ripple/app/ledger/LedgerMaster.h>
23 #include <ripple/app/ledger/OpenLedger.h>
24 #include <ripple/app/misc/Transaction.h>
25 #include <ripple/ledger/View.h>
26 #include <ripple/net/RPCErr.h>
27 #include <ripple/protocol/AccountID.h>
28 #include <ripple/protocol/Feature.h>
29 #include <ripple/rpc/Context.h>
30 #include <ripple/rpc/impl/RPCHelpers.h>
31 #include <boost/algorithm/string/case_conv.hpp>
32 
33 namespace ripple {
34 namespace RPC {
35 
36 bool
38  std::shared_ptr<STTx const> const& serializedTx,
39  TxMeta const& transactionMeta)
40 {
41  if (!serializedTx)
42  return false;
43 
44  TxType const tt = serializedTx->getTxnType();
45  if (tt != ttNFTOKEN_CREATE_OFFER)
46  return false;
47 
48  // if the transaction failed nothing could have been delivered.
49  if (transactionMeta.getResultTER() != tesSUCCESS)
50  return false;
51 
52  return true;
53 }
54 
56 getOfferIDFromCreatedOffer(TxMeta const& transactionMeta)
57 {
58  for (STObject const& node : transactionMeta.getNodes())
59  {
60  if (node.getFieldU16(sfLedgerEntryType) != ltNFTOKEN_OFFER ||
61  node.getFName() != sfCreatedNode)
62  continue;
63 
64  return node.getFieldH256(sfLedgerIndex);
65  }
66  return std::nullopt;
67 }
68 
69 void
71  Json::Value& response,
72  std::shared_ptr<STTx const> const& transaction,
73  TxMeta const& transactionMeta)
74 {
75  if (!canHaveNFTokenOfferID(transaction, transactionMeta))
76  return;
77 
78  std::optional<uint256> result = getOfferIDFromCreatedOffer(transactionMeta);
79 
80  if (result.has_value())
81  response[jss::offer_id] = to_string(result.value());
82 }
83 
84 } // namespace RPC
85 } // namespace ripple
ripple::ttNFTOKEN_CREATE_OFFER
@ ttNFTOKEN_CREATE_OFFER
This transaction creates a new offer to buy or sell an NFT.
Definition: TxFormats.h:134
std::optional::has_value
T has_value(T... args)
std::shared_ptr
STL class.
ripple::RPC::canHaveNFTokenOfferID
bool canHaveNFTokenOfferID(std::shared_ptr< STTx const > const &serializedTx, TxMeta const &transactionMeta)
Add an offer_id field to the meta output parameter.
Definition: NFTokenOfferID.cpp:37
ripple::sfLedgerIndex
const SF_UINT256 sfLedgerIndex
ripple::TxType
TxType
Transaction type identifiers.
Definition: TxFormats.h:56
ripple::TxMeta
Definition: TxMeta.h:32
ripple::TxMeta::getResultTER
TER getResultTER() const
Definition: TxMeta.h:68
ripple::ltNFTOKEN_OFFER
@ ltNFTOKEN_OFFER
A ledger object which identifies an offer to buy or sell an NFT.
Definition: LedgerFormats.h:162
std::optional::value
T value(T... args)
ripple::STObject
Definition: STObject.h:51
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::TxMeta::getNodes
STArray & getNodes()
Definition: TxMeta.h:100
ripple::sfLedgerEntryType
const SF_UINT16 sfLedgerEntryType
ripple::sfCreatedNode
const SField sfCreatedNode
ripple::RPC::getOfferIDFromCreatedOffer
std::optional< uint256 > getOfferIDFromCreatedOffer(TxMeta const &transactionMeta)
Definition: NFTokenOfferID.cpp:56
std::optional
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::RPC::insertNFTokenOfferID
void insertNFTokenOfferID(Json::Value &response, std::shared_ptr< STTx const > const &transaction, TxMeta const &transactionMeta)
Definition: NFTokenOfferID.cpp:70
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:222
Json::Value
Represents a JSON value.
Definition: json_value.h:145