Add get_matched_bets_for_bettor() to bookie plugin
This commit is contained in:
parent
2cd816ec6d
commit
eedd775405
5 changed files with 81 additions and 4 deletions
|
|
@ -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);
|
asset get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id);
|
||||||
std::vector<event_object> get_events_containing_sub_string(const std::string& sub_string, const std::string& language);
|
std::vector<event_object> get_events_containing_sub_string(const std::string& sub_string, const std::string& language);
|
||||||
fc::variants get_objects(const vector<object_id_type>& ids) const;
|
fc::variants get_objects(const vector<object_id_type>& ids) const;
|
||||||
|
std::vector<matched_bet_object> get_matched_bets_for_bettor(account_id_type bettor_id) const;
|
||||||
graphene::app::application& app;
|
graphene::app::application& app;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -178,6 +178,32 @@ fc::variants bookie_api_impl::get_objects(const vector<object_id_type>& ids) con
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<matched_bet_object> bookie_api_impl::get_matched_bets_for_bettor(account_id_type bettor_id) const
|
||||||
|
{
|
||||||
|
std::vector<matched_bet_object> result;
|
||||||
|
std::shared_ptr<graphene::chain::database> db = app.chain_database();
|
||||||
|
auto& persistent_bets_by_bettor_id = db->get_index_type<detail::persistent_bet_index>().indices().get<by_bettor_id>();
|
||||||
|
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<graphene::bookie::bookie_plugin> bookie_api_impl::get_plugin()
|
std::shared_ptr<graphene::bookie::bookie_plugin> bookie_api_impl::get_plugin()
|
||||||
{
|
{
|
||||||
return app.get_plugin<graphene::bookie::bookie_plugin>("bookie");
|
return app.get_plugin<graphene::bookie::bookie_plugin>("bookie");
|
||||||
|
|
@ -220,6 +246,11 @@ fc::variants bookie_api::get_objects(const vector<object_id_type>& ids) const
|
||||||
return my->get_objects(ids);
|
return my->get_objects(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<matched_bet_object> bookie_api::get_matched_bets_for_bettor(account_id_type bettor_id) const
|
||||||
|
{
|
||||||
|
return my->get_matched_bets_for_bettor(bettor_id);
|
||||||
|
}
|
||||||
|
|
||||||
} } // graphene::bookie
|
} } // graphene::bookie
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -283,7 +283,7 @@ void bookie_plugin_impl::on_block_applied( const signed_block& )
|
||||||
if( op.op.which() == operation::tag<bet_matched_operation>::value )
|
if( op.op.which() == operation::tag<bet_matched_operation>::value )
|
||||||
{
|
{
|
||||||
const bet_matched_operation& bet_matched_op = op.op.get<bet_matched_operation>();
|
const bet_matched_operation& bet_matched_op = op.op.get<bet_matched_operation>();
|
||||||
idump((bet_matched_op));
|
//idump((bet_matched_op));
|
||||||
const asset& amount_bet = bet_matched_op.amount_bet;
|
const asset& amount_bet = bet_matched_op.amount_bet;
|
||||||
// object may no longer exist
|
// object may no longer exist
|
||||||
//const bet_object& bet = bet_matched_op.bet_id(db);
|
//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());
|
assert(bet_iter != persistent_bets_by_bet_id.end());
|
||||||
if (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 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_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
|
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 ){
|
db.modify( betting_market_group, [&]( betting_market_group_object& obj ){
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,26 @@ struct binned_order_book {
|
||||||
std::vector<order_bin> aggregated_lay_bets;
|
std::vector<order_bin> 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<fc::time_point_sec> end_of_delay;
|
||||||
|
|
||||||
|
// plus fields from this plugin
|
||||||
|
share_type amount_matched;
|
||||||
|
};
|
||||||
|
|
||||||
class bookie_api
|
class bookie_api
|
||||||
{
|
{
|
||||||
public:
|
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);
|
asset get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id);
|
||||||
std::vector<event_object> get_events_containing_sub_string(const std::string& sub_string, const std::string& language);
|
std::vector<event_object> get_events_containing_sub_string(const std::string& sub_string, const std::string& language);
|
||||||
fc::variants get_objects(const vector<object_id_type>& ids)const;
|
fc::variants get_objects(const vector<object_id_type>& ids)const;
|
||||||
|
std::vector<matched_bet_object> get_matched_bets_for_bettor(account_id_type bettor_id) const;
|
||||||
|
|
||||||
std::shared_ptr<detail::bookie_api_impl> my;
|
std::shared_ptr<detail::bookie_api_impl> my;
|
||||||
};
|
};
|
||||||
|
|
@ -54,6 +75,7 @@ class bookie_api
|
||||||
|
|
||||||
FC_REFLECT(graphene::bookie::order_bin, (amount_to_bet)(backer_multiplier))
|
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::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,
|
FC_API(graphene::bookie::bookie_api,
|
||||||
(get_binned_order_book)
|
(get_binned_order_book)
|
||||||
|
|
|
||||||
|
|
@ -156,15 +156,27 @@ class persistent_bet_object : public graphene::db::abstract_object<persistent_be
|
||||||
|
|
||||||
bet_object ephemeral_bet_object;
|
bet_object ephemeral_bet_object;
|
||||||
|
|
||||||
|
// total amount of the bet that matched
|
||||||
|
share_type amount_matched;
|
||||||
|
|
||||||
bet_id_type get_bet_id() const { return ephemeral_bet_object.id; }
|
bet_id_type get_bet_id() const { return ephemeral_bet_object.id; }
|
||||||
|
account_id_type get_bettor_id() const { return ephemeral_bet_object.bettor_id; }
|
||||||
|
bool is_matched() const { return amount_matched != share_type(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct by_bet_id;
|
struct by_bet_id;
|
||||||
|
struct by_bettor_id;
|
||||||
typedef multi_index_container<
|
typedef multi_index_container<
|
||||||
persistent_bet_object,
|
persistent_bet_object,
|
||||||
indexed_by<
|
indexed_by<
|
||||||
ordered_unique<tag<by_id>, member<object, object_id_type, &object::id> >,
|
ordered_unique<tag<by_id>, member<object, object_id_type, &object::id> >,
|
||||||
ordered_unique<tag<by_bet_id>, const_mem_fun<persistent_bet_object, bet_id_type, &persistent_bet_object::get_bet_id> > > > persistent_bet_multi_index_type;
|
ordered_unique<tag<by_bet_id>, const_mem_fun<persistent_bet_object, bet_id_type, &persistent_bet_object::get_bet_id> >,
|
||||||
|
ordered_unique<tag<by_bettor_id>,
|
||||||
|
composite_key<
|
||||||
|
persistent_bet_object,
|
||||||
|
const_mem_fun<persistent_bet_object, account_id_type, &persistent_bet_object::get_bettor_id>,
|
||||||
|
const_mem_fun<persistent_bet_object, bool, &persistent_bet_object::is_matched>,
|
||||||
|
const_mem_fun<persistent_bet_object, bet_id_type, &persistent_bet_object::get_bet_id> > > > > persistent_bet_multi_index_type;
|
||||||
|
|
||||||
|
|
||||||
typedef generic_index<persistent_bet_object, persistent_bet_multi_index_type> persistent_bet_index;
|
typedef generic_index<persistent_bet_object, persistent_bet_multi_index_type> persistent_bet_index;
|
||||||
|
|
@ -174,5 +186,5 @@ typedef generic_index<persistent_bet_object, persistent_bet_multi_index_type> pe
|
||||||
FC_REFLECT_DERIVED( graphene::bookie::detail::persistent_event_object, (graphene::db::object), (ephemeral_event_object) )
|
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_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_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) )
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
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(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");
|
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});
|
objects_from_bookie = bookie_api.get_objects({first_bet_on_books, matching_bet});
|
||||||
BOOST_REQUIRE_EQUAL(objects_from_bookie.size(), 2);
|
BOOST_REQUIRE_EQUAL(objects_from_bookie.size(), 2);
|
||||||
BOOST_CHECK_MESSAGE(objects_from_bookie[0]["id"].as<bet_id_type>() == first_bet_on_books, "Bookie Plugin didn't return a bet that has been filled");
|
BOOST_CHECK_MESSAGE(objects_from_bookie[0]["id"].as<bet_id_type>() == first_bet_on_books, "Bookie Plugin didn't return a bet that has been filled");
|
||||||
BOOST_CHECK_MESSAGE(objects_from_bookie[1]["id"].as<bet_id_type>() == matching_bet, "Bookie Plugin didn't return a bet that has been filled");
|
BOOST_CHECK_MESSAGE(objects_from_bookie[1]["id"].as<bet_id_type>() == matching_bet, "Bookie Plugin didn't return a bet that has been filled");
|
||||||
|
|
||||||
|
std::vector<graphene::bookie::matched_bet_object> 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<graphene::bookie::matched_bet_object> 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()
|
FC_LOG_AND_RETHROW()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue