Implement nextgen nft_sell_offers handler (#571)

Fixes #570
This commit is contained in:
Alex Kremer
2023-03-30 12:46:54 +01:00
committed by GitHub
parent d86104577b
commit 232acaeff2
10 changed files with 1059 additions and 248 deletions

View File

@@ -24,38 +24,6 @@
#include <ripple/protocol/Indexes.h>
using namespace ripple;
using namespace ::RPC;
namespace ripple {
// TODO: move to some common serialization impl place
inline void
tag_invoke(
boost::json::value_from_tag,
boost::json::value& jv,
SLE const& offer)
{
auto amount = ::RPC::toBoostJson(
offer.getFieldAmount(sfAmount).getJson(JsonOptions::none));
boost::json::object obj = {
{JS(nft_offer_index), to_string(offer.key())},
{JS(flags), offer[sfFlags]},
{JS(owner), toBase58(offer.getAccountID(sfOwner))},
{JS(amount), std::move(amount)},
};
if (offer.isFieldPresent(sfDestination))
obj.insert_or_assign(
JS(destination), toBase58(offer.getAccountID(sfDestination)));
if (offer.isFieldPresent(sfExpiration))
obj.insert_or_assign(JS(expiration), offer.getFieldU32(sfExpiration));
jv = std::move(obj);
}
} // namespace ripple
namespace RPCng {
@@ -64,163 +32,8 @@ NFTBuyOffersHandler::process(
NFTBuyOffersHandler::Input input,
boost::asio::yield_context& yield) const
{
auto const tokenID = ripple::uint256{input.nftID.c_str()};
auto const range = sharedPtrBackend_->fetchLedgerRange();
auto const lgrInfoOrStatus = getLedgerInfoFromHashOrSeq(
*sharedPtrBackend_,
yield,
input.ledgerHash,
input.ledgerIndex,
range->maxSequence);
if (auto const status = std::get_if<Status>(&lgrInfoOrStatus))
return Error{*status};
auto const lgrInfo = std::get<LedgerInfo>(lgrInfoOrStatus);
auto const tokenID = uint256{input.nftID.c_str()};
auto const directory = keylet::nft_buys(tokenID);
// TODO: just check for existence without pulling
if (not sharedPtrBackend_->fetchLedgerObject(
directory.key, lgrInfo.seq, yield))
return Error{Status{RippledError::rpcOBJECT_NOT_FOUND, "notFound"}};
auto output = NFTBuyOffersHandler::Output{input.nftID};
auto offers = std::vector<ripple::SLE>{};
auto reserve = input.limit;
auto cursor = uint256{};
auto startHint = uint64_t{0ul};
if (input.marker)
{
cursor = uint256(input.marker->c_str());
// We have a start point. Use limit - 1 from the result and use the very
// last one for the resume.
auto const sle =
[this, &cursor, &lgrInfo, &yield]() -> std::shared_ptr<SLE const> {
auto const key = keylet::nftoffer(cursor).key;
if (auto const blob = sharedPtrBackend_->fetchLedgerObject(
key, lgrInfo.seq, yield);
blob)
{
return std::make_shared<SLE const>(
SerialIter{blob->data(), blob->size()}, key);
}
return nullptr;
}();
if (!sle ||
sle->getFieldU16(ripple::sfLedgerEntryType) !=
ripple::ltNFTOKEN_OFFER ||
tokenID != sle->getFieldH256(ripple::sfNFTokenID))
{
return Error{Status{RippledError::rpcINVALID_PARAMS}};
}
startHint = sle->getFieldU64(ripple::sfNFTokenOfferNode);
output.offers.push_back(*sle);
offers.reserve(reserve);
}
else
{
// We have no start point, limit should be one higher than requested.
offers.reserve(++reserve);
}
auto result = traverseOwnedNodes(
*sharedPtrBackend_,
directory,
cursor,
startHint,
lgrInfo.seq,
reserve,
{},
yield,
[&offers](ripple::SLE&& offer) {
if (offer.getType() == ripple::ltNFTOKEN_OFFER)
{
offers.push_back(std::move(offer));
return true;
}
return false;
});
if (auto status = std::get_if<Status>(&result))
return Error{*status};
if (offers.size() == reserve)
{
output.limit = input.limit;
output.marker = to_string(offers.back().key());
offers.pop_back();
}
std::move(
std::begin(offers),
std::end(offers),
std::back_inserter(output.offers));
return std::move(output);
}
void
tag_invoke(
boost::json::value_from_tag,
boost::json::value& jv,
NFTBuyOffersHandler::Output const& output)
{
auto object = boost::json::object{
{JS(nft_id), output.nftID},
{JS(validated), output.validated},
{JS(offers), output.offers},
};
if (output.marker)
object[JS(marker)] = *(output.marker);
if (output.limit)
object[JS(limit)] = *(output.limit);
jv = std::move(object);
}
NFTBuyOffersHandler::Input
tag_invoke(
boost::json::value_to_tag<NFTBuyOffersHandler::Input>,
boost::json::value const& jv)
{
auto const& jsonObject = jv.as_object();
NFTBuyOffersHandler::Input input;
input.nftID = jsonObject.at(JS(nft_id)).as_string().c_str();
if (jsonObject.contains(JS(ledger_hash)))
{
input.ledgerHash = jsonObject.at(JS(ledger_hash)).as_string().c_str();
}
if (jsonObject.contains(JS(ledger_index)))
{
if (!jsonObject.at(JS(ledger_index)).is_string())
{
input.ledgerIndex = jsonObject.at(JS(ledger_index)).as_int64();
}
else if (jsonObject.at(JS(ledger_index)).as_string() != "validated")
{
input.ledgerIndex =
std::stoi(jsonObject.at(JS(ledger_index)).as_string().c_str());
}
}
if (jsonObject.contains(JS(marker)))
{
input.marker = jsonObject.at(JS(marker)).as_string().c_str();
}
if (jsonObject.contains(JS(limit)))
{
input.limit = jsonObject.at(JS(limit)).as_int64();
}
return input;
return iterateOfferDirectory(input, tokenID, directory, yield);
}
} // namespace RPCng

View File

@@ -20,75 +20,21 @@
#pragma once
#include <backend/BackendInterface.h>
#include <rpc/RPCHelpers.h>
#include <rpc/common/Types.h>
#include <rpc/common/Validators.h>
#include <rpc/ngHandlers/NFTOffersCommon.h>
#include <boost/asio/spawn.hpp>
namespace RPCng {
class NFTBuyOffersHandler
class NFTBuyOffersHandler : public NFTOffersHandlerBase
{
std::shared_ptr<BackendInterface> sharedPtrBackend_;
public:
struct Output
{
std::string nftID;
std::vector<ripple::SLE> offers;
// validated should be sent via framework
bool validated = true;
std::optional<uint32_t> limit;
std::optional<std::string> marker;
};
struct Input
{
std::string nftID;
std::optional<std::string> ledgerHash;
std::optional<uint32_t> ledgerIndex;
uint32_t limit = 250;
std::optional<std::string> marker;
};
using Result = RPCng::HandlerReturnType<Output>;
NFTBuyOffersHandler(
std::shared_ptr<BackendInterface> const& sharedPtrBackend)
: sharedPtrBackend_(sharedPtrBackend)
: NFTOffersHandlerBase(sharedPtrBackend)
{
}
RpcSpecConstRef
spec() const
{
static auto const rpcSpec = RpcSpec{
{JS(nft_id),
validation::Required{},
validation::Uint256HexStringValidator},
{JS(ledger_hash), validation::Uint256HexStringValidator},
{JS(ledger_index), validation::LedgerIndexValidator},
{JS(limit),
validation::Type<uint32_t>{},
validation::Between{50, 500}},
{JS(marker), validation::Uint256HexStringValidator},
};
return rpcSpec;
}
Result
process(Input input, boost::asio::yield_context& yield) const;
};
void
tag_invoke(
boost::json::value_from_tag,
boost::json::value& jv,
NFTBuyOffersHandler::Output const& output);
NFTBuyOffersHandler::Input
tag_invoke(
boost::json::value_to_tag<NFTBuyOffersHandler::Input>,
boost::json::value const& jv);
} // namespace RPCng

View File

@@ -0,0 +1,228 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2023, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <rpc/RPCHelpers.h>
#include <rpc/ngHandlers/NFTOffersCommon.h>
#include <ripple/app/tx/impl/details/NFTokenUtils.h>
#include <ripple/protocol/Indexes.h>
using namespace ripple;
using namespace ::RPC;
namespace ripple {
// TODO: move to some common serialization impl place
inline void
tag_invoke(
boost::json::value_from_tag,
boost::json::value& jv,
SLE const& offer)
{
auto amount = ::RPC::toBoostJson(
offer.getFieldAmount(sfAmount).getJson(JsonOptions::none));
boost::json::object obj = {
{JS(nft_offer_index), to_string(offer.key())},
{JS(flags), offer[sfFlags]},
{JS(owner), toBase58(offer.getAccountID(sfOwner))},
{JS(amount), std::move(amount)},
};
if (offer.isFieldPresent(sfDestination))
obj.insert_or_assign(
JS(destination), toBase58(offer.getAccountID(sfDestination)));
if (offer.isFieldPresent(sfExpiration))
obj.insert_or_assign(JS(expiration), offer.getFieldU32(sfExpiration));
jv = std::move(obj);
}
} // namespace ripple
namespace RPCng {
NFTOffersHandlerBase::Result
NFTOffersHandlerBase::iterateOfferDirectory(
Input input,
ripple::uint256 const& tokenID,
ripple::Keylet const& directory,
boost::asio::yield_context& yield) const
{
auto const range = sharedPtrBackend_->fetchLedgerRange();
auto const lgrInfoOrStatus = getLedgerInfoFromHashOrSeq(
*sharedPtrBackend_,
yield,
input.ledgerHash,
input.ledgerIndex,
range->maxSequence);
if (auto const status = std::get_if<Status>(&lgrInfoOrStatus))
return Error{*status};
auto const lgrInfo = std::get<LedgerInfo>(lgrInfoOrStatus);
// TODO: just check for existence without pulling
if (not sharedPtrBackend_->fetchLedgerObject(
directory.key, lgrInfo.seq, yield))
return Error{Status{RippledError::rpcOBJECT_NOT_FOUND, "notFound"}};
auto output = Output{input.nftID};
auto offers = std::vector<ripple::SLE>{};
auto reserve = input.limit;
auto cursor = uint256{};
auto startHint = uint64_t{0ul};
if (input.marker)
{
cursor = uint256(input.marker->c_str());
// We have a start point. Use limit - 1 from the result and use the
// very last one for the resume.
auto const sle =
[this, &cursor, &lgrInfo, &yield]() -> std::shared_ptr<SLE const> {
auto const key = keylet::nftoffer(cursor).key;
if (auto const blob = sharedPtrBackend_->fetchLedgerObject(
key, lgrInfo.seq, yield);
blob)
{
return std::make_shared<SLE const>(
SerialIter{blob->data(), blob->size()}, key);
}
return nullptr;
}();
if (!sle ||
sle->getFieldU16(ripple::sfLedgerEntryType) !=
ripple::ltNFTOKEN_OFFER ||
tokenID != sle->getFieldH256(ripple::sfNFTokenID))
{
return Error{Status{RippledError::rpcINVALID_PARAMS}};
}
startHint = sle->getFieldU64(ripple::sfNFTokenOfferNode);
output.offers.push_back(*sle);
offers.reserve(reserve);
}
else
{
// We have no start point, limit should be one higher than
// requested.
offers.reserve(++reserve);
}
auto result = traverseOwnedNodes(
*sharedPtrBackend_,
directory,
cursor,
startHint,
lgrInfo.seq,
reserve,
{},
yield,
[&offers](ripple::SLE&& offer) {
if (offer.getType() == ripple::ltNFTOKEN_OFFER)
{
offers.push_back(std::move(offer));
return true;
}
return false;
});
if (auto status = std::get_if<Status>(&result))
return Error{*status};
if (offers.size() == reserve)
{
output.limit = input.limit;
output.marker = to_string(offers.back().key());
offers.pop_back();
}
std::move(
std::begin(offers),
std::end(offers),
std::back_inserter(output.offers));
return std::move(output);
}
void
tag_invoke(
boost::json::value_from_tag,
boost::json::value& jv,
NFTOffersHandlerBase::Output const& output)
{
auto object = boost::json::object{
{JS(nft_id), output.nftID},
{JS(validated), output.validated},
{JS(offers), output.offers},
};
if (output.marker)
object[JS(marker)] = *(output.marker);
if (output.limit)
object[JS(limit)] = *(output.limit);
jv = std::move(object);
}
NFTOffersHandlerBase::Input
tag_invoke(
boost::json::value_to_tag<NFTOffersHandlerBase::Input>,
boost::json::value const& jv)
{
auto const& jsonObject = jv.as_object();
NFTOffersHandlerBase::Input input;
input.nftID = jsonObject.at(JS(nft_id)).as_string().c_str();
if (jsonObject.contains(JS(ledger_hash)))
{
input.ledgerHash = jsonObject.at(JS(ledger_hash)).as_string().c_str();
}
if (jsonObject.contains(JS(ledger_index)))
{
if (!jsonObject.at(JS(ledger_index)).is_string())
{
input.ledgerIndex = jsonObject.at(JS(ledger_index)).as_int64();
}
else if (jsonObject.at(JS(ledger_index)).as_string() != "validated")
{
input.ledgerIndex =
std::stoi(jsonObject.at(JS(ledger_index)).as_string().c_str());
}
}
if (jsonObject.contains(JS(marker)))
{
input.marker = jsonObject.at(JS(marker)).as_string().c_str();
}
if (jsonObject.contains(JS(limit)))
{
input.limit = jsonObject.at(JS(limit)).as_int64();
}
return input;
}
} // namespace RPCng

View File

@@ -0,0 +1,100 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2023, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#pragma once
#include <backend/BackendInterface.h>
#include <rpc/RPCHelpers.h>
#include <rpc/common/Types.h>
#include <rpc/common/Validators.h>
#include <boost/asio/spawn.hpp>
namespace RPCng {
class NFTOffersHandlerBase
{
std::shared_ptr<BackendInterface> sharedPtrBackend_;
public:
struct Output
{
std::string nftID;
std::vector<ripple::SLE> offers;
// validated should be sent via framework
bool validated = true;
std::optional<uint32_t> limit;
std::optional<std::string> marker;
};
struct Input
{
std::string nftID;
std::optional<std::string> ledgerHash;
std::optional<uint32_t> ledgerIndex;
uint32_t limit = 250;
std::optional<std::string> marker;
};
using Result = RPCng::HandlerReturnType<Output>;
NFTOffersHandlerBase(
std::shared_ptr<BackendInterface> const& sharedPtrBackend)
: sharedPtrBackend_(sharedPtrBackend)
{
}
RpcSpecConstRef
spec() const
{
static auto const rpcSpec = RpcSpec{
{JS(nft_id),
validation::Required{},
validation::Uint256HexStringValidator},
{JS(ledger_hash), validation::Uint256HexStringValidator},
{JS(ledger_index), validation::LedgerIndexValidator},
{JS(limit),
validation::Type<uint32_t>{},
validation::Between{50, 500}},
{JS(marker), validation::Uint256HexStringValidator},
};
return rpcSpec;
}
protected:
Result
iterateOfferDirectory(
Input input,
ripple::uint256 const& tokenID,
ripple::Keylet const& directory,
boost::asio::yield_context& yield) const;
private:
friend void
tag_invoke(
boost::json::value_from_tag,
boost::json::value& jv,
Output const& output);
friend Input
tag_invoke(boost::json::value_to_tag<Input>, boost::json::value const& jv);
};
} // namespace RPCng

View File

@@ -0,0 +1,40 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2023, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <rpc/RPCHelpers.h>
#include <rpc/ngHandlers/NFTSellOffers.h>
#include <ripple/app/tx/impl/details/NFTokenUtils.h>
#include <ripple/protocol/Indexes.h>
using namespace ripple;
namespace RPCng {
NFTSellOffersHandler::Result
NFTSellOffersHandler::process(
NFTSellOffersHandler::Input input,
boost::asio::yield_context& yield) const
{
auto const tokenID = uint256{input.nftID.c_str()};
auto const directory = keylet::nft_sells(tokenID);
return iterateOfferDirectory(input, tokenID, directory, yield);
}
} // namespace RPCng

View File

@@ -0,0 +1,40 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2023, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#pragma once
#include <backend/BackendInterface.h>
#include <rpc/ngHandlers/NFTOffersCommon.h>
#include <boost/asio/spawn.hpp>
namespace RPCng {
class NFTSellOffersHandler : public NFTOffersHandlerBase
{
public:
NFTSellOffersHandler(
std::shared_ptr<BackendInterface> const& sharedPtrBackend)
: NFTOffersHandlerBase(sharedPtrBackend)
{
}
Result
process(Input input, boost::asio::yield_context& yield) const;
};
} // namespace RPCng