EOS测试链智能合约部署调用
ETH与EOS两者智能合约进行简单的对比。

1、编译智能合约(合约编译成.wasm与.abi格式后即可部署到区块链)
[root@C03-12U-26 testcontract]# cat testcontract.cpp
#include <eosiolib/eosio.hpp> class [[eosio::contract]] testcontract : public eosio::contract
{
public:
testcontract( eosio::name receiver, eosio::name code, eosio::datastream<const char*> ds )
: eosio::contract(receiver, code, ds), _students(receiver, code.value)
{} //添加学生
[[eosio::action]]
void add(eosio::name user, std::string studentName,std::string studentGender,uint32_t studentAge,uint32_t studentHeight)
{
require_auth(user);
eosio::print("Add student ", studentName);
_students.emplace(get_self(), [&](auto& p) {
p.id = _students.available_primary_key();
p.studentName = studentName;
p.studentGender = studentGender;
p.studentAge = studentAge;
p.studentHeight = studentHeight;
});
} //根据主键删除学生
[[eosio::action]]
void del(eosio::name user, uint64_t id)
{
require_auth(user);
eosio::print("Del student", id);
auto itr = _students.find(id);
if (itr != _students.end()) {
_students.erase(itr);
}
} //根据主键修改学生
[[eosio::action]]
void update(eosio::name user, uint64_t id,std::string studentName,std::string studentGender,uint32_t studentAge,uint32_t studentHeight)
{
require_auth(user);
eosio::print("update student", studentName);
auto itr = _students.find(id);
if (itr != _students.end())
{
_students.modify(itr, user, [&]( auto& p ) {
p.studentName = studentName;
p.studentGender = studentGender;
p.studentAge = studentAge;
p.studentHeight = studentHeight;
});
}
} //清空表,待定
[[eosio::action]]
void clear(eosio::name user)
{
require_auth(user);
std::vector<uint64_t> keysForDeletion;
for(auto& item : _students) {
keysForDeletion.push_back(item.id);
} for (uint64_t key : keysForDeletion) {
eosio::print("remove from _students ", key);
auto itr = _students.find(key);
if (itr != _students.end()) {
_students.erase(itr);
}
}
} //private: -- not private so the cleos get table call can see the table data.
// create the multi index tables to store the data
struct [[eosio::table]] students
{
uint64_t id; // primary key
std::string studentName;
std::string studentGender;
uint32_t studentAge;
uint32_t studentHeight; uint64_t primary_key() const { return id; }
uint64_t by_age() const {return studentAge; }
}; //数据表根据age排序
typedef eosio::multi_index<"students"_n, students, eosio::indexed_by<"studentage"_n, eosio::const_mem_fun<students, uint64_t, &students::by_age>>> studentstable; //students数据库表
studentstable _students;
}; EOSIO_DISPATCH( testcontract, (add)(del)(update)(clear))

