contracts/arisen.system/src/com.cpp
/**
    * @brief Performs maintenance operations on expired NET and CPU loans and sellcom orders
    *
    * @param max - maximum number of each of the three categories to be processed
    */
@brief Performs maintenance operations on expired NET and CPU loans and sellcom orders
@param max - maximum number of each of the three categories to be processed
@brief Performs maintenance operations on expired NET and CPU loans and sellcom orders @param max - maximum number of each of the three categories to be processed
void system_contract::runcom( uint16_t max )
   {
      check( com_system_initialized(), "com system not initialized yet" );
      const auto& pool = _compool.begin();
      auto process_expired_loan = [&]( auto& idx, const auto& itr ) -> std::pair<bool, int64_t> {
         remove_loan_from_com_pool( *itr );
         bool    delete_loan   = false;
         int64_t delta_stake   = 0;
         int64_t rented_tokens = exchange_state::get_bancor_output( pool->total_rent.amount,
                                                                    pool->total_unlent.amount,
                                                                    itr->payment.amount );
         bool renew_loan = itr->payment <= itr->balance        
                        && itr->payment.amount < rented_tokens 
                        && com_loans_available();              
         if ( renew_loan ) {
            add_loan_to_com_pool( itr->payment, rented_tokens, false );
            delta_stake = update_renewed_loan( idx, itr, rented_tokens );
         } else {
            delete_loan = true;
            delta_stake = -( itr->total_staked.amount );
            if ( itr->balance.amount > 0 ) {
               transfer_to_fund( itr->from, itr->balance );
            }
         }
         return { delete_loan, delta_stake };
      };
      if ( pool->namebid_proceeds.amount > 0 ) {
         channel_to_com( names_account, pool->namebid_proceeds );
         _compool.modify( pool, same_payer, [&]( auto& rt ) {
            rt.namebid_proceeds.amount = 0;
         });
      }
      {
         com_cpu_loan_table cpu_loans( get_self(), get_self().value );
         auto cpu_idx = cpu_loans.get_index<"byexpr"_n>();
         for ( uint16_t i = 0; i < max; ++i ) {
            auto itr = cpu_idx.begin();
            if ( itr == cpu_idx.end() || itr->expiration > current_time_point() ) break;
            auto result = process_expired_loan( cpu_idx, itr );
            if ( result.second != 0 )
               update_resource_limits( itr->from, itr->receiver, 0, result.second );
            if ( result.first )
               cpu_idx.erase( itr );
         }
      }
      {
         com_net_loan_table net_loans( get_self(), get_self().value );
         auto net_idx = net_loans.get_index<"byexpr"_n>();
         for ( uint16_t i = 0; i < max; ++i ) {
            auto itr = net_idx.begin();
            if ( itr == net_idx.end() || itr->expiration > current_time_point() ) break;
            auto result = process_expired_loan( net_idx, itr );
            if ( result.second != 0 )
               update_resource_limits( itr->from, itr->receiver, result.second, 0 );
            if ( result.first )
               net_idx.erase( itr );
         }
      }
      if ( _comorders.begin() != _comorders.end() ) {
         auto idx  = _comorders.get_index<"bytime"_n>();
         auto oitr = idx.begin();
         for ( uint16_t i = 0; i < max; ++i ) {
            if ( oitr == idx.end() || !oitr->is_open ) break;
            auto next = oitr;
            ++next;
            auto bitr = _combalance.find( oitr->owner.value );
            if ( bitr != _combalance.end() ) { 
               auto result = fill_com_order( bitr, oitr->com_requested );
               if ( result.success ) {
                  const name order_owner = oitr->owner;
                  idx.modify( oitr, same_payer, [&]( auto& order ) {
                     order.proceeds.amount     = result.proceeds.amount;
                     order.stake_change.amount = result.stake_change.amount;
                     order.close();
                  });
                  com_results::orderresult_action order_act( com_account, std::vector<arisen::permission_level>{ } );
                  order_act.send( order_owner, result.proceeds );
               }
            }
            oitr = next;
         }
      }
   }
