EOS自帶ram購買智能合約,在eosio.system中的exchange_state.cpp斗搞,ram的購買核心思想是bancor算法日缨,使用EOS購買RAM時,其實先從EOS通過bancor轉(zhuǎn)換成中間代幣RAMCORE黑竞,再從RAMCORE通過bancor轉(zhuǎn)換成為RAM
EOS==(bancor算法)===》RAMCORE==(bancor算法)===》RAM
這里,RAMCORE是子幣疏旨,EOS和RAM是母幣很魂,EOS和ram之間的轉(zhuǎn)換是這2種母幣通過子幣RAMCORE來實現(xiàn)兌換,如果你看過bancor算法白皮書以及bancor網(wǎng)絡(luò)在以太網(wǎng)上的實現(xiàn)的話檐涝,exchange_state.cpp中的bancor算法在公式上其實是錯誤的遏匆,即使在最新的代碼中我也沒看到有修改過來法挨。EOS和RAM之間的轉(zhuǎn)換有一種快速的轉(zhuǎn)換方法,這里因為F系數(shù)一樣的所以中間的2步轉(zhuǎn)換可以抵消掉幅聘,如下:
R1 = FP1S
R2 = FP2S
R1:當(dāng)前RAM余量
P1:當(dāng)前S以R1定價的價格
R2:當(dāng)前EOS余量
P2:當(dāng)前S以R2定價的價格
S:當(dāng)前RAMCORE余量
上述因為F是相同的凡纳,最終 R1/R2 = P1/P2,推出
pr = R2/R1
pe = R1/R2
pr:ram的價格以eos計價
pe:eos的價格以ram計價
ram的價格(以eos計價)pr = R2/R1帝蒿,可以發(fā)現(xiàn)ram的價格只跟R1和R2相關(guān)荐糜,要想提高ram的價格可以降低R1余量或者提高R2余量來實現(xiàn),要想降低ram的價格可以通過提高R1余量或者降低R2余量來實現(xiàn)葛超。下面給出ram購買算法以及添加了快速購買quick_conver的python版實現(xiàn)
- 下面快速轉(zhuǎn)換quick_conver的推導(dǎo)過程:
1)out1 = ((1 + asset_in/R1)F - 1) * S
2)S = S + out1
3)out2 = (1 - (1 - out1/S)(1/F)) * R2
asset_in:要購買ram的eos數(shù)量
R1:EOS余量
S:RAMCORE余量
out1:購買的RAMCORE數(shù)量
R2:RAM余量
out2:購買的RAM數(shù)量
最終 out2 = (asset_in/(R1+asset_in))*R2
#-*- coding: UTF-8 -*-
class exchange(object):
def __init__(self, quote_eos, quote_ram, quote_coreram, F):
self.quote_eos = quote_eos
self.quote_ram = quote_ram
self.quote_coreram = quote_coreram
self.F = F
def convert_to_exchange(self, asset_in, symbol):
# Bancor算法 out = ((1 + asset_in/R)**F - 1) * S
if symbol == 'eos':
#out = ((1 + asset_in/(self.quote_eos+asset_in))**self.F - 1) * self.quote_coreram eos代碼中的公式其實是錯的
out = ((1 + asset_in/self.quote_eos)**self.F - 1) * self.quote_coreram
self.quote_eos += asset_in
elif symbol == 'ram':
#out = ((1 + asset_in/(self.quote_ram+asset_in))**self.F - 1) * self.quote_coreram eos代碼中的公式其實是錯的
out = ((1 + asset_in/self.quote_ram)**self.F - 1) * self.quote_coreram
self.quote_ram += asset_in
self.quote_coreram += out
return out
def convert_from_exchange(self, asset_in, symbol):
# Bancor算法 out = (1 - (1 - asset_in/S)**(1/F)) * R
if symbol == 'eos':
#out = ((1 + asset_in/(self.quote_coreram-asset_in))**(1/self.F) - 1) * self.quote_eos eos代碼中的公式其實是錯的
out = (1 - (1 - asset_in/self.quote_coreram)**(1/self.F)) * self.quote_eos
self.quote_eos -= out
elif symbol == 'ram':
#out = ((1 + asset_in/(self.quote_coreram-asset_in))**(1/self.F) - 1) * self.quote_ram eos代碼中的公式其實是錯的
out = (1 - (1 - asset_in/self.quote_coreram)**(1/self.F)) * self.quote_ram
self.quote_ram -= out
self.quote_coreram -= asset_in
return out
# ram_out = quick_conver(eos_in) 等價于 ram_out = convert_from_exchange(convert_to_exchange(eos_in, 'eos'), 'ram')
def quick_conver(self, eos_in):
self.quote_eos += eos_in
ram = (eos_in/self.quote_eos)*self.quote_ram
self.quote_ram -= ram
return ram
def func():
ec = exchange(1000000., 64*1024*1024*1024., 100000000000., 0.5)
ram = ec.convert_from_exchange(ec.convert_to_exchange(100, 'eos'), 'ram')
ram = ec.convert_from_exchange(ec.convert_to_exchange(1000, 'eos'), 'ram')
print(ram)
def func2():
ec = exchange(1000000., 64*1024*1024*1024., 100000000000., 0.5)
ram = ec.quick_conver(100)
ram = ec.quick_conver(1000)
print(ram)
if __name__ == '__main__':
#驗證eos到ram的快速轉(zhuǎn)換
func()
func2()
輸出結(jié)果:
> python3 et3.py
68637104.66032256
68637104.66032611
可以看到是等價的狞尔,所以當(dāng)F值相同時,ram的買賣完全可以通過上述的quick_conver來代替巩掺,可惜在exchange_state.cpp中沒有quick_conver的實現(xiàn)