您正在查看: 2018年10月

[eosiocpp] account_name 与 std:string 互相转换

  • account_name to std:string
auto username = name{from};
std::string from_name = username.to_string();
  • std::string to account_name
account_name from_name = string_to_name(username);

How to pass struct to action

I have a simple struct with one member and want to pass this to an action.

struct field {
    bool is_private;
  };

  // @abi action
  // Create a new profile
  void create(const account_name& account, const field& first_name);

The .abi looks like this:

  "structs": [{
      "name": "field",
      "base": "",
      "fields": [{
          "name": "is_private",
          "type": "bool"
        }
      ]
    },{
      "name": "create",
      "base": "",
      "fields": [{
          "name": "account",
          "type": "name"
        },{
          "name": "first_name",
          "type": "field"
        }
      ]
    }
...

I tried a lot of different variations along the lines of cleos push action "contract" "create" '[acc_name, {"is_private": "true"}]' -p acc_name@active. But none of them seem to work. Are structs supported and if yes how do you pass the argument?

Thanks a lot!


Found the solution myself. You have to recreate the .abi structure in json:
cleos push action contract create '{"account":"acc_name","first_name":{"is_private":true"}}' -p acc_name@active

https://github.com/EOSIO/eos/issues/4914

Failed with error: Out of Range (8) read datastream of length 9 over by 1

麒麟测试网部署测试合约时出现一下错误

suroudeMacBook-Pro:contracts surou$ cleos -u http://api.kylin.eosbeijing.one:8880  get table cryptokylinq cryptokylinq results
2018-10-10T14:09:41.215 thread-0   main.cpp:3143                 main                 ] Failed with error: Out of Range (8)
read datastream of length 9 over by 1

开始演示步骤

开始部署合约中表的结构为

//@abi table results i64
    struct approval_info{
        account_name name;
        uint8_t iresult;
        uint64_t primary_key()const { return name; }
        EOSLIB_SERIALIZE(approval_info, (name)(iresult))
    };

    typedef eosio::multi_index<N(results),approval_info> result_table;

然后直接修改了 iresultstd::string

//@abi table results i64
    struct approval_info{
        account_name name;
        std::string iresult;
        uint64_t primary_key()const { return name; }
        EOSLIB_SERIALIZE(approval_info, (name)(iresult))
    };

    typedef eosio::multi_index<N(results),approval_info> result_table;

由于前面合约已部署,就像数据库中表字段的类型已经建好了,下次再部署时,导致表错乱,所以改个表名或者重新部署个账号测试把。等后面发现好的办法再跟帖

编译 eosio.cdt 出现error: redefinition of 'public_key'

先执行

rm -fr /usr/local/include/eosiolib

清理掉旧lib,再编译

sudo ./build.sh EOS

参考:GitHub