Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
YottaChain
YTBP
提交
a4662032
Y
YTBP
项目概览
YottaChain
/
YTBP
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Y
YTBP
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a4662032
编写于
4月 23, 2019
作者:
B
big4david
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix some issue
上级
ba628bff
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
387 addition
and
381 deletion
+387
-381
contracts/hdddata/hdddata.cpp
contracts/hdddata/hdddata.cpp
+295
-289
contracts/hdddata/hdddata.hpp
contracts/hdddata/hdddata.hpp
+92
-92
未找到文件。
contracts/hdddata/hdddata.cpp
浏览文件 @
a4662032
...
...
@@ -6,6 +6,9 @@
#include <eosio.token/eosio.token.hpp>
#include <cmath>
#include <string>
#include <type_traits>
#include <optional>
using
namespace
eosio
;
const
uint32_t
hours_in_one_day
=
24
;
...
...
@@ -13,372 +16,378 @@ const uint32_t minutes_in_one_day = hours_in_one_day * 60;
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
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
;
// 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
()
{
}
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
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
hbalance_itr
=
_hbalance
.
find
(
owner
.
value
);
eosio_assert
(
hbalance_itr
==
_hbalance
.
end
(),
"_hbalance table has already been initialized"
);
if
(
hbalance_itr
==
_hbalance
.
end
())
{
_hbalance
.
emplace
(
_self
,
[
&
](
auto
&
row
)
{
//todo check the 1st time insert
row
.
owner
=
owner
;
row
.
last_hdd_balance
=
10
;
row
.
hdd_per_cycle_fee
=
10
;
row
.
hdd_per_cycle_profit
=
10
;
row
.
hdd_space
=
20
;
row
.
last_hdd_time
=
current_time
();
});
}
auto
itr
=
_hmarket
.
find
(
hddcore_symbol
.
raw
());
eosio_assert
(
itr
==
_hmarket
.
end
(),
"_hmarket has already been initialized"
);
:
eosio
::
contract
(
s
,
code
,
ds
),
_maccount
(
_self
,
_self
.
value
),
_hmarket
(
_self
,
_self
.
value
),
_producer
(
_self
,
_self
.
value
)
{
}
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
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
hbalance_itr
=
_hbalance
.
find
(
owner
.
value
);
eosio_assert
(
hbalance_itr
==
_hbalance
.
end
(),
"_hbalance table has already been initialized"
);
_hbalance
.
emplace
(
_self
,
[
&
](
auto
&
row
)
{
//todo check the 1st time insert
row
.
owner
=
owner
;
row
.
last_hdd_balance
=
10
;
row
.
hdd_per_cycle_fee
=
10
;
row
.
hdd_per_cycle_profit
=
10
;
row
.
hdd_space
=
20
;
row
.
last_hdd_time
=
current_time
();
});
auto
itr
=
_hmarket
.
find
(
hddcore_symbol
.
raw
());
eosio_assert
(
itr
==
_hmarket
.
end
(),
"_hmarket has already been initialized"
);
auto
system_token_supply
=
eosio
::
token
::
get_supply
(
token_account
,
yta_symbol
.
code
()
);
eosio_assert
(
system_token_supply
.
symbol
==
yta_symbol
,
"specified core symbol does not exist (precision mismatch)"
);
eosio_assert
(
system_token_supply
.
amount
>
0
,
"system token supply must be greater than 0"
);
_hmarket
.
emplace
(
_self
,
[
&
](
auto
&
m
)
{
m
.
supply
.
amount
=
100000000000000ll
;
m
.
supply
.
symbol
=
hddcore_symbol
;
m
.
base
.
balance
.
amount
=
1024ll
*
1024
*
1024
*
1024
*
1024
;
m
.
base
.
balance
.
symbol
=
hdd_symbol
;
m
.
quote
.
balance
.
amount
=
system_token_supply
.
amount
/
1000
;
m
.
quote
.
balance
.
symbol
=
yta_symbol
;
eosio_assert
(
system_token_supply
.
amount
>
0
,
"system token supply must be greater than 0"
);
_hmarket
.
emplace
(
_self
,
[
&
](
auto
&
m
)
{
m
.
supply
.
amount
=
100000000000000ll
;
m
.
supply
.
symbol
=
hddcore_symbol
;
m
.
base
.
balance
.
amount
=
1024ll
*
1024
*
1024
*
1024
*
1024
;
m
.
base
.
balance
.
symbol
=
hdd_symbol
;
m
.
quote
.
balance
.
amount
=
system_token_supply
.
amount
/
1000
;
m
.
quote
.
balance
.
symbol
=
yta_symbol
;
});
}
}
//@abi action
void
hdddata
::
gethbalance
(
name
owner
)
{
require_auth
(
owner
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
hbalance_itr
=
_hbalance
.
find
(
owner
.
value
);
if
(
hbalance_itr
==
_hbalance
.
end
())
{
_hbalance
.
emplace
(
owner
,
[
&
](
auto
&
row
)
{
print
(
"A gethbalance : "
,
owner
,
" is new ...
\n
"
);
row
.
owner
=
owner
;
row
.
last_hdd_balance
=
10
;
row
.
hdd_per_cycle_fee
=
10
;
row
.
hdd_per_cycle_profit
=
10
;
row
.
hdd_space
=
10
;
row
.
last_hdd_time
=
current_time
();
});
}
else
{
_hbalance
.
modify
(
hbalance_itr
,
_self
,
[
&
](
auto
&
row
)
{
//todo check overflow and time cycle
print
(
"A gethbalance last_hdd_balance : "
,
hbalance_itr
->
get_last_hdd_balance
(),
"
\n
"
);
row
.
last_hdd_balance
=
hbalance_itr
->
get_last_hdd_balance
()
+
//( current_time() - (hbalance_itr->last_hdd_time) )
10
*
(
hbalance_itr
->
get_hdd_per_cycle_profit
()
-
hbalance_itr
->
get_hdd_per_cycle_fee
()
);
print
(
"B gethbalance .last_hdd_balance : "
,
row
.
last_hdd_balance
,
"
\n
"
);
row
.
last_hdd_time
=
current_time
();
});
}
require_auth
(
owner
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
hbalance_itr
=
_hbalance
.
find
(
owner
.
value
);
if
(
hbalance_itr
==
_hbalance
.
end
())
{
_hbalance
.
emplace
(
owner
,
[
&
](
auto
&
row
)
{
print
(
"A gethbalance : "
,
owner
,
" is new ...
\n
"
);
row
.
owner
=
owner
;
row
.
last_hdd_balance
=
10
;
row
.
hdd_per_cycle_fee
=
10
;
row
.
hdd_per_cycle_profit
=
10
;
row
.
hdd_space
=
10
;
row
.
last_hdd_time
=
current_time
();
});
}
else
{
_hbalance
.
modify
(
hbalance_itr
,
_self
,
[
&
](
auto
&
row
)
{
//todo check overflow and time cycle
print
(
"A gethbalance last_hdd_balance : "
,
hbalance_itr
->
get_last_hdd_balance
(),
"
\n
"
);
row
.
last_hdd_balance
=
hbalance_itr
->
get_last_hdd_balance
()
+
//( current_time() - (hbalance_itr->last_hdd_time) )
10
*
(
hbalance_itr
->
get_hdd_per_cycle_profit
()
-
hbalance_itr
->
get_hdd_per_cycle_fee
()
);
print
(
"B gethbalance .last_hdd_balance : "
,
row
.
last_hdd_balance
,
"
\n
"
);
row
.
last_hdd_time
=
current_time
();
});
}
}
void
hdddata
::
gethsum
()
{
require_auth
(
_self
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
hbalance_itr
=
_hbalance
.
find
(
HDD_OFFICIAL
.
value
);
if
(
hbalance_itr
!=
_hbalance
.
end
())
{
//todo check the 1st time insert
_hbalance
.
modify
(
hbalance_itr
,
_self
,
[
&
](
auto
&
row
)
{
//todo check overflow and time cycle
row
.
last_hdd_balance
=
hbalance_itr
->
get_last_hdd_balance
()
+
// todo ((uint64_t)( current_time() - hbalance_itr->last_hdd_time ))
10
*
(
hbalance_itr
->
get_hdd_per_cycle_profit
()
-
hbalance_itr
->
get_hdd_per_cycle_fee
()
);
row
.
last_hdd_time
=
current_time
();
});
}
require_auth
(
_self
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
hbalance_itr
=
_hbalance
.
find
(
HDD_OFFICIAL
.
value
);
if
(
hbalance_itr
!=
_hbalance
.
end
())
{
//todo check the 1st time insert
_hbalance
.
modify
(
hbalance_itr
,
_self
,
[
&
](
auto
&
row
)
{
//todo check overflow and time cycle
row
.
last_hdd_balance
=
hbalance_itr
->
get_last_hdd_balance
()
+
// todo ((uint64_t)( current_time() - hbalance_itr->last_hdd_time ))
10
*
(
hbalance_itr
->
get_hdd_per_cycle_profit
()
-
hbalance_itr
->
get_hdd_per_cycle_fee
()
);
row
.
last_hdd_time
=
current_time
();
});
}
}
//@abi action
void
hdddata
::
sethfee
(
name
owner
,
uint64_t
fee
)
{
require_auth
(
_self
);
require_auth
(
owner
);
hbalance_table
_hbalance
(
owner
,
owner
.
value
);
auto
hbalance_itr
=
_hbalance
.
find
(
owner
.
value
);
if
(
hbalance_itr
!=
_hbalance
.
end
())
{
//每周期费用 <= (占用存储空间*数据分片大小/1GB)*(记账周期/ 1年)
//eos_assert( xxx, "");
_hbalance
.
modify
(
hbalance_itr
,
owner
,
[
&
](
auto
&
row
)
{
print
(
"A row.hdd_per_cycle_fee : "
,
row
.
hdd_per_cycle_fee
,
"
\n
"
);
//todo check overflow
row
.
hdd_per_cycle_fee
-=
fee
;
});
print
(
"B row.hdd_per_cycle_fee : "
,
hbalance_itr
->
get_hdd_per_cycle_fee
(),
"
\n
"
);
}
else
{
print
(
"no owner in _hbalance : "
,
owner
,
"
\n
"
);
}
//每周期费用 <= (占用存储空间*数据分片大小/1GB)*(记账周期/ 1年)
require_auth
(
_self
);
require_auth
(
owner
);
hbalance_table
_hbalance
(
owner
,
owner
.
value
);
auto
hbalance_itr
=
_hbalance
.
find
(
owner
.
value
);
eosio_assert
(
hbalance_itr
!=
_hbalance
.
end
(),
"no owner exists in _hbalance table"
);
//每周期费用 <= (占用存储空间*数据分片大小/1GB)*(记账周期/ 1年)
//eos_assert( xxx, "");
_hbalance
.
modify
(
hbalance_itr
,
owner
,
[
&
](
auto
&
row
)
{
print
(
"A row.hdd_per_cycle_fee : "
,
row
.
hdd_per_cycle_fee
,
"
\n
"
);
//todo check overflow
row
.
hdd_per_cycle_fee
-=
fee
;
});
print
(
"B row.hdd_per_cycle_fee : "
,
hbalance_itr
->
get_hdd_per_cycle_fee
(),
"
\n
"
);
//每周期费用 <= (占用存储空间*数据分片大小/1GB)*(记账周期/ 1年)
}
//@abi action
void
hdddata
::
subhbalance
(
name
owner
,
uint64_t
balance
){
require_auth
(
_self
);
require_auth
(
owner
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
hbalance_itr
=
_hbalance
.
find
(
owner
.
value
);
if
(
hbalance_itr
!=
_hbalance
.
end
())
{
_hbalance
.
modify
(
hbalance_itr
,
owner
,
[
&
](
auto
&
row
)
{
//todo check overflow
row
.
last_hdd_balance
-=
balance
;
});
}
else
{
print
(
"no owner in _hbalance : "
,
owner
,
"
\n
"
);
}
require_auth
(
_self
);
require_auth
(
owner
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
hbalance_itr
=
_hbalance
.
find
(
owner
.
value
);
eosio_assert
(
hbalance_itr
!=
_hbalance
.
end
(),
"no owner exists in _hbalance table"
);
_hbalance
.
modify
(
hbalance_itr
,
owner
,
[
&
](
auto
&
row
)
{
//todo check overflow
row
.
last_hdd_balance
-=
balance
;
});
}
//@abi action
void
hdddata
::
addhspace
(
name
owner
,
name
hddaccount
,
uint64_t
space
){
require_auth
(
owner
);
require_auth
(
hddaccount
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
hbalance_itr
=
_hbalance
.
find
(
hddaccount
.
value
);
if
(
hbalance_itr
!=
_hbalance
.
end
())
{
_hbalance
.
modify
(
hbalance_itr
,
hddaccount
,
[
&
](
auto
&
row
)
{
//todo check overflow
row
.
hdd_space
+=
space
;
});
}
else
{
print
(
"no owner in _hbalance : "
,
owner
,
"
\n
"
);
}
require_auth
(
owner
);
require_auth
(
hddaccount
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
hbalance_itr
=
_hbalance
.
find
(
hddaccount
.
value
);
eosio_assert
(
hbalance_itr
!=
_hbalance
.
end
(),
"no owner exists in _hbalance table"
);
_hbalance
.
modify
(
hbalance_itr
,
hddaccount
,
[
&
](
auto
&
row
)
{
//todo check overflow
row
.
hdd_space
+=
space
;
});
}
//@abi action
void
hdddata
::
subhspace
(
name
owner
,
name
hddaccount
,
uint64_t
space
){
require_auth
(
owner
);
require_auth
(
hddaccount
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
hbalance_itr
=
_hbalance
.
find
(
hddaccount
.
value
);
if
(
hbalance_itr
!=
_hbalance
.
end
())
{
_hbalance
.
modify
(
hbalance_itr
,
hddaccount
,
[
&
](
auto
&
row
)
{
//todo check overflow
row
.
hdd_space
-=
space
;
});
}
else
{
print
(
"no owner in _hbalance : "
,
owner
,
"
\n
"
)
;
}
require_auth
(
owner
);
require_auth
(
hddaccount
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
hbalance_itr
=
_hbalance
.
find
(
hddaccount
.
value
);
eosio_assert
(
hbalance_itr
!=
_hbalance
.
end
(),
"no owner exists in _hbalance table"
);
_hbalance
.
modify
(
hbalance_itr
,
hddaccount
,
[
&
](
auto
&
row
)
{
//todo check overflow
row
.
hdd_space
-=
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
);
if
(
maccount_itr
==
_maccount
.
end
())
{
_maccount
.
emplace
(
mname
,
[
&
](
auto
&
row
)
{
row
.
ming_id
=
mname
.
value
;
row
.
owner
=
owner
;
});
}
else
{
print
(
"owner : "
,
owner
,
" already exist in _hbalance
\n
"
);
return
;
}
require_auth
(
owner
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
hbalance_itr
=
_hbalance
.
find
(
owner
.
value
);
if
(
hbalance_itr
==
_hbalance
.
end
())
{
_hbalance
.
emplace
(
owner
,
[
&
](
auto
&
row
)
{
//todo check the 1st time insert to modify
row
.
owner
=
owner
;
row
.
last_hdd_balance
=
10
;
row
.
hdd_per_cycle_fee
=
10
;
row
.
hdd_per_cycle_profit
=
10
;
row
.
hdd_space
=
0
;
row
.
last_hdd_time
=
current_time
();
});
}
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
"
);
_maccount
.
emplace
(
mname
,
[
&
](
auto
&
row
)
{
row
.
ming_id
=
mname
.
value
;
row
.
owner
=
owner
;
});
require_auth
(
owner
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
hbalance_itr
=
_hbalance
.
find
(
owner
.
value
);
eosio_assert
(
hbalance_itr
==
_hbalance
.
end
(),
"no owner exists in _hbalance table"
);
_hbalance
.
emplace
(
owner
,
[
&
](
auto
&
row
)
{
//todo check the 1st time insert to modify
row
.
owner
=
owner
;
row
.
last_hdd_balance
=
10
;
row
.
hdd_per_cycle_fee
=
10
;
row
.
hdd_per_cycle_profit
=
10
;
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
);
if
(
maccount_itr
!=
_maccount
.
end
())
{
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
owner_id
=
maccount_itr
->
get_owner
();
auto
hbalance_itr
=
_hbalance
.
find
(
owner_id
);
if
(
hbalance_itr
!=
_hbalance
.
end
())
{
_hbalance
.
modify
(
hbalance_itr
,
_self
,
[
&
](
auto
&
row
)
{
//todo check the 1st time insert
//row.owner = owner;
row
.
hdd_space
=
space
;
});
}
else
{
//todo
print
(
"no owner in _hbalance : "
,
owner_id
,
" .
\n
"
);
}
}
//每周期收益 += (预采购空间*数据分片大小/1GB)*(记账周期/ 1年)
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
);
auto
owner_id
=
maccount_itr
->
get_owner
();
auto
hbalance_itr
=
_hbalance
.
find
(
owner_id
);
eosio_assert
(
hbalance_itr
==
_hbalance
.
end
(),
"no owner exists in _hbalance table"
);
//space verification
eosio_assert
(
hbalance_itr
->
get_hdd_space
()
+
data_slice_size
==
space
,
"not correct verification"
);
_hbalance
.
modify
(
hbalance_itr
,
_self
,
[
&
](
auto
&
row
)
{
//todo check the 1st time insert
//row.owner = owner;
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"
);
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
)(
token_account
,
{
buyer
,
active_permission
},
{
buyer
,
hdd_account
,
quant
,
std
::
string
(
"buy hdd"
)
}
);
int64_t
bytes_out
;
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
;
});
eosio_assert
(
bytes_out
>
0
,
"must reserve a positive amount"
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
res_itr
=
_hbalance
.
find
(
receiver
.
value
);
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
;
});
print
(
"bytes_out: "
,
bytes_out
,
"
\n
"
);
eosio_assert
(
bytes_out
>
0
,
"must reserve a positive amount"
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
res_itr
=
_hbalance
.
find
(
receiver
.
value
);
if
(
res_itr
==
_hbalance
.
end
()
)
{
res_itr
=
_hbalance
.
emplace
(
receiver
,
[
&
](
auto
&
res
)
{
res
.
owner
=
receiver
;
res
.
last_hdd_balance
=
bytes_out
;
res
.
last_hdd_time
=
current_time
();
res_itr
=
_hbalance
.
emplace
(
receiver
,
[
&
](
auto
&
res
)
{
res
.
owner
=
receiver
;
res
.
last_hdd_balance
=
bytes_out
;
res
.
last_hdd_time
=
current_time
();
});
}
else
{
_hbalance
.
modify
(
res_itr
,
receiver
,
[
&
](
auto
&
res
)
{
res
.
last_hdd_balance
+=
bytes_out
;
res
.
last_hdd_time
=
current_time
();
_hbalance
.
modify
(
res_itr
,
receiver
,
[
&
](
auto
&
res
)
{
res
.
last_hdd_balance
+=
bytes_out
;
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
);
auto
res_itr
=
_hbalance
.
find
(
account
.
value
);
eosio_assert
(
res_itr
!=
_hbalance
.
end
(),
"no resource row"
);
//need to calculate the latest hddbalance
require_auth
(
account
);
eosio_assert
(
quant
>
0
,
"cannot sell negative hdd"
);
hbalance_table
_hbalance
(
_self
,
_self
.
value
);
auto
res_itr
=
_hbalance
.
find
(
account
.
value
);
eosio_assert
(
res_itr
!=
_hbalance
.
end
(),
"no resource row"
);
//need to calculate the latest hddbalance
eosio_assert
(
res_itr
->
get_last_hdd_balance
()
>=
quant
,
"insufficient hdd"
);
asset
tokens_out
;
asset
tokens_out
;
auto
itr
=
_hmarket
.
find
(
hddcore_symbol
.
raw
());
_hmarket
.
modify
(
itr
,
same_payer
,
[
&
](
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
());
});
_hbalance
.
modify
(
res_itr
,
account
,
[
&
](
auto
&
res
)
{
res
.
last_hdd_balance
-=
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"
)
}
/// 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
());
});
_hbalance
.
modify
(
res_itr
,
account
,
[
&
](
auto
&
res
)
{
res
.
last_hdd_balance
-=
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"
)
}
);
}
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
);
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
)
);
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"
);
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
(
1.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
);
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
;
//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
)
{
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
"
);
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
);
if
(
to
!=
from
.
symbol
)
return
convert
(
from
,
to
);
return
from
;
}
return
from
;
}
extern
"C"
{
void
apply
(
uint64_t
receiver
,
uint64_t
code
,
uint64_t
action
)
{
if
(
code
==
receiver
)
...
...
@@ -388,8 +397,5 @@ extern "C" {
EOSIO_DISPATCH_HELPER
(
hdddata
,
(
init
)(
gethbalance
)(
gethsum
)(
sethfee
)(
newmaccount
)(
addmprofit
)(
subhbalance
)(
buyhdd
)(
sellhdd
)(
addhspace
)(
subhspace
)
)
}
}
else
if
(
code
==
HDD_OFFICIAL
.
value
&&
action
==
"transfer"
_n
.
value
)
{
//execute_action( name(receiver), name(code), &hdddata::deposithdd );
}
}
};
contracts/hdddata/hdddata.hpp
浏览文件 @
a4662032
...
...
@@ -21,46 +21,45 @@ typedef double real_type;
* side effects associated with using this API.
*/
struct
[[
eosio
::
table
,
eosio
::
contract
(
"hdddata"
)]]
exchange_state
{
asset
supply
;
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
.
raw
();
}
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
,
const
symbol
&
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
;
CONTRACT
hdddata
:
public
contract
{
public:
using
contract
::
contract
;
hdddata
(
name
s
,
name
code
,
datastream
<
const
char
*>
ds
);
CONTRACT
hdddata
:
public
contract
{
public:
using
contract
::
contract
;
hdddata
(
name
s
,
name
code
,
datastream
<
const
char
*>
ds
);
~
hdddata
();
ACTION
init
(
name
owner
);
ACTION
gethbalance
(
name
owner
);
ACTION
gethsum
();
ACTION
init
(
name
owner
);
ACTION
gethbalance
(
name
owner
);
ACTION
gethsum
();
ACTION
sethfee
(
name
owner
,
uint64_t
fee
);
ACTION
newmaccount
(
name
mname
,
name
owner
);
...
...
@@ -70,83 +69,84 @@ CONTRACT hdddata : public contract
ACTION
subhbalance
(
name
owner
,
uint64_t
balance
);
ACTION
buyhdd
(
name
buyer
,
name
receiver
,
asset
quant
);
ACTION
sellhdd
(
name
account
,
uint64_t
quant
);
ACTION
addhspace
(
name
owner
,
name
hddaccount
,
uint64_t
space
);
ACTION
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
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
symbol
get_core_symbol
(
name
system_account
=
"hddofficial"
_n
)
{
hmarket_table
rm
(
system_account
,
system_account
.
value
);
const
static
auto
sym
=
get_core_symbol
(
rm
);
return
sym
;
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
symbol
get_core_symbol
(
name
system_account
=
"hddofficial"
_n
)
{
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"
);
return
itr
->
quote
.
balance
.
symbol
;
}
symbol
core_symbol
()
const
;
TABLE
hbalance
{
name
owner
=
"hddofficial"
_n
;
uint64_t
last_hdd_balance
=
0
;
uint64_t
hdd_per_cycle_fee
=
0
;
uint64_t
hdd_per_cycle_profit
=
0
;
uint64_t
hdd_space
=
0
;
uint64_t
last_hdd_time
=
current_time
();
uint64_t
primary_key
()
const
{
return
owner
.
value
;
}
uint64_t
get_last_hdd_balance
()
const
{
return
last_hdd_balance
;
}
uint64_t
get_hdd_per_cycle_fee
()
const
{
return
hdd_per_cycle_fee
;
}
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"
);
return
itr
->
quote
.
balance
.
symbol
;
}
symbol
core_symbol
()
const
;
TABLE
hbalance
{
name
owner
=
"hddofficial"
_n
;
uint64_t
last_hdd_balance
=
0
;
uint64_t
hdd_per_cycle_fee
=
0
;
uint64_t
hdd_per_cycle_profit
=
0
;
uint64_t
hdd_space
=
0
;
uint64_t
last_hdd_time
=
current_time
();
uint64_t
primary_key
()
const
{
return
owner
.
value
;
}
uint64_t
get_last_hdd_balance
()
const
{
return
last_hdd_balance
;
}
uint64_t
get_hdd_per_cycle_fee
()
const
{
return
hdd_per_cycle_fee
;
}
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
>>>
hbalance_table
;
// ming account
TABLE
maccount
{
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
>>>
hbalance_table
;
// ming account
TABLE
maccount
{
uint64_t
ming_id
;
name
owner
;
uint64_t
primary_key
()
const
{
return
ming_id
;
}
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
>>>
maccount_table
;
TABLE
producer
{
name
owner
;
eosio
::
public_key
producer_key
;
uint64_t
primary_key
()
const
{
return
owner
.
value
;
}
};
indexed_by
<
"byowner"
_n
,
const_mem_fun
<
maccount
,
uint64_t
,
&
maccount
::
get_owner
>>>
maccount_table
;
TABLE
producer
{
name
owner
;
eosio
::
public_key
producer_key
;
uint64_t
primary_key
()
const
{
return
owner
.
value
;
}
};
typedef
multi_index
<
"producer"
_n
,
producer
>
producer_table
;
private:
//hbalance_table _hbalance;
maccount_table
_maccount
;
producer_table
_producer
;
private:
//hbalance_table _hbalance;
maccount_table
_maccount
;
producer_table
_producer
;
hmarket_table
_hmarket
;
};
\ No newline at end of file
};
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录