EOS记事本智能合约

本次分享的内容是一个记事本合约,调用合约中的写入动作可以将文本和作者保存到数据库中,通过记事本合约来学习EOS智能合约数据存储当中的主键自增。

合约动作

  • 写入动作

记事本合约必须要有的写入文本action,用来存储记录文本和记录作者。

  • 删除动作

记事本中同样需要有删除记录的action,用来删除记录信息。

合约代码

note.cpp

  1. #include <eosiolib/eosio.hpp>
  2. #include <string>
  3. using namespace eosio;
  4. using std::string;
  5. class record : public eosio::contract {
  6. public:
  7. /// @abi table notes i64
  8. struct note {
  9. uint64_t id;
  10. account_name author;
  11. string text;
  12. auto primary_key() const { return id; }
  13. };
  14. typedef multi_index<N(notes), note> notes;
  15. using contract::contract;
  16. /// @abi action
  17. void write(account_name author, string text) {
  18. require_auth(author);
  19. print("Hello, ", name{author});
  20. notes nt( _self, _self );
  21. uint64_t noteId;
  22. nt.emplace(author, [&](auto &n) {
  23. n.id = nt.available_primary_key();// 主键自增
  24. n.author = author;
  25. n.text = text;
  26. noteId = n.id;
  27. });
  28. print("----noteId = ", noteId);
  29. }
  30. void remove(uint64_t id) {
  31. notes nt( _self, _self );
  32. auto it = nt.find( id );
  33. eosio_assert( it != nt.end(), "the id not found" );
  34. require_auth(it->author);
  35. nt.erase( it );
  36. }
  37. };
  38. EOSIO_ABI(record, (write)(remove))

合约涉及数据库操作部分

  • 在表中增加记录:emplace
  • 删除一条数据:erase
  • 查询记录:find
  • 主键自增:available_primary_key

其中主键自增非常重要,不写主键自增会导致无法存入多条记录。

合约调用演示

  • 调用write动作
  1. $ cleos push action note write '{"author":"user","text":"This is my first diary"}' -p user
  2. executed transaction: ab59fc4e04342690af46d5bf4dd48c8418d4655e8bcaea81ca3fdc0c99b6fed7 216 bytes 511 us
  3. # note <= note::write {"author":"user","text":"This is my first diary"}
  4. >> Hello, user----noteId = 0
  5. warning: transaction executed locally, but may not be confirmed by the network yet

调用成功会返回信息,其中noteId是记录的id,在删除记录的时候需要用到。

  • cleos get table 查询表
  1. $ cleos get table note note notes
  2. {
  3. "rows": [{
  4. "id": 0,
  5. "author": "user",
  6. "text": "This is my first diary"
  7. },{
  8. "id": 1,
  9. "author": "student",
  10. "text": "my name is student!"
  11. },{
  12. "id": 2,
  13. "author": "miaomiao",
  14. "text": "my name is miaomiao"
  15. }
  16. ],
  17. "more": false
  18. }
  • 调用remove动作

删除时进行了授权限制,每个账户只能删除自己的记录,无法删除其他账户的记录

错误的授权:

  1. $ cleos push action note remove '{"id":2}' -p user
  2. Error 3090004: missing required authority
  3. Ensure that you have the related authority inside your transaction!;
  4. If you are currently using 'cleos push action' command, try to add the relevant authority using -p option.
  5. Error Details:
  6. missing authority of miaomiao

正确的授权:

  1. $ cleos push action note remove '{"id":2}' -p miaomiao
  2. executed transaction: 51eb63f0fdb7d5d01676e898a0f9bc144ee1feda344780042782f359541a578d 192 bytes 442 us
  3. # note <= note::remove {"id":2}
  4. $ cleos get table note note notes
  5. {
  6. "rows": [{
  7. "id": 0,
  8. "author": "user",
  9. "text": "This is my first diary"
  10. },{
  11. "id": 1,
  12. "author": "student",
  13. "text": "my name is student!"
  14. }
  15. ],
  16. "more": false
  17. }

在编写记事本合约时为了找到让主键增加的方法差了很多资料,也走了很多弯路。最后发现其实就是一行代码就能解决的事情。主键自增的使用详见这里

