change of version 1.3.x+,EOSIO.CDT

After eos version 1.3.x, generation of cdt tools, Smart Contracts has changed alot.

Both source code and compilation.

Compilation and application

1. download and install cdt

$ git clone --recursive https://github.com/eosio/eosio.cdt
$ cd eosio.cdt
$ ./build.sh
$ sudo ./install.sh

2. Building your first smart contract

This will generate two files:old===>new

eg:

eosiocpp -g hello.abi hello.hpp
eosiocpp -o heool.wast hello.cpp
===>
eosio-cpp -abigen hello.cpp -o hello.wasm

3. Original contract change to cdt contract need do somethings:

  • name replace account_name
  • symbol("EOS",4) replace S(4,EOS)
  • add #define N(X) name("X") to .hpp
  • ACTION replace void
  • [[eosio::action]] replace @abi action
  • struct [[eosio::table("testtabb")]] test_table2{} replace //@abi table testtabb
  • EOSIO_DISPATCH() replace EOSIO_ABI()

Modules changes

1. console Defines C++ wrapper to log/print text messages. More...

e.g.

print( "hello world, this is a number: ", 5 );
print_f("Number of apples: %", 10); const char *s = "Hello World!";
uint64_t unsigned_64_bit_int = 1e+18;
uint128_t unsigned_128_bit_int (87654323456);
uint64_t string_as_unsigned_64_bit = N(abcde);
std::out << s << " " << unsigned_64_bit_int << " " << unsigned_128_bit_int << " " << string_as_unsigned_64_bit;

2. multiindex Defines EOSIO Multi Index Table

e.g.

#include <eosiolib/eosio.hpp>
using namespace eosio;
using namespace std;
class addressbook: contract {
struct address {
uint64_t account_name;
string first_name;
string last_name;
string street;
string city;
string state;
uint64_t primary_key() const { return account_name; }
};
public:
addressbook(name receiver, name code, datastream<const char*> ds):contract(receiver, code, ds) {}
typedef eosio::multi_index< name("address"), address > address_index;
void myaction() {
address_index addresses(_self, _self.value); // code, scope
}
}
EOSIO_DISPATCH( addressbook, (myaction) )

3. inline actions delete currency.hpp

/**
* Send inline action
*
* @brief Send inline action
* @param CONTRACT - The account this action is intended for
* @param NAME - The name of the action
* @param ... - The member of the action specified as ("action_member1_name", action_member1_value)("action_member2_name", action_member2_value)
*/
#define SEND_INLINE_ACTION( CONTRACT, NAME, ... )\
INLINE_ACTION_SENDER(std::decay_t<decltype(CONTRACT)>, NAME)( (CONTRACT).get_self(),\
BOOST_PP_TUPLE_ENUM(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__), BOOST_PP_VARIADIC_TO_TUPLE(__VA_ARGS__)) );

e.g.1:owner call owner function ,create inline option

SEND_INLINE_ACTION(*this,transfer,{_self, N(active)},{_self,from,"1.0000 EOS","inline transfer"})

e.g.2:contract B call contract A's hi function

#include <eosiolib/eosio.hpp>
using namespace eosio;
CONTRACT A : public eosio::contract {
public:
using contract::contract;
ACTION hi( name user );
// accessor for external contracts to easily send inline actions to your contract
using hi_action = action_wrapper<"hi"_n, &A::hi>;
};
===>
#include <A.hpp>
using namespace eosio;
CONTRACT send_inline : public eosio::contract {
public:
using contract::contract;
ACTION test( name user, name inline_code ) {
print_f( "Hello % from send_inline", user );
// constructor takes two arguments (the code the contract is deployed on and the set of permissions)
A::hi_action hi(inline_code, {_self, "active"_n});
hi.send(user);
}
// accessor for external contracts to easily send inline actions to your contract
using test_action = action_wrapper<"test"_n, &send_inline::test>;
};

4. others

  • delete eosio::vector,bytes replace to std::vector<>,std::vector

Headers changes

1.change eosiolib/types.hpp

  • delete account_name and replace to name (details later)
  • delete permission_name,table_name,scope_name,action_name
  • delete time,weight_type
  • change name public_key to capi_public_key,signature to capi_signature the same as capi_checksum256...

2.add eosiolib/name.hpp

  • Added enum class eosio::name::raw which is implicitly converted from an eosio::name (used for template non-type parameters)
