EOS记事本智能合约

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

合约动作

  • 写入动作

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

  • 删除动作

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

合约代码

note.cpp

#include <eosiolib/eosio.hpp>
#include <string>using namespace eosio;using std::string;class record : public eosio::contract {
public:/// @abi table notes i64struct note {uint64_t        id;account_name    author;string          text;auto primary_key() const { return id; }};typedef multi_index<N(notes), note> notes;using contract::contract;/// @abi actionvoid write(account_name author, string text) {require_auth(author);print("Hello, ", name{author});notes nt( _self, _self );uint64_t noteId;nt.emplace(author, [&](auto &n) {n.id = nt.available_primary_key();// 主键自增n.author = author;n.text = text;noteId = n.id;});print("----noteId = ", noteId);}void remove(uint64_t id) {notes nt( _self, _self );auto it = nt.find( id );eosio_assert( it != nt.end(), "the id not found" );require_auth(it->author);nt.erase( it );}};EOSIO_ABI(record, (write)(remove))

合约涉及数据库操作部分

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

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

合约调用演示

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

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

  • cleos get table 查询表
$ cleos get table note note notes
{"rows": [{"id": 0,"author": "user","text": "This is my first diary"},{"id": 1,"author": "student","text": "my name is student!"},{"id": 2,"author": "miaomiao","text": "my name is miaomiao"}],"more": false
}
  • 调用remove动作

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

错误的授权:

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

正确的授权:

$ cleos push action note remove '{"id":2}' -p miaomiao
executed transaction: 51eb63f0fdb7d5d01676e898a0f9bc144ee1feda344780042782f359541a578d  192 bytes  442 us
#          note <= note::remove                 {"id":2}$ cleos get table note note notes
{"rows": [{"id": 0,"author": "user","text": "This is my first diary"},{"id": 1,"author": "student","text": "my name is student!"}],"more": false
}

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

转载于:https://www.cnblogs.com/tokenpai/p/9175960.html

EOS之记事本智能合约相关推荐

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

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

  2. 【许晓笛】EOS 什么是智能合约(3)

    详解 EOS 智能合约的 abi 文件 这次向大家介绍 eosio.token 智能合约的最后一个文件 -- abi文件.ABI 全称 Application Binary Interface,中文名 ...

  3. EOS系列 - WASM智能合约 - 特性

    构造函数 addressbook(name receiver, name code, datastream<const char*> ds):contract(receiver, code ...

  4. eos 连接mysql_EOS智能合约中数据库的使用与常见问题

    阅读本文前,您需要熟悉eos节点的操作流程,熟悉cleos客户端基础指令,并且对自定义合约的开发有着一定的了解. 操作系统:MAC OS 10.13.x,EOSIO版本号:1.1.3 背景 在EOS自 ...

  5. EOS开发HelloWorld智能合约

    我们将介绍一个使用EOS智能合约构建hello World的例子. 一般环境设置通过上一篇文章已经说明,这方面的问题大家可以看本博客上一篇文章,本文引用了官方EOS在Git上的示例. 运行nodeos ...

  6. [EOS源码分析]5.EOS编写HelloWorld智能合约及各种坑

    本文所有实践都是基于EOS dawn-v4.1.0,请切到该分支然后实践 切换命令:git checkout dawn-v4.1.0 HelloWorld源码 #include <eosioli ...

  7. EOS区块链 智能合约 教程1(发布合约)

    EOS 智能合约 启动EOS单节点区块链 教程地址:https://github.com/EOSIO/eos/wiki/Local-Environment#2-building-eosio 命令: c ...

  8. EOS智能合约:system系统合约源码分析

    链客,专为开发者而生,有问必答! 此文章来自区块链技术社区,未经允许拒绝转载. eosio.system 概览 笔者使用的IDE是VScode,首先来看eosio.system的源码结构.如下图所示. ...

  9. 十分钟教你开发EOS智能合约

    十分钟教你开发EOS智能合约 在CSDN.柏链道捷(PDJ Education).HelloEOS.中关村区块链产业联盟主办的「EOS入门及最新技术解读」专场沙龙上,柏链道捷(PDJ Educatio ...

最新文章

  1. c语言函数传参时候的类型强制装换
  2. VGG卷积神经网络模型加载与运行
  3. 小人大作战v0.02原型(单机)发布
  4. wxWidgets:wxLogFormatter类用法
  5. 操作软件_如何提升办公软件的操作能力
  6. 前端学习(2566):vue的生命周期
  7. Mybatis注解开发之@Results
  8. 2021年货节消费趋势报告
  9. android mac转数据格式转换,mac环境下Android 反编译
  10. java多层panel,java-在h:panelGrid中具有多个子组件的自定义Facelets-Tag
  11. c++的.o文件的链接顺序
  12. 数据挖掘 姓名用字特点 目录 1. 姓名用字特点 1 2. 男性姓名专用字210个(三字词,双字词都适用) 1 2.1. 男性姓名专用字 双字词适用317个 1 3. 女人姓名用字 2 3.1.
  13. 《人工智能:一种现代的方法》笔记(一)
  14. websockets_Websockets在数据工程中鲜为人知的模式
  15. GitHub上下载代码
  16. 谷歌浏览器,添加手机模拟器
  17. ASPNET 5 和 dnx commands
  18. ARM M3综合细节描述:
  19. VCS仿真学习(5)Debugging with DVE
  20. Java锁深入理解5——共享锁

热门文章

  1. org.apache.log4j.Logger详解
  2. Visual Studio 2013编译Mozilla NPAPI 示例注意事项
  3. 第八节 字符串的插入
  4. NASM学习之Windows下编写16位程序
  5. lvs直接路由模式简单部署
  6. Linux教程 网络管理命令Netstat的使用
  7. Scala for循环示例
  8. 已知bug列表——Solidity中文文档(12)
  9. 原来BCH是这样转给别人的
  10. redis在Linux上的安装