EOS之记事本智能合约的更多相关文章

  1. EOS之hello智能合约解析

    传送门: 柏链项目学院   EOS的智能合约与以太坊区别很大, EOS 的智能合约基于 WebAssembly(WASM) 技术执行用户生成的应用程序和代码.WASM是一项新兴的网络标准,得到了谷歌, ...

  2. Eos的Wasm智能合约的局限性

    官方只支持用C++写智能合约 用C++写智能合约门槛过高,会把许多开发者挡在门外,C++的复杂性也会让智能合约的设计变得困难. Wasm智能合约的效率并不是最优 由于C++最终也是编译成wasm字节码 ...

  3. EOS测试链智能合约部署调用

    ETH与EOS两者智能合约进行简单的对比. 1.编译智能合约(合约编译成.wasm与.abi格式后即可部署到区块链) [root@C03-12U-26 testcontract]# cat testc ...

  4. 【精】EOS智能合约:system系统合约源码分析

    系统合约在链启动阶段就会被部署,是因为系统合约赋予了EOS链资源.命名拍卖.基础数据准备.生产者信息.投票等能力.本篇文章将会从源码角度详细研究system合约. 关键字:EOS,eosio.syst ...

  5. EOS技术研究:合约与数据库交互

    智能合约操作链数据库是很常见的应用场景.EOS提供了专门的工具来做这件事(相当于Ethereum的leveldb),专业术语叫做持久化API,本文将完整严密地介绍这个工具以及对它的使用测试. 关键字: ...

  6. EOS基础全家桶(十一)智能合约IDE-EOS_Studio

    简介 我们马上要进入智能合约的开发了,以太坊最初提供了智能合约的功能,并宣告区块链进入2.0时代,而EOS的智能合约更进一步,提供了更多的便利性和可能性.为了进一步了解智能合约,并进行开发,我们需要先 ...

  7. 【精解】EOS智能合约演练

    EOS,智能合约,abi,wasm,cleos,eosiocpp,开发调试,钱包,账户,签名权限 热身 本文旨在针对EOS智能合约进行一个完整的实操演练,过程中深入熟悉掌握整个EOS智能合约的流程,过 ...

  8. [转]EOS智能合约 & 私链激活 & 基本操作

    链接:https://www.jianshu.com/p/90dea623ffdf 简介 本篇文章,将跟大家介绍eos私链的激活.基础智能合约的安装,以及为大家演示转账等基础操作.还没有安装eos私链 ...

  9. EOS智能合约存储实例讲解

    EOS智能合约存储实例 智能合约中的基础功能之一是token在某种规则下转移.以EOS提供的token.cpp为例,定义了eos token的数据结构:typedef eos::token<ui ...

随机推荐

  1. 04 Tensorflow的中的常量、变量和数据类型

    打开Python Shell,先输入import tensorflow as tf,然后可以执行以下命令. Tensorflow中的常量创建方法: hello = tf.constant('Hello ...

  2. jQuery.prop , jQuery.attr ,jQuery.data

    理一下这几个概念吧.根据jquery官网. jquery.prop 获取匹配的元素中第一个元素特定的属性值,或者是设置多个元素的属性值. 有4个重载. .prop(propertyName) 获取属性 ...

  3. Jenkins入门之新建任务

    简单了解了Jenkins界面之后,下面我们简单介绍一下如何使用jenkins创建一个任务.打开Jenkins web管理界面之后,点击左侧最上方的NewItem图标 便会进入如下界面 产生要输入一个构 ...

  4. Perl中的自增、自减

    自增和自减 perl也支持数值类型的自增和自减操作.不仅如此,还支持字符.字符串的自增.自减. 如果自增(++)和自减(--)符号放在数值的前面,则先增减,再返回: 如果自增(++)和自减(--)符号 ...

  5. SpringBoot系列——WebSocket

    关于websocket的介绍与实现,我之前写过一篇博客,记录了用springboot-websocket实现了私聊.群聊的简单实例,这里就只提供一个入口,不再重复的写了,WebSocket+Java ...

  6. spark之JDBC开发(连接数据库测试)

    spark之JDBC开发(连接数据库测试) 以下操作属于本地模式操作: 1.在Eclipse4.5中建立工程RDDToJDBC,并创建一个文件夹lib用于放置第三方驱动包 [hadoop@CloudD ...

  7. PHP 科学计数 转 Double

    本文转自:https://stackoverflow.com/questions/4576927/convert-a-string-containing-a-number-in-scientific- ...

  8. 第一册:lesson fifty five。

    原文: The Sawyer family. The Sawyers live at 87 King street. In the morning Mr.Sawyer goes to work and ...

  9. Python在Office 365 开发中的应用

    我在昨天发布的文章 -- 简明 Python 教程:人生苦短,快用Python -- 中提到了Python已经在Office 365开发中全面受支持,有不同朋友留言或私信说想了解更加详细的说明,所以特 ...

  10. phpStorm ctrl+左键无法找到类

    场景 在使用phpstrom时,通过commd+鼠标左键的方式找不到该类 报如下异常: Cannot load settings from file ‘/*/.idea/xdp_stat.iml': ...