From 69192a889d7da808080a8532d39441d4dfbda465 Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Fri, 24 Mar 2017 11:51:35 -0400 Subject: [PATCH] Generate a virtual op when a market is resolved --- libraries/app/impacted.cpp | 4 +++ libraries/chain/db_bet.cpp | 27 ++++++++++----- .../chain/protocol/betting_market.hpp | 34 +++++++++++++++++++ .../graphene/chain/protocol/operations.hpp | 1 + 4 files changed, 57 insertions(+), 9 deletions(-) diff --git a/libraries/app/impacted.cpp b/libraries/app/impacted.cpp index 5ea1f4e6..3038c8f7 100644 --- a/libraries/app/impacted.cpp +++ b/libraries/app/impacted.cpp @@ -226,6 +226,10 @@ struct get_impacted_account_visitor { _impacted.insert( op.bettor_id ); } + void operator()( const betting_market_resolved_operation& op ) + { + _impacted.insert( op.bettor_id ); + } }; diff --git a/libraries/chain/db_bet.cpp b/libraries/chain/db_bet.cpp index c9670b07..00f45b4a 100644 --- a/libraries/chain/db_bet.cpp +++ b/libraries/chain/db_bet.cpp @@ -4,6 +4,8 @@ #include #include +#include + namespace graphene { namespace chain { void database::cancel_bet( const bet_object& bet, bool create_virtual_op ) @@ -35,22 +37,24 @@ void database::cancel_all_unmatched_bets_on_betting_market(const betting_market_ void database::cancel_all_betting_markets_for_event(const event_object& event_obj) { - //for each betting market group of event auto& betting_market_group_index = get_index_type().indices().get(); - auto betting_market_group_itr = betting_market_group_index.lower_bound(event_obj.id); - while (betting_market_group_itr != betting_market_group_index.end() && - betting_market_group_itr->event_id == event_obj.id) + auto& betting_market_index = get_index_type().indices().get(); + + //for each betting market group of event + for (const betting_market_group_object& betting_market_group : + boost::make_iterator_range(betting_market_group_index.equal_range(event_obj.id))) { //for each betting market in the betting market group - auto& betting_market_index = get_index_type().indices().get(); - auto betting_market_itr = betting_market_index.lower_bound(betting_market_group_itr->id); + auto betting_market_itr = betting_market_index.lower_bound(betting_market_group.id); while (betting_market_itr != betting_market_index.end() && - betting_market_itr->group_id == betting_market_group_itr->id) + betting_market_itr->group_id == betting_market_group.id) { - resolve_betting_market(*betting_market_itr, betting_market_resolution_type::cancel); + auto old_betting_market_itr = betting_market_itr; ++betting_market_itr; + resolve_betting_market(*old_betting_market_itr, betting_market_resolution_type::cancel); } } + //TODO: should we remove market groups once all their markets are resolved? } void database::resolve_betting_market(const betting_market_object& betting_market, @@ -85,7 +89,12 @@ void database::resolve_betting_market(const betting_market_object& betting_marke adjust_balance(position.bettor_id, asset(payout_amount, betting_market.asset_id)); //TODO: pay the fees to the correct (dividend-distribution) account adjust_balance(account_id_type(), asset(position.fees_collected, betting_market.asset_id)); - //TODO: generate a virtual op to notify the bettor that they won or lost + + push_applied_operation(betting_market_resolved_operation(position.bettor_id, + betting_market.id, + resolution, + payout_amount, + position.fees_collected)); remove(position); } diff --git a/libraries/chain/include/graphene/chain/protocol/betting_market.hpp b/libraries/chain/include/graphene/chain/protocol/betting_market.hpp index c0fb9b35..5bedf242 100644 --- a/libraries/chain/include/graphene/chain/protocol/betting_market.hpp +++ b/libraries/chain/include/graphene/chain/protocol/betting_market.hpp @@ -110,6 +110,37 @@ struct betting_market_resolve_operation : public base_operation void validate()const; }; +struct betting_market_resolved_operation : public base_operation +{ + struct fee_parameters_type {}; + + account_id_type bettor_id; + betting_market_id_type betting_market_id; + betting_market_resolution_type resolution; + asset winnings; + share_type fees_paid; + + asset fee; // unused in a virtual operation + + betting_market_resolved_operation() {} + betting_market_resolved_operation(account_id_type bettor_id, + betting_market_id_type betting_market_id, + betting_market_resolution_type resolution, + asset winnings, + share_type fees_paid) : + bettor_id(bettor_id), + betting_market_id(betting_market_id), + resolution(resolution), + winnings(winnings), + fees_paid(fees_paid) + {} + + account_id_type fee_payer()const { return bettor_id; } + void validate()const { FC_ASSERT(false, "virtual operation"); } + /// This is a virtual operation; there is no fee + share_type calculate_fee(const fee_parameters_type& k)const { return 0; } +}; + enum class bet_type { back, lay }; struct bet_place_operation : public base_operation @@ -252,6 +283,9 @@ FC_REFLECT( graphene::chain::betting_market_resolve_operation::fee_parameters_ty FC_REFLECT( graphene::chain::betting_market_resolve_operation, (fee)(betting_market_id)(resolution)(extensions) ) +FC_REFLECT( graphene::chain::betting_market_resolved_operation::fee_parameters_type, ) +FC_REFLECT( graphene::chain::betting_market_resolved_operation, + (bettor_id)(betting_market_id)(resolution)(winnings)(fees_paid)(fee) ) FC_REFLECT_ENUM( graphene::chain::bet_type, (back)(lay) ) FC_REFLECT( graphene::chain::bet_place_operation::fee_parameters_type, (fee) ) diff --git a/libraries/chain/include/graphene/chain/protocol/operations.hpp b/libraries/chain/include/graphene/chain/protocol/operations.hpp index 816caa73..8ba64fb7 100644 --- a/libraries/chain/include/graphene/chain/protocol/operations.hpp +++ b/libraries/chain/include/graphene/chain/protocol/operations.hpp @@ -105,6 +105,7 @@ namespace graphene { namespace chain { betting_market_create_operation, bet_place_operation, betting_market_resolve_operation, + betting_market_resolved_operation, // VIRTUAL bet_matched_operation, // VIRTUAL bet_cancel_operation, bet_canceled_operation // VIRTUAL