EOS 智能合约开发 - HelloWorld
本文给出 EOS 智能合约开发的示例 HelloWorld。此示例参考了官网上的示例 HelloWorld。
0. 示例的最终结构
0.1 最终的目录结构
[furnace@localhost hello]$ pwd
/home/furnace/bitbucket/zblockchain/eoscodes/contracts/hello
[furnace@localhost hello]$ tree -L 1
.
├── hello.abi
├── hello.cpp
└── hello.wasm
0 directories, 3 files
0.2 最终的合约代码
[furnace@localhost hello]$ cat hello.cpp[furnace@localhost hello]$ cat hello.cpp
#include <eosio/eosio.hpp>
using namespace eosio;
class [[eosio::contract]] hello : public contract {
public:
using contract::contract;
[[eosio::action]]
void hi( name user ) {
require_auth(user);
print( "Hello, ", name{user});
}
};
//EOSIO_ABI(hello, hi)
//EOSIO_DISPATCH(hello, (hi))
1. 源代码
[furnace@localhost hello]$ cat hello.cpp
#include <eosio/eosio.hpp>
using namespace eosio;
class [[eosio::contract]] hello : public contract {
public:
using contract::contract;
[[eosio::action]]
void hi( name user ) {
print( "Hello, ", name{user});
}
};
//EOSIO_ABI(hello, hi)
//EOSIO_DISPATCH(hello, (hi))
2. 编译
通过下列命令编译 hello.cpp 并生成 hello.wasm。
$ eosio-cpp -abigen -o hello.wasm hello.cpp
当部署一个合约时,合约被部署到一个账户,并且账户成为了合约的交互接口。
3. 创建智能合约 hllo 的账号 hello
Create an account for the contract using cleos create account, with the command provided below.
cleos create account eosio hello EOS6YYpQ6jJ3p4d9bvYiStnkoE6zELJxbQJNZcj4tReGza4eoTiSC -p eosio@active
4。 部署智能合约 hello
通过下列命令将编译后的智能合约 hello 部署后网络中。
$ cleos set contract hello /home/centos/bitbucket/zblockchain/eoscodes/contracts/hello -p hello@active
其中,目录 /home/centos/bitbucket/zblockchain/eoscodes/contracts/hello 替换为你的目录。
当然,也可以采用相对目录。比如:
$ cd /home/centos/bitbucket/zblockchain/eoscodes/contracts
$ cleos set contract hello hello -p hello@active
5. 执行
很好,现在已经成功的部署了合约 hello。现在,可以调用其中的 action。
$ cleos push action hello hi '["bob"]' -p bob@active
执行上述命名,结果如下
executed transaction: 4c10c1426c16b1656e802f3302677594731b380b18a44851d38e8b5275072857 244 bytes 1000 cycles
# hello.code <= hello.code::hi {"user":"bob"}
>> Hello, bob
根据代码,合约将会允许一个账户向任意用户打招呼。
$ cleos push action hello hi '["bob"]' -p alice@active
执行上述命名,产生如下结果
executed transaction: 28d92256c8ffd8b0255be324e4596b7c745f50f85722d0c4400471bc184b9a16 244 bytes 1000 cycles
# hello.code <= hello.code::hi {"user":"bob"}
>> Hello, bob
正如我们期待的,输出结果为 "Hello, bob"。
6。 扩展
现在我们想修改合约,使得对于指定账户,其只能和自己打招呼。这需要使用内置方法 "require_auth"。方法 "require_auth" 接受一个名字作为参数,同时,它将检查执行此 action 的账户是否匹配参数中的名字。
hello.cpp 修改部分
void hi( name user ) {
require_auth( user );
print( "Hello, ", name{user} );
}
重新编译
$ eosio-cpp -abigen -o hello.wasm hello.cpp
重新部署
$ cleos set contract hello /home/centos/bitbucket/zblockchain/eoscodes/contracts/hello -p hello@active
执行
$ cleos push action hello hi '["bob"]' -p alice@active
报错
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.
执行
$ cleos push action hello hi '["alice"]' -p alice@active
正确
executed transaction: 235bd766c2097f4a698cfb948eb2e709532df8d18458b92c9c6aae74ed8e4518 244 bytes 1000 cycles
# hello <= hello::hi {"user":"alice"}
>> Hello, alice
Reference
项目源代码
项目源代码会逐步上传到 Github,地址为:
- https://github.com/windstamp/blockchain
- https://github.com/windstamp/eoscodes
- https://github.com/windstamp/installscripts
Contributor
- Windstamp, https://github.com/windstamp
网友评论