struct name
{
public:
enum class raw : uint64_t {};
uint64_t value = 0;
}
  • delete macro N(X) replace to "X"_n or name("X")
#define N(X) name("X")
#define V(X) N(X).value

3.change eosiolib/contract.hpp

  • change constract function
class contract {
public:
/**
* Construct a new contract given the contract name
* @brief Construct a new contract object.
* @param n - The name of this contract
*/
contract( account_name n ):_self(n){}
/**
* Get this contract name
* @brief Get this contract name.
* @return account_name - The name of this contract
*/
inline account_name get_self()const { return _self; } protected:
/**
* The name of this contract
* @brief The name of this contract.
*/
account_name _self;
};
==========================================================================>
class contract {
public:
/**
* Construct a new contract given the contract name
* @brief Construct a new contract object.
* @param receiver - The name of this contract
* @param code - The code name of the action this contract is processing.
* @param ds - The datastream used
*/
contract( name receiver, name code, datastream<const char*> ds ):_self(receiver),_code(code),_ds(ds) {}
/**
* Get this contract name
* @brief Get this contract name.
* @return name - The name of this contract
*/
inline name get_self()const { return _self; }
/**
* The code name of the action this contract is processing.
* @brief The code name of the action this contract is processing.
* @return name - The code name of the action this contract is processing.
*/
inline name get_code()const { return _code; }
/**
* Get the datastream for this contract
* @brief Get the datastream for this contract
* @return datastream<const char*> - The datastream for this contract
*/
inline datastream<const char*> get_datastream()const { return _ds; }
protected:
/**
* The name of this contract
* @brief The name of this contract.
*/
name _self;
/**
* The code name of the action this contract is processing.
* @brief The code name of the action this contract is processing.
*/
name _code;
/**
* The datastream for this contract
*@ The datastream for this contract
*/
datastream<const char*> _ds = datastream<const char*>(nullptr, 0);
};

4.change eosiolib/eosio.hpp

  • Added ACTION macro which is simply a shortcut for [[eosio::action]] void.

  • Added TABLE macro which is simply a shortcut for struct [[eosio::table]].

  • Added CONTRACT macro which is simply a shortcut for class [[eosio::contract]].

  • e.g. abitest.cpp

#include <eosiolib/eosio.hpp>
#include <optional> using namespace eosio; typedef int type_def; namespace test {
// mark this struct as an action
struct [[ eosio::action ]] testa {
void printddd() { print("testa"); }
int fielda;
float fieldb;
capi_name name;
}; struct testb {
float field;
void printll() { print("testb"); }
}; // mark this struct as an action and specify the name explicitly
struct [[ using eosio: action("testc"), contract("abitest") ]] test_c : testb {
uint64_t num;
};
} CONTRACT abitest : public eosio::contract {
public:
using contract::contract; // mark this method as an action and specify the name explicity
[[ eosio::action("testacta") ]]
void testact_a( name user, const std::string& s, std::vector<int>& c, std::vector<std::string> sv ) {
print( "Hello, ", name{user} );
symbol sym("TEST", 4);
} // mark this method as an action
ACTION testactb( test::test_c input, type_def td, std::optional<int> cc, bool d ) {
print(input.num);
} // mark this struct as a table and allow multi_index typedefs to define the tables
TABLE testtable {
uint64_t owner;
uint64_t third;
uint64_t primary_key() const { return owner; }
}; // mark this struct as a table and allow multi_index typedefs to define the tables, and specify a primitive table (non multi_index) with an explicit name
struct [[eosio::table("testtabb")]] test_table2 {
uint64_t owner;
uint64_t sec;
uint64_t third;
uint64_t primary_key() const { return owner; }
};
}; typedef eosio::multi_index< "testtab"_n, abitest::testtable > testtable_t;
typedef eosio::multi_index< "testtaba"_n, abitest::testtable > testtable_a_t;
typedef eosio::multi_index< "testtab2"_n, abitest::test_table2 > testtable2_t; #define EOSIO_DISPATCH_EX( TYPE, MEMBERS ) \
extern "C"
void apply(uint64_t, uint64_t, uint64_t){
auto self = receiver; \
if( action == "onerror"_n.value) { \
/* onerror is only valid if it is for the "eosio" code account and authorized by "eosio"'s "active permission */ \
eosio_assert(code == "eosio"_n.value, "onerror action's are only valid from the \"eosio\" system account"); \
} \
if( (code == self && action != "transfer"_n.value) || action == "onerror"_n.value || (code == "eosio.token"_n.value && action == "transfer"_n.value)) { \
switch( action ) { \
EOSIO_DISPATCH_HELPER( TYPE, MEMBERS ) \
} \
/* does not allow destructor of thiscontract to run: eosio_exit(0); */ \
} \
} \
}
EOSIO_DISPATCH_EX( abitest, (testactb) )

