提交 1d1ac19c 编写于 作者: W Wang Zhi

update hdddata for build problem

update hdddata for build problem
上级 217f0d03
file(GLOB ABI_FILES "*.abi")
configure_file("${ABI_FILES}" "${CMAKE_CURRENT_BINARY_DIR}" COPYONLY)
add_wast_executable(TARGET hdddata
INCLUDE_FOLDERS ${STANDARD_INCLUDE_FOLDERS}
LIBRARIES libc++ libc eosiolib eosio.token
DESTINATION_FOLDER ${CMAKE_CURRENT_BINARY_DIR}
)
{
"____comment": "This file was generated with eosio-abigen. DO NOT EDIT Wed Apr 17 02:07:54 2019",
"version": "eosio::abi/1.1",
"version": "eosio::abi/1.0",
"structs": [
{
"name": "addhspace",
......@@ -129,16 +128,6 @@
}
]
},
{
"name": "init",
"base": "",
"fields": [
{
"name": "owner",
"type": "name"
}
]
},
{
"name": "maccount",
"base": "",
......@@ -177,7 +166,7 @@
},
{
"name": "producer_key",
"type": "public_key"
"type": "name"
}
]
},
......@@ -269,11 +258,6 @@
"type": "gethsum",
"ricardian_contract": ""
},
{
"name": "init",
"type": "init",
"ricardian_contract": ""
},
{
"name": "newmaccount",
"type": "newmaccount",
......@@ -331,6 +315,5 @@
}
],
"ricardian_clauses": [],
"variants": [],
"abi_extensions": []
}
\ No newline at end of file
......@@ -21,24 +21,56 @@ const int64_t useconds_per_day = 24 * 3600 * int64_t(1000000);
const uint32_t one_gb = 1024*1024*1024; //1GB
const uint32_t data_slice_size = 8*1024; // among 4k-32k,set it as 8k
const uint32_t preprocure_space = one_gb; // set as 1G
const name HDD_OFFICIAL = "hddofficial"_n;
const name HDD_OFFICIAL = name{N(hddofficial)};
// constructor
hdddata::hdddata( name s, name code, datastream<const char*> ds )
: eosio::contract(s, code, ds),
_maccount(_self, _self.value),
_hmarket(_self, _self.value),
_producer(_self, _self.value) {
hdddata::hdddata( account_name s )
: eosio::contract(s),
_maccount(_self, _self),
_hmarket(_self, _self),
_producer(_self, _self) {
auto itr = _hmarket.find(S(4,HDDCORE));
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 = S(4,HDDCORE);
m.base.balance.amount = 1024ll*1024*1024*1024*1024;
m.base.balance.symbol = S(0,HDD);
m.quote.balance.amount = system_token_supply / 1000;
m.quote.balance.symbol = CORE_SYMBOL;
});
}
} else {
//print( "ram market already created" );
}
}
/*
hdddata::hdddata( account_name s )
: contract(s)
{
}
*/
hdddata:: ~hdddata() {
}
/*
symbol hdddata::core_symbol()const {
const static auto sym = get_core_symbol( _hmarket );
return sym;
}
*/
/*
//@abit action
void hdddata::init(name owner) {
require_auth( owner );
......@@ -77,10 +109,12 @@ void hdddata::init(name owner) {
});
}
*/
//@abi action
void hdddata::gethbalance(name owner) {
require_auth(owner);
hbalance_table _hbalance(_self, _self.value);
hbalance_table _hbalance(_self, _self);
auto hbalance_itr = _hbalance.find(owner.value);
if(hbalance_itr == _hbalance.end()) {
_hbalance.emplace(owner, [&](auto &row) {
......@@ -114,6 +148,7 @@ void hdddata::gethbalance(name owner) {
}
void hdddata::update_hddofficial(hbalance_table& _hbalance, const uint64_t _hb, const uint64_t time) {
auto hbalance_itr = _hbalance.find(HDD_OFFICIAL.value);
eosio_assert( hbalance_itr != _hbalance.end(), "no HDD_OFFICIAL exists in hbalance table" );
......@@ -126,7 +161,7 @@ void hdddata::update_hddofficial(hbalance_table& _hbalance, const uint64_t _hb,
void hdddata::gethsum() {
require_auth(_self);
hbalance_table _hbalance(_self, _self.value);
hbalance_table _hbalance(_self, _self);
auto hbalance_itr = _hbalance.find(HDD_OFFICIAL.value);
eosio_assert( hbalance_itr != _hbalance.end(), "no HDD_OFFICIAL exists in hbalance table" );
//todo check the 1st time insert
......@@ -172,7 +207,7 @@ void hdddata::sethfee(name owner, uint64_t fee) {
void hdddata::subhbalance(name owner, uint64_t balance){
require_auth(_self);
require_auth(owner);
hbalance_table _hbalance(_self, _self.value);
hbalance_table _hbalance(_self, _self);
auto hbalance_itr = _hbalance.find(owner.value);
eosio_assert( hbalance_itr != _hbalance.end(), "no owner exists in _hbalance table" );
......@@ -188,7 +223,7 @@ void hdddata::subhbalance(name owner, uint64_t balance){
void hdddata::addhspace(name owner, name hddaccount, uint64_t space){
require_auth(owner);
require_auth(hddaccount);
hbalance_table _hbalance(_self, _self.value);
hbalance_table _hbalance(_self, _self);
auto hbalance_itr = _hbalance.find(hddaccount.value);
eosio_assert( hbalance_itr != _hbalance.end(), "no owner exists in _hbalance table" );
......@@ -204,7 +239,7 @@ void hdddata::addhspace(name owner, name hddaccount, uint64_t space){
void hdddata::subhspace(name owner, name hddaccount, uint64_t space){
require_auth(owner);
require_auth(hddaccount);
hbalance_table _hbalance(_self, _self.value);
hbalance_table _hbalance(_self, _self);
auto hbalance_itr = _hbalance.find(hddaccount.value);
eosio_assert( hbalance_itr != _hbalance.end(), "no owner exists in _hbalance table" );
......@@ -220,6 +255,7 @@ void hdddata::subhspace(name owner, name hddaccount, uint64_t space){
//@abi action
void hdddata::newmaccount(name mname, name owner) {
require_auth(mname);
//maccount_table _maccount(_self, _self.value);
auto maccount_itr = _maccount.find(mname.value);
eosio_assert( maccount_itr == _maccount.end(), "owner already exist in _maccount table \n" );
......@@ -230,7 +266,7 @@ void hdddata::newmaccount(name mname, name owner) {
});
require_auth(owner);
hbalance_table _hbalance(_self, _self.value);
hbalance_table _hbalance(_self, _self);
auto hbalance_itr = _hbalance.find(owner.value);
eosio_assert( hbalance_itr == _hbalance.end(), "no owner exists in _hbalance table" );
......@@ -244,16 +280,18 @@ void hdddata::newmaccount(name mname, name owner) {
row.hdd_space=0;
row.last_hdd_time=current_time();
});
}
//@abi action
void hdddata::addmprofit(name mname, uint64_t space){
require_auth(mname);
//maccount_table _maccount(_self, _self.value);
auto maccount_itr = _maccount.find(mname.value);
eosio_assert( maccount_itr != _maccount.end(), "no owner exists in _hbalance table" );
hbalance_table _hbalance(_self, _self.value);
hbalance_table _hbalance(_self, _self);
auto owner_id = maccount_itr->get_owner();
auto hbalance_itr = _hbalance.find(owner_id);
......@@ -268,27 +306,30 @@ void hdddata::addmprofit(name mname, uint64_t space){
row.hdd_space += data_slice_size;
row.hdd_per_cycle_profit += (preprocure_space*data_slice_size/one_gb);
});
//每周期收益 += (预采购空间*数据分片大小/1GB)*(记账周期/ 1年)
}
//@abi action
void hdddata::buyhdd(name buyer, name receiver, asset quant) {
require_auth(buyer);
eosio_assert( quant.amount > 0, "must purchase a positive amount" );
INLINE_ACTION_SENDER(eosio::token, transfer)( token_account, {buyer,active_permission},
{ buyer, hdd_account, quant, std::string("buy hdd") } );
INLINE_ACTION_SENDER(eosio::token, transfer)( N(eosio.token), {buyer,N(active)},
{ buyer, N(eosio.hdd), quant, std::string("buy hdd") } );
int64_t bytes_out;
const auto& market = _hmarket.get(hddcore_symbol.raw(), "hdd market does not exist");
_hmarket.modify( market, same_payer, [&]( auto& es ) {
bytes_out = es.convert( quant, hdd_symbol).amount;
const auto& market = _hmarket.get(S(4,HDDCORE), "hdd market does not exist");
_hmarket.modify( market, 0, [&]( auto& es ) {
bytes_out = es.convert( quant, S(0,HDD)).amount;
});
print("bytes_out: ", bytes_out, "\n");
eosio_assert( bytes_out > 0, "must reserve a positive amount" );
hbalance_table _hbalance(_self, _self.value);
hbalance_table _hbalance(_self, _self);
auto res_itr = _hbalance.find( receiver.value );
if( res_itr == _hbalance.end() ) {
res_itr = _hbalance.emplace( receiver, [&]( auto& res ) {
......@@ -302,13 +343,15 @@ void hdddata::buyhdd(name buyer, name receiver, asset quant) {
res.last_hdd_time = current_time();
});
}
}
//@abi action
void hdddata::sellhdd(name account, uint64_t quant){
require_auth(account);
eosio_assert( quant > 0, "cannot sell negative hdd" );
hbalance_table _hbalance(_self, _self.value);
hbalance_table _hbalance(_self, _self);
auto res_itr = _hbalance.find( account.value );
eosio_assert( res_itr != _hbalance.end(), "no resource row" );
......@@ -316,10 +359,10 @@ void hdddata::sellhdd(name account, uint64_t quant){
eosio_assert( res_itr->get_last_hdd_balance() >= quant, "insufficient hdd" );
asset tokens_out;
auto itr = _hmarket.find(hddcore_symbol.raw());
_hmarket.modify( itr, same_payer, [&]( auto& es ) {
auto itr = _hmarket.find(S(4,HDDCORE));
_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
tokens_out = es.convert( asset(quant, hdd_symbol), core_symbol());
tokens_out = es.convert( asset(quant, S(0,HDD)), CORE_SYMBOL);
});
_hbalance.modify( res_itr, account, [&]( auto& res ) {
......@@ -327,98 +370,103 @@ void hdddata::sellhdd(name account, uint64_t quant){
res.last_hdd_time = current_time();
});
INLINE_ACTION_SENDER(eosio::token, transfer)(
token_account, { {hdd_account, active_permission}, {account, active_permission} },
{ hdd_account, account, asset(tokens_out), std::string("sell ram") }
);
INLINE_ACTION_SENDER(eosio::token, transfer)( N(eosio.token), {N(eosio.hdd),N(active)},
{ N(eosio.hdd), account, asset(tokens_out), std::string("sell hdd") } );
}
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);
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) );
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(1.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);
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, const symbol& to ) {
auto sell_symbol = from.symbol;
auto ex_symbol = supply.symbol;
auto base_symbol = base.balance.symbol;
auto quote_symbol = quote.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("sell_symbol:", sell_symbol, "\n");
print( "base: ", base_symbol, "\n" );
print( "quote: ", quote_symbol, "\n" );
print( "ex_symbol: ", supply.symbol, "\n" );
//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 ) {
if( sell_symbol != ex_symbol ) {
if( sell_symbol == base_symbol ) {
from = convert_to_exchange( base, from );
} else if( sell_symbol == quote_symbol ) {
} else if( sell_symbol == quote_symbol ) {
from = convert_to_exchange( quote, from );
} else {
} else {
eosio_assert( false, "invalid sell" );
}
} else {
if( to == base_symbol ) {
}
} else {
if( to == base_symbol ) {
from = convert_from_exchange( base, from );
} else if( to == quote_symbol ) {
} else if( to == quote_symbol ) {
from = convert_from_exchange( quote, from );
} else {
} 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( hdddata, (gethbalance)(gethsum)(sethfee)(newmaccount)(addmprofit)(subhbalance)(buyhdd)(sellhdd)(addhspace)(subhspace))
/*
extern "C" {
void apply(uint64_t receiver, uint64_t code, uint64_t action) {
if(code==receiver)
{
switch(action)
{
EOSIO_DISPATCH_HELPER( hdddata, (init)(gethbalance)(gethsum)(sethfee)(newmaccount)(addmprofit)(subhbalance)(buyhdd)(sellhdd)(addhspace)(subhspace) )
EOSIO_DISPATCH_HELPER( hdddata, (gethbalance)(gethsum)(sethfee)(newmaccount)(addmprofit)(subhbalance)(buyhdd)(sellhdd)(addhspace)(subhspace) )
}
}
}
};
*/
......@@ -6,8 +6,7 @@
using namespace eosio;
using eosio::name;
using eosio::asset;
using eosio::symbol;
using eosio::symbol_code;
using eosio::symbol_type;
using eosio::indexed_by;
using eosio::const_mem_fun;
using eosio::microseconds;
......@@ -20,79 +19,79 @@ typedef double real_type;
* bancor exchange is entirely contained within this struct. There are no external
* side effects associated with using this API.
*/
struct [[eosio::table, eosio::contract("hdddata")]] 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.raw(); }
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, const symbol& 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
typedef multi_index<"hmarket"_n, exchange_state> hmarket_table;
typedef multi_index<N(hmarket), exchange_state> hmarket_table;
CONTRACT hdddata : public contract {
class hdddata : public contract {
public:
using contract::contract;
hdddata( name s, name code, datastream<const char*> ds);
hdddata( account_name s);
~hdddata();
void gethbalance(name owner);
ACTION init(name owner);
ACTION gethbalance(name owner);
ACTION gethsum();
void gethsum();
ACTION sethfee(name owner, uint64_t fee);
void sethfee(name owner, uint64_t fee);
ACTION newmaccount(name mname, name owner);
void newmaccount(name mname, name owner);
ACTION addmprofit(name mname, uint64_t space);
void addmprofit(name mname, uint64_t space);
ACTION subhbalance(name owner, uint64_t balance);
void subhbalance(name owner, uint64_t balance);
ACTION buyhdd(name buyer, name receiver, asset quant);
void buyhdd(name buyer, name receiver, asset quant);
ACTION sellhdd(name account, uint64_t quant);
void sellhdd(name account, uint64_t quant);
ACTION addhspace(name owner, name hddaccount, uint64_t space);
void addhspace(name owner, name hddaccount, uint64_t space);
ACTION subhspace(name owner, name hddaccount, uint64_t space);
void subhspace(name owner, name hddaccount, uint64_t space);
static constexpr symbol hddcore_symbol = symbol(symbol_code("HDDCORE"), 4);
static constexpr symbol hdd_symbol = symbol(symbol_code("HDD"), 0);
static constexpr symbol yta_symbol = symbol(symbol_code("YTA"), 4);
static constexpr eosio::name token_account{"eosio.token"_n};
static constexpr eosio::name hdd_account{"eosio.hdd"_n};
static constexpr eosio::name hddfee_account{"eosio.hddfee"_n};
static constexpr eosio::name active_permission{"active"_n};
//static constexpr symbol hddcore_symbol = symbol(symbol_code("HDDCORE"), 4);
//static constexpr symbol hdd_symbol = symbol(symbol_code("HDD"), 0);
//static constexpr symbol yta_symbol = symbol(symbol_code("YTA"), 4);
//static constexpr eosio::name token_account{N(eosio.token)};
//static constexpr eosio::name hdd_account{N(eosio.hdd)};
//static constexpr eosio::name hddfee_account{N(eosio.hddfee)};
//static constexpr eosio::name active_permission{N(active)};
static symbol get_core_symbol( name system_account = "hddofficial"_n ) {
/*
static symbol get_core_symbol( name system_account = N(hddofficial) ) {
hmarket_table rm(system_account, system_account.value);
const static auto sym = get_core_symbol( rm );
return sym;
}
*/
private:
// Implementation details:
/*
static symbol get_core_symbol( const hmarket_table& hdd ) {
auto itr = hdd.find(hddcore_symbol.raw());
check(itr != hdd.end(), "system contract must first be initialized");
......@@ -100,9 +99,9 @@ private:
}
symbol core_symbol() const;
TABLE hbalance {
name owner = "hddofficial"_n;
*/
struct hbalance {
name owner = name{N(hddofficial)};
uint64_t last_hdd_balance=0;
uint64_t hdd_per_cycle_fee=0;
uint64_t hdd_per_cycle_profit=0;
......@@ -114,15 +113,15 @@ private:
uint64_t get_hdd_per_cycle_profit() const { return hdd_per_cycle_profit; }
uint64_t get_hdd_space() const { return hdd_space; }
};
typedef multi_index<"hbalance"_n, hbalance,
indexed_by<"bybalance"_n, const_mem_fun<hbalance, uint64_t, &hbalance::get_last_hdd_balance>>,
indexed_by<"byfee"_n, const_mem_fun<hbalance, uint64_t, &hbalance::get_hdd_per_cycle_fee>>,
indexed_by<"byprofit"_n, const_mem_fun<hbalance, uint64_t, &hbalance::get_hdd_per_cycle_profit>>,
indexed_by<"byspace"_n, const_mem_fun<hbalance, uint64_t, &hbalance::get_hdd_space>>>
typedef multi_index<N(hbalance), hbalance,
indexed_by<N(bybalance), const_mem_fun<hbalance, uint64_t, &hbalance::get_last_hdd_balance>>,
indexed_by<N(byfee), const_mem_fun<hbalance, uint64_t, &hbalance::get_hdd_per_cycle_fee>>,
indexed_by<N(byprofit), const_mem_fun<hbalance, uint64_t, &hbalance::get_hdd_per_cycle_profit>>,
indexed_by<N(byspace), const_mem_fun<hbalance, uint64_t, &hbalance::get_hdd_space>>>
hbalance_table;
// ming account
TABLE maccount {
struct maccount {
uint64_t ming_id;
name owner;
......@@ -130,18 +129,18 @@ private:
uint64_t get_owner() const { return owner.value; }
};
typedef multi_index<"maccount"_n, maccount,
indexed_by<"byowner"_n, const_mem_fun<maccount, uint64_t, &maccount::get_owner>>>
typedef multi_index<N(maccount), maccount,
indexed_by<N(byowner), const_mem_fun<maccount, uint64_t, &maccount::get_owner>>>
maccount_table;
TABLE producer {
struct producer {
name owner;
eosio::public_key producer_key;
name producer_key;
uint64_t primary_key() const { return owner.value; }
};
typedef multi_index<"producer"_n, producer> producer_table;
typedef multi_index<N(producer), producer> producer_table;
private:
void update_hddofficial(hbalance_table& _hblance, const uint64_t _hb, const uint64_t time);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册