Refactored handling of asset_id

This commit is contained in:
Peter Conrad 2018-04-13 14:45:17 +02:00 committed by Fabian Schuh
parent 2ecde50d48
commit 517c86c60f
No known key found for this signature in database
GPG key ID: F2538A4B282D6238
5 changed files with 48 additions and 40 deletions

View file

@ -29,12 +29,12 @@
namespace graphene { namespace chain {
share_type affiliate_payout_helper::payout( account_id_type player, const asset& amount )
share_type affiliate_payout_helper::payout( account_id_type player, share_type amount )
{
return payout( player(_db), amount );
}
share_type affiliate_payout_helper::payout( const account_object& player, const asset& amount )
share_type affiliate_payout_helper::payout( const account_object& player, share_type amount )
{
if( !player.affiliate_distributions.valid() )
return 0;
@ -42,12 +42,13 @@ namespace graphene { namespace chain {
if( dist == player.affiliate_distributions->_dists.end() || dist->second._dist.empty() )
return 0;
share_type to_pay = amount.amount.value / 5; // 20% fixed
if( to_pay <= 0 )
amount = amount.value / 5; // 20% fixed
if( amount <= 0 )
return 0;
uint16_t remaining = GRAPHENE_100_PERCENT;
share_type paid = 0;
share_type to_pay = amount;
for( const auto& entry : dist->second._dist )
{
const account_id_type affiliate = entry.first;
@ -65,16 +66,17 @@ namespace graphene { namespace chain {
if( payout > 0 )
{
if ( accumulator.find(affiliate) == accumulator.end() )
accumulator[affiliate] = asset( payout.to_uint64(), amount.asset_id );
accumulator[affiliate] = payout.to_uint64();
else
accumulator[affiliate] += asset( payout.to_uint64(), amount.asset_id );
accumulator[affiliate] += payout.to_uint64();
to_pay -= payout.to_uint64();
paid += payout.to_uint64();
}
}
FC_ASSERT( to_pay == 0 );
FC_ASSERT( paid == amount );
_db.push_applied_operation( affiliate_referral_payout_operation( player.id, amount ) );
_db.push_applied_operation( affiliate_referral_payout_operation( player.id, asset( amount, payout_asset ) ) );
return paid;
}
@ -83,7 +85,7 @@ namespace graphene { namespace chain {
{
for( const auto& entry : accumulator )
{
_db.adjust_balance( entry.first, entry.second );
_db.adjust_balance( entry.first, asset( entry.second, payout_asset ) );
_db.push_applied_operation( affiliate_payout_operation( entry.first, tag, entry.second ) );
}
accumulator.clear();

View file

@ -265,7 +265,7 @@ void database::settle_betting_market_group(const betting_market_group_object& be
{
rake_amount = ((fc::uint128_t(net_profits.value) * rake_fee_percentage + GRAPHENE_100_PERCENT - 1) / GRAPHENE_100_PERCENT).to_uint64();
if (rake_amount.value)
rake_amount -= payout_helper.payout( bettor_id, asset( rake_amount, betting_market_group.asset_id ) );
rake_amount -= payout_helper.payout( bettor_id, rake_amount );
FC_ASSERT( rake_amount.value >= 0 );
if (rake_amount.value)
adjust_balance(*rake_account_id, asset(rake_amount, betting_market_group.asset_id));

View file

@ -55,20 +55,36 @@ namespace graphene { namespace chain {
return game.options.game_options.visit( impl::game_type_visitor() );
}
template<typename GAME>
asset_id_type get_asset_for_game( const GAME& game );
template<>
inline asset_id_type get_asset_for_game( const betting_market_group_object& game )
{
return game.asset_id;
}
template<>
inline asset_id_type get_asset_for_game( const tournament_object& game )
{
return game.options.buy_in.asset_id;
}
class affiliate_payout_helper {
public:
template<typename GAME>
affiliate_payout_helper( database& db, const GAME& game )
: _db(db), tag( get_tag_for_game( game ) ) {}
: _db(db), tag( get_tag_for_game( game ) ), payout_asset( get_asset_for_game( game ) ) {}
share_type payout( account_id_type player, const asset& amount );
share_type payout( const account_object& player, const asset& amount );
share_type payout( account_id_type player, share_type amount );
share_type payout( const account_object& player, share_type amount );
void commit();
private:
database& _db;
app_tag tag;
std::map<account_id_type, asset> accumulator;
database& _db;
app_tag tag;
asset_id_type payout_asset;
std::map<account_id_type, share_type> accumulator;
};
} } // graphene::chain

View file

@ -337,7 +337,7 @@ namespace graphene { namespace chain {
if (rake_amount.value)
{
affiliate_payout_helper payout_helper( event.db, tournament_obj );
rake_amount -= payout_helper.payout( winner, asset( rake_amount, tournament_obj.options.buy_in.asset_id ) );
rake_amount -= payout_helper.payout( winner, rake_amount );
payout_helper.commit();
FC_ASSERT( rake_amount.value >= 0 );
}

View file

@ -216,30 +216,27 @@ BOOST_AUTO_TEST_CASE( affiliate_payout_helper_test )
});
affiliate_payout_helper helper = affiliate_payout_helper( db, game );
// Alice has no distribution set
BOOST_CHECK_EQUAL( 0, helper.payout( alice_id, asset(1000) ).value );
BOOST_CHECK_EQUAL( 0, helper.payout( alice_id, 1000 ).value );
// Paula has nothing for Bookie
BOOST_CHECK_EQUAL( 0, helper.payout( paula_id, asset(1000) ).value );
BOOST_CHECK_EQUAL( 0, helper.payout( paula_id, 1000 ).value );
// 20% of 4 gets rounded down to 0
BOOST_CHECK_EQUAL( 0, helper.payout( penny_id, asset(4) ).value );
BOOST_CHECK_EQUAL( 0, helper.payout( penny_id, 4 ).value );
// 20% of 5 = 1 is paid to Audrey
BOOST_CHECK_EQUAL( 1, helper.payout( penny_id, asset(5) ).value );
BOOST_CHECK_EQUAL( 1, helper.payout( penny_id, 5 ).value );
audrey_ppy++;
// 20% of 50 = 10: 2 to Alice, 3 to Ann, 5 to Audrey
BOOST_CHECK_EQUAL( 10, helper.payout( penny_id, asset(50) ).value );
BOOST_CHECK_EQUAL( 10, helper.payout( penny_id, 50 ).value );
alice_ppy += 2;
ann_ppy += 3;
audrey_ppy += 5;
// 20% of 59 = 11: 1 to Ann, 10 to Audrey
BOOST_CHECK_EQUAL( 11, helper.payout( petra_id, asset(59) ).value );
BOOST_CHECK_EQUAL( 11, helper.payout( petra_id, 59 ).value );
audrey_ppy += 10;
ann_ppy += 1;
// Cannot add BTC after paying out PPY
BOOST_CHECK_THROW( helper.payout( penny_id, asset( 1000, btc_id ) ), fc::assert_exception );
helper.commit();
BOOST_CHECK_EQUAL( alice_ppy, get_balance( alice_id, asset_id_type() ) );
@ -254,12 +251,10 @@ BOOST_AUTO_TEST_CASE( affiliate_payout_helper_test )
});
affiliate_payout_helper helper = affiliate_payout_helper( db, game );
// 20% of 60 = 12: 2 to Alice, 3 to Ann, 7 to Audrey
BOOST_CHECK_EQUAL( 12, helper.payout( penny_id, asset( 60, btc_id ) ).value );
BOOST_CHECK_EQUAL( 12, helper.payout( penny_id, 60 ).value );
alice_btc += 2;
ann_btc += 3;
audrey_btc += 7;
// Cannot add PPY after paying out BTC
BOOST_CHECK_THROW( helper.payout( penny_id, asset( 1000 ) ), fc::assert_exception );
helper.commit();
BOOST_CHECK_EQUAL( alice_btc, get_balance( alice_id, btc_id ) );
BOOST_CHECK_EQUAL( ann_btc, get_balance( ann_id, btc_id ) );
@ -272,31 +267,28 @@ BOOST_AUTO_TEST_CASE( affiliate_payout_helper_test )
} );
affiliate_payout_helper helper = affiliate_payout_helper( db, game );
// Alice has no distribution set
BOOST_CHECK_EQUAL( 0, helper.payout( alice_id, asset(1000) ).value );
BOOST_CHECK_EQUAL( 0, helper.payout( alice_id, 1000 ).value );
// Petra has nothing for Bookie
BOOST_CHECK_EQUAL( 0, helper.payout( petra_id, asset(1000) ).value );
BOOST_CHECK_EQUAL( 0, helper.payout( petra_id, 1000 ).value );
// 20% of 4 gets rounded down to 0
BOOST_CHECK_EQUAL( 0, helper.payout( penny_id, asset(4) ).value );
BOOST_CHECK_EQUAL( 0, helper.payout( penny_id, 4 ).value );
// 20% of 5 = 1 is paid to Ann
BOOST_CHECK_EQUAL( 1, helper.payout( penny_id, asset(5) ).value );
BOOST_CHECK_EQUAL( 1, helper.payout( penny_id, 5 ).value );
ann_ppy++;
// 20% of 40 = 8: 8 to Alice
BOOST_CHECK_EQUAL( 8, helper.payout( paula_id, asset(40) ).value );
BOOST_CHECK_EQUAL( 8, helper.payout( paula_id, 40 ).value );
alice_ppy += 8;
// intermediate commit should clear internal accumulator
helper.commit();
// 20% of 59 = 11: 6 to Alice, 5 to Ann
BOOST_CHECK_EQUAL( 11, helper.payout( penny_id, asset(59) ).value );
BOOST_CHECK_EQUAL( 11, helper.payout( penny_id, 59 ).value );
alice_ppy += 6;
ann_ppy += 5;
// Cannot add BTC after paying out PPY
BOOST_CHECK_THROW( helper.payout( penny_id, asset( 1000, btc_id ) ), fc::assert_exception );
helper.commit();
BOOST_CHECK_EQUAL( alice_ppy, get_balance( alice_id, asset_id_type() ) );
@ -310,11 +302,9 @@ BOOST_AUTO_TEST_CASE( affiliate_payout_helper_test )
} );
affiliate_payout_helper helper = affiliate_payout_helper( db, game );
// 20% of 60 = 12: 7 to Alice, 5 to Ann
BOOST_CHECK_EQUAL( 12, helper.payout( penny_id, asset( 60, btc_id ) ).value );
BOOST_CHECK_EQUAL( 12, helper.payout( penny_id, 60 ).value );
alice_btc += 7;
ann_btc += 5;
// Cannot add PPY after paying out BTC
BOOST_CHECK_THROW( helper.payout( penny_id, asset( 1000 ) ), fc::assert_exception );
helper.commit();
BOOST_CHECK_EQUAL( alice_btc, get_balance( alice_id, btc_id ) );
BOOST_CHECK_EQUAL( ann_btc, get_balance( ann_id, btc_id ) );