基于EOS3.0實現(xiàn)todolist上篇-智能合約

環(huán)境 ubuntu16.04 64位

一 編寫todolist的合約代碼

mkdir todo
vim todo.cpp 輸入以下合約內容

#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
using namespace eosio;

class todo_contract : public eosio::contract {
public:
    todo_contract(account_name self)
    :eosio::contract(self),
    todos(_self, _self)
    {}

    //@abi action
    void create(account_name author, const uint32_t id, const std::string& description){
        todos.emplace(author, [&](auto& new_todo) {
            new_todo.id = id;
            new_todo.description = description;
            new_todo.completed = 0;
        });
    
        eosio::print("todo#", id, " created");
    }

    //@abi action
    void complete(account_name author, const uint32_t id) {
        auto todo_lookup = todos.find(id);
        eosio_assert(todo_lookup != todos.end(), "Todo does not exist");
        todos.modify(todo_lookup, author, [&](auto& modifiable_todo){
            modifiable_todo.completed = 1;
        });
    
        eosio::print("todo#", id, " marked as complete");
    }

    //@abi action
    void destroy(account_name author, const uint32_t id) {
        auto todo_lookup = todos.find(id);
        todos.erase(todo_lookup);
    
        eosio::print("todo#", id, " destroyed");
    }

private:
    //@abi table todos i64
    struct todo{
        uint64_t id;               //結構體id
        std::string description;   //todolist的描述信息
        uint64_t completed;        //todolist的完成情況,0未完成  1完成
    
        //主鍵函數(shù)
        uint64_t primary_key() const {return id;}
        //聲明序列化宏公般,第一個參數(shù)todo為結構體名稱   (id)(description)(completed)為結構體內的對象
        EOSLIB_SERIALIZE(todo, (id)(description)(completed))

    };

    typedef eosio::multi_index<N(todos), todo> todo_table; //聲明自定義的多索引容器類型; 
    todo_table todos;
};

EOSIO_ABI(todo_contract, (create)(complete)(destroy))

備注:“typedef eosio::multi_index<N(todos), todo> todo_table; //聲明自定義的多索引容器類型;
todo_table todos;”聲明完成后砰琢,可以使用以下功能:

todos.emplace(scope, [&]( auto& g ) { ... }) 添加數(shù)據(jù);
todos.find(primary_key) 用關鍵字查找履植;
todos.modify(itr, scope, [&]( auto& g ) { ... }) 修改數(shù)據(jù)计雌;
todos.erase(itr) 刪除
todos.begin() 數(shù)據(jù)起始
todos.end() 數(shù)據(jù)末尾

二 運行eos客戶端,啟動私有網(wǎng)絡

nodeos -e -p eosio --plugin eosio::wallet_api_plugin --plugin eosio::chain_api_plugin --plugin eosio::account_history_api_plugin

eosio generated block bcc817bc... #637 @ 2018-05-10T09:22:13.000 with 0 trxs, lib: 636
eosio generated block 527d07ae... #638 @ 2018-05-10T09:22:13.500 with 0 trxs, lib: 637

三 編譯合約

eosiocpp -o todo.wast todo.cpp && eosiocpp -g todo.abi todo.cpp

In file included from todo.cpp:1:
In file included from /usr/local/include/eosiolib/eosio.hpp:7:
In file included from /usr/local/include/eosiolib/action.hpp:7:
In file included from /usr/local/include/eosiolib/datastream.hpp:9:
In file included from /home/li/opt/boost_1_66_0/include/boost/container/flat_map.hpp:26:
In file included from /home/li/opt/boost_1_66_0/include/boost/container/new_allocator.hpp:24:
/home/li/opt/boost_1_66_0/include/boost/container/throw_exception.hpp:56:21: warning: 
      address of array 'msg' will always evaluate to 'true'
      [-Wpointer-bool-conversion]
      BOOST_ASSERT(!msg);
                   ~^~~
/home/li/opt/boost_1_66_0/include/boost/assert.hpp:60:36: note: expanded from
      macro 'BOOST_ASSERT'
# define BOOST_ASSERT(expr) assert(expr)
                                   ^~~~
/usr/local/include/musl/upstream/include/assert.h:8:28: note: expanded from
      macro 'assert'
#define assert(x) ((void)((x) || (__assert_fail(#x, __FILE__, __LINE__, ...
                           ^
