0

Good Day!!

I want to change add_balance in transfer

How can change quantity value in add_balance?

Ex) invalid operands to binary expression ('eosio::asset' and 'int') add_balance( to, quantity - 100 , payer );

void token::transfer( const name&    from,
                      const name&    to,
                      const asset&   quantity,
                      const string&  memo )
{
    check( from != to, "cannot transfer to self" );
    require_auth( from );
    check( is_account( to ), "to account does not exist");
    auto sym = quantity.symbol.code();
    stats statstable( get_self(), sym.raw() );
    const auto& st = statstable.get( sym.raw() );

    require_recipient( from );
    require_recipient( to );

    check( quantity.is_valid(), "invalid quantity" );
    check( quantity.amount > 0, "must transfer positive quantity" );
    check( quantity.symbol == st.supply.symbol, "symbol precision mismatch" );
    check( memo.size() <= 256, "memo has more than 256 bytes" );

    auto payer = has_auth( to ) ? to : from;

    sub_balance( from, quantity );
    add_balance( to, quantity, payer );
}


void token::add_balance( const name& owner, const asset& value, const name& ram_payer )
{
   accounts to_acnts( get_self(), owner.value );
   auto to = to_acnts.find( value.symbol.code().raw() );
   if( to == to_acnts.end() ) {
      to_acnts.emplace( ram_payer, [&]( auto& a ){  
        a.balance = value;
      });
   } else {
      to_acnts.modify( to, same_payer, [&]( auto& a ) {
        a.balance += value;
      });
   }
  
   
}
0
namespace eosio {
  struct asset {
    int64_t amount;
    symbol symbol;
  }
}

'quantity' is eosio::asset type that is a struct with two fields, amount and symbol. You can add or subtract value from amount directly (not recommended), or use asset type for calculation.

For example,

  // not recommended
  quantity.amount -= 100;

  // recommended
  quantity -= asset(100, quantity.symbol);

The reason to recommend the second way is that arithmetic operations between asset type will perform additional check like integer overflow or symbol equality.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.