EOS笔记二:编写eos合约

安装过程

参考王大锤的eos 合约笔记

自己搭建的时候因为自身的环境出现一些的错误
特记录下来安装过程

主要流程

1 搭建测试网络

nodeos -e -p eosio  --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin

工具介绍
cleos - 管理账户、查询链信息、部署合约以及和合约交互等的客户端工具;
eosiocpp - eos的编译器,会产生部署合约需要的.wast和.abi文件;
nodeos - 负责整体链管理的命令工具,例如启动/停止
keosd - 虽然我们使用cleos来创建钱包,但是在这之下的钱包管理工具就是keos

2 创建eosid 管理员钱包

管理员钱包可以在测试网络中创建所有账户有最高权限,所以先创建它

cleos wallet create -n eosio --to-console
Creating wallet: eosio
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5J9WoF7Z9L4hStucWuQYW1xcDaQTiRHdTWutuZnDR8SJYNFhe6a"
配置文件中的公私钥对:
EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
=KEY:5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
cleos wallet import -n eosio 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3

3 创建测试钱包mytest

cleos wallet create -n mytest --to-console
Creating wallet: mytest
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5JTQjiFcbKGRfP6xSS5K9wzEJ1XPTnzWapBF59ZEdBVxpmsCd3a"
owner
cleos create key --to-console
Private key: 5J7j3EePrtmw3HGTcyCpxX2o3Z5wE83AJFWVY4iRFFqC8nL6oZJ
Public key: EOS8SK13SYxwzYyyDLcJEDPU6THSVQtienDrPPNYpZutk2ApDCuDk

active
cleos create key --to-console
Private key: 5JUxFHGSuQEKXokrki77H88bmgTTHvPz9WRsCRgqiAP276VHb8M
Public key: EOS6WZvKYcDokim7sqPAs4gKiKe7qdQ7yMuTuUscooTUdUhhBtVaY
cleos wallet import -n mytest
原命令
cleos create account eosio ${new_account} ${owner_key} ${active_key}
省略privatekey
cleos create account eosio mytesteosio EOS8SK13SYxwzYyyDLcJEDPU6THSVQtienDrPPNYpZutk2ApDCuDk  EOS6WZvKYcDokim7sqPAs4gKiKe7qdQ7yMuTuUscooTUdUhhBtVaY
#载入基础BIOS合约
cleos set contract eosio build/contracts/eosio.bios -p eosio@active

4 简单示例合约

ps:
1 在 build/tools 路径下有一个eosiocpp 工具 加入path
2 WebAssembly是一种新的编码方式,可以在现代的网络浏览器中运行。一般以.wasm结尾

# 创建合约hello1 在./hello1 文件夹下会生成两个文件
eosiocpp -n hello1
#hello1.hpp是智能合约的头文件,可以包含一些变量,常量和函数的声明。
#hello1.cpp是合约的源码文件,包含合约的具体实现。

#进入hello1文件夹下执行以下命令:
#使用 -o 生成wast文件和wasm文件
eosiocpp -o ./hello1.wast ./hello1.cpp
#使用 -g 生成abi文件
eosiocpp -g ./hello1.abi ./hello1.cpp
#这时可以看到在当前文件夹下生成了hello1.wast、hello1.wasm和hello1.abi文件。
//

5 部署合约

cleos set contract eosio ./ ./hello1.wasm hello1.abi -p eosio@active

这行命令有五个参数:eosio表示部署合约的账户,./表示合约所在的文件夹,后面两个参数依次是.wasm和.abi文件的路径,最后的-p eosio@active表示权限。

6 调用合约

cleos push action eosio hi '["cowkeys"]' -p mytesteosio

7 智能合约存储介绍

参考资料比较清晰 参考教程6:合约介绍

8 源码示例

文末有app.hpp / app.cpp 两个源码
编译:

eosiocpp -o ./app.wast ./app.cpp
eosiocpp -g ./app.abi ./app.cpp

部署:

cleos set contract eosio ./ ./app.wasm app.abi -p eosio@active

查询

cleos get table eosio eosio profile