2、部署智能合约
解锁账号钱包
cleos -u https://jungle2.cryptolions.io:443 wallet unlock -n zmcheng-wallet
部署合约
cleos -u https://jungle2.cryptolions.io:443 set contract zmcheng12345 /root/github.com/testcontract -p zmcheng12345@active
Reading WASM from /root/github.com/testcontract/testcontract.wasm...
Publishing contract...
executed transaction: 785f421fe911a142b7cc4b15131cf3f4947847531ed8c52150aa148fe405ea0d 7536 bytes 1560 us
# eosio <= eosio::setcode {"account":"zmcheng12345","vmtype":0,"vmversion":0,"code":"0061736d0100000001d0012060000060037f7e7e0...
# eosio <= eosio::setabi {"account":"zmcheng12345","abi":"0e656f73696f3a3a6162692f312e3100050361646400050475736572046e616d650...
warning: transaction executed locally, but may not be confirmed by the network yet
调用合约(增删改查)
[root@C03-12U-26 testcontract]# cleos -u https://jungle2.cryptolions.io:443 push action zmcheng12345 add '["zmcheng12345","zmcheng","male",25,170]' -p zmcheng12345@active
executed transaction: a64271659aea1ce07fd05a4ff1bb0188c441de04263f3a18b819cbee205d2bbd 128 bytes 273 us
# zmcheng12345 <= zmcheng12345::add {"user":"zmcheng12345","studentName":"zmcheng","studentGender":"male","studentAge":25,"studentHeight...
>> Add student zmcheng
warning: transaction executed locally, but may not be confirmed by the network yet ]
[root@C03-12U-26 testcontract]# cleos -u https://jungle2.cryptolions.io:443 get table zmcheng12345 zmcheng12345 students
{
"rows": [{
"id": 0,
"studentName": "zmcheng",
"studentGender": "male",
"studentAge": 25,
"studentHeight": 170
},{
"id": 1,
"studentName": "www",
"studentGender": "male",
"studentAge": 26,
"studentHeight": 170
}
],
"more": false
}
[root@C03-12U-26 testcontract]# cleos -u https://jungle2.cryptolions.io:443 push action zmcheng12345 update '["zmcheng12345",1,"www","male",12,123]' -p zmcheng12345@active
executed transaction: 765c35452964de54771c3fe505daad272a8a53ec61954a2cc43350f7f84e86e3 128 bytes 213 us
# zmcheng12345 <= zmcheng12345::update {"user":"zmcheng12345","id":1,"studentName":"www","studentGender":"male","studentAge":12,"studentHei...
>> update studentwww
warning: transaction executed locally, but may not be confirmed by the network yet ]
[root@C03-12U-26 testcontract]# cleos -u https://jungle2.cryptolions.io:443 get table zmcheng12345 zmcheng12345 students
{
"rows": [{
"id": 0,
"studentName": "zmcheng",
"studentGender": "male",
"studentAge": 25,
"studentHeight": 170
},{
"id": 1,
"studentName": "www",
"studentGender": "male",
"studentAge": 12,
"studentHeight": 123
}
],
"more": false
}
[root@C03-12U-26 testcontract]#
[root@C03-12U-26 testcontract]# cleos -u https://jungle2.cryptolions.io:443 push action zmcheng12345 del '["zmcheng12345",1]' -p zmcheng12345@active
executed transaction: 1a64a42ebccac0f9b7061c42a0fa8daaebe7b1a18db1cb058fe479608ee9170a 112 bytes 255 us
# zmcheng12345 <= zmcheng12345::del {"user":"zmcheng12345","id":1}
>> Del student1
warning: transaction executed locally, but may not be confirmed by the network yet ]
[root@C03-12U-26 testcontract]# cleos -u https://jungle2.cryptolions.io:443 get table zmcheng12345 zmcheng12345 students
{
"rows": [{
"id": 0,
"studentName": "zmcheng",
"studentGender": "male",
"studentAge": 25,
"studentHeight": 170
}
],
"more": false
}
EOS测试链智能合约部署调用的更多相关文章
- Solidity truffle,部署合约到Ropsten测试链或主链,调用合约(转)
Solidity truffle,部署合约到Ropsten测试链或主链,调用合约 转 https://blog.csdn.net/houyanhua1/article/details/89010896 ...
- EOS智能合约开发(四):智能合约部署及调试(附编程示例)
EOS智能合约开发(一):EOS环境搭建和创建节点 EOS智能合约开发(二):EOS创建和管理钱包 EOS智能合约开发(三):EOS创建和管理账号 部署智能合约的示例代码如下: $ cleos set ...
- 使用 Browser-solidity 在 Go-Ethereum1.7.2 上进行简单的智能合约部署
目录 目录 1.基本概念 1.1.什么是智能合约? 1.2.什么是Solidity? 1.2.1.Solidity的语言特性 1.3.什么是 Browser-solidity? 2.Browser-s ...
- EOS之hello智能合约解析
传送门: 柏链项目学院 EOS的智能合约与以太坊区别很大, EOS 的智能合约基于 WebAssembly(WASM) 技术执行用户生成的应用程序和代码.WASM是一项新兴的网络标准,得到了谷歌, ...
- Eos的Wasm智能合约的局限性
官方只支持用C++写智能合约 用C++写智能合约门槛过高,会把许多开发者挡在门外,C++的复杂性也会让智能合约的设计变得困难. Wasm智能合约的效率并不是最优 由于C++最终也是编译成wasm字节码 ...
- Remix+Geth 实现智能合约部署和调用详解
Remix编写智能合约 编写代码 在线调试 实现部署 调用接口 Geth实现私有链部署合约和调用接口 部署合约 调用合约 获得合约实例 通过实例调用合约接口 Remix编写智能合约 编写代码 Remi ...
- 用Java为Hyperledger Fabric(超级账本)开发区块链智能合约链代码之部署与运行示例代码
部署并运行 Java 链代码示例 您已经定义并启动了本地区块链网络,而且已构建 Java shim 客户端 JAR 并安装到本地 Maven 存储库中,现在已准备好在之前下载的 Hyperledger ...
- 区块链入门(5)Truffle 项目实战,Solidity IDE, 智能合约部署
在上一张我们学习了Truffle项目的创建,部署等相关内容,今天我们就来实战一下. 今天我们要做3件事: 1) 学习搭建一个Solidity IDE(Remix). 2) 使用这个Solidity I ...
- 用Java为Hyperledger Fabric(超级账本)编写区块链智能合约链代码
编写第一个 Java 链代码程序 在上一节中,您已经熟悉了如何构建.运行.部署和调用链代码,但尚未编写任何 Java 代码. 在本节中,将会使用 Eclipse IDE.一个用于 Eclipse 的 ...
随机推荐
- day 09作业 预科
作业 1.简述定义函数的三种方式. 定义函数的三种方式为空函数,有参函数和无参函数 2.简述函数的返回值. 如果函数没有返回值,会直接返回到None: 函数可以通过return返回出返回值: retu ...
- 【Docker】iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8480 -j DNAT --to-destination 172.17.0.2:80 ! -i docker0: iptables: No chain/target/match by that name
启动容器的时候,出现如下错误: Error response / --dport -j DNAT --to-destination ! -i docker0: iptables: No chain/t ...
- asp.net core 默认采用小驼峰命名和自定义模型验证
services.AddMvc(options => { options.Filters.Add<ApiExceptionAttribute>(); }).SetCompatibil ...
- 《构建之法》个人第二次作业之git学习
GIT地址 点一下 GIT用户名 Mretron 学号后五位 62517 博客地址 点一下 作业链接 点一下 在征得陈老师的同意下,使用java面向对象语言+IDEA工具完成本次作业 一.前期配置 虽 ...
- Navicat Premium 12破解激活11
下载Navicat Premium 12并安装: 百度云下载:Navicat Premium 12注册机 链接:https://pan.baidu.com/s/1UcA5yXjtdfXlBZI-2 ...
- HDU-4544 湫湫系列故事——消灭兔子 (贪心+优先队列)
题目思路 将兔子的血量从大到小排列,将箭的属性写在类中(结构体也成),排序按照伤害从大到小排列,若有相等的则按价格从小到大排. 代码 #include<bits/stdc++.h> usi ...
- K8s基本概念入门
序言 没等到风来,绵绵小雨,所以写个随笔,聊聊k8s的基本概念. k8s是一个编排容器的工具,其实也是管理应用的全生命周期的一个工具,从创建应用,应用的部署,应用提供服务,扩容缩容应用,应用更新,都非 ...
- python为什么要使用闭包
为什么要使用闭包 闭包避免了使用全局变量,此外,闭包允许将函数与其所操作的某些数据(环境)关连起来.这一点与面向对象编程是非常类似的,在面对象编程中,对象允许我们将某些数据(对象的属性)与一个或者多个 ...
- 文件上传相关报错: The current request is not a multipart request或is a MultipartResolver configured?
1:from中涉及到图片上传的就要用post提交方式.否则就会报这个错误. 2:第一中:在jsp页面的<head></head>标签里面加上<meta http-equi ...
- XJCO1711 Procedural Programming
University of Leeds School of ComputingProcedural Programming XJCO1711Semester 1, 2019-2020Coursewor ...