EOS之记事本智能合约
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 i64
struct 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 action
void 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
}
在编写记事本合约时为了找到让主键增加的方法差了很多资料,也走了很多弯路。最后发现其实就是一行代码就能解决的事情。主键自增的使用详见这里。

EOS之记事本智能合约的更多相关文章
- EOS之hello智能合约解析
传送门: 柏链项目学院 EOS的智能合约与以太坊区别很大, EOS 的智能合约基于 WebAssembly(WASM) 技术执行用户生成的应用程序和代码.WASM是一项新兴的网络标准,得到了谷歌, ...
- Eos的Wasm智能合约的局限性
官方只支持用C++写智能合约 用C++写智能合约门槛过高,会把许多开发者挡在门外,C++的复杂性也会让智能合约的设计变得困难. Wasm智能合约的效率并不是最优 由于C++最终也是编译成wasm字节码 ...
- EOS测试链智能合约部署调用
ETH与EOS两者智能合约进行简单的对比. 1.编译智能合约(合约编译成.wasm与.abi格式后即可部署到区块链) [root@C03-12U-26 testcontract]# cat testc ...
- 【精】EOS智能合约:system系统合约源码分析
系统合约在链启动阶段就会被部署,是因为系统合约赋予了EOS链资源.命名拍卖.基础数据准备.生产者信息.投票等能力.本篇文章将会从源码角度详细研究system合约. 关键字:EOS,eosio.syst ...
- EOS技术研究:合约与数据库交互
智能合约操作链数据库是很常见的应用场景.EOS提供了专门的工具来做这件事(相当于Ethereum的leveldb),专业术语叫做持久化API,本文将完整严密地介绍这个工具以及对它的使用测试. 关键字: ...
- EOS基础全家桶(十一)智能合约IDE-EOS_Studio
简介 我们马上要进入智能合约的开发了,以太坊最初提供了智能合约的功能,并宣告区块链进入2.0时代,而EOS的智能合约更进一步,提供了更多的便利性和可能性.为了进一步了解智能合约,并进行开发,我们需要先 ...
- 【精解】EOS智能合约演练
EOS,智能合约,abi,wasm,cleos,eosiocpp,开发调试,钱包,账户,签名权限 热身 本文旨在针对EOS智能合约进行一个完整的实操演练,过程中深入熟悉掌握整个EOS智能合约的流程,过 ...
- [转]EOS智能合约 & 私链激活 & 基本操作
链接:https://www.jianshu.com/p/90dea623ffdf 简介 本篇文章,将跟大家介绍eos私链的激活.基础智能合约的安装,以及为大家演示转账等基础操作.还没有安装eos私链 ...
- EOS智能合约存储实例讲解
EOS智能合约存储实例 智能合约中的基础功能之一是token在某种规则下转移.以EOS提供的token.cpp为例,定义了eos token的数据结构:typedef eos::token<ui ...
随机推荐
- leetcode — first-missing-positive
/** * * Source : https://oj.leetcode.com/problems/first-missing-positive/ * * Created by lverpeng on ...
- Perl回调函数和闭包
在Perl中,子程序的引用常用来做回调函数(callback).闭包(closure),特别是匿名子程序. 回调函数(callback) 关于什么是回调函数,见一文搞懂:词法作用域.动态作用域.回调函 ...
- 多选穿梭框总结 (vue + element)
博客地址:https://ainyi.com/23 示例 介绍 实现省市区三级多选联动,可任选一个省级.市级.区级,加入已选框,也可以在已选框中删除对应的区域. 选择对应仓库,自动勾选仓库对应的省,取 ...
- C#调用C++(QT5.5.1项目)的C++/CLI(CLR项目)项目技术笔记
导航 1.编译环境 2.项目配置 1.设置附加包含目录 2.设置附加库目录 3.设置附加依赖项 3.CLR中各种定义 1.接口定义 2.类定义 3.枚举定义 4.属性定义 4.CLR中各种使用 1.类 ...
- MVC 视图助手书写规范及注意点
@Html.TextBoxFor() 讲解(其他类似的 @Html.LabelFor 等)同理 @Html.TextBoxFor(model => model.SearchParams.Name ...
- 从零开始学安全(六)●黑客常用的Dos命令
cd 文件路径 要切换的路径cd \ 直接回根目录dir ...
- php的依赖注入容器
这里接着上一篇 php依赖注入,直接贴出完整代码如下: <?php class C { public function doSomething() { echo __METHOD__, '我是C ...
- sqoop将mysql数据导入hbase、hive的常见异常处理
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10700700.html,否则将追究法律责任!!! 一.需求: 1.将以下这张表(test_ ...
- Java基础篇——JVM之GC原理(干货满满)
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10697554.html ,多多支持哈! 一.什么是GC? GC是垃圾收集的意思,内存处理是 ...
- python基础学习(十)字符串
字符串的定义 字符串 就是 一串字符,是编程语言中表示文本的数据类型 在 Python 中可以使用 一对双引号 " 或者 一对单引号 ' 定义一个字符串 虽然可以使用 \" 或者 ...