RAM购买机制结合多种合约详解
945坏男人
发表于 2022-11-12 16:42:51
203
0
0
// native.hpp (newaccount definition is actually in eosio.system.cpp)
(newaccount)(updateauth)(deleteauth)(linkauth)(unlinkauth)(canceldelay)(onerror)* |, U* M+ P2 Z- e' M( c4 J
// eosio.system.cpp# l K3 z. D {2 B1 ^# Q$ y
(setram)(setparams)(setpriv)(rmvproducer)(bidname)) P' \6 K! N! B7 A2 o3 T6 |
// delegate_bandwidth.cpp1 ]) `- p* `+ e5 ?+ A
(buyrambytes)(buyram)(sellram)(delegatebw)(undelegatebw)(refund)
// voting.cpp
(regproducer)(unregprod)(voteproducer)(regproxy)
// producer_pay.cpp/ n2 [# J6 n: ?. y. F" K E
(onblock)(claimrewards)( J% Q# R: N* t j; O" M# v
ram,cpu和net操作相关方法的都定义在delegate_bandwidth.cpp,其中和RAM相关的是buyrambytes(通过指定字节数购买ram),buyram(通过指定货币购买ram)。
delegate_bandwidth.cpp
//根据当前市场的份额,将需要购买的字节数转化为指定的EOS进行购买) N0 h# ~+ C0 ?) I9 a, S9 Z
void system_contract::buyrambytes( account_name payer, account_name receiver, uint32_t bytes ) {
//在数据库中查询RAMCORE发行量,默认为1000000000000007 W; C* Z9 b5 r ?% }
auto itr = _rammarket.find(S(4,RAMCORE));
auto tmp = *itr;7 f$ g8 F. |' ?( I7 N1 m8 V
auto eosout = tmp.convert( asset(bytes,S(0,RAM)), CORE_SYMBOL );
//通过转化后,调用buyram使用EOS购买0 O; F6 ]- K4 V, D
buyram( payer, receiver, eosout );
}
解析( X2 q+ a& [: W: L: t6 ~* }
RAM的交易机制采用Bancor算法,使每字节的价格保持不变,通过中间代币(RAMCORE)来保证EOS和RAM之间的交易流通性。从上源码看首先获得RAMCORE的发行量,再通过tmp.convert方法RAM->RAMCORE,RAMCORE->EOS(CORE_SYMBOL)再调用buyram进行购买。这里的CORE_SYMBOL不一定是指EOS,查看core_symbol.hpp,发现源码内定义为SYS,也就是说在没有修改的前提下,需要提前发行SYS代币,才能进行RAM购买。
core_symbol.hpp8 v P' e# b# n# q8 m
#define CORE_SYMBOL S(4,SYS)
delegate_bandwidth.cpp
void system_contract::buyram(account_name payer, account_name receiver, asset quant)( [$ `( m5 C+ v! B* _
{
//验证权限
require_auth(payer);8 t& S7 X, K! F4 i q
//不能为0& K' i( A- i1 Y$ v) S
eosio_assert(quant.amount > 0, "must purchase a positive amount");6 r. M5 f: r( W3 p- r' p y
auto fee = quant;
//手续费为0.5%,如果amoun为1则手续费为1,如果小于1,则手续费在amoun 0)
{
INLINE_ACTION_SENDER(eosio::token, transfer)0 n% V# f; L" C! v$ n
(N(eosio.token), {payer, N(active)},
{payer, N(eosio.ramfee), fee, std::string("ram fee")});
}
int64_t bytes_out;" G# U5 L4 E6 `" f; G/ |5 x
//根据ram市场里的EOS和RAM实时汇率计算出能够购买的RAM总量, {/ _; P4 _; L) e& B9 U
const auto &market = _rammarket.get(S(4, RAMCORE), "ram market does not exist");; C, N1 V4 c- g" f. s& \8 G
_rammarket.modify(market, 0, [&](auto &es) {
//转化方法请参考下半部分 i: A; \* g- {8 u) f; [5 W6 ?
bytes_out = es.convert(quant_after_fee, S(0, RAM)).amount;
});
//剩余总量大于0判断* x' }5 \/ p* y
eosio_assert(bytes_out > 0, "must reserve a positive amount");5 Y9 C; f+ C; J& C. }
//更新全局变量,总共可以使用的内存大小" w/ J* n7 g9 o* A6 K
_gstate.total_ram_bytes_reserved += uint64_t(bytes_out);4 r: h$ k# \( D: t& @/ W
//更新全局变量,购买RAM冻结总金额
_gstate.total_ram_stake += quant_after_fee.amount;
user_resources_table userres(_self, receiver);
auto res_itr = userres.find(receiver);0 g& G- r/ L: W: _9 ~$ ~) Z) g
if (res_itr == userres.end())
{
//在userres表中添加ram相关记录
res_itr = userres.emplace(receiver, [&](auto &res) {) U2 N; d% S* x' y! s9 z; @
res.owner = receiver;
res.ram_bytes = bytes_out;
});
}0 y0 v1 b# F' ~& o0 Y& o
else
{$ d l% q. ^6 W$ D1 z! i) o
//在userres表中修改ram相关记录0 `; |. c* `' u# n3 b
userres.modify(res_itr, receiver, [&](auto &res) {
res.ram_bytes += bytes_out;
});0 l4 ]( o Q' b8 f
}
//更新账号的RAM拥有量
set_resource_limits(res_itr->owner, res_itr->ram_bytes, res_itr->net_weight.amount, res_itr->cpu_weight.amount);
}
相关注释已经在代码中,这里还会用到一个比较重要的内容,那就是代币转化为RAM的公式,此方法请参考下面, 这方面还未深入研究会在接下来补充。
exchange_state.cpp
asset exchange_state::convert_to_exchange( connector& c, asset in ) {/ b# E, \: y/ s7 Q: ^! b
real_type R(supply.amount); //RAM已经售出的总量
real_type C(c.balance.amount+in.amount); //RAM总购买金额+本次购买的量9 w- F0 |2 D! F0 q z: v9 j1 m. [) Q
real_type F(c.weight/1000.0);
real_type T(in.amount);
real_type ONE(1.0);' h1 D- V6 y9 _! J- f) o6 n$ \$ z
real_type E = -R * (ONE - std::pow( ONE + T / C, F) );//换算出EOS对应的RAM量
//print( "E: ", E, "\n");: Y' j1 ~4 n) T4 G/ s" z/ W8 F
int64_t issued = int64_t(E);
supply.amount += issued;//更新RAM已经售出的总量# U9 Y$ h. ^( I7 z2 Y: s) N5 |# V
c.balance.amount += in.amount;//更新RAM总购买金额; h4 J$ L+ m- r( [( _6 Q( ~
return asset( issued, supply.symbol );
}$ ]6 g0 j, |/ B# M% y
* E+ o. u4 M' i2 D
asset exchange_state::convert( asset from, symbol_type to ) {
auto sell_symbol = from.symbol; ) J- ?$ b" x7 A6 C5 s; i
auto ex_symbol = supply.symbol;
auto base_symbol = base.balance.symbol; ; ]5 @5 O7 }8 \; T! U7 L( T
auto quote_symbol = quote.balance.symbol;: F5 T3 }& m8 H/ d: z x
\: x" b1 ~2 f7 B, S# Q
//根据币种转化可以购买的RAM量
if( sell_symbol != ex_symbol ) {/ p4 B* L; }) x9 r: {- y
if( sell_symbol == base_symbol ) {
from = convert_to_exchange( base, from );, @2 y/ R' L2 l1 x9 V7 I
} else if( sell_symbol == quote_symbol ) {
from = convert_to_exchange( quote, from );
} else { 3 }5 O% L( r% s5 t/ I% b
eosio_assert( false, "invalid sell" );
}! M4 \( z* h1 T' A. z
} else {
if( to == base_symbol ) {5 C! q+ d! b# p+ g
from = convert_from_exchange( base, from ); , T3 Z L6 f" H- B( c3 d
} else if( to == quote_symbol ) {
from = convert_from_exchange( quote, from );
} else {+ V/ D* v7 [8 t' @/ g9 m
eosio_assert( false, "invalid conversion" );
}
}" e2 V5 i4 _3 L+ d7 A) `
if( to != from.symbol )
return convert( from, to );
return from; C+ I0 u. \* E+ g( Z
}$ v3 B- f' N# T( w
convert_to_exchange的转化公式如下。
ram购买的量是一个绝对值,是根据购买EOS的金额和当前市场内ram的数量计算出来的。一般来说在ram总量不增加的情况下,一样金额的EOS,所能获得的ram会越来越少。所以如果早期你购买了ram,然后过段时间后通过sellram卖掉ram可能还能挣钱。
成为第一个吐槽的人