From eedd775405ea8fe94c55153017861e70e317c8d8 Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Sat, 2 Sep 2017 19:03:36 -0400 Subject: [PATCH] Add get_matched_bets_for_bettor() to bookie plugin --- libraries/plugins/bookie/bookie_api.cpp | 33 ++++++++++++++++++- libraries/plugins/bookie/bookie_plugin.cpp | 6 +++- .../include/graphene/bookie/bookie_api.hpp | 22 +++++++++++++ .../graphene/bookie/bookie_objects.hpp | 16 +++++++-- tests/betting/betting_tests.cpp | 8 +++++ 5 files changed, 81 insertions(+), 4 deletions(-) diff --git a/libraries/plugins/bookie/bookie_api.cpp b/libraries/plugins/bookie/bookie_api.cpp index d3c399e5..0e6fa016 100644 --- a/libraries/plugins/bookie/bookie_api.cpp +++ b/libraries/plugins/bookie/bookie_api.cpp @@ -31,7 +31,7 @@ class bookie_api_impl asset get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id); std::vector get_events_containing_sub_string(const std::string& sub_string, const std::string& language); fc::variants get_objects(const vector& ids) const; - + std::vector get_matched_bets_for_bettor(account_id_type bettor_id) const; graphene::app::application& app; }; @@ -178,6 +178,32 @@ fc::variants bookie_api_impl::get_objects(const vector& ids) con return result; } +std::vector bookie_api_impl::get_matched_bets_for_bettor(account_id_type bettor_id) const +{ + std::vector result; + std::shared_ptr db = app.chain_database(); + auto& persistent_bets_by_bettor_id = db->get_index_type().indices().get(); + auto iter = persistent_bets_by_bettor_id.lower_bound(std::make_tuple(bettor_id, true)); + while (iter != persistent_bets_by_bettor_id.end() && + iter->get_bettor_id() == bettor_id && + iter->is_matched()) + { + matched_bet_object match; + match.id = iter->ephemeral_bet_object.id; + match.bettor_id = iter->ephemeral_bet_object.bettor_id; + match.betting_market_id = iter->ephemeral_bet_object.betting_market_id; + match.amount_to_bet = iter->ephemeral_bet_object.amount_to_bet; + match.back_or_lay = iter->ephemeral_bet_object.back_or_lay; + match.end_of_delay = iter->ephemeral_bet_object.end_of_delay; + match.amount_matched = iter->amount_matched; + result.emplace_back(std::move(match)); + + ++iter; + } + return result; +} + + std::shared_ptr bookie_api_impl::get_plugin() { return app.get_plugin("bookie"); @@ -220,6 +246,11 @@ fc::variants bookie_api::get_objects(const vector& ids) const return my->get_objects(ids); } +std::vector bookie_api::get_matched_bets_for_bettor(account_id_type bettor_id) const +{ + return my->get_matched_bets_for_bettor(bettor_id); +} + } } // graphene::bookie diff --git a/libraries/plugins/bookie/bookie_plugin.cpp b/libraries/plugins/bookie/bookie_plugin.cpp index f4e817fe..f7a3f8b8 100644 --- a/libraries/plugins/bookie/bookie_plugin.cpp +++ b/libraries/plugins/bookie/bookie_plugin.cpp @@ -283,7 +283,7 @@ void bookie_plugin_impl::on_block_applied( const signed_block& ) if( op.op.which() == operation::tag::value ) { const bet_matched_operation& bet_matched_op = op.op.get(); - idump((bet_matched_op)); + //idump((bet_matched_op)); const asset& amount_bet = bet_matched_op.amount_bet; // object may no longer exist //const bet_object& bet = bet_matched_op.bet_id(db); @@ -292,7 +292,11 @@ void bookie_plugin_impl::on_block_applied( const signed_block& ) assert(bet_iter != persistent_bets_by_bet_id.end()); if (bet_iter != persistent_bets_by_bet_id.end()) { + db.modify(*bet_iter, [&]( persistent_bet_object& obj ) { + obj.amount_matched += amount_bet.amount; + }); const bet_object& bet_obj = bet_iter->ephemeral_bet_object; + const betting_market_object& betting_market = bet_obj.betting_market_id(db); // TODO: this needs to look at the persistent version const betting_market_group_object& betting_market_group = betting_market.group_id(db); // TODO: as does this db.modify( betting_market_group, [&]( betting_market_group_object& obj ){ diff --git a/libraries/plugins/bookie/include/graphene/bookie/bookie_api.hpp b/libraries/plugins/bookie/include/graphene/bookie/bookie_api.hpp index de5e60b3..5039a216 100644 --- a/libraries/plugins/bookie/include/graphene/bookie/bookie_api.hpp +++ b/libraries/plugins/bookie/include/graphene/bookie/bookie_api.hpp @@ -32,6 +32,26 @@ struct binned_order_book { std::vector aggregated_lay_bets; }; +struct matched_bet_object { + // all fields from bet_object + bet_id_type id; + + account_id_type bettor_id; + + betting_market_id_type betting_market_id; + + asset amount_to_bet; // this is the original amount, not the amount remaining + + bet_multiplier_type backer_multiplier; + + bet_type back_or_lay; + + fc::optional end_of_delay; + + // plus fields from this plugin + share_type amount_matched; +}; + class bookie_api { public: @@ -46,6 +66,7 @@ class bookie_api asset get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id); std::vector get_events_containing_sub_string(const std::string& sub_string, const std::string& language); fc::variants get_objects(const vector& ids)const; + std::vector get_matched_bets_for_bettor(account_id_type bettor_id) const; std::shared_ptr my; }; @@ -54,6 +75,7 @@ class bookie_api FC_REFLECT(graphene::bookie::order_bin, (amount_to_bet)(backer_multiplier)) FC_REFLECT(graphene::bookie::binned_order_book, (aggregated_back_bets)(aggregated_lay_bets)) +FC_REFLECT(graphene::bookie::matched_bet_object, (id)(bettor_id)(betting_market_id)(amount_to_bet)(backer_multiplier)(back_or_lay)(end_of_delay)(amount_matched)) FC_API(graphene::bookie::bookie_api, (get_binned_order_book) diff --git a/libraries/plugins/bookie/include/graphene/bookie/bookie_objects.hpp b/libraries/plugins/bookie/include/graphene/bookie/bookie_objects.hpp index be49213f..cff2d399 100644 --- a/libraries/plugins/bookie/include/graphene/bookie/bookie_objects.hpp +++ b/libraries/plugins/bookie/include/graphene/bookie/bookie_objects.hpp @@ -156,15 +156,27 @@ class persistent_bet_object : public graphene::db::abstract_object, member >, - ordered_unique, const_mem_fun > > > persistent_bet_multi_index_type; + ordered_unique, const_mem_fun >, + ordered_unique, + composite_key< + persistent_bet_object, + const_mem_fun, + const_mem_fun, + const_mem_fun > > > > persistent_bet_multi_index_type; typedef generic_index persistent_bet_index; @@ -174,5 +186,5 @@ typedef generic_index pe FC_REFLECT_DERIVED( graphene::bookie::detail::persistent_event_object, (graphene::db::object), (ephemeral_event_object) ) FC_REFLECT_DERIVED( graphene::bookie::detail::persistent_betting_market_group_object, (graphene::db::object), (ephemeral_betting_market_group_object)(total_matched_bets_amount) ) FC_REFLECT_DERIVED( graphene::bookie::detail::persistent_betting_market_object, (graphene::db::object), (ephemeral_betting_market_object) ) -FC_REFLECT_DERIVED( graphene::bookie::detail::persistent_bet_object, (graphene::db::object), (ephemeral_bet_object) ) +FC_REFLECT_DERIVED( graphene::bookie::detail::persistent_bet_object, (graphene::db::object), (ephemeral_bet_object)(amount_matched) ) diff --git a/tests/betting/betting_tests.cpp b/tests/betting/betting_tests.cpp index ff635b52..84fbf73a 100644 --- a/tests/betting/betting_tests.cpp +++ b/tests/betting/betting_tests.cpp @@ -379,11 +379,19 @@ BOOST_AUTO_TEST_CASE(persistent_objects_test) bet_id_type matching_bet = place_bet(bob_id, capitals_win_market.id, bet_type::back, asset(50, asset_id_type()), 194 * GRAPHENE_BETTING_ODDS_PRECISION / 100); BOOST_CHECK_MESSAGE(!db.find(first_bet_on_books), "Bet should have been filled, but the blockchain still knows about it"); BOOST_CHECK_MESSAGE(!db.find(matching_bet), "Bet should have been filled, but the blockchain still knows about it"); + generate_blocks(1); // the bookie plugin doesn't detect matches until a block is generated objects_from_bookie = bookie_api.get_objects({first_bet_on_books, matching_bet}); BOOST_REQUIRE_EQUAL(objects_from_bookie.size(), 2); BOOST_CHECK_MESSAGE(objects_from_bookie[0]["id"].as() == first_bet_on_books, "Bookie Plugin didn't return a bet that has been filled"); BOOST_CHECK_MESSAGE(objects_from_bookie[1]["id"].as() == matching_bet, "Bookie Plugin didn't return a bet that has been filled"); + + std::vector alice_matched_bets = bookie_api.get_matched_bets_for_bettor(alice_id); + BOOST_REQUIRE_EQUAL(alice_matched_bets.size(), 1); + BOOST_CHECK(alice_matched_bets[0].amount_matched == 47); + std::vector bob_matched_bets = bookie_api.get_matched_bets_for_bettor(bob_id); + BOOST_REQUIRE_EQUAL(bob_matched_bets.size(), 1); + BOOST_CHECK(bob_matched_bets[0].amount_matched == 50); } FC_LOG_AND_RETHROW() }