5.eosiolib/dispatcher.hpp

  • Renamed the macro EOSIO_ABI to EOSIO_DISPATCH as this is more descriptive of what this macro actually does.
  • Modified the definition of EOSIO_DISPATCH to work with the new constructor for eosio::contrac
  • delete EOSIO_API and replace to EOSIO_DISPATCH_HELPER

e.g.

#define EOSIO_ABI_EX( TYPE, MEMBERS ) \
extern "C" { \
void apply( uint64_t receiver, uint64_t code, uint64_t action ) { \
auto self = receiver; \
if( action == N(onerror)) { \
/* onerror is only valid if it is for the "eosio" code account and authorized by "eosio"'s "active permission */ \
eosio_assert(code == N(eosio), "onerror action's are only valid from the \"eosio\" system account"); \
} \
if( (code == self && action != N(transfer)) || action == N(onerror) || (code == N(eosio.token) && action == N(transfer))) { \
TYPE thiscontract( self ); \
switch( action ) { \
EOSIO_API( TYPE, MEMBERS ) \
} \
/* does not allow destructor of thiscontract to run: eosio_exit(0); */ \
} \
} \
}
EOSIO_ABI_EX( monster,(transfer))
==> #define EOSIO_DISPATCH_EX( TYPE, MEMBERS ) \
extern "C" { \
void apply( uint64_t receiver, uint64_t code, uint64_t action ) { \
auto self = receiver; \
if( action == "onerror"_n.value) { \
/* onerror is only valid if it is for the "eosio" code account and authorized by "eosio"'s "active permission */ \
eosio_assert(code == "eosio"_n.value, "onerror action's are only valid from the \"eosio\" system account"); \
} \
if( (code == self && action != "transfer"_n.value) || action == "onerror"_n.value || (code == "eosio.token"_n.value && action == "transfer"_n.value)) { \
switch( action ) { \
EOSIO_DISPATCH_HELPER( TYPE, MEMBERS ) \
} \
/* does not allow destructor of thiscontract to run: eosio_exit(0); */ \
} \
} \
}

6.Removed eosiolib/core_symbol.hpp and change symbol.hpp

  • change contract function.Removed eosio::symbol_type struct and replaced with eosio::symbol class
struct symbol_type {
symbol_name value;
};
==================================>
class symbol_code {
public:
constexpr symbol_code() : value(0) {}
constexpr explicit symbol_code( uint64_t raw )
:value(raw)
{}
private:
uint64_t value = 0;
};
  • Removed the S macro. The symbol constructor should be used as a type safe replacement
S(4,SYS)
===>
symbol(symbol_code("SYS"), 4) = symbol("SYS", 4)

安来根-EOS 开发社区

---

