距離EOS主網上線還有不到一周,現在持有EOS的人請注意:如果不在交易所的,一定要去注冊下。近日,區塊鏈領域專家谷老師做了關于EOS部署、智能合約應用開發和代幣映射內容的技術分享,引起了眾多同行的關注。
目前最大的EOS應用是區塊鏈百科(https://everipedia.org/),已獲得7500萬美元的融資,它對標的是維基百科。這個應用的大概規則是:比如當你要編輯一個詞條時,你需要抵押自己的Token,然后每天會產生限量token激勵,社區里的事情都要進行投票,進行自治,大于75%的投票,才可以修改詞條。這是因為它的內容是存放在IPFS里。當然,還有很多EOS應用,我只挑了一個典型的。
EOS如何部署私有環境?
https://github.com/EOSIO/eos/wiki
編譯命令:https://github.com/EOSIO/eos/wiki/Local-Environment#2-building-eosio
EOS網絡是由無數個nodes組成的,提供單獨keosd 錢包節點,可以單獨下。相對而言,以太坊節點會自動同步數據,會很慢。
啟動EOS節點命令:./nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin
使用cleos創建錢包及賬號(https://github.com/EOSIO/eos/wiki/Programs-&-Tools#cleos),這里要注意EOS錢包和以太坊錢包的區別:EOS主要管理私鑰,不是一個離線賬號,是建立在EOS節點上注冊后的賬號。
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
我們先來創建私鑰:
cleos -u http://47.98.56.32:8888/ --wallet-url http://47.98.56.32:9999/ wallet import 私鑰
現在就多了一組賬號。
然后開始創建EOS上的賬戶。
以太坊的賬號是一個40字符的字符串,很難被記住。EOS權限分組,owner是最高權限,active 可以管理賬號的資產。
cleos -u http://47.98.56.32:8888/ --wallet-url http://47.98.56.32:9999/ create account eosio 唯一名字(可以搶注) OwnerKey ActiveKey
這樣你的唯一名字就注冊到EOS網絡上了,別人可以直接給你這個名字的賬號里打錢或者各種操作,以上操作意味著創建賬戶和管理私鑰的內容就完成了。
接下來就是創建合約,先來講2個案例:
第一個案例:飛行寶
飛行寶的具體邏輯是:用戶要坐飛機,在飛行前,用戶買了哪一期的哪一個航班,EOS提供了一個多索引的數據表結構叫multi_Index,
uint64_t term_Id; 每一天開一期
account_name 就是我們剛剛在eos上注冊的唯一用戶名,代表購買者賬號
uint64_t content; 可以留言
account_name get_poster() const { return account }
fmulti_index多索引,類似一個mysql一個表名,第一個字段是表名,后面是列,就是定義了一個數據表。
總共有4個操作:第一個操作是購買某一天的航班,購買就是一個添加數據的過程;第二操作是獲取購買的情況;第三個操作是查詢你的所有購買記錄,返回一個數組;第四個操作是修改操作。
我們來實際操作下:
eosiocpp -o flybaby.wast flybaby.cpp
生成一個 webassembly 文件,這是ABI文件(現在EOS網絡還有一些bug,要手動改下某幾處ABI文件才可以運行)。
現在開始部署合約
cleos -u http://47.98.56.32:8888/ --wallet-url http://47.98.56.32:9999/ set contract 唯一名字 flybaby
和以太坊有區別的是EOS可以修改智能合約。
接下來是調用方法
cleos -u http://47.98.56.32:8888/ --wallet-url http://47.98.56.32:9999/ push action 唯一名字 create '[1, 1, "唯一名字", 3000, "備注"]' -p 唯一名字
說明調用成功
cleos -u http://47.98.56.32:8888/ --wallet-url http://47.98.56.32:9999/ get table 唯一名字 唯一名字 fdata
返回一個JSON數據,用一個新賬號再來買一個。返回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; //飛行寶期數+航班ID作為主鍵
account_name customer; //購買者賬號
uint64_t amount; //購買數量
string content; //留言
//客戶在當期僅能買一次某航班,但可以買多個不同航班.
uint64_t primary_key()const { return term_id; }
//根據客戶篩選出他購買的航班列表.
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 ); //驗證權限,只能用自己的賬號給你自己買.
records datable( _self, _self); //定義數據庫對象,數據庫屬于合約創建者,并且都存在一個表中.
//簡化僅表達意思,沒做校驗,注意運算符優先級.
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");
});//數據庫內容創建
}
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的協議,來看下邏輯:
currency_stats代表一個資產的結構體,有資產的代號、名稱;
max_supply 最大發行量;
issuer 是代幣的發行者;
填寫好Token的名字、代號、發行量,做好準備工作。
require_auth(st.issuer) 只有發行者可以修改當前的發行量;
token::issue 方法就是發布出去;
token::transfer 就是轉賬功能;
就是對余額進行增加、減少的處理。
合約源碼:
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
提問環節
1:講講你的EOS信仰
谷老師:我一般對EOS失去信心的時候,我就會去reddit看下EOS上朋友們的留言,上面的消息是很及時,而且上面的朋友特別友好,給你不割肉的動力。比如:big news is coming soon……winter is coming……Image you are one of them……The Dawn is coming……
每當我難受的時候,我就來這里,我的信仰就是來自這里。
2:主鏈上線時間有沒有風險?
谷老師:現在EOS 1.0 上線已經完成 94%,EOS還是可以的,有一定風險會延遲。現在講一下EOS百萬TPS的梗:它一共21個超級節點,有點類似我們在用負載均衡,比如10臺機器,輪訓來分發流量,如果當前有1萬交易,會分到某個節點上,由于單臺服務器節點很高,能一次性處理。
另外我推薦ONO這個區塊鏈應用,未來隨著區塊鏈的升級換代,提升基礎設施,也許我們后端開發就不像現在這樣去買一臺云服務器,而未來所有的后端就是一個區塊鏈,所有應用都會直接構建到區塊鏈上,像聊天、打車就直接構建在像這樣的EOS區塊鏈上,打車的結算公開在區塊鏈上,就不存在殺熟的問題。甚至公司的形式也會發生變化,而是由使用者來決定。未來區塊鏈就不只是炒幣的功能。
3:如果一個用戶開發了一個很好的應用,大戶如果抄襲,大戶會不會搶走EOS運行的資源?
谷老師:根據你抵押的EOS份額來租用算力。這是一個商業邏輯,如果你的應用很火,就會有資本投你,算力不太可能被壟斷。
谷老師在最后還講到,伴隨夏季的來臨,航班延誤或取消都進入了高發期。而航空延誤險則是很多常旅客的必備,而全球區塊鏈互助社區HMS,其基于區塊鏈技術和底層的風險保障邏輯,設計出的全自動的智能型全球航空飛行延誤互助保障合約——“飛行寶”現在已完成首例賠付。
一直以來,區塊鏈技術和基于區塊鏈的項目,因為與現實世界脫節而蒙上了神秘的面紗。與此同時,底層技術不夠成熟、缺乏智能合約公鏈平臺,影響了區塊鏈技術的發展以及應用化的過程。HMS一直在探索區塊鏈技術和互助業務相結合的創新,為更多的現實場景提供服務,對促進區塊鏈世界與現實世界的進一步聯動有深刻意義,他非常看好這一應用。