contracts/arisen.system/src/com.cpp
/**
    * @brief Updates COM pool balances upon COM purchase
    *
    * @param payment - amount of core tokens paid
    *
    * @return asset - calculated amount of COM tokens purchased
    */
@brief Updates COM pool balances upon COM purchase
@param payment - amount of core tokens paid

@return asset - calculated amount of COM tokens purchased
@brief Updates COM pool balances upon COM purchase @param payment - amount of core tokens paid @return asset - calculated amount of COM tokens purchased
asset system_contract::add_to_com_pool( const asset& payment )
   {
      const int64_t com_ratio = 10000;
      const asset   init_total_rent( 20'000'0000, core_symbol() ); 
      asset com_received( 0, com_symbol );
      auto itr = _compool.begin();
      if ( !com_system_initialized() ) {
         _compool.emplace( get_self(), [&]( auto& rp ) {
            com_received.amount = payment.amount * com_ratio;
            rp.total_lendable   = payment;
            rp.total_lent       = asset( 0, core_symbol() );
            rp.total_unlent     = rp.total_lendable - rp.total_lent;
            rp.total_rent       = init_total_rent;
            rp.total_com        = com_received;
            rp.namebid_proceeds = asset( 0, core_symbol() );
         });
      } else if ( !com_available() ) { 
         _compool.modify( itr, same_payer, [&]( auto& rp ) {
            com_received.amount      = payment.amount * com_ratio;
            rp.total_lendable.amount = payment.amount;
            rp.total_lent.amount     = 0;
            rp.total_unlent.amount   = rp.total_lendable.amount - rp.total_lent.amount;
            rp.total_rent.amount     = init_total_rent.amount;
            rp.total_com.amount      = com_received.amount;
         });
      } else {
         check( itr->total_lendable.amount > 0, "lendable COM pool is empty" );
         const int64_t S0 = itr->total_lendable.amount;
         const int64_t S1 = S0 + payment.amount;
         const int64_t R0 = itr->total_com.amount;
         const int64_t R1 = (uint128_t(S1) * R0) / S0;
         com_received.amount = R1 - R0;
         _compool.modify( itr, same_payer, [&]( auto& rp ) {
            rp.total_lendable.amount = S1;
            rp.total_com.amount      = R1;
            rp.total_unlent.amount   = rp.total_lendable.amount - rp.total_lent.amount;
            check( rp.total_unlent.amount >= 0, "programmer error, this should never go negative" );
         });
      }
      return com_received;
   }
asset system_contract :: add_to_com_pool ( const asset & payment ) { const int64_t com_ratio = 10000 ; const asset init_total_rent ( 20'000'0000 , core_symbol ( ) ) ; asset com_received ( 0 , com_symbol ) ; auto itr = _compool . begin ( ) ; if ( ! com_system_initialized ( ) ) { _compool . emplace ( get_self ( ) , [ & ] ( auto & rp ) { com_received . amount = payment . amount * com_ratio ; rp . total_lendable = payment ; rp . total_lent = asset ( 0 , core_symbol ( ) ) ; rp . total_unlent = rp . total_lendable - rp . total_lent ; rp . total_rent = init_total_rent ; rp . total_com = com_received ; rp . namebid_proceeds = asset ( 0 , core_symbol ( ) ) ; } ) ; } else if ( ! com_available ( ) ) { _compool . modify ( itr , same_payer , [ & ] ( auto & rp ) { com_received . amount = payment . amount * com_ratio ; rp . total_lendable . amount = payment . amount ; rp . total_lent . amount = 0 ; rp . total_unlent . amount = rp . total_lendable . amount - rp . total_lent . amount ; rp . total_rent . amount = init_total_rent . amount ; rp . total_com . amount = com_received . amount ; } ) ; } else { check ( itr -> total_lendable . amount > 0 , " " ) ; const int64_t S0 = itr -> total_lendable . amount ; const int64_t S1 = S0 + payment . amount ; const int64_t R0 = itr -> total_com . amount ; const int64_t R1 = ( uint128_t ( S1 ) * R0 ) / S0 ; com_received . amount = R1 - R0 ; _compool . modify ( itr , same_payer , [ & ] ( auto & rp ) { rp . total_lendable . amount = S1 ; rp . total_com . amount = R1 ; rp . total_unlent . amount = rp . total_lendable . amount - rp . total_lent . amount ; check ( rp . total_unlent . amount >= 0 , " " ) ; } ) ; } return com_received ; }
@brief Updates COM pool balances upon COM purchase
 @param payment - amount of core tokens paid
@brief Updates COM pool balances upon COM purchase @param payment - amount of core tokens paid
/**
       * If CORE_SYMBOL is (RIX,4), maximum supply is 10^10 tokens (10 billion tokens), i.e., maximum amount
       * of indivisible units is 10^14. com_ratio = 10^4 sets the upper bound on (COM,4) indivisible units to
       * 10^18 and that is within the maximum allowable amount field of asset type which is set to 2^62
       * (approximately 4.6 * 10^18). For a different CORE_SYMBOL, and in order for maximum (COM,4) amount not
       * to exceed that limit, maximum amount of indivisible units cannot be set to a value larger than 4 * 10^14.
       * If precision of CORE_SYMBOL is 4, that corresponds to a maximum supply of 40 billion tokens.
       */ /// base balance prevents renting profitably until at least a minimum number of core_symbol() is made available /// initialize COM pool /// should be a rare corner case, COM pool is initialized but empty /// total_lendable > 0 if total_com > 0 except in a rare case and due to rounding errors