建構式
addressbook(name receiver, name code, datastream<const char*> ds):contract(receiver, code, ds) {}
#單例表(code和scope都用receiver的表)也可在初始化串列中實體化
singleton_example( name receiver, name code, datastream<const char*> ds ) :
contract(receiver, code, ds),
singleton_instance(receiver, receiver.value)
{}
內置函式
- 權限檢測函式
require_auth(name): 必須具有某權限, 沒有就不會往下執行require_auth2(user.value, "active"_n.value): 指定必須某賬戶的某種權限has_auth(name): 判斷是否具有某權限, 回傳 true | false .eosio::check(eosio::has_auth(...), ...)
get_self: 獲取合約名稱, 合約部署者get_code|get_first_receiver(): 部署此合約的帳戶名稱require_recipient(name): 發出通知, 將action復制到發件人- 呼叫
require_recipient將一個帳戶添加到 require_recipient 集合并確保這些帳戶收到正在執行的action的通知
- 呼叫
check( is_account( oracleClient ), "Must have a valid oracleClient" ): 斷言is_account(name)賬號是否存在
EOSIO 名稱類
- 適用于所有 EOSIO 編碼的名稱(帳戶、操作、表等)
uint64_t在區塊鏈上編碼為 64 位無符號整數 ( ),- 前 12 個字符(如果有)
base32使用以下字符編碼:.,1-5,a-z - 第 13 個字符(如果適用)
base16使用以下字符編碼:.,1-5,a-j
例子:
auto eosio_user = eosio::name{user}; //encodes user string to eosio::name object
auto user_str = user_name_obj.to_string(); //decodes eosio::name obj to string
auto standard_account = "standardname"_n; //encodes literal string to eosio::name
auto non_standard_account = ".standard"_n; //encodes literal string to eosio::name
行內操作
行內操作在呼叫者操作的相同范圍和權限內作業, 行內操作保證在同一個事務中執行
eosio.code 權限
該eosio.code權限是一種偽權限,用于增強安全性,并使合約能夠執行行內action,
例如: 為了從 addressbook發送行內action,請將eosio.code權限添加到合約帳戶addressbook的active權限中, 否則會報錯 Authorization failure with inline action sent to self
#加權限
cleos set account permission addressbook active --add-code
#刪權限
cleos set account permission addressbook active --remove-code
行內action-本合約內
需要 eosio.code
action(
//permission_level, 一個權限級別結構(需要將`eosio.code`權限添加到合約帳戶的active權限中)
//code, 要呼叫的合約,部署合約的帳戶(使用eosio::name型別初始化)
//action, action(使用eosio::name型別初始化)
//data 傳遞給action的資料,與被呼叫的action相關的位置元組,
).send();
action(
permission_level{get_self(),"active"_n},
get_self(),
"notify"_n,
std::make_tuple(user, name{user}.to_string() + message)
).send();
可通過 ./cleos get actions addressbook 命令查看
行內action-外部合約
使用 action_wrapper , 需要 eosio.code
#A合約內宣告
[[eosio::action]] void count(name user, std::string type){
//可在此限定只有哪個賬戶/合約才能授權該命令
require_auth(name("addressbook")); //只有addressbook合約才能成功執行此操作
...
}
using count_action = action_wrapper<"count"_n, &abcounter::count>;
#B合約內呼叫A合約
abcounter::count_action count("abcounter"_n, {get_self(), "active"_n});
count.send(user, type);
可通過 cleos get table abcounter abcounter counts --lower alice --limit 1 查看統計結果
創建自定義權限
介紹權限
/*The authority JSON object*/
{
"threshold" : 100, /*An integer that defines cumulative signature weight required for authorization*/
"keys" : [], /*An array made up of individual permissions defined with an EOS PUBLIC KEY*/
"accounts" : [] /*An array made up of individual permissions defined with an EOS ACCOUNT*/
}
/*Set Permission with Key*/
{
"permission" : {
"key" : "EOS8X7Mp7apQWtL6T2sfSZzBcQNUqZB7tARFEm9gA9Tn9nbMdsvBB",
"permission" : "active"
},
weight : 25 /*Set the weight of a signature from this permission*/
}
/*Set Permission with Account*/
{
"permission" : {
"account" : "sandwich",
"permission" : "active"
},
weight : 75 /*Set the weight of a signature from this permission*/
}
//例如
'{"threshold":1,"keys":[{"key":"EOS8X7Mp7apQWtL6T2sfSZzBcQNUqZB7tARFEm9gA9Tn9nbMdsvBB","weight":1}],"accounts":[{"permission":{"actor":"acc2","permission":"active"},"weight":50}]}'
給賬戶添加自定義權限
./cleos set account permission alice upsert '{"threshold":1,"keys":[{"key":"EOS63gKbqNRZjboQyfXBJPijZHNr1GtXJu5eCan3e6iSqN7yP5nFZ","weight":1}],"accounts":[]}' owner -p alice@owner
將操作action的權限鏈接到自定義的權限
將呼叫upsert操作的授權與新創建的upsert權限關聯起來:
cleos set action permission alice addressbook upsert upsert
on_notify 屬性
on_notify當且僅當從指定的合約和指定的動作發送通知時,使用屬性注釋action可確保任何傳入通知被轉發到帶注釋的action,
callback類回呼回呼函式可以采用這種方法
[[eosio::on_notify("VALID_EOSIO_ACCOUNT_NAME::VALID_EOSIO_ACTION_NAME")]]
//例如
[[eosio::on_notify("eosio.token::transfer")]]
[[eosio::on_notify("eosio.token::transfer")]]
void on_token_transfer(name from, name to, assert quantity, std::string memo) {
// do something on eosio.token contract's transfer action from any account to the account where the contract is deployed.
}
[[eosio::on_notify("*::transfer")]]
void on_any_transfer(name from, name to, assert quantity, std::string memo) {
// do something on any contract's transfer action from any account to the account where the contract is deployed.
}
測驗
#轉賬時會觸發
./cleos transfer han hodl '0.0001 SYS' 'Hodl!' -p han@active
#觸發后查看表中資料
./cleos get table hodl han balance
單例表 eosio::singleton
eosio::singleton是 code和scope都用receiver的單例表, 底層還是 eosio::multi_index
#宣告
using singleton_type = eosio::singleton<"testtable"_n, testtable>;
singleton_type singleton_instance;
#在合約的建構式初始化串列中進行初始化
singleton_example( name receiver, name code, datastream<const char*> ds ) :
contract(receiver, code, ds),
singleton_instance(receiver, receiver.value) // (code, scope)
{}
#用法
singleton_instance.get_or_create(name, def) //獲取存盤在單例表中的值,如果它不存在,它將使用指定的默認值創建一個新的
singleton_instance.set(value, name) //為單例表設定新值 (name:為存盤的新值支付的帳戶)
singleton_instance.exists //檢查單例表是否存在
singleton_instance.get //獲取存盤在單例表中的值,如果不存在則拋出例外
筆記
-
RAM 是 EOSIO 區塊鏈上的持久系統資源,不屬于 Staking 機制的范圍,
-
選擇不使用 DPoS,則不需要系統資源
-
智能合約
- 可以在基于 EOSIO 的區塊鏈上部署無法修改的智能合約
- 表中有資料時不能修改其資料結構,如果您需要以任何方式更改表的資料結構,首先需要洗掉其所有行
- EOSIO 能夠按多達 16 個索引對表進行排序, 二級索引需要是數字欄位
- 可以在B合約內讀取A合約內的表內容
producers_table ptable("eosio"_n, name("eosio").value);
-
創建和鏈接自定義權限
- 創建自定義權限時,該權限將始終在父權限下創建,
-
cleos
- 使用
-d -j指示“不廣播”和“將交易作為 json 回傳”的選項cleos push action eosio.token issue '["alice", "100.0000 SYS", "memo"]' -p alice@active -d -j
- 查看token資訊
./cleos get currency balance eosio.token bob SYS./cleos get currency stats eosio.token SYS
- 使用
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/294545.html
標籤:區塊鏈
上一篇:從零搭建FPGA區塊鏈運算機——ASIC、GPU、FPGA對比
下一篇:區塊鏈應用簡介