调用 create

cleos push action eosio create '["eosio","cowkeys","27","programmer"]' -p eosio@active

等等。。。

9 源码

1 app.hpp

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

class app : public contract {
public:
    using contract::contract;

    app(account_name self)
            : contract(self) {}

    // @abi action
    void hello(const account_name account);

    // @abi action
    void create(const account_name account,
                const string&      username,
                uint32_t           age,
                const string&      bio);

    // @abi action
    void get(const account_name account);

    // @abi action
    void update(const account_name account,
                const string&      username,
                uint32_t           age,
                const string&      bio);

    // @abi action
    void remove(const account_name account);

    // @abi action
    void byage(uint32_t age);

    // @abi action
    void agerange(uint32_t young, uint32_t old);

    private:
    // @abi table profile i64
    struct profile {
        account_name    account;
        string          username;
        uint32_t        age;
        string          bio;

        account_name primary_key() const { return account; }
        uint64_t     by_age() const { return age; }

        EOSLIB_SERIALIZE(profile, (account)(username)(age)(bio))
    };

    typedef eosio::multi_index< N(profile), profile,
            // N(name of interface)
            indexed_by< N(age),
                        const_mem_fun<profile, uint64_t, &profile::by_age>
            >
    > profile_table;

};

EOSIO_ABI(app, (hello)(create)(get)(update)(remove)(byage)(agerange))

2 app.cpp

#include <app.hpp>
void app::hello(account_name account) {
    print("Hello ", name{account});
}
void app::create(const account_name account,
                     const string&      username,
                     uint32_t           age,
                     const string&      bio) {
    require_auth(account);

    profile_table profiles(_self, _self);

    auto itr = profiles.find(account);

    eosio_assert(itr == profiles.end(), "Account already exists");

    profiles.emplace(account, [&](auto& p) {
        p.account  = account;
        p.username = username;
        p.age      = age;
        p.bio      = bio;
    });
}

void app::get(const account_name account) {
    profile_table profiles(_self, _self);

    auto itr = profiles.find(account);

    eosio_assert(itr != profiles.end(), "Account does not exist");

    print("Account: ", name{itr->account}, " , ");
    print("Username: ", itr->username.c_str(), " , ");
    print("Age: ", itr->age , " , ");
    print("Bio: ", itr->bio.c_str());
}

void app::update(const account_name account,
                     const string&      username,
                     uint32_t           age,
                     const string&      bio) {
    require_auth(account);

    profile_table profiles(_self, _self);

    auto itr = profiles.find(account);

    eosio_assert(itr != profiles.end(), "Account does not exist");

    profiles.modify(itr, account, [&](auto& p) {
        p.username = username;
        p.age      = age;
        p.bio      = bio;
    });
}

void app::remove(const account_name account) {
    require_auth(account);

    profile_table profiles(_self, _self);

    auto itr = profiles.find(account);

    eosio_assert(itr != profiles.end(), "Account does not exist");

    profiles.erase(itr);
    print(name{account} , " deleted!");
}

void app::byage(uint32_t age) {
    print("Checking age: ", age, "\n");
    profile_table profiles(_self, _self);

    // get an interface to the 'profiles' containter
    // that looks up a profile by its age
    auto age_index = profiles.get_index<N(age)>();

    auto itr = age_index.lower_bound(age);

    for(; itr != age_index.end() && itr->age == age; ++itr) {
        print(itr->username.c_str(), " is ", itr->age, " years old\n");
    }
}

void app::agerange(uint32_t young, uint32_t old) {
    profile_table profiles(_self, _self);

    auto age_index = profiles.get_index<N(age)>();

    auto begin = age_index.lower_bound(young);
    auto end   = age_index.upper_bound(old);

    for_each(begin, end, [&](auto& p) {
        print(p.username.c_str(), " is ", p.age, " years old\n");
    });
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,012评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,589评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,819评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,652评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,954评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,381评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,687评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,404评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,082评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,355评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,880评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,249评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,864评论 3 232
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,007评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,760评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,394评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,281评论 2 259

推荐阅读更多精彩内容