距離EOS主網(wǎng)上線還有不到一周,現(xiàn)在持有EOS的人請注意:如果不在交易所的,一定要去注冊下。近日,區(qū)塊鏈領(lǐng)域?qū)<夜壤蠋熥隽岁P(guān)于EOS部署、智能合約應(yīng)用開發(fā)和代幣映射內(nèi)容的技術(shù)分享,引起了眾多同行的關(guān)注。
目前最大的EOS應(yīng)用是區(qū)塊鏈百科(https://everipedia.org/),已獲得7500萬美元的融資,它對標的是維基百科。這個應(yīng)用的大概規(guī)則是:比如當你要編輯一個詞條時,你需要抵押自己的Token,然后每天會產(chǎn)生限量token激勵,社區(qū)里的事情都要進行投票,進行自治,大于75%的投票,才可以修改詞條。這是因為它的內(nèi)容是存放在IPFS里。當然,還有很多EOS應(yīng)用,我只挑了一個典型的。
EOS如何部署私有環(huán)境?
https://github.com/EOSIO/eos/wiki
編譯命令:https://github.com/EOSIO/eos/wiki/Local-Environment#2-building-eosio
EOS網(wǎng)絡(luò)是由無數(shù)個nodes組成的,提供單獨keosd 錢包節(jié)點,可以單獨下。相對而言,以太坊節(jié)點會自動同步數(shù)據(jù),會很慢。
啟動EOS節(jié)點命令:./nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin
使用cleos創(chuàng)建錢包及賬號(https://github.com/EOSIO/eos/wiki/Programs-&-Tools#cleos),這里要注意EOS錢包和以太坊錢包的區(qū)別:EOS主要管理私鑰,不是一個離線賬號,是建立在EOS節(jié)點上注冊后的賬號。
cleos -u http://47.98.56.32:8888/ --wallet-url http://47.98.56.32:9999/ wallet unlock
cleos -u http://47.98.56.32:8888/ --wallet-url http://47.98.56.32:9999/ wallet list
管理2組賬號,第一組賬號是eosio,其他所有eos賬號都是在第一個eosio上派生出來的。
cleos -u http://47.98.56.32:8888/ --wallet-url http://47.98.56.32:9999/ create key
我們先來創(chuàng)建私鑰:
cleos -u http://47.98.56.32:8888/ --wallet-url http://47.98.56.32:9999/ wallet import 私鑰
現(xiàn)在就多了一組賬號。
然后開始創(chuàng)建EOS上的賬戶。
以太坊的賬號是一個40字符的字符串,很難被記住。EOS權(quán)限分組,owner是最高權(quán)限,active 可以管理賬號的資產(chǎn)。
cleos -u http://47.98.56.32:8888/ --wallet-url http://47.98.56.32:9999/ create account eosio 唯一名字(可以搶注) OwnerKey ActiveKey
這樣你的唯一名字就注冊到EOS網(wǎng)絡(luò)上了,別人可以直接給你這個名字的賬號里打錢或者各種操作,以上操作意味著創(chuàng)建賬戶和管理私鑰的內(nèi)容就完成了。
接下來就是創(chuàng)建合約,先來講2個案例:
第一個案例:飛行寶
飛行寶的具體邏輯是:用戶要坐飛機,在飛行前,用戶買了哪一期的哪一個航班,EOS提供了一個多索引的數(shù)據(jù)表結(jié)構(gòu)叫multi_Index,
uint64_t term_Id; 每一天開一期
account_name 就是我們剛剛在eos上注冊的唯一用戶名,代表購買者賬號
uint64_t content; 可以留言
account_name get_poster() const { return account }
fmulti_index多索引,類似一個mysql一個表名,第一個字段是表名,后面是列,就是定義了一個數(shù)據(jù)表。
總共有4個操作:第一個操作是購買某一天的航班,購買就是一個添加數(shù)據(jù)的過程;第二操作是獲取購買的情況;第三個操作是查詢你的所有購買記錄,返回一個數(shù)組;第四個操作是修改操作。
我們來實際操作下:
eosiocpp -o flybaby.wast flybaby.cpp
生成一個 webassembly 文件,這是ABI文件(現(xiàn)在EOS網(wǎng)絡(luò)還有一些bug,要手動改下某幾處ABI文件才可以運行)。
現(xiàn)在開始部署合約
cleos -u http://47.98.56.32:8888/ --wallet-url http://47.98.56.32:9999/ set contract 唯一名字 flybaby
和以太坊有區(qū)別的是EOS可以修改智能合約。
接下來是調(diào)用方法
cleos -u http://47.98.56.32:8888/ --wallet-url http://47.98.56.32:9999/ push action 唯一名字 create '[1, 1, "唯一名字", 3000, "備注"]' -p 唯一名字
說明調(diào)用成功
cleos -u http://47.98.56.32:8888/ --wallet-url http://47.98.56.32:9999/ get table 唯一名字 唯一名字 fdata
返回一個JSON數(shù)據(jù),用一個新賬號再來買一個。返回2個飛行寶購買記錄,說明成功。get 方法是獲取某一天的航班,list方法返回我所有的飛行寶購買記錄。
合約源碼:
flybaby.cpp
#include
#include
using namespace eosio;
using std::string;
class flybaby : public eosio::contract {
public:
//self 代表合約擁有者賬號.
flybaby( account_name self ):contract(self){}
/// @abi table
struct record {
uint64_t term_id; //飛行寶期數(shù)+航班ID作為主鍵
account_name customer; //購買者賬號
uint64_t amount; //購買數(shù)量
string content; //留言
//客戶在當期僅能買一次某航班,但可以買多個不同航班.
uint64_t primary_key()const { return term_id; }
//根據(jù)客戶篩選出他購買的航班列表.
account_name get_poster() const { return customer; }
EOSLIB_SERIALIZE(record, (term_id)(customer)(amount)(content))
};
typedef eosio::multi_index
indexed_by
const_mem_fun
using contract::contract;
/// @abi action
void create(uint64_t term,uint64_t id,account_name user,uint64_t amount, string content) {
require_auth( user ); //驗證權(quán)限,只能用自己的賬號給你自己買.
records datable( _self, _self); //定義數(shù)據(jù)庫對象,數(shù)據(jù)庫屬于合約創(chuàng)建者,并且都存在一個表中.
//簡化僅表達意思,沒做校驗,注意運算符優(yōu)先級.
uint64_t term_id = (term << 32) + id;
datable.emplace(user, [&]( record & d){
eosio::print("ok this is lamda");
//d.term_id = datable.available_primary_key();
d.term_id = term_id;
d.customer = user;
d.amount = amount;
d.content = content;
eosio::print("update");
});//數(shù)據(jù)庫內(nèi)容創(chuàng)建
}
void get(uint64_t term,uint64_t id,account_name user) {
require_auth(user);
records datable(_self, _self);
uint64_t term_id = (term << 32) + id;
auto info = datable.find(term_id);
eosio::print("Term_id: ", info->term_id,
" Customer: ", name{info->customer},
" Amount: ", info->amount,
" Content: ", info->content.c_str());
}
void list(account_name user) {
require_auth(user);
records datable(_self, _self);
auto poster_index = datable.template get_index
auto pos = poster_index.find( user );
for (; pos != poster_index.end(); pos++)
{
eosio::print("Term_id: ", pos->term_id,
" Customer: ", name{pos->customer},
" Amount: ", pos->amount,
" Content: ", pos->content.c_str());
eosio::print("||");
}
}
void change(account_name user, uint64_t term, uint64_t id, string content)
{
require_auth(user);
records datable( _self, _self);
uint64_t term_id = (term << 32) + id;
auto info = datable.find(term_id);
eosio_assert(info->customer == user, "not your account");
//此處payer不是user
datable.modify(info, _self, [&](auto& p){
if (content != "")
p.content = content;
});
}
void dele(account_name user, uint64_t term, uint64_t id)
{
require_auth(user);
records datable( _self, _self);
uint64_t term_id = (term << 32) + id;
auto info = datable.find(term_id);
eosio::print(info->content.c_str());
eosio_assert(info->customer == user, "not your account");
datable.erase(info);
}
};
EOSIO_ABI(flybaby, (create)(get)(list)(change)(dele))
接下來講解 Token 的合約
EOS上沒有ERC20的協(xié)議,來看下邏輯:
currency_stats代表一個資產(chǎn)的結(jié)構(gòu)體,有資產(chǎn)的代號、名稱;
max_supply 最大發(fā)行量;
issuer 是代幣的發(fā)行者;
填寫好Token的名字、代號、發(fā)行量,做好準備工作。
require_auth(st.issuer) 只有發(fā)行者可以修改當前的發(fā)行量;
token::issue 方法就是發(fā)布出去;
token::transfer 就是轉(zhuǎn)賬功能;
就是對余額進行增加、減少的處理。
合約源碼:
token.cpp
/**
* @file
* @copyright defined in eos/LICENSE.txt
*/
#include "mydogcon.hpp"
namespace eosio {
void token::create( account_name issuer,
asset maximum_supply )
{
require_auth( _self );
auto sym = maximum_supply.symbol;
eosio_assert( sym.is_valid(), "invalid symbol name" );
eosio_assert( maximum_supply.is_valid(), "invalid supply");
eosio_assert( maximum_supply.amount > 0, "max-supply must be positive");
stats statstable( _self, sym.name() );
auto existing = statstable.find( sym.name() );
eosio_assert( existing == statstable.end(), "token with symbol already exists" );
statstable.emplace( _self, [&]( auto& s ) {
s.supply.symbol = maximum_supply.symbol;
s.max_supply = maximum_supply;
s.issuer = issuer;
});
}
void token::issue( account_name to, asset quantity, string memo )
{
auto sym = quantity.symbol;
eosio_assert( sym.is_valid(), "invalid symbol name" );
auto sym_name = sym.name();
stats statstable( _self, sym_name );
auto existing = statstable.find( sym_name );
eosio_assert( existing != statstable.end(), "token with symbol does not exist, create token before issue" );
const auto& st = *existing;
require_auth( st.issuer );
eosio_assert( quantity.is_valid(), "invalid quantity" );
eosio_assert( quantity.amount > 0, "must issue positive quantity" );
eosio_assert( quantity.symbol == st.supply.symbol, "symbol precision mismatch" );
eosio_assert( quantity.amount <= st.max_supply.amount - st.supply.amount, "quantity exceeds available supply");
statstable.modify( st, 0, [&]( auto& s ) {
s.supply += quantity;
});
add_balance( st.issuer, quantity, st, st.issuer );
if( to != st.issuer ) {
SEND_INLINE_ACTION( *this, transfer, {st.issuer,N(active)}, {st.issuer, to, quantity, memo} );
}
}
void token::transfer( account_name from,
account_name to,
asset quantity,
string /*memo*/ )
{
eosio_assert( from != to, "cannot transfer to self" );
require_auth( from );
eosio_assert( is_account( to ), "to account does not exist");
auto sym = quantity.symbol.name();
stats statstable( _self, sym );
const auto& st = statstable.get( sym );
require_recipient( from );
require_recipient( to );
eosio_assert( quantity.is_valid(), "invalid quantity" );
eosio_assert( quantity.amount > 0, "must transfer positive quantity" );
eosio_assert( quantity.symbol == st.supply.symbol, "symbol precision mismatch" );
sub_balance( from, quantity, st );
add_balance( to, quantity, st, from );
}
void token::sub_balance( account_name owner, asset value, const currency_stats& st ) {
accounts from_acnts( _self, owner );
const auto& from = from_acnts.get( value.symbol.name() );
eosio_assert( from.balance.amount >= value.amount, "overdrawn balance" );
if( from.balance.amount == value.amount ) {
from_acnts.erase( from );
} else {
from_acnts.modify( from, owner, [&]( auto& a ) {
a.balance -= value;
});
}
}
void token::add_balance( account_name owner, asset value, const currency_stats& st, account_name ram_payer )
{
accounts to_acnts( _self, owner );
auto to = to_acnts.find( value.symbol.name() );
if( to == to_acnts.end() ) {
to_acnts.emplace( ram_payer, [&]( auto& a ){
a.balance = value;
});
} else {
to_acnts.modify( to, 0, [&]( auto& a ) {
a.balance += value;
});
}
}
} /// namespace eosio
EOSIO_ABI( eosio::token, (create)(issue)(transfer) )
token.hpp
/**
* @file
* @copyright defined in eos/LICENSE.txt
*/
#pragma once
#include
#include
#include
namespace eosiosystem {
class system_contract;
}
namespace eosio {
using std::string;
class token : public contract {
public:
token( account_name self ):contract(self){}
void create( account_name issuer,
asset maximum_supply);
void issue( account_name to, asset quantity, string memo );
void transfer( account_name from,
account_name to,
asset quantity,
string memo );
private:
friend eosiosystem::system_contract;
inline asset get_supply( symbol_name sym )const;
inline asset get_balance( account_name owner, symbol_name sym )const;
private:
struct account {
asset balance;
uint64_t primary_key()const { return balance.symbol.name(); }
};
struct currency_stats {
asset supply;
asset max_supply;
account_name issuer;
uint64_t primary_key()const { return supply.symbol.name(); }
};
typedef eosio::multi_index
typedef eosio::multi_index
void sub_balance( account_name owner, asset value, const currency_stats& st );
void add_balance( account_name owner, asset value, const currency_stats& st,
account_name ram_payer );
public:
struct transfer_args {
account_name from;
account_name to;
asset quantity;
string memo;
};
};
asset token::get_supply( symbol_name sym )const
{
stats statstable( _self, sym );
const auto& st = statstable.get( sym );
return st.supply;
}
asset token::get_balance( account_name owner, symbol_name sym )const
{
accounts accountstable( _self, owner );
const auto& ac = accountstable.get( sym );
return ac.balance;
}
} /// namespace eosio
提問環(huán)節(jié)
1:講講你的EOS信仰
谷老師:我一般對EOS失去信心的時候,我就會去reddit看下EOS上朋友們的留言,上面的消息是很及時,而且上面的朋友特別友好,給你不割肉的動力。比如:big news is coming soon……winter is coming……Image you are one of them……The Dawn is coming……
每當我難受的時候,我就來這里,我的信仰就是來自這里。
2:主鏈上線時間有沒有風險?
谷老師:現(xiàn)在EOS 1.0 上線已經(jīng)完成 94%,EOS還是可以的,有一定風險會延遲。現(xiàn)在講一下EOS百萬TPS的梗:它一共21個超級節(jié)點,有點類似我們在用負載均衡,比如10臺機器,輪訓來分發(fā)流量,如果當前有1萬交易,會分到某個節(jié)點上,由于單臺服務(wù)器節(jié)點很高,能一次性處理。
另外我推薦ONO這個區(qū)塊鏈應(yīng)用,未來隨著區(qū)塊鏈的升級換代,提升基礎(chǔ)設(shè)施,也許我們后端開發(fā)就不像現(xiàn)在這樣去買一臺云服務(wù)器,而未來所有的后端就是一個區(qū)塊鏈,所有應(yīng)用都會直接構(gòu)建到區(qū)塊鏈上,像聊天、打車就直接構(gòu)建在像這樣的EOS區(qū)塊鏈上,打車的結(jié)算公開在區(qū)塊鏈上,就不存在殺熟的問題。甚至公司的形式也會發(fā)生變化,而是由使用者來決定。未來區(qū)塊鏈就不只是炒幣的功能。
3:如果一個用戶開發(fā)了一個很好的應(yīng)用,大戶如果抄襲,大戶會不會搶走EOS運行的資源?
谷老師:根據(jù)你抵押的EOS份額來租用算力。這是一個商業(yè)邏輯,如果你的應(yīng)用很火,就會有資本投你,算力不太可能被壟斷。
谷老師在最后還講到,伴隨夏季的來臨,航班延誤或取消都進入了高發(fā)期。而航空延誤險則是很多常旅客的必備,而全球區(qū)塊鏈互助社區(qū)HMS,其基于區(qū)塊鏈技術(shù)和底層的風險保障邏輯,設(shè)計出的全自動的智能型全球航空飛行延誤互助保障合約——“飛行寶”現(xiàn)在已完成首例賠付。
一直以來,區(qū)塊鏈技術(shù)和基于區(qū)塊鏈的項目,因為與現(xiàn)實世界脫節(jié)而蒙上了神秘的面紗。與此同時,底層技術(shù)不夠成熟、缺乏智能合約公鏈平臺,影響了區(qū)塊鏈技術(shù)的發(fā)展以及應(yīng)用化的過程。HMS一直在探索區(qū)塊鏈技術(shù)和互助業(yè)務(wù)相結(jié)合的創(chuàng)新,為更多的現(xiàn)實場景提供服務(wù),對促進區(qū)塊鏈世界與現(xiàn)實世界的進一步聯(lián)動有深刻意義,他非常看好這一應(yīng)用。