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 ...
随机推荐
- 04 Tensorflow的中的常量、变量和数据类型
打开Python Shell,先输入import tensorflow as tf,然后可以执行以下命令. Tensorflow中的常量创建方法: hello = tf.constant('Hello ...
- jQuery.prop , jQuery.attr ,jQuery.data
理一下这几个概念吧.根据jquery官网. jquery.prop 获取匹配的元素中第一个元素特定的属性值,或者是设置多个元素的属性值. 有4个重载. .prop(propertyName) 获取属性 ...
- Jenkins入门之新建任务
简单了解了Jenkins界面之后,下面我们简单介绍一下如何使用jenkins创建一个任务.打开Jenkins web管理界面之后,点击左侧最上方的NewItem图标 便会进入如下界面 产生要输入一个构 ...
- Perl中的自增、自减
自增和自减 perl也支持数值类型的自增和自减操作.不仅如此,还支持字符.字符串的自增.自减. 如果自增(++)和自减(--)符号放在数值的前面,则先增减,再返回: 如果自增(++)和自减(--)符号 ...
- SpringBoot系列——WebSocket
关于websocket的介绍与实现,我之前写过一篇博客,记录了用springboot-websocket实现了私聊.群聊的简单实例,这里就只提供一个入口,不再重复的写了,WebSocket+Java ...
- spark之JDBC开发(连接数据库测试)
spark之JDBC开发(连接数据库测试) 以下操作属于本地模式操作: 1.在Eclipse4.5中建立工程RDDToJDBC,并创建一个文件夹lib用于放置第三方驱动包 [hadoop@CloudD ...
- PHP 科学计数 转 Double
本文转自:https://stackoverflow.com/questions/4576927/convert-a-string-containing-a-number-in-scientific- ...
- 第一册:lesson fifty five。
原文: The Sawyer family. The Sawyers live at 87 King street. In the morning Mr.Sawyer goes to work and ...
- Python在Office 365 开发中的应用
我在昨天发布的文章 -- 简明 Python 教程:人生苦短,快用Python -- 中提到了Python已经在Office 365开发中全面受支持,有不同朋友留言或私信说想了解更加详细的说明,所以特 ...
- phpStorm ctrl+左键无法找到类
场景 在使用phpstrom时,通过commd+鼠标左键的方式找不到该类 报如下异常: Cannot load settings from file ‘/*/.idea/xdp_stat.iml': ...