EOS笔记二:编写eos合约

作者: cowkeys | 来源:发表于2018-10-08 15:42 被阅读8次

    安装过程

    参考王大锤的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");
        });
    }
    

    相关文章

      网友评论

        本文标题:EOS笔记二:编写eos合约

        本文链接:https://www.haomeiwen.com/subject/wondaftx.html