提交 016636ae 编写于 作者: W Wang Zhi

hddpool contract: code formatting

上级 098ae99f
......@@ -21,10 +21,10 @@ const uint32_t seconds_in_one_day = minutes_in_one_day * 60;
//const uint32_t seconds_in_one_week = seconds_in_one_day * 7;
const uint32_t seconds_in_one_year = seconds_in_one_day * 365;
const uint32_t fee_cycle = seconds_in_one_day ; //计费周期(秒为单位)
const uint32_t fee_cycle = seconds_in_one_day; //计费周期(秒为单位)
const uint32_t one_gb = 1024*1024*1024; //1GB
const uint32_t data_slice_size = 16*1024; // among 4k-32k,set it as 16k
const uint32_t one_gb = 1024 * 1024 * 1024; //1GB
const uint32_t data_slice_size = 16 * 1024; // among 4k-32k,set it as 16k
static constexpr eosio::name HDD_OFFICIAL{N(hddofficial)};
......@@ -32,68 +32,70 @@ static constexpr eosio::name active_permission{N(active)};
static constexpr eosio::name token_account{N(eosio.token)};
static constexpr eosio::name hdd_account{N(hddpool12345)};
#define HDD_SYMBOL_BANCOR S(4,HDD)
#define HDDCORE_SYMBOL_BANCOR S(4,HDDCORE)
#define HDD_SYMBOL_BANCOR S(4, HDD)
#define HDDCORE_SYMBOL_BANCOR S(4, HDDCORE)
const int64_t inc_hdd_amount = 1000000000;
const int64_t price_delta = 1;
hddpool::hddpool( account_name s)
:contract(s),
_global(_self, _self),
_global2(_self, _self),
_global3(_self, _self),
_ghddprice(_self, _self),
_hmarket(_self, _self)
hddpool::hddpool(account_name s)
: contract(s),
_global(_self, _self),
_global2(_self, _self),
_global3(_self, _self),
_ghddprice(_self, _self),
_hmarket(_self, _self)
{
if(_global.exists())
if (_global.exists())
_gstate = _global.get();
else
else
_gstate = hdd_global_state{};
if(_global2.exists())
if (_global2.exists())
_gstate2 = _global2.get();
else
else
_gstate2 = hdd_global_state2{};
if(_global3.exists())
if (_global3.exists())
_gstate3 = _global3.get();
else
else
_gstate3 = hdd_global_state3{};
if(_ghddprice.exists())
if (_ghddprice.exists())
_ghddpriceState = _ghddprice.get();
else
_ghddpriceState = hdd_global_price{};
auto itr = _hmarket.find(HDDCORE_SYMBOL_BANCOR);
auto itr = _hmarket.find(HDDCORE_SYMBOL_BANCOR);
//print( "check hdd market\n" );
//print( "check hdd market\n" );
if( itr == _hmarket.end() ) {
auto system_token_supply = eosio::token(N(eosio.token)).get_supply(eosio::symbol_type(CORE_SYMBOL).name()).amount;
if( system_token_supply > 0 ) {
itr = _hmarket.emplace( _self, [&]( auto& m ) {
m.supply.amount = 100000000000000ll;
m.supply.symbol = HDDCORE_SYMBOL_BANCOR;
//m.base.balance.amount = 400000000000000000ll;
m.base.balance.amount = 40000000000000ll / 1000;
m.base.weight = 0.35;
m.base.balance.symbol = HDD_SYMBOL_BANCOR;;
m.quote.balance.amount = system_token_supply / 1000;
m.quote.balance.symbol = CORE_SYMBOL;
m.quote.weight = 0.5;
});
}
} else {
//print( "hdd market already created\n" );
//_hmarket.erase(_hmarket.begin());
if (itr == _hmarket.end())
{
auto system_token_supply = eosio::token(N(eosio.token)).get_supply(eosio::symbol_type(CORE_SYMBOL).name()).amount;
if (system_token_supply > 0)
{
itr = _hmarket.emplace(_self, [&](auto &m) {
m.supply.amount = 100000000000000ll;
m.supply.symbol = HDDCORE_SYMBOL_BANCOR;
//m.base.balance.amount = 400000000000000000ll;
m.base.balance.amount = 40000000000000ll / 1000;
m.base.weight = 0.35;
m.base.balance.symbol = HDD_SYMBOL_BANCOR;
;
m.quote.balance.amount = system_token_supply / 1000;
m.quote.balance.symbol = CORE_SYMBOL;
m.quote.weight = 0.5;
});
}
}
else
{
//print( "hdd market already created\n" );
//_hmarket.erase(_hmarket.begin());
}
}
hddpool::~hddpool()
......@@ -104,7 +106,6 @@ hddpool::~hddpool()
_ghddprice.set(_ghddpriceState, _self);
}
void hddpool::getbalance(name user)
{
//require_auth( user );
......@@ -114,7 +115,8 @@ void hddpool::getbalance(name user)
//userhdd_index _userhdd(_self, _self);
userhdd_index _userhdd(_self, user.value);
auto it = _userhdd.find(user.value);
if(it == _userhdd.end()){
if (it == _userhdd.end())
{
_userhdd.emplace(_self, [&](auto &row) {
row.account_name = user;
row.hdd_balance = inc_hdd_amount;
......@@ -125,47 +127,46 @@ void hddpool::getbalance(name user)
_gstate2.hdd_total_user += 1;
});
print("{\"balance\":" , inc_hdd_amount, "}");
print("{\"balance\":", inc_hdd_amount, "}");
}
else {
else
{
_userhdd.modify(it, _self, [&](auto &row) {
uint64_t tmp_t = current_time();
int64_t tmp_last_balance = it->hdd_balance;
int64_t new_balance;
if(calculate_balance(tmp_last_balance, it->hdd_per_cycle_fee, it->hdd_per_cycle_profit, it->last_hdd_time, tmp_t, new_balance)) {
if (calculate_balance(tmp_last_balance, it->hdd_per_cycle_fee, it->hdd_per_cycle_profit, it->last_hdd_time, tmp_t, new_balance))
{
row.hdd_balance = new_balance;
row.last_hdd_time = tmp_t;
row.last_hdd_time = tmp_t;
}
//计算该账户下所有矿机的收益
maccount_index _maccount(_self, user.value);
for(auto it_m = _maccount.begin(); it_m != _maccount.end(); it_m++) {
for (auto it_m = _maccount.begin(); it_m != _maccount.end(); it_m++)
{
int64_t balance_delta_m = 0;
_maccount.modify(it_m, _self, [&](auto &row_m) {
uint64_t tmp_t_m = current_time();
int64_t tmp_last_balance_m = it_m->hdd_balance;
int64_t new_balance_m = 0;
if(calculate_balance(tmp_last_balance_m, 0, it_m->hdd_per_cycle_profit, it_m->last_hdd_time, tmp_t_m, new_balance_m)) {
if (calculate_balance(tmp_last_balance_m, 0, it_m->hdd_per_cycle_profit, it_m->last_hdd_time, tmp_t_m, new_balance_m))
{
balance_delta_m = new_balance_m - row_m.hdd_balance;
row_m.hdd_balance = new_balance_m;
row_m.last_hdd_time = tmp_t;
}
});
row.hdd_balance += balance_delta_m;
row.hdd_balance += balance_delta_m;
}
print("{\"balance\":" , it->hdd_balance, "}");
print("{\"balance\":", it->hdd_balance, "}");
});
}
}
}
bool hddpool::calculate_balance(int64_t oldbalance, int64_t hdd_per_cycle_fee, int64_t hdd_per_cycle_profit, uint64_t last_hdd_time, uint64_t current_time, int64_t &new_balance) {
bool hddpool::calculate_balance(int64_t oldbalance, int64_t hdd_per_cycle_fee, int64_t hdd_per_cycle_profit, uint64_t last_hdd_time, uint64_t current_time, int64_t &new_balance)
{
uint64_t slot_t = (current_time - last_hdd_time)/1000000ll; //convert to seconds
uint64_t slot_t = (current_time - last_hdd_time) / 1000000ll; //convert to seconds
new_balance = 0;
//print( "oldbalance: ", oldbalance, "\n" );
//print("hdd_per_cycle_fee:", hdd_per_cycle_fee, "\n");
......@@ -173,32 +174,32 @@ bool hddpool::calculate_balance(int64_t oldbalance, int64_t hdd_per_cycle_fee, i
//print( "slot_t: ", slot_t, "\n" );
//print( "fee_cycle: ", fee_cycle, "\n" );
double tick = (double)((double)slot_t/fee_cycle);
double tick = (double)((double)slot_t / fee_cycle);
new_balance = oldbalance;
int64_t delta = (int64_t)(tick * (hdd_per_cycle_profit - hdd_per_cycle_fee ));
if(delta < 0 && oldbalance <= -delta)
int64_t delta = (int64_t)(tick * (hdd_per_cycle_profit - hdd_per_cycle_fee));
if (delta < 0 && oldbalance <= -delta)
delta = -oldbalance;
new_balance += delta;
//print( "new_balance: ", new_balance, "\n" );
update_total_hdd_balance( delta );
update_total_hdd_balance(delta);
return true;
}
void hddpool::update_total_hdd_balance( int64_t _balance_delta ) {
void hddpool::update_total_hdd_balance(int64_t _balance_delta)
{
_gstate.hdd_total_balance += _balance_delta;
if( _gstate.hdd_total_balance < 0 )
if (_gstate.hdd_total_balance < 0)
_gstate.hdd_total_balance = 0;
}
void hddpool::update_hddofficial( const int64_t _balance,
const int64_t _fee, const int64_t _profit,
const int64_t _space )
void hddpool::update_hddofficial(const int64_t _balance,
const int64_t _fee, const int64_t _profit,
const int64_t _space)
{
return;
......@@ -206,54 +207,56 @@ void hddpool::update_hddofficial( const int64_t _balance,
userhdd_index _hbalance(_self, HDD_OFFICIAL.value);
auto hbalance_itr = _hbalance.find(HDD_OFFICIAL.value);
if(hbalance_itr == _hbalance.end()) {
if (hbalance_itr == _hbalance.end())
{
_hbalance.emplace(_self, [&](auto &row) {
row.account_name = HDD_OFFICIAL;
row.hdd_balance = inc_hdd_amount;
row.hdd_per_cycle_fee = _fee;
row.hdd_per_cycle_profit = _profit;
if(_space >= 0)
if (_space >= 0)
row.hdd_space = (uint64_t)_space;
else
row.hdd_space = 0;
row.last_hdd_time = current_time();
});
} else {
}
else
{
_hbalance.modify(hbalance_itr, _self, [&](auto &row) {
//todo check overflow and time cycle
row.hdd_balance += _balance;
if(_fee != 0 || _profit != 0) {
if (_fee != 0 || _profit != 0)
{
uint64_t tmp_t = current_time();
int64_t tmp_last_balance = hbalance_itr->hdd_balance;
int64_t new_balance;
if(calculate_balance(tmp_last_balance, hbalance_itr->hdd_per_cycle_fee, hbalance_itr->hdd_per_cycle_profit, hbalance_itr->last_hdd_time, tmp_t, new_balance)) {
if (calculate_balance(tmp_last_balance, hbalance_itr->hdd_per_cycle_fee, hbalance_itr->hdd_per_cycle_profit, hbalance_itr->last_hdd_time, tmp_t, new_balance))
{
row.hdd_balance = new_balance;
row.last_hdd_time = tmp_t;
row.last_hdd_time = tmp_t;
}
}
}
row.hdd_per_cycle_fee += _fee;
row.hdd_per_cycle_profit += _profit;
if(_space >= 0)
if (_space >= 0)
row.hdd_space += (uint64_t)_space;
else
row.hdd_space -= (uint64_t)(-_space);
row.hdd_space -= (uint64_t)(-_space);
});
}
}
}
void hddpool::buyhdd( name user , asset quant)
void hddpool::buyhdd(name user, asset quant)
{
require_auth( user );
require_auth(user);
//auto system_token_supply = eosio::token(N(eosio.token)).get_supply(eosio::symbol_type(CORE_SYMBOL).name()).amount;
//print( "quant.amount: ", system_token_supply , "\n" );
//return;
eosio_assert(is_account(user), "user not a account");
eosio_assert(is_account(hdd_account), "to not a account");
eosio_assert(quant.is_valid(), "asset is invalid");
......@@ -262,23 +265,23 @@ void hddpool::buyhdd( name user , asset quant)
//print( "quant.amount: ", quant.amount , "\n" );
action(
permission_level{user, active_permission},
token_account, N(transfer),
std::make_tuple(user, hdd_account, quant, std::string("buy hdd"))
).send();
permission_level{user, active_permission},
token_account, N(transfer),
std::make_tuple(user, hdd_account, quant, std::string("buy hdd")))
.send();
int64_t _hdd_amount = quant.amount * 10000;
const auto& market = _hmarket.get(HDDCORE_SYMBOL_BANCOR, "hdd market does not exist");
_hmarket.modify( market, 0, [&]( auto& es ) {
_hdd_amount = (es.convert( quant, HDD_SYMBOL_BANCOR).amount) * 10000;
const auto &market = _hmarket.get(HDDCORE_SYMBOL_BANCOR, "hdd market does not exist");
_hmarket.modify(market, 0, [&](auto &es) {
_hdd_amount = (es.convert(quant, HDD_SYMBOL_BANCOR).amount) * 10000;
});
print("_hdd_amount: ", _hdd_amount, "\n");
//userhdd_index _userhdd(_self, _self);
userhdd_index _userhdd(_self, user.value);
auto it = _userhdd.find(user.value);
if(it == _userhdd.end()){
if (it == _userhdd.end())
{
_userhdd.emplace(_self, [&](auto &row) {
row.account_name = user;
row.hdd_balance = _hdd_amount;
......@@ -289,54 +292,53 @@ void hddpool::buyhdd( name user , asset quant)
row.last_hdd_time = current_time();
});
}
else {
else
{
_userhdd.modify(it, _self, [&](auto &row) {
row.hdd_balance += _hdd_amount;
//row.hdd_balance += inc_hdd_amount;
});
});
}
update_hddofficial(_hdd_amount , 0, 0, 0);
update_hddofficial(_hdd_amount, 0, 0, 0);
//update_hddofficial(inc_hdd_amount , 0, 0, 0);
update_total_hdd_balance(_hdd_amount);
}
void hddpool::sellhdd (name user, int64_t amount)
void hddpool::sellhdd(name user, int64_t amount)
{
require_auth( user );
require_auth(user);
//userhdd_index _userhdd(_self, _self);
userhdd_index _userhdd(_self, user.value);
auto it = _userhdd.find(user.value);
eosio_assert( it != _userhdd.end(), "user not exists in userhdd table." );
eosio_assert( it->hdd_balance >= amount, "hdd overdrawn." );
eosio_assert(it != _userhdd.end(), "user not exists in userhdd table.");
eosio_assert(it->hdd_balance >= amount, "hdd overdrawn.");
_userhdd.modify(it, _self, [&](auto &row) {
row.hdd_balance -= amount;
//row.hdd_balance -= inc_hdd_amount;
});
row.hdd_balance -= amount;
//row.hdd_balance -= inc_hdd_amount;
});
int64_t _yta_amount = (int64_t)((double)amount/10000);
int64_t _yta_amount = (int64_t)((double)amount / 10000);
//int64_t _yta_amount = (int64_t)( (((double)amount/10000)*_ghddpriceState.price)/100 );
//asset tokens_out;
auto itr = _hmarket.find(HDDCORE_SYMBOL_BANCOR);
_hmarket.modify( itr, 0, [&]( auto& es ) {
/// the cast to int64_t of quant is safe because we certify quant is <= quota which is limited by prior purchases
_yta_amount = es.convert( asset(amount/10000, HDD_SYMBOL_BANCOR), CORE_SYMBOL).amount;
_hmarket.modify(itr, 0, [&](auto &es) {
/// the cast to int64_t of quant is safe because we certify quant is <= quota which is limited by prior purchases
_yta_amount = es.convert(asset(amount / 10000, HDD_SYMBOL_BANCOR), CORE_SYMBOL).amount;
});
print("_yta_amount: ", _yta_amount, "\n");
asset quant{_yta_amount, CORE_SYMBOL};
action(
permission_level{hdd_account, active_permission},
token_account, N(transfer),
std::make_tuple(hdd_account, user, quant, std::string("sell hdd"))
).send();
permission_level{hdd_account, active_permission},
token_account, N(transfer),
std::make_tuple(hdd_account, user, quant, std::string("sell hdd")))
.send();
update_hddofficial(-amount , 0, 0, 0);
update_hddofficial(-amount, 0, 0, 0);
//update_hddofficial(-inc_hdd_amount , 0, 0, 0);
update_total_hdd_balance(-amount);
......@@ -433,36 +435,37 @@ void hddpool::sellhdd (name user, int64_t amount)
}
*/
void hddpool::sethfee( name user, int64_t fee)
void hddpool::sethfee(name user, int64_t fee)
{
eosio_assert(is_account(user), "user invalidate");
//userhdd_index _userhdd(_self, _self);
userhdd_index _userhdd(_self, user.value);
auto it = _userhdd.find(user.value);
eosio_assert( it != _userhdd.end(), "user not exists in userhdd table" );
eosio_assert(it != _userhdd.end(), "user not exists in userhdd table");
eosio_assert(fee != it->hdd_per_cycle_fee, " the fee is the same \n");
//每周期费用 <= (占用存储空间*数据分片大小/1GB)*(记账周期/ 1年)
//bool istrue = fee <= (int64_t)(((double)(it->hdd_space * data_slice_size)/(double)one_gb) * ((double)fee_cycle/(double)seconds_in_one_year));
//eosio_assert(istrue , "the fee verification is not right \n");
//eosio_assert(istrue , "the fee verification is not right \n");
int64_t delta_fee = 0;
_userhdd.modify(it, _self, [&](auto &row) {
//设置每周期费用之前可能需要将以前的余额做一次计算,然后更改last_hdd_time
uint64_t tmp_t = current_time();
int64_t tmp_last_balance = it->hdd_balance;
int64_t tmp_last_balance = it->hdd_balance;
int64_t new_balance;
if(calculate_balance(tmp_last_balance, it->hdd_per_cycle_fee, it->hdd_per_cycle_profit, it->last_hdd_time, tmp_t, new_balance)) {
if (calculate_balance(tmp_last_balance, it->hdd_per_cycle_fee, it->hdd_per_cycle_profit, it->last_hdd_time, tmp_t, new_balance))
{
row.hdd_balance = new_balance;
row.last_hdd_time = tmp_t;
}
row.last_hdd_time = tmp_t;
}
delta_fee = fee - it->hdd_per_cycle_fee;
row.hdd_per_cycle_fee = fee;
});
//变更总账户的每周期费用
update_hddofficial(0, delta_fee, 0, 0);
});
//变更总账户的每周期费用
update_hddofficial(0, delta_fee, 0, 0);
}
void hddpool::subbalance ( name user, int64_t balance)
void hddpool::subbalance(name user, int64_t balance)
{
require_auth(user);
......@@ -470,14 +473,14 @@ void hddpool::subbalance ( name user, int64_t balance)
//userhdd_index _userhdd(_self, _self);
userhdd_index _userhdd(_self, user.value);
auto it = _userhdd.find(user.value);
eosio_assert( it != _userhdd.end(), "user not exists in userhdd table" );
eosio_assert(it != _userhdd.end(), "user not exists in userhdd table");
_userhdd.modify(it, _self, [&](auto &row) {
row.hdd_balance -= balance;
row.hdd_balance -= balance;
});
update_hddofficial(-balance, 0, 0, 0);
update_total_hdd_balance(-balance);
}
......@@ -487,11 +490,11 @@ void hddpool::addhspace(name user, uint64_t space)
//userhdd_index _userhdd(_self, _self);
userhdd_index _userhdd(_self, user.value);
auto it = _userhdd.find(user.value);
eosio_assert( it != _userhdd.end(), "user not exists in userhdd table" );
eosio_assert(it != _userhdd.end(), "user not exists in userhdd table");
_userhdd.modify(it, _self, [&](auto &row) {
row.hdd_space += space;
});
row.hdd_space += space;
});
update_hddofficial(0, 0, 0, (int64_t)space);
}
......@@ -502,14 +505,13 @@ void hddpool::subhspace(name user, uint64_t space)
//userhdd_index _userhdd(_self, _self);
userhdd_index _userhdd(_self, user.value);
auto it = _userhdd.find(user.value);
eosio_assert( it != _userhdd.end(), "user not exists in userhdd table" );
eosio_assert(it != _userhdd.end(), "user not exists in userhdd table");
_userhdd.modify(it, _self, [&](auto &row) {
row.hdd_space -= space;
row.hdd_space -= space;
});
update_hddofficial(0, 0, 0, (int64_t)(-space));
}
void hddpool::newmaccount(name owner, uint64_t minerid)
......@@ -517,13 +519,14 @@ void hddpool::newmaccount(name owner, uint64_t minerid)
eosio_assert(is_account(owner), "owner invalidate");
//maccount_index _maccount(_self, _self);
maccount_index _maccount(_self, owner.value);
if (_maccount.begin() == _maccount.end()){
if (_maccount.begin() == _maccount.end())
{
//miner pool inc
_gstate3.hdd_macc_user += 1;
}
auto it = _maccount.find(minerid);
eosio_assert( it == _maccount.end(), "minerid already exist in maccount table \n" );
eosio_assert(it == _maccount.end(), "minerid already exist in maccount table \n");
_maccount.emplace(_self, [&](auto &row) {
row.minerid = minerid;
......@@ -532,13 +535,13 @@ void hddpool::newmaccount(name owner, uint64_t minerid)
row.hdd_per_cycle_profit = 0;
row.hdd_balance = 0;
row.last_hdd_time = current_time();
});
//userhdd_index _userhdd(_self, _self);
userhdd_index _userhdd(_self, owner.value);
auto userhdd_itr = _userhdd.find(owner.value);
if(userhdd_itr == _userhdd.end()) {
if (userhdd_itr == _userhdd.end())
{
_userhdd.emplace(_self, [&](auto &row) {
row.account_name = owner;
row.hdd_balance = inc_hdd_amount;
......@@ -548,8 +551,8 @@ void hddpool::newmaccount(name owner, uint64_t minerid)
row.last_hdd_time = current_time();
_gstate2.hdd_total_user += 1;
});
}
});
}
}
void hddpool::addmprofit(name owner, uint64_t minerid, uint64_t space)
......@@ -557,21 +560,21 @@ void hddpool::addmprofit(name owner, uint64_t minerid, uint64_t space)
//maccount_index _maccount(_self, _self);
maccount_index _maccount(_self, owner.value);
auto it = _maccount.find(minerid);
eosio_assert( it != _maccount.end(), "minerid not register \n" );
eosio_assert(it != _maccount.end(), "minerid not register \n");
//name owner = it->owner;
int64_t profit_delta = 0;
//每周期收益 += (生产空间*数据分片大小/1GB)*(记账周期/ 1年)
profit_delta = (int64_t)( ((double)(space*data_slice_size)/(double)one_gb) * ((double)fee_cycle / (double)seconds_in_one_year) * 100000000 );
profit_delta = (int64_t)(((double)(space * data_slice_size) / (double)one_gb) * ((double)fee_cycle / (double)seconds_in_one_year) * 100000000);
int64_t balance_delta = 0;
_maccount.modify(it, _self, [&](auto &row) {
uint64_t tmp_t = current_time();
int64_t tmp_last_balance = it->hdd_balance;
int64_t new_balance;
if(calculate_balance(tmp_last_balance, 0, it->hdd_per_cycle_profit, it->last_hdd_time, tmp_t, new_balance)) {
if (calculate_balance(tmp_last_balance, 0, it->hdd_per_cycle_profit, it->last_hdd_time, tmp_t, new_balance))
{
balance_delta = new_balance - row.hdd_balance;
row.hdd_balance = new_balance;
row.last_hdd_time = tmp_t;
......@@ -579,14 +582,12 @@ void hddpool::addmprofit(name owner, uint64_t minerid, uint64_t space)
row.space += space;
row.hdd_per_cycle_profit += profit_delta;
});
});
//userhdd_index _userhdd(_self, _self);
userhdd_index _userhdd(_self, owner.value);
auto userhdd_itr = _userhdd.find(owner.value);
eosio_assert( userhdd_itr != _userhdd.end(), "no owner exists in userhdd table" );
auto userhdd_itr = _userhdd.find(owner.value);
eosio_assert(userhdd_itr != _userhdd.end(), "no owner exists in userhdd table");
_userhdd.modify(userhdd_itr, _self, [&](auto &row) {
uint64_t tmp_t = current_time();
......@@ -598,11 +599,10 @@ void hddpool::addmprofit(name owner, uint64_t minerid, uint64_t space)
row.last_hdd_time = tmp_t;
} */
row.last_hdd_time = tmp_t;
row.last_hdd_time = tmp_t;
row.hdd_balance += balance_delta;
row.hdd_per_cycle_profit = 0;
});
});
//变更总账户的每周期费用
update_hddofficial(0, 0, profit_delta, 0);
......@@ -613,7 +613,7 @@ void hddpool::calcmbalance(name owner, uint64_t minerid)
//maccount_index _maccount(_self, _self);
maccount_index _maccount(_self, owner.value);
auto it = _maccount.find(minerid);
eosio_assert( it != _maccount.end(), "minerid not register \n" );
eosio_assert(it != _maccount.end(), "minerid not register \n");
int64_t balance_delta = 0;
......@@ -621,30 +621,45 @@ void hddpool::calcmbalance(name owner, uint64_t minerid)
uint64_t tmp_t = current_time();
int64_t tmp_last_balance = it->hdd_balance;
int64_t new_balance;
if(calculate_balance(tmp_last_balance, 0, it->hdd_per_cycle_profit, it->last_hdd_time, tmp_t, new_balance)) {
if (calculate_balance(tmp_last_balance, 0, it->hdd_per_cycle_profit, it->last_hdd_time, tmp_t, new_balance))
{
balance_delta = new_balance - row.hdd_balance;
row.hdd_balance = new_balance;
row.last_hdd_time = tmp_t;
}
});
});
//userhdd_index _userhdd(_self, _self);
userhdd_index _userhdd(_self, owner.value);
auto userhdd_itr = _userhdd.find(owner.value);
eosio_assert( userhdd_itr != _userhdd.end(), "no owner exists in userhdd table" );
auto userhdd_itr = _userhdd.find(owner.value);
eosio_assert(userhdd_itr != _userhdd.end(), "no owner exists in userhdd table");
_userhdd.modify(userhdd_itr, _self, [&](auto &row) {
uint64_t tmp_t = current_time();
row.last_hdd_time = tmp_t;
row.last_hdd_time = tmp_t;
row.hdd_balance += balance_delta;
row.hdd_per_cycle_profit = 0;
});
});
}
void hddpool::clearall()
{
require_auth(_self);
/*
if(is_bp_account(N(producer1))) {
print( "procuder1 is bp account\n" );
}
if(is_bp_account(N(producer2))) {
print( "procuder2 is bp account\n" );
}
if(is_bp_account(N(producer4))) {
print( "procuder4 is bp account\n" );
}
if(is_bp_account(N(producer3))) {
print( "procuder3 is bp account\n" );
}
return;
*/
/*
userhdd_index _userhdd( _self , _self );
while (_userhdd.begin() != _userhdd.end())
......@@ -659,7 +674,8 @@ void hddpool::clearall()
//print( "check hdd market\n" );
if( itr != _hmarket.end() ) {
if (itr != _hmarket.end())
{
_hmarket.erase(_hmarket.begin());
}
}
......@@ -667,94 +683,108 @@ void hddpool::clearall()
bool hddpool::is_bp_account(uint64_t uservalue)
{
account_name producers[21];
uint32_t bytes_populated = get_active_producers(producers, sizeof(account_name)*21);
uint32_t count = bytes_populated/sizeof(account_name);
for(uint32_t i = 0 ; i < count; i++ ) {
if(producers[i] == uservalue)
uint32_t bytes_populated = get_active_producers(producers, sizeof(account_name) * 21);
uint32_t count = bytes_populated / sizeof(account_name);
for (uint32_t i = 0; i < count; i++)
{
if (producers[i] == uservalue)
return true;
}
return false;
}
asset exchange_state::convert_to_exchange(connector &c, asset in)
{
asset exchange_state::convert_to_exchange( connector& c, asset in ) {
real_type R(supply.amount);
real_type C(c.balance.amount + in.amount);
real_type F(c.weight / 1000.0);
real_type T(in.amount);
real_type ONE(1.0);
real_type R(supply.amount);
real_type C(c.balance.amount+in.amount);
real_type F(c.weight/1000.0);
real_type T(in.amount);
real_type ONE(1.0);
real_type E = -R * (ONE - std::pow(ONE + T / C, F));
//print( "E: ", E, "\n");
int64_t issued = int64_t(E);
real_type E = -R * (ONE - std::pow( ONE + T / C, F) );
//print( "E: ", E, "\n");
int64_t issued = int64_t(E);
supply.amount += issued;
c.balance.amount += in.amount;
supply.amount += issued;
c.balance.amount += in.amount;
return asset(issued, supply.symbol);
}
return asset( issued, supply.symbol );
}
asset exchange_state::convert_from_exchange(connector &c, asset in)
{
eosio_assert(in.symbol == supply.symbol, "unexpected asset symbol input");
asset exchange_state::convert_from_exchange( connector& c, asset in ) {
eosio_assert( in.symbol== supply.symbol, "unexpected asset symbol input" );
real_type R(supply.amount - in.amount);
real_type C(c.balance.amount);
real_type F(1000.0 / c.weight);
real_type E(in.amount);
real_type ONE(1.0);
real_type R(supply.amount - in.amount);
real_type C(c.balance.amount);
real_type F(1000.0/c.weight);
real_type E(in.amount);
real_type ONE(1.0);
// potentially more accurate:
// The functions std::expm1 and std::log1p are useful for financial calculations, for example,
// when calculating small daily interest rates: (1+x)n
// -1 can be expressed as std::expm1(n * std::log1p(x)).
// real_type T = C * std::expm1( F * std::log1p(E/R) );
real_type T = C * (std::pow(ONE + E / R, F) - ONE);
//print( "T: ", T, "\n");
int64_t out = int64_t(T);
// potentially more accurate:
// The functions std::expm1 and std::log1p are useful for financial calculations, for example,
// when calculating small daily interest rates: (1+x)n
// -1 can be expressed as std::expm1(n * std::log1p(x)).
// real_type T = C * std::expm1( F * std::log1p(E/R) );
real_type T = C * (std::pow( ONE + E/R, F) - ONE);
//print( "T: ", T, "\n");
int64_t out = int64_t(T);
supply.amount -= in.amount;
c.balance.amount -= out;
supply.amount -= in.amount;
c.balance.amount -= out;
return asset(out, c.balance.symbol);
}
return asset( out, c.balance.symbol );
asset exchange_state::convert(asset from, symbol_type to)
{
auto sell_symbol = from.symbol;
auto ex_symbol = supply.symbol;
auto base_symbol = base.balance.symbol;
auto quote_symbol = quote.balance.symbol;
//print( "From: ", from, " TO ", asset( 0,to), "\n" );
//print( "base: ", base_symbol, "\n" );
//print( "quote: ", quote_symbol, "\n" );
//print( "ex: ", supply.symbol, "\n" );
if (sell_symbol != ex_symbol)
{
if (sell_symbol == base_symbol)
{
from = convert_to_exchange(base, from);
}
else if (sell_symbol == quote_symbol)
{
from = convert_to_exchange(quote, from);
}
else
{
eosio_assert(false, "invalid sell");
}
}
asset exchange_state::convert( asset from, symbol_type to ) {
auto sell_symbol = from.symbol;
auto ex_symbol = supply.symbol;
auto base_symbol = base.balance.symbol;
auto quote_symbol = quote.balance.symbol;
//print( "From: ", from, " TO ", asset( 0,to), "\n" );
//print( "base: ", base_symbol, "\n" );
//print( "quote: ", quote_symbol, "\n" );
//print( "ex: ", supply.symbol, "\n" );
if( sell_symbol != ex_symbol ) {
if( sell_symbol == base_symbol ) {
from = convert_to_exchange( base, from );
} else if( sell_symbol == quote_symbol ) {
from = convert_to_exchange( quote, from );
} else {
eosio_assert( false, "invalid sell" );
}
} else {
if( to == base_symbol ) {
from = convert_from_exchange( base, from );
} else if( to == quote_symbol ) {
from = convert_from_exchange( quote, from );
} else {
eosio_assert( false, "invalid conversion" );
}
else
{
if (to == base_symbol)
{
from = convert_from_exchange(base, from);
}
else if (to == quote_symbol)
{
from = convert_from_exchange(quote, from);
}
else
{
eosio_assert(false, "invalid conversion");
}
if( to != from.symbol )
return convert( from, to );
return from;
}
if (to != from.symbol)
return convert(from, to);
return from;
}
EOSIO_ABI( hddpool, (getbalance)(buyhdd)(sellhdd)(sethfee)(subbalance)(addhspace)(subhspace)(newmaccount)(addmprofit)(clearall)(calcmbalance))
EOSIO_ABI(hddpool, (getbalance)(buyhdd)(sellhdd)(sethfee)(subbalance)(addhspace)(subhspace)(newmaccount)(addmprofit)(clearall)(calcmbalance))
......@@ -3,142 +3,145 @@
#include <eosiolib/time.hpp>
#include <eosiolib/singleton.hpp>
using eosio::contract;
using eosio::name;
using eosio::asset;
using eosio::symbol_type;
using eosio::indexed_by;
using eosio::const_mem_fun;
using eosio::microseconds;
using eosio::contract;
using eosio::datastream;
using eosio::indexed_by;
using eosio::microseconds;
using eosio::multi_index;
using eosio::name;
using eosio::symbol_type;
typedef double real_type;
//copy from eosio.system
//copy from eosio.system
/**
* Uses Bancor math to create a 50/50 relay between two asset types. The state of the
* bancor exchange is entirely contained within this struct. There are no external
* side effects associated with using this API.
*/
struct exchange_state {
asset supply;
struct exchange_state
{
asset supply;
struct connector {
asset balance;
double weight = .5;
struct connector
{
asset balance;
double weight = .5;
EOSLIB_SERIALIZE( connector, (balance)(weight) )
};
EOSLIB_SERIALIZE(connector, (balance)(weight))
};
connector base;
connector quote;
connector base;
connector quote;
uint64_t primary_key()const { return supply.symbol; }
uint64_t primary_key() const { return supply.symbol; }
asset convert_to_exchange( connector& c, asset in );
asset convert_from_exchange( connector& c, asset in );
asset convert( asset from, symbol_type to );
asset convert_to_exchange(connector &c, asset in);
asset convert_from_exchange(connector &c, asset in);
asset convert(asset from, symbol_type to);
EOSLIB_SERIALIZE( exchange_state, (supply)(base)(quote) )
};
EOSLIB_SERIALIZE(exchange_state, (supply)(base)(quote))
};
//comment old style declaration
//comment old style declaration
typedef multi_index<N(hmarket), exchange_state> hmarket_table;
class hddpool : public contract {
public:
using contract::contract;
hddpool( account_name s);
~hddpool();
void getbalance( name user );
void buyhdd( name user , asset quant);
//void buyhdd( name user , int64_t amount);
void sellhdd( name user, int64_t amount);
void sethfee( name user, int64_t fee);
void subbalance ( name user, int64_t balance);
void addhspace(name user, uint64_t space);
void subhspace(name user, uint64_t space);
void newmaccount(name owner, uint64_t minerid);
void addmprofit(name owner, uint64_t minerid, uint64_t space);
void clearall();
void calcmbalance(name owner, uint64_t minerid);
private:
struct userhdd {
name account_name; //账户名
int64_t hdd_balance; //余额
int64_t hdd_per_cycle_fee; //每周期费用
int64_t hdd_per_cycle_profit; //每周期收益
uint64_t hdd_space; //占用存储空间
uint64_t last_hdd_time; //上次余额计算时间 microseconds from 1970
uint64_t primary_key() const { return account_name.value; }
};
typedef multi_index<N(userhdd), userhdd> userhdd_index;
struct maccount {
uint64_t minerid; //矿机id
name owner; //拥有矿机的矿主的账户名
uint64_t space; //生产空间
int64_t hdd_per_cycle_profit; //每周期收益
int64_t hdd_balance; //余额
uint64_t last_hdd_time; //上次余额计算时间 microseconds from 1970
uint64_t primary_key() const { return minerid; }
//uint64_t get_owner() const { return owner.value; }
};
typedef multi_index< N(maccount), maccount > maccount_index;
struct hdd_global_state {
int64_t hdd_total_balance = 10000000000;
};
struct hdd_global_state2 {
uint64_t hdd_total_user = 4;
};
struct hdd_global_state3 {
uint64_t hdd_macc_user = 2;
};
struct hdd_global_price {
int64_t price = 70;
};
typedef eosio::singleton<N(hddglobal), hdd_global_state> global_state_singleton;
typedef eosio::singleton<N(gusercount), hdd_global_state2> gusercount_singleton;
typedef eosio::singleton<N(gmacccount), hdd_global_state3> gmacccount_singleton;
typedef eosio::singleton<N(ghddprice), hdd_global_price> ghddprice_singleton;
global_state_singleton _global;
hdd_global_state _gstate;
gusercount_singleton _global2;
hdd_global_state2 _gstate2;
gmacccount_singleton _global3;
hdd_global_state3 _gstate3;
ghddprice_singleton _ghddprice;
hdd_global_price _ghddpriceState;
hmarket_table _hmarket;
bool is_bp_account(uint64_t uservalue);
bool calculate_balance(int64_t oldbalance, int64_t hdd_per_cycle_fee,
int64_t hdd_per_cycle_profit, uint64_t last_hdd_time, uint64_t current_time,
int64_t &new_balance);
void update_hddofficial( const int64_t _balance,
const int64_t _fee, const int64_t _profit,
const int64_t _space );
void update_total_hdd_balance( int64_t _balance_delta );
class hddpool : public contract
{
public:
using contract::contract;
hddpool(account_name s);
~hddpool();
void getbalance(name user);
void buyhdd(name user, asset quant);
//void buyhdd( name user , int64_t amount);
void sellhdd(name user, int64_t amount);
void sethfee(name user, int64_t fee);
void subbalance(name user, int64_t balance);
void addhspace(name user, uint64_t space);
void subhspace(name user, uint64_t space);
void newmaccount(name owner, uint64_t minerid);
void addmprofit(name owner, uint64_t minerid, uint64_t space);
void clearall();
void calcmbalance(name owner, uint64_t minerid);
private:
struct userhdd
{
name account_name; //账户名
int64_t hdd_balance; //余额
int64_t hdd_per_cycle_fee; //每周期费用
int64_t hdd_per_cycle_profit; //每周期收益
uint64_t hdd_space; //占用存储空间
uint64_t last_hdd_time; //上次余额计算时间 microseconds from 1970
uint64_t primary_key() const { return account_name.value; }
};
typedef multi_index<N(userhdd), userhdd> userhdd_index;
struct maccount
{
uint64_t minerid; //矿机id
name owner; //拥有矿机的矿主的账户名
uint64_t space; //生产空间
int64_t hdd_per_cycle_profit; //每周期收益
int64_t hdd_balance; //余额
uint64_t last_hdd_time; //上次余额计算时间 microseconds from 1970
uint64_t primary_key() const { return minerid; }
//uint64_t get_owner() const { return owner.value; }
};
typedef multi_index<N(maccount), maccount> maccount_index;
struct hdd_global_state
{
int64_t hdd_total_balance = 10000000000;
};
struct hdd_global_state2
{
uint64_t hdd_total_user = 4;
};
struct hdd_global_state3
{
uint64_t hdd_macc_user = 2;
};
struct hdd_global_price
{
int64_t price = 70;
};
typedef eosio::singleton<N(hddglobal), hdd_global_state> global_state_singleton;
typedef eosio::singleton<N(gusercount), hdd_global_state2> gusercount_singleton;
typedef eosio::singleton<N(gmacccount), hdd_global_state3> gmacccount_singleton;
typedef eosio::singleton<N(ghddprice), hdd_global_price> ghddprice_singleton;
global_state_singleton _global;
hdd_global_state _gstate;
gusercount_singleton _global2;
hdd_global_state2 _gstate2;
gmacccount_singleton _global3;
hdd_global_state3 _gstate3;
ghddprice_singleton _ghddprice;
hdd_global_price _ghddpriceState;
hmarket_table _hmarket;
bool is_bp_account(uint64_t uservalue);
bool calculate_balance(int64_t oldbalance, int64_t hdd_per_cycle_fee,
int64_t hdd_per_cycle_profit, uint64_t last_hdd_time, uint64_t current_time,
int64_t &new_balance);
void update_hddofficial(const int64_t _balance,
const int64_t _fee, const int64_t _profit,
const int64_t _space);
void update_total_hdd_balance(int64_t _balance_delta);
};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册