zoukankan      html  css  js  c++  java
  • Add Inline Actions

    Add Inline Actions

    1、为了使用 inline action,需要给 eosio.code 账号添加active权限。

      To use the 'activeauthority inline you will need your contract's to give active authority to eosio.code` pseudo-authority

      In order for the inline actions to be sent from addressbook, add the eosio.code permission to the contract's account's active permission.

    cleos set account permission addressbook active '{"threshold": 1,"keys": [{"key": "EOS5NKJziczzVQefMxp3n1cp1X3FUegS7jw8LtxLvubZehqBKGiWT","weight": 1}], "accounts": [{"permission":{"actor":"addressbook","permission":"eosio.code"},"weight":1}]}' -p addressbook@owner

      The eosio.code authority is a pseudo authority implemented to enhance security, and enable contracts to execute inline actions.

    2、Notify方法

      [[eosio::action]]
      void notify(name user, std::string msg) {
        require_auth(get_self());
        require_recipient(user);
      }

      get_self:Get this contract name. 对于本例,就是addressbook。

      require_recipient:对 name 账号调用本方法。

    3、send_summary()方法

      private: 
        void send_summary(name user, std::string message){
          action(
            //permission_level,
            //code,
            //action,
            //data
          );   
        }

    The action constructor requires a number of parameters.

    • permission_level struct
    • The contract to call (initialised using eosio::name type)
    • The action (initialised using eosio::name type)
    • The data to pass to the action, a tuple of positionals that correlate to the actions being called.

    4、实现 send_summary

      private: 
        void send_summary(name user, std::string message){
          action(
            permission_level{get_self(),"active"_n},
            get_self(),
            "notify"_n,
            std::make_tuple(user, name{user}.to_string() + message)
          ).send();
        }

      make_tuplea function available through std C++ library. Data passed in the tuple is positional, and determined by the order of the parameters accepted by the action that being called.

    5、合约发布后,我们测试一下。

      1)尝试插入数据

    cleos push action addressbook upsert '["alice", "alice", "liddell", 21, "123 drink me way", "wonderland", "amsterdam"]' -p alice@active

      我们发现会触发3个action。下图,第二个#表明 addressbook 触发了 notify, 第三个#表明 alice 触发了 notify。

    executed transaction: e9e30524186bb6501cf490ceb744fe50654eb393ce0dd733f3bb6c68ff4b5622  160 bytes  9810 us
    #   addressbook <= addressbook::upsert          {"user":"alice","first_name":"alice","last_name":"liddell","age":21,"street":"123 drink me way","cit...
    #   addressbook <= addressbook::notify          {"user":"alice","msg":"alicesuccessfully emplaced record to addressbook"}
    #         alice <= addressbook::notify          {"user":"alice","msg":"alicesuccessfully emplaced record to addressbook"}

      2)查看alice动作

    cleos get actions alice

      结果如下,有两个动作

      

      

    参考:https://developers.eos.io/eosio-home/docs/inline-actions

  • 相关阅读:
    多维DP UVA 11552 Fewest Flop
    思维/构造 HDOJ 5353 Average
    map Codeforces Round #Pi (Div. 2) C. Geometric Progression
    构造 Codeforces Round #Pi (Div. 2) B. Berland National Library
    贪心+优先队列 HDOJ 5360 Hiking
    贪心 HDOJ 5355 Cake
    LIS UVA 10534 Wavio Sequence
    又见斐波那契~矩阵快速幂入门题
    Big Christmas Tree(poj-3013)最短路
    poj 2449 Remmarguts' Date 第k短路 (最短路变形)
  • 原文地址:https://www.cnblogs.com/tekkaman/p/10009239.html
Copyright © 2011-2022 走看看