合约编写部署和执行

假设你已经安装了EOSIO并且启动了nodeos。

创建钱包

$ cleos wallet create -n mywallet
Creating wallet: mywallet
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5KTrGBhmMeDAZxM69PgZxcHXaT2aaebFRWriZq5PDshPUaowpJF"


使用钱包之前,需要解锁钱包:

$ cleos wallet unlock --password PW5KTrGBhmMeDAZxM69PgZxcHXaT2aaebFRWriZq5PDshPUaowpJF
Unlocked: default

上面的命令你的密码将会记录到shell历史中,更安全的,你可以通过下面的方法解锁:

$ cleos wallet unlock
password:

你不使用它的时候,锁定它是一个良好的习惯:

$ cleos wallet lock
Locked: default

Bios关键的系统合约

EOS相当于一个操作系统,而Bios就是这个系统的引导。进入到EOSIO的source code root目录下:

$ cleos set contract eosio build/contracts/eosio.bios -p eosio
Reading WAST...
Assembling WASM...
Publishing contract...
executed transaction: 414cf0dc7740d22474992779b2416b0eabdbc91522c16521307dd682051af083  4068 bytes  10000 cycles
#         eosio <= eosio::setcode               {"account":"eosio","vmtype":0,"vmversion":0,"code":"0061736d0100000001ab011960037f7e7f0060057f7e7e7e...
#         eosio <= eosio::setabi                {"account":"eosio","abi":{"types":[],"structs":[{"name":"set_account_limits","base":"","fields":[{"n...

创建账户

首先需要为账户创建一个key:

$ cleos create key
Private key: 5JiconQnk5WTpAbX5zcfkgXHhuki6Yy2U2zYKQmL5ovLUKprANN
Public key: EOS6oPBCtWDzeRBxFMHZebxTgHFFybYqurt8GLwMSHabEkZk4tYku

然后把key导入钱包:

$ cleos wallet import 5JiconQnk5WTpAbX5zcfkgXHhuki6Yy2U2zYKQmL5ovLUKprANN -n mywallet
imported private key for: EOS6oPBCtWDzeRBxFMHZebxTgHFFybYqurt8GLwMSHabEkZk4tYku

通过该钱包创建两个账户 tester1tester2

$ cleos create account eosio tester1 EOS6oPBCtWDzeRBxFMHZebxTgHFFybYqurt8GLwMSHabEkZk4tYku EOS6oPBCtWDzeRBxFMHZebxTgHFFybYqurt8GLwMSHabEkZk4tYku
executed transaction: 49b5aad108995ce096a840f2537376ae4cfadc57e06af7b1f435ae4e291991d7  352 bytes  102400 cycles
#         eosio <= eosio::newaccount            {"creator":"eosio","name":"tester1","owner":{"threshold":1,"keys":[{"key":"EOS6oPBCtWDzeRBxFMHZebxTg...

同理,把”tester1”改为”tester2”即可创建 tester2 账户。

一个简单的合约

建立一个文件夹命名为hello,并且建立一个hello.cpp文件:

$ mkdir hello
$ cd hello
$ touch hello.cpp

hello.cpp中输入以下内容:

#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
using namespace eosio;

class hello : public eosio::contract {
  public:
      using contract::contract;

      /// @abi action
      void hi( account_name user ) {
         print( "Hello, ", name{user} );
      }
};

EOSIO_ABI( hello, (hi) )

编译出wast和abi文件,过程中可能会出现warning,可以暂时不需要理会:

$ eosiocpp -o hello.wast hello.cpp
$ eosiocpp -g hello.abi hello.cpp

接下来通过下面命令部署合约:

$ cleos set contract tester1 ../hello -p tester1

现在,我们试着运行一下合约:

$ cleos push action tester1 hi '["tester2"]' -p tester2
executed transaction: 28d92256c8ffd8b0255be324e4596b7c745f50f85722d0c4400471bc184b9a16  244 bytes  1000 cycles
#    tester1 <= tester1::hi               {"user":"tester2"}
>> Hello, tester2

“-p tester2”表示赋予 tester2 权限。

参考

原文来自我的个人博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值