2015-07-08 22:45:53 +00:00
|
|
|
#include <graphene/chain/protocol/fee_schedule.hpp>
|
|
|
|
|
#include <fc/smart_ref_impl.hpp>
|
2015-07-07 22:46:27 +00:00
|
|
|
|
|
|
|
|
namespace graphene { namespace chain {
|
|
|
|
|
|
2015-07-08 22:45:53 +00:00
|
|
|
typedef fc::smart_ref<fee_schedule> smart_fee_schedule;
|
|
|
|
|
|
2015-07-07 22:46:27 +00:00
|
|
|
fee_schedule::fee_schedule()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fee_schedule fee_schedule::get_default()
|
|
|
|
|
{
|
|
|
|
|
fee_schedule result;
|
2015-07-08 22:45:53 +00:00
|
|
|
for( uint32_t i = 0; i < fee_parameters().count(); ++i )
|
2015-07-07 22:46:27 +00:00
|
|
|
{
|
|
|
|
|
fee_parameters x; x.set_which(i);
|
|
|
|
|
result.parameters.insert(x);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct fee_schedule_validate_visitor
|
|
|
|
|
{
|
2015-07-08 22:45:53 +00:00
|
|
|
typedef void result_type;
|
2015-07-07 22:46:27 +00:00
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
void operator()( const T& p )const
|
|
|
|
|
{
|
2015-07-08 22:45:53 +00:00
|
|
|
//p.validate();
|
2015-07-07 22:46:27 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void fee_schedule::validate()const
|
|
|
|
|
{
|
|
|
|
|
for( const auto& f : parameters )
|
|
|
|
|
f.visit( fee_schedule_validate_visitor() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct calc_fee_visitor
|
|
|
|
|
{
|
2015-07-08 22:45:53 +00:00
|
|
|
typedef uint64_t result_type;
|
2015-07-07 22:46:27 +00:00
|
|
|
|
|
|
|
|
const fee_parameters& param;
|
|
|
|
|
calc_fee_visitor( const fee_parameters& p ):param(p){}
|
|
|
|
|
|
|
|
|
|
template<typename OpType>
|
2015-07-08 22:45:53 +00:00
|
|
|
result_type operator()( const OpType& op )const
|
2015-07-07 22:46:27 +00:00
|
|
|
{
|
2015-07-08 22:45:53 +00:00
|
|
|
return op.calculate_fee( param.get<typename OpType::fee_parameters_type>() ).value;
|
2015-07-07 22:46:27 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-07-08 22:45:53 +00:00
|
|
|
struct set_fee_visitor
|
|
|
|
|
{
|
|
|
|
|
typedef void result_type;
|
|
|
|
|
asset _fee;
|
|
|
|
|
|
|
|
|
|
set_fee_visitor( asset f ):_fee(f){}
|
|
|
|
|
|
|
|
|
|
template<typename OpType>
|
|
|
|
|
void operator()( OpType& op )const
|
|
|
|
|
{
|
|
|
|
|
op.fee = _fee;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct zero_fee_visitor
|
|
|
|
|
{
|
|
|
|
|
typedef void result_type;
|
|
|
|
|
|
|
|
|
|
template<typename ParamType>
|
|
|
|
|
result_type operator()( ParamType& op )const
|
|
|
|
|
{
|
2015-07-09 14:15:53 +00:00
|
|
|
memset( (char*)&op, 0, sizeof(op) );
|
2015-07-08 22:45:53 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void fee_schedule::zero_all_fees()
|
|
|
|
|
{
|
|
|
|
|
*this = get_default();
|
2015-07-09 14:15:53 +00:00
|
|
|
for( fee_parameters& i : parameters )
|
2015-07-08 22:45:53 +00:00
|
|
|
i.visit( zero_fee_visitor() );
|
2015-07-09 15:40:37 +00:00
|
|
|
this->scale = 0;
|
2015-07-08 22:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
2015-07-07 22:46:27 +00:00
|
|
|
asset fee_schedule::calculate_fee( const operation& op, const price& core_exchange_rate )const
|
|
|
|
|
{
|
2015-07-09 14:15:53 +00:00
|
|
|
//idump( (op)(core_exchange_rate) );
|
2015-07-07 22:46:27 +00:00
|
|
|
fee_parameters params; params.set_which(op.which());
|
|
|
|
|
auto itr = parameters.find(params);
|
|
|
|
|
if( itr != parameters.end() ) params = *itr;
|
2015-07-08 22:45:53 +00:00
|
|
|
auto base_value = op.visit( calc_fee_visitor( params ) );
|
|
|
|
|
auto scaled = fc::uint128(base_value) * scale;
|
2015-07-07 22:46:27 +00:00
|
|
|
scaled /= GRAPHENE_100_PERCENT;
|
|
|
|
|
FC_ASSERT( scaled <= GRAPHENE_MAX_SHARE_SUPPLY );
|
|
|
|
|
auto result = asset( scaled.to_uint64(), 0 ) * core_exchange_rate;
|
|
|
|
|
FC_ASSERT( result.amount <= GRAPHENE_MAX_SHARE_SUPPLY );
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-08 22:45:53 +00:00
|
|
|
asset fee_schedule::set_fee( operation& op, const price& core_exchange_rate )const
|
|
|
|
|
{
|
|
|
|
|
auto f = calculate_fee( op, core_exchange_rate );
|
|
|
|
|
op.visit( set_fee_visitor( f ) );
|
|
|
|
|
return f;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 22:46:27 +00:00
|
|
|
} } // graphene::chain
|