From d1f64ca9196662608542745b9e44258c6f02306e Mon Sep 17 00:00:00 2001 From: sierra19XX <15652887+sierra19XX@users.noreply.github.com> Date: Wed, 8 Jul 2020 14:39:17 +0000 Subject: [PATCH] ppy marketplace 8 - Add pagination for list APIs --- libraries/app/database_api.cpp | 154 +++++++++++++++--- .../app/include/graphene/app/database_api.hpp | 19 ++- .../include/graphene/chain/offer_object.hpp | 2 +- .../wallet/include/graphene/wallet/wallet.hpp | 18 +- libraries/wallet/wallet.cpp | 68 ++++++-- 5 files changed, 214 insertions(+), 47 deletions(-) diff --git a/libraries/app/database_api.cpp b/libraries/app/database_api.cpp index bc84c8ef..171c9d64 100644 --- a/libraries/app/database_api.cpp +++ b/libraries/app/database_api.cpp @@ -197,11 +197,15 @@ class database_api_impl : public std::enable_shared_from_this nft_object nft_token_of_owner_by_index(const nft_metadata_id_type nft_metadata_id, const account_id_type owner, const uint64_t token_idx) const; // Marketplace - vector list_offers(uint32_t limit) const; - vector list_sell_offers(uint32_t limit) const; - vector list_buy_offers(uint32_t limit) const; - vector get_offers_by_issuer(const account_id_type issuer_account_id, uint32_t limit) const; - vector get_offers_by_item(nft_id_type item, uint32_t limit)const; + vector list_offers(const offer_id_type lower_id, uint32_t limit) const; + vector list_sell_offers(const offer_id_type lower_id, uint32_t limit) const; + vector list_buy_offers(const offer_id_type lower_id, uint32_t limit) const; + vector list_offer_history(const offer_history_id_type lower_id, uint32_t limit) const; + vector get_offers_by_issuer(const offer_id_type lower_id, const account_id_type issuer_account_id, uint32_t limit) const; + vector get_offers_by_item(const offer_id_type lower_id, const nft_id_type item, uint32_t limit) const; + vector get_offer_history_by_issuer(const offer_history_id_type lower_id, const account_id_type issuer_account_id, uint32_t limit) const; + vector get_offer_history_by_item(const offer_history_id_type lower_id, const nft_id_type item, uint32_t limit) const; + vector get_offer_history_by_bidder(const offer_history_id_type lower_id, const account_id_type bidder_account_id, uint32_t limit) const; //private: const account_object* get_account_from_string( const std::string& name_or_id, @@ -2489,19 +2493,19 @@ nft_object database_api_impl::nft_token_of_owner_by_index(const nft_metadata_id_ } // Marketplace -vector database_api::list_offers(uint32_t limit) const +vector database_api::list_offers(const offer_id_type lower_id, uint32_t limit) const { - return my->list_offers(limit); + return my->list_offers(lower_id, limit); } -vector database_api_impl::list_offers(uint32_t limit) const +vector database_api_impl::list_offers(const offer_id_type lower_id, uint32_t limit) const { FC_ASSERT( limit <= 100 ); const auto& offers_idx = _db.get_index_type().indices().get(); vector result; result.reserve(limit); - auto itr = offers_idx.begin(); + auto itr = offers_idx.lower_bound(lower_id); while(limit-- && itr != offers_idx.end()) result.emplace_back(*itr++); @@ -2509,19 +2513,19 @@ vector database_api_impl::list_offers(uint32_t limit) const return result; } -vector database_api::list_sell_offers(uint32_t limit) const +vector database_api::list_sell_offers(const offer_id_type lower_id, uint32_t limit) const { - return my->list_sell_offers(limit); + return my->list_sell_offers(lower_id, limit); } -vector database_api_impl::list_sell_offers(uint32_t limit) const +vector database_api_impl::list_sell_offers(const offer_id_type lower_id, uint32_t limit) const { FC_ASSERT( limit <= 100 ); const auto& offers_idx = _db.get_index_type().indices().get(); vector result; result.reserve(limit); - auto itr = offers_idx.begin(); + auto itr = offers_idx.lower_bound(lower_id); while(limit && itr != offers_idx.end()) { @@ -2535,19 +2539,19 @@ vector database_api_impl::list_sell_offers(uint32_t limit) const return result; } -vector database_api::list_buy_offers(uint32_t limit) const +vector database_api::list_buy_offers(const offer_id_type lower_id, uint32_t limit) const { - return my->list_buy_offers(limit); + return my->list_buy_offers(lower_id, limit); } -vector database_api_impl::list_buy_offers(uint32_t limit) const +vector database_api_impl::list_buy_offers(const offer_id_type lower_id, uint32_t limit) const { FC_ASSERT( limit <= 100 ); const auto& offers_idx = _db.get_index_type().indices().get(); vector result; result.reserve(limit); - auto itr = offers_idx.begin(); + auto itr = offers_idx.lower_bound(lower_id); while(limit && itr != offers_idx.end()) { @@ -2561,18 +2565,39 @@ vector database_api_impl::list_buy_offers(uint32_t limit) const return result; } -vector database_api::get_offers_by_issuer(const account_id_type issuer_account_id, uint32_t limit) const + +vector database_api::list_offer_history(const offer_history_id_type lower_id, uint32_t limit) const { - return my->get_offers_by_issuer(issuer_account_id, limit); + return my->list_offer_history(lower_id, limit); } -vector database_api_impl::get_offers_by_issuer(const account_id_type issuer_account_id, uint32_t limit) const +vector database_api_impl::list_offer_history(const offer_history_id_type lower_id, uint32_t limit) const +{ + FC_ASSERT( limit <= 100 ); + const auto& oh_idx = _db.get_index_type().indices().get(); + vector result; + result.reserve(limit); + + auto itr = oh_idx.lower_bound(lower_id); + + while(limit-- && itr != oh_idx.end()) + result.emplace_back(*itr++); + + return result; +} + +vector database_api::get_offers_by_issuer(const offer_id_type lower_id, const account_id_type issuer_account_id, uint32_t limit) const +{ + return my->get_offers_by_issuer(lower_id, issuer_account_id, limit); +} + +vector database_api_impl::get_offers_by_issuer(const offer_id_type lower_id, const account_id_type issuer_account_id, uint32_t limit) const { FC_ASSERT( limit <= 100 ); const auto& offers_idx = _db.get_index_type().indices().get(); vector result; result.reserve(limit); - auto itr = offers_idx.begin(); + auto itr = offers_idx.lower_bound(lower_id); while(limit && itr != offers_idx.end()) { if(itr->issuer == issuer_account_id) @@ -2585,19 +2610,19 @@ vector database_api_impl::get_offers_by_issuer(const account_id_ty return result; } -vector database_api::get_offers_by_item(nft_id_type item, uint32_t limit) const +vector database_api::get_offers_by_item(const offer_id_type lower_id, const nft_id_type item, uint32_t limit) const { - return my->get_offers_by_item(item, limit); + return my->get_offers_by_item(lower_id, item, limit); } -vector database_api_impl::get_offers_by_item(nft_id_type item, uint32_t limit) const +vector database_api_impl::get_offers_by_item(const offer_id_type lower_id, const nft_id_type item, uint32_t limit) const { FC_ASSERT( limit <= 100 ); const auto& offers_idx = _db.get_index_type().indices().get(); vector result; result.reserve(limit); - auto itr = offers_idx.begin(); + auto itr = offers_idx.lower_bound(lower_id); while(limit && itr != offers_idx.end()) { if(itr->item_ids.find(item) != itr->item_ids.end()) @@ -2610,6 +2635,85 @@ vector database_api_impl::get_offers_by_item(nft_id_type item, uin return result; } +vector database_api::get_offer_history_by_issuer(const offer_history_id_type lower_id, const account_id_type issuer_account_id, uint32_t limit) const +{ + return my->get_offer_history_by_issuer(lower_id, issuer_account_id, limit); +} + +vector database_api::get_offer_history_by_item(const offer_history_id_type lower_id, const nft_id_type item, uint32_t limit) const +{ + return my->get_offer_history_by_item(lower_id, item, limit); +} + +vector database_api::get_offer_history_by_bidder(const offer_history_id_type lower_id, const account_id_type bidder_account_id, uint32_t limit) const +{ + return my->get_offer_history_by_bidder(lower_id, bidder_account_id, limit); +} + +vector database_api_impl::get_offer_history_by_issuer(const offer_history_id_type lower_id, const account_id_type issuer_account_id, uint32_t limit) const +{ + FC_ASSERT( limit <= 100 ); + const auto& oh_idx = _db.get_index_type().indices().get(); + vector result; + result.reserve(limit); + + auto itr = oh_idx.lower_bound(lower_id); + + while(limit && itr != oh_idx.end()) + { + if(itr->issuer == issuer_account_id) + { + result.emplace_back(*itr); + limit--; + } + itr++; + } + return result; +} + +vector database_api_impl::get_offer_history_by_item(const offer_history_id_type lower_id, const nft_id_type item, uint32_t limit) const +{ + FC_ASSERT( limit <= 100 ); + const auto& oh_idx = _db.get_index_type().indices().get(); + vector result; + result.reserve(limit); + + auto itr = oh_idx.lower_bound(lower_id); + + while(limit && itr != oh_idx.end()) + { + if(itr->item_ids.find(item) != itr->item_ids.end()) + { + result.emplace_back(*itr); + limit--; + } + itr++; + } + + return result; +} + +vector database_api_impl::get_offer_history_by_bidder(const offer_history_id_type lower_id, const account_id_type bidder_account_id, uint32_t limit) const +{ + FC_ASSERT( limit <= 100 ); + const auto& oh_idx = _db.get_index_type().indices().get(); + vector result; + result.reserve(limit); + + auto itr = oh_idx.lower_bound(lower_id); + + while(limit && itr != oh_idx.end()) + { + if(itr->bidder && *itr->bidder == bidder_account_id) + { + result.emplace_back(*itr); + limit--; + } + itr++; + } + + return result; +} ////////////////////////////////////////////////////////////////////// // // // Private methods // diff --git a/libraries/app/include/graphene/app/database_api.hpp b/libraries/app/include/graphene/app/database_api.hpp index 19a0ed5e..f3aed62b 100644 --- a/libraries/app/include/graphene/app/database_api.hpp +++ b/libraries/app/include/graphene/app/database_api.hpp @@ -792,12 +792,15 @@ class database_api ////////////////// // MARKET PLACE // ////////////////// - vector list_offers(uint32_t limit) const; - vector list_sell_offers(uint32_t limit) const; - vector list_buy_offers(uint32_t limit) const; - vector get_offers_by_issuer(const account_id_type issuer_account_id, uint32_t limit) const; - vector get_offers_by_item(nft_id_type item, uint32_t limit) const; - + vector list_offers(const offer_id_type lower_id, uint32_t limit) const; + vector list_sell_offers(const offer_id_type lower_id, uint32_t limit) const; + vector list_buy_offers(const offer_id_type lower_id, uint32_t limit) const; + vector list_offer_history(const offer_history_id_type lower_id, uint32_t limit) const; + vector get_offers_by_issuer(const offer_id_type lower_id, const account_id_type issuer_account_id, uint32_t limit) const; + vector get_offers_by_item(const offer_id_type lower_id, const nft_id_type item, uint32_t limit) const; + vector get_offer_history_by_issuer(const offer_history_id_type lower_id, const account_id_type issuer_account_id, uint32_t limit) const; + vector get_offer_history_by_item(const offer_history_id_type lower_id, const nft_id_type item, uint32_t limit) const; + vector get_offer_history_by_bidder(const offer_history_id_type lower_id, const account_id_type bidder_account_id, uint32_t limit) const; private: std::shared_ptr< database_api_impl > my; }; @@ -952,6 +955,10 @@ FC_API(graphene::app::database_api, (list_offers) (list_sell_offers) (list_buy_offers) + (list_offer_history) (get_offers_by_issuer) (get_offers_by_item) + (get_offer_history_by_issuer) + (get_offer_history_by_item) + (get_offer_history_by_bidder) ) diff --git a/libraries/chain/include/graphene/chain/offer_object.hpp b/libraries/chain/include/graphene/chain/offer_object.hpp index 8fecc4da..5c757485 100644 --- a/libraries/chain/include/graphene/chain/offer_object.hpp +++ b/libraries/chain/include/graphene/chain/offer_object.hpp @@ -68,7 +68,7 @@ namespace graphene bool operator()(const fc::time_point_sec &o1, const fc::time_point_sec &o2) const { - return o1 > o2; + return o1 < o2; } }; diff --git a/libraries/wallet/include/graphene/wallet/wallet.hpp b/libraries/wallet/include/graphene/wallet/wallet.hpp index ae1b3ff5..2f3dc85a 100644 --- a/libraries/wallet/include/graphene/wallet/wallet.hpp +++ b/libraries/wallet/include/graphene/wallet/wallet.hpp @@ -2052,12 +2052,16 @@ class wallet_api asset bid_price, offer_id_type offer_id, bool broadcast); - vector list_offers(uint32_t limit) const; - vector list_sell_offers(uint32_t limit) const; - vector list_buy_offers(uint32_t limit) const; + vector list_offers(uint32_t limit, optional lower_id) const; + vector list_sell_offers(uint32_t limit, optional lower_id) const; + vector list_buy_offers(uint32_t limit, optional lower_id) const; + vector list_offer_history(uint32_t limit, optional lower_id) const; vector get_offers_by_issuer(string issuer_account_id_or_name, - uint32_t limit) const; - vector get_offers_by_item(nft_id_type item, uint32_t limit) const; + uint32_t limit, optional lower_id) const; + vector get_offers_by_item(const nft_id_type item, uint32_t limit, optional lower_id) const; + vector get_offer_history_by_issuer(string issuer_account_id_or_name, uint32_t limit, optional lower_id) const; + vector get_offer_history_by_item(const nft_id_type item, uint32_t limit, optional lower_id) const; + vector get_offer_history_by_bidder(string bidder_account_id_or_name, uint32_t limit, optional lower_id) const; void dbg_make_uia(string creator, string symbol); void dbg_make_mia(string creator, string symbol); @@ -2325,8 +2329,12 @@ FC_API( graphene::wallet::wallet_api, (list_offers) (list_sell_offers) (list_buy_offers) + (list_offer_history) (get_offers_by_issuer) (get_offers_by_item) + (get_offer_history_by_issuer) + (get_offer_history_by_item) + (get_offer_history_by_bidder) (get_upcoming_tournaments) (get_tournaments) (get_tournaments_by_state) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index da5f6696..a42646f5 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -6401,33 +6401,81 @@ signed_transaction wallet_api::create_bid(string bidder_account_id_or_name, return my->sign_transaction( trx, broadcast ); } -vector wallet_api::list_offers(uint32_t limit) const +vector wallet_api::list_offers(uint32_t limit, optional lower_id) const { - return my->_remote_db->list_offers(limit); + offer_id_type lb_id; + if(lower_id) + lb_id = *lower_id; + return my->_remote_db->list_offers(lb_id, limit); } -vector wallet_api::list_sell_offers(uint32_t limit) const +vector wallet_api::list_sell_offers(uint32_t limit, optional lower_id) const { - return my->_remote_db->list_sell_offers(limit); + offer_id_type lb_id; + if(lower_id) + lb_id = *lower_id; + return my->_remote_db->list_sell_offers(lb_id, limit); } -vector wallet_api::list_buy_offers(uint32_t limit) const +vector wallet_api::list_buy_offers(uint32_t limit, optional lower_id) const { - return my->_remote_db->list_buy_offers(limit); + offer_id_type lb_id; + if(lower_id) + lb_id = *lower_id; + return my->_remote_db->list_buy_offers(lb_id, limit); +} + +vector wallet_api::list_offer_history(uint32_t limit, optional lower_id) const +{ + offer_history_id_type lb_id; + if(lower_id) + lb_id = *lower_id; + return my->_remote_db->list_offer_history(lb_id, limit); } vector wallet_api::get_offers_by_issuer(string issuer_account_id_or_name, - uint32_t limit) const + uint32_t limit, optional lower_id) const { + offer_id_type lb_id; + if(lower_id) + lb_id = *lower_id; account_object issuer_account = my->get_account(issuer_account_id_or_name); - return my->_remote_db->get_offers_by_issuer(issuer_account.id, limit); + return my->_remote_db->get_offers_by_issuer(lb_id, issuer_account.id, limit); } -vector wallet_api::get_offers_by_item(nft_id_type item, uint32_t limit) const +vector wallet_api::get_offers_by_item(const nft_id_type item, uint32_t limit, optional lower_id) const { - return my->_remote_db->get_offers_by_item(item, limit); + offer_id_type lb_id; + if(lower_id) + lb_id = *lower_id; + return my->_remote_db->get_offers_by_item(lb_id, item, limit); } +vector wallet_api::get_offer_history_by_issuer(string issuer_account_id_or_name, uint32_t limit, optional lower_id) const +{ + offer_history_id_type lb_id; + if(lower_id) + lb_id = *lower_id; + account_object issuer_account = my->get_account(issuer_account_id_or_name); + return my->_remote_db->get_offer_history_by_issuer(lb_id, issuer_account.id, limit); +} + +vector wallet_api::get_offer_history_by_item(const nft_id_type item, uint32_t limit, optional lower_id) const +{ + offer_history_id_type lb_id; + if(lower_id) + lb_id = *lower_id; + return my->_remote_db->get_offer_history_by_item(lb_id, item, limit); +} + +vector wallet_api::get_offer_history_by_bidder(string bidder_account_id_or_name, uint32_t limit, optional lower_id) const +{ + offer_history_id_type lb_id; + if(lower_id) + lb_id = *lower_id; + account_object bidder_account = my->get_account(bidder_account_id_or_name); + return my->_remote_db->get_offer_history_by_bidder(lb_id, bidder_account.id, limit); +} // default ctor necessary for FC_REFLECT signed_block_with_info::signed_block_with_info() {