void system_contract :: runcom ( uint16_t max ) { check ( com_system_initialized ( ) , " " ) ; const auto & pool = _compool . begin ( ) ; auto process_expired_loan = [ & ] ( auto & idx , const auto & itr ) -> std :: pair < bool , int64_t > { remove_loan_from_com_pool ( * itr ) ; bool delete_loan = false ; int64_t delta_stake = 0 ; int64_t rented_tokens = exchange_state :: get_bancor_output ( pool -> total_rent . amount , pool -> total_unlent . amount , itr -> payment . amount ) ; bool renew_loan = itr -> payment <= itr -> balance && itr -> payment . amount < rented_tokens && com_loans_available ( ) ; if ( renew_loan ) { add_loan_to_com_pool ( itr -> payment , rented_tokens , false ) ; delta_stake = update_renewed_loan ( idx , itr , rented_tokens ) ; } else { delete_loan = true ; delta_stake = - ( itr -> total_staked . amount ) ; if ( itr -> balance . amount > 0 ) { transfer_to_fund ( itr -> from , itr -> balance ) ; } } return { delete_loan , delta_stake } ; } ; if ( pool -> namebid_proceeds . amount > 0 ) { channel_to_com ( names_account , pool -> namebid_proceeds ) ; _compool . modify ( pool , same_payer , [ & ] ( auto & rt ) { rt . namebid_proceeds . amount = 0 ; } ) ; } { com_cpu_loan_table cpu_loans ( get_self ( ) , get_self ( ) . value ) ; auto cpu_idx = cpu_loans . get_index < " " _n > ( ) ; for ( uint16_t i = 0 ; i < max ; ++ i ) { auto itr = cpu_idx . begin ( ) ; if ( itr == cpu_idx . end ( ) || itr -> expiration > current_time_point ( ) ) break ; auto result = process_expired_loan ( cpu_idx , itr ) ; if ( result . second != 0 ) update_resource_limits ( itr -> from , itr -> receiver , 0 , result . second ) ; if ( result . first ) cpu_idx . erase ( itr ) ; } } { com_net_loan_table net_loans ( get_self ( ) , get_self ( ) . value ) ; auto net_idx = net_loans . get_index < " " _n > ( ) ; for ( uint16_t i = 0 ; i < max ; ++ i ) { auto itr = net_idx . begin ( ) ; if ( itr == net_idx . end ( ) || itr -> expiration > current_time_point ( ) ) break ; auto result = process_expired_loan ( net_idx , itr ) ; if ( result . second != 0 ) update_resource_limits ( itr -> from , itr -> receiver , result . second , 0 ) ; if ( result . first ) net_idx . erase ( itr ) ; } } if ( _comorders . begin ( ) != _comorders . end ( ) ) { auto idx = _comorders . get_index < " " _n > ( ) ; auto oitr = idx . begin ( ) ; for ( uint16_t i = 0 ; i < max ; ++ i ) { if ( oitr == idx . end ( ) || ! oitr -> is_open ) break ; auto next = oitr ; ++ next ; auto bitr = _combalance . find ( oitr -> owner . value ) ; if ( bitr != _combalance . end ( ) ) { auto result = fill_com_order ( bitr , oitr -> com_requested ) ; if ( result . success ) { const name order_owner = oitr -> owner ; idx . modify ( oitr , same_payer , [ & ] ( auto & order ) { order . proceeds . amount = result . proceeds . amount ; order . stake_change . amount = result . stake_change . amount ; order . close ( ) ; } ) ; com_results :: orderresult_action order_act ( com_account , std :: vector < arisen :: permission_level > { } ) ; order_act . send ( order_owner , result . proceeds ) ; } } oitr = next ; } } }
@brief Performs maintenance operations on expired NET and CPU loans and sellcom orders
 @param max - maximum number of each of the three categories to be processed
@brief Performs maintenance operations on expired NET and CPU loans and sellcom orders @param max - maximum number of each of the three categories to be processed
/// update com_pool in order to delete existing loan /// calculate rented tokens at current price /// conditions for loan renewal /// loan has sufficient balance /// loan has favorable return /// no pending sell orders /// update com_pool in order to account for renewed loan /// update renewed loan fields /// refund "from" account if the closed loan balance is positive /// transfer from arisen.names to arisen.com /// process cpu loans /// process net loans /// process sellcom orders // should always be true /// send dummy action to show owner and proceeds of filled sellcom order