EOS系列 - WASM智能合约 - 特性
构造函数
addressbook(name receiver, name code, datastream<const char*> ds):contract(receiver, code, ds) {}#单例表(code和scope都用receiver的表)也可在初始化列表中实例化
singleton_example( name receiver, name code, datastream<const char*> ds ) :contract(receiver, code, ds),singleton_instance(receiver, receiver.value){}
内置函数
- 权限检测函数
require_auth(name)
: 必须具有某权限, 没有就不会往下执行require_auth2(user.value, "active"_n.value)
: 指定必须某账户的某种权限has_auth(name)
: 判断是否具有某权限, 返回 true | false .eosio::check(eosio::has_auth(...), ...)
get_self
: 获取合约名称, 合约部署者get_code
|get_first_receiver()
: 部署此合约的帐户名称require_recipient(name)
: 发出通知, 将action复制到发件人- 调用
require_recipient
将一个帐户添加到 require_recipient 集合并确保这些帐户收到正在执行的action的通知
- 调用
check( is_account( oracleClient ), "Must have a valid oracleClient" )
: 断言is_account(name)
账号是否存在
EOSIO 名称类
- 适用于所有 EOSIO 编码的名称(帐户、操作、表等)
uint64_t
在区块链上编码为 64 位无符号整数 ( )。- 前 12 个字符(如果有)
base32
使用以下字符编码:.
,1-5
,a-z
- 第 13 个字符(如果适用)
base16
使用以下字符编码:.
,1-5
,a-j
例子:
auto eosio_user = eosio::name{user}; //encodes user string to eosio::name object
auto user_str = user_name_obj.to_string(); //decodes eosio::name obj to string
auto standard_account = "standardname"_n; //encodes literal string to eosio::name
auto non_standard_account = ".standard"_n; //encodes literal string to eosio::name
内联操作
内联操作在调用者操作的相同范围和权限内工作, 内联操作保证在同一个事务中执行
eosio.code
权限
该eosio.code
权限是一种伪权限,用于增强安全性,并使合约能够执行内联action。
例如: 为了从 addressbook
发送内联action,请将eosio.code
权限添加到合约帐户addressbook
的active权限中, 否则会报错 Authorization failure with inline action sent to self
#加权限
cleos set account permission addressbook active --add-code#删权限
cleos set account permission addressbook active --remove-code
内联action-本合约内
需要 eosio.code
action(//permission_level, 一个权限级别结构(需要将`eosio.code`权限添加到合约帐户的active权限中)//code, 要调用的合约,部署合约的帐户(使用eosio::name类型初始化)//action, action(使用eosio::name类型初始化)//data 传递给action的数据,与被调用的action相关的位置元组。
).send();action(permission_level{get_self(),"active"_n},get_self(),"notify"_n,std::make_tuple(user, name{user}.to_string() + message)
).send();
可通过 ./cleos get actions addressbook
命令查看
内联action-外部合约
使用 action_wrapper
, 需要 eosio.code
#A合约内声明
[[eosio::action]] void count(name user, std::string type){//可在此限定只有哪个账户/合约才能授权该命令require_auth(name("addressbook")); //只有addressbook合约才能成功执行此操作...
}
using count_action = action_wrapper<"count"_n, &abcounter::count>;#B合约内调用A合约
abcounter::count_action count("abcounter"_n, {get_self(), "active"_n});
count.send(user, type);
可通过 cleos get table abcounter abcounter counts --lower alice --limit 1
查看统计结果
创建自定义权限
介绍权限
/*The authority JSON object*/
{"threshold" : 100, /*An integer that defines cumulative signature weight required for authorization*/"keys" : [], /*An array made up of individual permissions defined with an EOS PUBLIC KEY*/"accounts" : [] /*An array made up of individual permissions defined with an EOS ACCOUNT*/
}/*Set Permission with Key*/
{"permission" : {"key" : "EOS8X7Mp7apQWtL6T2sfSZzBcQNUqZB7tARFEm9gA9Tn9nbMdsvBB","permission" : "active"},weight : 25 /*Set the weight of a signature from this permission*/
}/*Set Permission with Account*/
{"permission" : {"account" : "sandwich","permission" : "active"},weight : 75 /*Set the weight of a signature from this permission*/
}//例如
'{"threshold":1,"keys":[{"key":"EOS8X7Mp7apQWtL6T2sfSZzBcQNUqZB7tARFEm9gA9Tn9nbMdsvBB","weight":1}],"accounts":[{"permission":{"actor":"acc2","permission":"active"},"weight":50}]}'
给账户添加自定义权限
./cleos set account permission alice upsert '{"threshold":1,"keys":[{"key":"EOS63gKbqNRZjboQyfXBJPijZHNr1GtXJu5eCan3e6iSqN7yP5nFZ","weight":1}],"accounts":[]}' owner -p alice@owner
将操作action的权限链接到自定义的权限
将调用upsert
操作的授权与新创建的upsert
权限关联起来:
cleos set action permission alice addressbook upsert upsert
on_notify
属性
on_notify
当且仅当从指定的合约和指定的动作发送通知时,使用属性注释action可确保任何传入通知被转发到带注释的action。
callback
类回调回调函数可以采用这种方法
[[eosio::on_notify("VALID_EOSIO_ACCOUNT_NAME::VALID_EOSIO_ACTION_NAME")]]//例如
[[eosio::on_notify("eosio.token::transfer")]][[eosio::on_notify("eosio.token::transfer")]]
void on_token_transfer(name from, name to, assert quantity, std::string memo) {// do something on eosio.token contract's transfer action from any account to the account where the contract is deployed.
}[[eosio::on_notify("*::transfer")]]
void on_any_transfer(name from, name to, assert quantity, std::string memo) {// do something on any contract's transfer action from any account to the account where the contract is deployed.
}
测试
#转账时会触发
./cleos transfer han hodl '0.0001 SYS' 'Hodl!' -p han@active#触发后查看表中数据
./cleos get table hodl han balance
单例表 eosio::singleton
eosio::singleton
是 code和scope都用receiver的单例表, 底层还是 eosio::multi_index
#声明
using singleton_type = eosio::singleton<"testtable"_n, testtable>;
singleton_type singleton_instance;#在合约的构造函数初始化列表中进行初始化
singleton_example( name receiver, name code, datastream<const char*> ds ) :contract(receiver, code, ds),singleton_instance(receiver, receiver.value) // (code, scope){}#用法
singleton_instance.get_or_create(name, def) //获取存储在单例表中的值。如果它不存在,它将使用指定的默认值创建一个新的
singleton_instance.set(value, name) //为单例表设置新值 (name:为存储的新值支付的帐户)
singleton_instance.exists //检查单例表是否存在
singleton_instance.get //获取存储在单例表中的值。如果不存在则抛出异常
笔记
RAM 是 EOSIO 区块链上的持久系统资源,不属于 Staking 机制的范围。
选择不使用 DPoS,则不需要系统资源
智能合约
- 可以在基于 EOSIO 的区块链上部署无法修改的智能合约
- 表中有数据时不能修改其数据结构。如果您需要以任何方式更改表的数据结构,首先需要删除其所有行
- EOSIO 能够按多达 16 个索引对表进行排序, 二级索引需要是数字字段
- 可以在B合约内读取A合约内的表内容
producers_table ptable("eosio"_n, name("eosio").value);
创建和链接自定义权限
- 创建自定义权限时,该权限将始终在父权限下创建。
cleos
- 使用
-d -j
指示“不广播”和“将交易作为 json 返回”的选项cleos push action eosio.token issue '["alice", "100.0000 SYS", "memo"]' -p alice@active -d -j
- 查看token信息
./cleos get currency balance eosio.token bob SYS
./cleos get currency stats eosio.token SYS
- 使用
EOS系列 - WASM智能合约 - 特性相关推荐
- Eos的Wasm智能合约的局限性
官方只支持用C++写智能合约 用C++写智能合约门槛过高,会把许多开发者挡在门外,C++的复杂性也会让智能合约的设计变得困难. Wasm智能合约的效率并不是最优 由于C++最终也是编译成wasm字节码 ...
- 调用wasm_PDX Utopia区块链协议栈使用Solidity调用wasm智能合约
在这个瞬息万变的世界中,智能合约已成为所有平台中强有力的服务.Solidity是一种趋势,PDX Utopia区块链协议栈使用Solidity调用wasm智能合约. ▼ 什么是Solidity? So ...
- EOS开发HelloWorld智能合约
我们将介绍一个使用EOS智能合约构建hello World的例子. 一般环境设置通过上一篇文章已经说明,这方面的问题大家可以看本博客上一篇文章,本文引用了官方EOS在Git上的示例. 运行nodeos ...
- [EOS源码分析]5.EOS编写HelloWorld智能合约及各种坑
本文所有实践都是基于EOS dawn-v4.1.0,请切到该分支然后实践 切换命令:git checkout dawn-v4.1.0 HelloWorld源码 #include <eosioli ...
- EOS区块链 智能合约 教程1(发布合约)
EOS 智能合约 启动EOS单节点区块链 教程地址:https://github.com/EOSIO/eos/wiki/Local-Environment#2-building-eosio 命令: c ...
- EOS之记事本智能合约
EOS记事本智能合约 本次分享的内容是一个记事本合约,调用合约中的写入动作可以将文本和作者保存到数据库中,通过记事本合约来学习EOS智能合约数据存储当中的主键自增. 合约动作 写入动作 记事本合约必须 ...
- 【许晓笛】EOS 什么是智能合约(3)
详解 EOS 智能合约的 abi 文件 这次向大家介绍 eosio.token 智能合约的最后一个文件 -- abi文件.ABI 全称 Application Binary Interface,中文名 ...
- eos 连接mysql_EOS智能合约中数据库的使用与常见问题
阅读本文前,您需要熟悉eos节点的操作流程,熟悉cleos客户端基础指令,并且对自定义合约的开发有着一定的了解. 操作系统:MAC OS 10.13.x,EOSIO版本号:1.1.3 背景 在EOS自 ...
- EOS 智能合约源代码解读 (11)wrap合约“action_wrapper类”
1. 功能 对于许多用例,需要从合同代码向另一个合同发送新操作.这是合同之间能够积极沟通的唯一途径.它为特定智能合约代码的特定操作创建"操作模板",然后可使用该模板来调用此操作. ...
最新文章
- 如何给女朋友解释为什么Java里面的String对象是不可变的?
- java返回有什么用,java中的return this什么时候用,返回的是类里面的方法类型,还是实例类?上面的代码什么意思...
- Java int -1无符号右移_java中的无符号右移
- 从WildFly 9(子系统)中运行OkHttpClient
- python装饰器 property_介绍Python的@property装饰器的用法
- 微软这次开源的是 Windows 计算器
- 联合光伏:雨后复斜阳 关山阵阵苍
- 顾樵数学物理方法_圣彼得堡国立大学硕士研究生:物理与天文学
- 【串口服务器】串口转WIFI
- 计算机触摸板设置方法,笔记本触摸板设置,小编教你笔记本触摸板怎么设置
- 支付宝转账支付宝转卡(H5飞行模式)
- 论文阅读:(NIPS 2021)NeRV: Neural Representations for Videos
- qq空间java版_Java版 QQ空间自动登录无需拷贝cookie一天抓取30WQQ说说数据流程分析【转】...
- 微信小程序实现拼团成功动画
- oracle中rebuild,ORACLE中index的rebuild(转)
- flash player安装教程--亲测有效
- Windows电脑怎么格式化?
- 一条校招/社招潜规则~
- 七牛云这个API,让我轻松搞定Banner背景自动切换的功能
- Unity 3D光源-Spot Light聚光灯用法详解、模拟手电筒、台灯等线性教程
热门文章
- 学会给视频添加渐入、色彩变幻特效,简单几步骤做创意小视频
- 软件测试的主要阶段有哪些?
- MAC地址分类-----单播,组播,广播的特征
- 直接裁7000!任正非:我不要你觉得
- h5打开麦克风权限录音_h5打开麦克风权限录音_原来电脑上自带录音功能,很多人还不知道,真的太实用了......
- 机器学习——基础概念
- 什么是软件测试,软件测试的目的?
- 高通平台开发系列讲解(AtCoP篇)AtCoP架构简介
- 清华、复旦、武大……全国近30所高校,超200位学子将相聚世界区块链大会·武汉高校分论坛...
- [C语言]Pow函数的实现