In file included from todo.cpp:1:
In file included from /usr/local/include/eosiolib/eosio.hpp:7:
In file included from /usr/local/include/eosiolib/action.hpp:7:
In file included from /usr/local/include/eosiolib/datastream.hpp:9:
In file included from /home/li/opt/boost_1_66_0/include/boost/container/flat_map.hpp:26:
In file included from /home/li/opt/boost_1_66_0/include/boost/container/new_allocator.hpp:24:
/home/li/opt/boost_1_66_0/include/boost/container/throw_exception.hpp:64:25: warning: 
      address of array 'msg' will always evaluate to 'true'
      [-Wpointer-bool-conversion]
      BOOST_ASSERT_MSG(!msg, str);
                       ~^~~
/home/li/opt/boost_1_66_0/include/boost/assert.hpp:61:46: note: expanded from
      macro 'BOOST_ASSERT_MSG'
# define BOOST_ASSERT_MSG(expr, msg) assert((expr)&&(msg))
                                             ^~~~
/usr/local/include/musl/upstream/include/assert.h:8:28: note: expanded from
      macro 'assert'
#define assert(x) ((void)((x) || (__assert_fail(#x, __FILE__, __LINE__, ...
                           ^
In file included from todo.cpp:1:
In file included from /usr/local/include/eosiolib/eosio.hpp:7:
In file included from /usr/local/include/eosiolib/action.hpp:7:
In file included from /usr/local/include/eosiolib/datastream.hpp:9:
In file included from /home/li/opt/boost_1_66_0/include/boost/container/flat_map.hpp:26:
In file included from /home/li/opt/boost_1_66_0/include/boost/container/new_allocator.hpp:24:
/home/li/opt/boost_1_66_0/include/boost/container/throw_exception.hpp:72:25: warning: 
      address of array 'msg' will always evaluate to 'true'
      [-Wpointer-bool-conversion]
      BOOST_ASSERT_MSG(!msg, str);
                       ~^~~
/home/li/opt/boost_1_66_0/include/boost/assert.hpp:61:46: note: expanded from
      macro 'BOOST_ASSERT_MSG'
# define BOOST_ASSERT_MSG(expr, msg) assert((expr)&&(msg))
                                             ^~~~
/usr/local/include/musl/upstream/include/assert.h:8:28: note: expanded from
      macro 'assert'
#define assert(x) ((void)((x) || (__assert_fail(#x, __FILE__, __LINE__, ...
                           ^
In file included from todo.cpp:1:
In file included from /usr/local/include/eosiolib/eosio.hpp:7:
In file included from /usr/local/include/eosiolib/action.hpp:7:
In file included from /usr/local/include/eosiolib/datastream.hpp:9:
In file included from /home/li/opt/boost_1_66_0/include/boost/container/flat_map.hpp:26:
In file included from /home/li/opt/boost_1_66_0/include/boost/container/new_allocator.hpp:24:
/home/li/opt/boost_1_66_0/include/boost/container/throw_exception.hpp:80:25: warning: 
      address of array 'msg' will always evaluate to 'true'
      [-Wpointer-bool-conversion]
      BOOST_ASSERT_MSG(!msg, str);
                       ~^~~
/home/li/opt/boost_1_66_0/include/boost/assert.hpp:61:46: note: expanded from
      macro 'BOOST_ASSERT_MSG'
# define BOOST_ASSERT_MSG(expr, msg) assert((expr)&&(msg))
                                             ^~~~
/usr/local/include/musl/upstream/include/assert.h:8:28: note: expanded from
      macro 'assert'
#define assert(x) ((void)((x) || (__assert_fail(#x, __FILE__, __LINE__, ...
                           ^
In file included from todo.cpp:1:
In file included from /usr/local/include/eosiolib/eosio.hpp:7:
In file included from /usr/local/include/eosiolib/action.hpp:7:
In file included from /usr/local/include/eosiolib/datastream.hpp:9:
In file included from /home/li/opt/boost_1_66_0/include/boost/container/flat_map.hpp:26:
In file included from /home/li/opt/boost_1_66_0/include/boost/container/new_allocator.hpp:24:
/home/li/opt/boost_1_66_0/include/boost/container/throw_exception.hpp:88:25: warning: 
      address of array 'msg' will always evaluate to 'true'
      [-Wpointer-bool-conversion]
      BOOST_ASSERT_MSG(!msg, str);
                       ~^~~
/home/li/opt/boost_1_66_0/include/boost/assert.hpp:61:46: note: expanded from
      macro 'BOOST_ASSERT_MSG'
# define BOOST_ASSERT_MSG(expr, msg) assert((expr)&&(msg))
                                             ^~~~
/usr/local/include/musl/upstream/include/assert.h:8:28: note: expanded from
      macro 'assert'