![](https://img2018.cnblogs.com/blog/1015273/201812/1015273-20181218201530501-1531824304.jpg)

eosio.cdt发布带来的变化的更多相关文章

  1. eosio.cdt:EOS智能合约工具集

    目前EOS已经迎来了1.5.x时代,很多内容都有了较大的改变.其中智能合约的工作流程发生了改变,EOSIO为智能合约提供了独立且功能完整的工具集eosio.cdt.该工具集基于WASM平台,可解耦于e ...

  2. 安装EOS合约工具eosio.cdt

    安装: 安装完之后 会产生 eosio-cpp_, eosio-cc, eosio-ld, eosio-pp, and _eosio_abigen (These are the C++ compile ...

  3. 【重构前端知识体系之HTML】HTML5给网页音频带来的变化

    [重构前端知识体系之HTML]HTML5给网页音频带来的变化 引言 音乐播放,相信大家都很熟悉,但是早在之前的音乐播放之前,你的浏览器会问你,是否下载flash插件.然而现在,估计一些年轻的开发者都不 ...

  4. 随着visual studio 2013 发布.带来的一些变化

    1.asp.net a.在2013中, asp.net走向了统一.使用不同的asp.net 框架搭(web forms ,api, mvc )建混合应用 b.身份验证 无身份验证 个人用户账户 (窗体 ...

  5. ASP.NET 5 Beta5 对TagHelper带来的变化

    最近做的TagHelper项目要从原来的ASP.NET 5 Beta 4升级到Beta 5,特地整理了升级后的变化: 新增ImageTagHelper <img asp-file-version ...

  6. 新抽象语法树(AST)给 PHP7 带来的变化

    本文大部分内容参照 AST 的 RFC 文档而成:https://wiki.php.net/rfc/abstract_syntax_tree,为了易于理解从源文档中节选部分进行介绍. 我的官方群点击此 ...

  7. 【总结】设备树对platform平台设备驱动带来的变化(史上最强分析)【转】

    本文转载自:http://blog.csdn.net/fengyuwuzu0519/article/details/74375086 版权声明:本文为博主原创文章,转载请注明http://blog.c ...

  8. MyEclipse 2017 CI 9 发布(附下载)

    挑战全年最低价!MyEclipse线上狂欢继续!火热开启中>> 在进入年底之时,2017 CI 9是我们最大的版本发布之一.在新版本中,我们添加了对Angular 5和TypeScript ...

  9. 腾讯优图联手Science发布主题报告:计算机视觉的研发和应用

    近日,腾讯优图与<科学>(Science)杂志共同发布<Seeing is believing: R&D applications of computer vision> ...

  10. 重磅发布 | 黑镜调查:深渊背后的真相之「DDoS 威胁与黑灰产业调查报告」

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由云鼎实验室发表于云+社区专栏 本文经授权转载自 FreeBuf 2018年世界杯硝烟散尽,但关于她的话题却远远没有结束.说起世界杯,就 ...

随机推荐

  1. Redis 先操作数据库和先删除缓存, 一致性分析

    初始状态: 数据库和缓存中v=10 第一种,先删除缓存在操作数据库: 线程1准备更新数据库的值v=20,先删除缓存, 此时线程2进来, 缓存未命中,查询数据库v=10, 写入缓存v=10, cpu回到 ...

  2. 我要涨知识 —— TypeScript 常见面试题(一)

    1.ts 中的 any 和 unknown 有什么区别? unknown 和 any 的主要区别是 unknown 类型会更加严格:在对 unknown 类型的值执行大多数操作之前,我们必须进行某种形 ...

  3. Git安装与常用操作

    Git作为一个版本控制工具,使用前需进行下载安装:可自行到官网下载. 一.安装(windows) 1.双击下载好的文件进行安装,弹窗中点击"next" 2.默认勾选,继续点击&qu ...

  4. (译)TDD(测试驱动开发)的5个步骤

    原文:5 steps of test-driven development https://developer.ibm.com/articles/5-steps-of-test-driven-deve ...

  5. Terminal(oh-my-zsh) 美化

    如果你使用Mac进行开发,那么Terminal.app应该是你使用非常频繁的app了.初体验Terminal时你可能觉得单调乏味,阅读密密麻麻的内容也很费劲.但是如果你跟着我一起配置它,就会发现你平时 ...

  6. 思维分析逻辑 3 DAY

    目录 指标分析 指标选择原则 指标体系建立 步骤 流量分析 渠道分析(从哪来) 常见渠道及渠道分类 渠道推广过程 渠道的指标 渠道分析方法 转化及价值分析(经过什么?产生什么价值?) 漏斗分析 功能模 ...

  7. Vue2 到 Vue3,重温这 5 个常用的 API

    距离Vue3发布已经过去一年多时间了,从Vue2到Vue3是一个不小的升级,包括周边生态等.虽然目前大多数开发者们在使用的仍旧以Vue2为准,但Vue3显然是Vue开发者们未来必须面对的,而且前不久V ...

  8. 2022春每日一题:Day 41

    题目:I Hate It 一个基础的线段树模板,单点修改+区间查询 代码: #include <cstdio> #include <cstdlib> #include < ...

  9. 【OpenStack云平台】安装Centos操作系统

    视频教程:https://live.csdn.net/v/236820 1.环境准备 准备实验所需要的环境,需要安装VMware Workstation.使用的系统镜像为CentOS-7.5-x86_ ...

  10. 使用位运算优化 N 皇后问题

    使用位运算优化 N 皇后问题 作者:Grey 原文地址: 博客园:使用位运算优化 N 皇后问题 CSDN:使用位运算优化 N 皇后问题 问题描述 N 皇后问题是指在 n * n 的棋盘上要摆 n 个皇 ...