#define assert(x) ((void)((x) || (__assert_fail(#x, __FILE__, __LINE__, ...
                           ^
5 warnings generated.
Generated todo.abi ...

四 部署合約

cleos set contract bytemaster ../todo -p bytemaster

Reading WAST...
Assembling WASM...
Publishing contract...
executed transaction: d1713c2589a8fffe5a54b7312dc10aff3b9e45cc74ec8f9556616213a86d7747  4712 bytes  2200576 cycles
#         eosio <= eosio::setcode               {"account":"bytemaster","vmtype":0,"vmversion":0,"code":"0061736d01000000017e1460047f7e7f7f0060037f7...
#         eosio <= eosio::setabi                {"account":"bytemaster","abi":{"types":[],"structs":[{"name":"todo","base":"","fields":[{"name":"id"...

注意:
(1)如錢包鎖定,請使用命令解鎖“ cleos wallet unlock --password 錢包私鑰”玫霎,不然提示

Error 3030002: signatures do not satisfy declared authorizations
Ensure that you have the related private keys inside your wallet and you wallet is unlocked.

(2)用戶用上一章創(chuàng)建的bytemaster,如未創(chuàng)建凿滤,參考《使用EOS3.0發(fā)行代幣》創(chuàng)建用戶妈橄。(http://www.reibang.com/p/596593bd308f

五 調用合約create action

cleos push action bytemaster create '["bytemaster", 1, "hello world Message"]' -p bytemaster

executed transaction: fa3b73feba3475e444be13992403f31161bc39c678f00bdb060ae26e30dce8ff  256 bytes  106496 cycles
#    bytemaster <= bytemaster::create           {"author":"bytemaster","id":1,"description":"hello world Message"}
>> todo#1 created

獲取數(shù)據(jù)
cleos get table bytemaster bytemaster todos

{
  "rows": [{
      "id": 1,
      "description": "hello world Message",
      "completed": 0
    }
  ],
  "more": false
}

六 總結

(1)了解多索引容器使用,了解數(shù)據(jù)增刪改
(2)了解智能合約編寫結構
(3)鞏固智能合約編寫發(fā)布流程

七 引用

https://steemit.com/eos/@eos-asia/part-2-building-a-to-do-list-with-eos-or-working-with-persistent-data-in-eos 《Part 2: Building a To-do list with EOS》

https://mp.weixin.qq.com/s/QVzKIrhYpw7JdrWe38TEJw 《[EOS智能合約]第二節(jié):用EOS開發(fā)一個To-do List小應用》

https://github.com/EOSIO/eos/wiki/Persistence-API 《Persistence-API》
https://eosfans.io/wiki/eosiodb 《EOSIO 3.0 數(shù)據(jù)庫使用指南

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末翁脆,一起剝皮案震驚了整個濱河市眷蚓,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌反番,老刑警劉巖沙热,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異罢缸,居然都是意外死亡篙贸,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進店門枫疆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來爵川,“玉大人,你說我怎么就攤上這事息楔⊙丬剑” “怎么了?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵钞螟,是天一觀的道長兔甘。 經(jīng)常有香客問我,道長鳞滨,這世上最難降的妖魔是什么洞焙? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮拯啦,結果婚禮上澡匪,老公的妹妹穿的比我還像新娘。我一直安慰自己褒链,他們只是感情好唁情,可當我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著甫匹,像睡著了一般甸鸟。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上兵迅,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天抢韭,我揣著相機與錄音,去河邊找鬼恍箭。 笑死刻恭,一個胖子當著我的面吹牛,可吹牛的內容都是我干的扯夭。 我是一名探鬼主播鳍贾,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼鞍匾,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了骑科?” 一聲冷哼從身側響起候学,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎纵散,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體隐圾,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡伍掀,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了暇藏。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蜜笤。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖盐碱,靈堂內的尸體忽然破棺而出把兔,到底是詐尸還是另有隱情,我是刑警寧澤瓮顽,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布县好,位于F島的核電站,受9級特大地震影響暖混,放射性物質發(fā)生泄漏缕贡。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一拣播、第九天 我趴在偏房一處隱蔽的房頂上張望晾咪。 院中可真熱鬧,春花似錦贮配、人聲如沸谍倦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽昼蛀。三九已至,卻和暖如春圆存,著一層夾襖步出監(jiān)牢的瞬間曹洽,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工辽剧, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留送淆,地道東北人。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓怕轿,卻偏偏與公主長得像偷崩,于是被迫代替她去往敵國和親辟拷。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,914評論 2 355

推薦閱讀更多精彩內容