您正在查看: EOS 分类下的文章

EOS 新建账户合约,转账到执行账户,创建账户

查看主要代码

//signdappplay.cpp

#include "signdappplay.hpp"

void signdappplay::transfer(account_name from, account_name to, asset quantity, string memo)
{
    if (from == _self || to != _self) {
        return;
    }

    // don't do anything on transfers from our reference account
    if (from == N(signdappplay)) {
      return;
    }

    eosio_assert(quantity.symbol == CORE_SYMBOL, "only core token allowed"); //string_to_symbol(4, "EOS")
    eosio_assert(quantity.is_valid(), "Invalid token transfer");
    eosio_assert(quantity.amount > 0, "Quantity must be positive");

    //memo "your_account_name-your_owner_public_key-your_active_public_key" 分隔符支持“-” “:” 空格
    //去掉memo前面的空格
    memo.erase(memo.begin(), find_if(memo.begin(), memo.end(), [](int ch) {
        return !isspace(ch);
    }));
    //去掉memo后面的空格
    memo.erase(find_if(memo.rbegin(), memo.rend(), [](int ch) {
        return !isspace(ch);
    }).base(), memo.end());

    eosio_assert(memo.length() == 120 || memo.length() == 66, "Malformed Memo (not right length)");
    const string account_string = memo.substr(0, 12);
    const account_name new_account_name = string_to_name(account_string.c_str());
    eosio_assert(memo[12] == ':' || memo[12] == '-' || memo[12] == ' ', "Malformed Memo [12] == : or - or space");

    const string owner_key_str = memo.substr(13, 53);
    string active_key_str;

    if(memo[66] == ':' || memo[66] == '-' || memo[66] == ' ') {
      // active key provided
      active_key_str = memo.substr(67, 53);
    } else {
      // active key is the same as owner
      active_key_str = owner_key_str;
    }

    const abieos::public_key owner_pubkey =
        abieos::string_to_public_key(owner_key_str);
    const abieos::public_key active_pubkey =
        abieos::string_to_public_key(active_key_str);

    array<char, 33> owner_pubkey_char;
    copy(owner_pubkey.data.begin(), owner_pubkey.data.end(),
         owner_pubkey_char.begin());

    array<char, 33> active_pubkey_char;
    copy(active_pubkey.data.begin(), active_pubkey.data.end(),
         active_pubkey_char.begin());

    key_weight owner_pubkey_weight = {
        .key = {0, owner_pubkey_char},
        .weight = 1
    };

    key_weight active_pubkey_weight = {
        .key = {0, owner_pubkey_char},
        .weight = 1
    };

    authority owner = authority{
        .threshold = 1,
        .keys = {owner_pubkey_weight},
        .accounts = {},
        .waits = {}
    };

    authority active = authority{
        .threshold = 1,
        .keys = {active_pubkey_weight},
        .accounts = {},
        .waits = {}
    };

    newaccount new_account = newaccount{
        .creator = _self,
        .name = new_account_name,
        .owner = owner,
        .active = active
    };

    asset stake_net(1000, CORE_SYMBOL);
    asset stake_cpu(1000, CORE_SYMBOL);
    asset buy_ram = quantity - stake_net - stake_cpu;
    eosio_assert(buy_ram.amount > 0, "Not enough balance to buy ram");

    // create account
    action(
            permission_level{ _self, N(active) },
            N(eosio),
            N(newaccount),
            new_account
    ).send();
    // buy ram
    action(
            permission_level{ _self, N(active)},
            N(eosio),
            N(buyram),
            make_tuple(_self, new_account_name, buy_ram)
    ).send();
    // delegate and transfer cpu and net
    action(
            permission_level{ _self, N(active)},
            N(eosio),
            N(delegatebw),
            make_tuple(_self, new_account_name, stake_net, stake_cpu, true)
    ).send();
}

开始部署

  • 下载代码
    git clone https://github.com/cppfuns/signdappplay.git
  • 编译代码
    //进入源码目录
    cd signdappplay/
    //生成abi文件
    eosiocpp -g signdappplay.abi signdappplay.cpp
    //生成wast文件
    eosiocpp -o signdappplay.wast signdappplay.cpp
  • 部署合约
    //提前创建好signdappplay 将合约部署到此账号
    cleos set contract signdappplay signdappplay/ -p signdappplay
  • signdappplay@active里添加signdappplay@eosio.code授权
    cleos set account permission signdappplay active '{"threshold": 1,"keys": [{"key": "EOS7nK2w6ZT8hKdrxr48xdt3CFj1MXpaDV6jagRHKayFhqJBX5GEf","weight": 1}],"accounts": [{"permission":{"actor":"signdappplay","permission":"eosio.code"},"weight":1}]}' owner -p signdappplay

开始转账创建账号

signdappplay账号转账,并按规则(新建账户名:owner公钥:active公钥)填写memo。(分隔符支持"-" ":"或者空格,如果owner公钥与active公钥一致,可以只填写一个,如实例)。

root@iZj6cbx3duprxf6dasczbpZ:~# cleos push action eosio.token transfer '["dapp.exec", "signdappplay","100.0000 EOS","dingtet12345:EOS8Hdw6vismBgoYPzfLhr2rtHrdsR3F8UYAL23LSc9wdV8eNhNH8"]' -p dapp.exec
executed transaction: e6d94f39de7e6508e06b469327eb63eb6100ce129deb152a484acc0399bae292  192 bytes  12152 us
#   eosio.token <= eosio.token::transfer        {"from":"dapp.exec","to":"signdappplay","quantity":"100.0000 EOS","memo":"dingtet12345:EOS8Hdw6vismB...
#     dapp.exec <= eosio.token::transfer        {"from":"dapp.exec","to":"signdappplay","quantity":"100.0000 EOS","memo":"dingtet12345:EOS8Hdw6vismB...
#  signdappplay <= eosio.token::transfer        {"from":"dapp.exec","to":"signdappplay","quantity":"100.0000 EOS","memo":"dingtet12345:EOS8Hdw6vismB...
#         eosio <= eosio::newaccount            {"creator":"signdappplay","name":"dingtet12345","owner":{"threshold":1,"keys":[{"key":"EOS8Hdw6vismB...
#         eosio <= eosio::buyram                {"payer":"signdappplay","receiver":"dingtet12345","quant":"99.8000 EOS"}
#   eosio.token <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.ram","quantity":"99.3010 EOS","memo":"buy ram"}
#  signdappplay <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.ram","quantity":"99.3010 EOS","memo":"buy ram"}
#     eosio.ram <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.ram","quantity":"99.3010 EOS","memo":"buy ram"}
#   eosio.token <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.ramfee","quantity":"0.4990 EOS","memo":"ram fee"}
#  signdappplay <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.ramfee","quantity":"0.4990 EOS","memo":"ram fee"}
#  eosio.ramfee <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.ramfee","quantity":"0.4990 EOS","memo":"ram fee"}
#         eosio <= eosio::delegatebw            {"from":"signdappplay","receiver":"dingtet12345","stake_net_quantity":"0.1000 EOS","stake_cpu_quanti...
#   eosio.token <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.stake","quantity":"0.2000 EOS","memo":"stake bandwidth"}
#  signdappplay <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.stake","quantity":"0.2000 EOS","memo":"stake bandwidth"}
#   eosio.stake <= eosio.token::transfer        {"from":"signdappplay","to":"eosio.stake","quantity":"0.2000 EOS","memo":"stake bandwidth"}
warning: transaction executed locally, but may not be confirmed by the network yet

账户向signdappplay账号转一定量的EOS,合约将会自动创建新账户,为CPU和NET各抵押0.1EOS,剩余EOS全部为新账户购买RAM(0.5%手续费)。

参考:

EOS 支持 sql_db_plugin及连接测试

更新sql_db_plugin代码

git clone https://github.com/superoneio/eos_sql_db_plugin
cp -rv eos_sql_db_plugin/sql_db_plugin/* /home/surou/Documents/github/eos/plugins/sql_db_plugin

对比了以下几个维护的sql_db_plugin版本代码

开启sql_db_plugin编译支持

  1. 打开eos/plugins/CMakeLists.txt
    取消add_subdirectory(sql_db_plugin)前面的#注释
  2. 打开 eos/programs/nodeos/CMakeLists.txt
    // 取消下面代码前的注释,如果无注释,则忽略此步骤。
    if(TARGET sql_db_plugin)
     target_link_libraries( nodeos PRIVATE -Wl,${whole_archive_flag} sql_db_plugin -Wl,${no_whole_archive_flag} )
    endif()

    重新编译EOS

    ./eosio_build.sh
    cd build
    sudo make install

    编译完成后,执行

    nodeos --help | grep "sql_db-uri"

    如果输出如下内容,证明编译成功。

    –sql_db-uri arg Sql DB URI connection string If not

Mysql 8.0 安装

  • 参考 Ubuntu 安装MySql 8.0
  • 接下来我们安装 soci。soci 是 C++ 连接 MySQL 的 Library。Ubuntu 可以快速安装,命令如下:
    sudo apt-get -y install libsoci-dev
  • 安装 mysql-client
    sudo apt-get -y install mysql-client
    sudo apt-get install libmysqlclient-dev

配置Eos数据库

  • 进入mysql
    mysql -p
  • 创建数据库以及分配账号
    CREATE DATABASE eos DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    GRANT ALL PRIVILEGES ON eos.* TO 你的账户名@'%' IDENTIFIED BY '你的密码';
    GRANT ALL PRIVILEGES ON eos.* TO 你的账户名@'127.0.0.1' IDENTIFIED BY '你的密码';
  • 查看数据库
    mysql > show databases;

  • 导入数据库(eos.db)
    mysql > use eos;  //切换到eos 数据库
    mysql > source eos_sql_db_plugin/sql_db_plugin/eos.db; //导入数据库

    执行数据同步

    nodeos --sql_db-uri="mysql://db=eos user=root host=127.0.0.1 password='数据库密码'" --sql_db-block-start=0  --delete-all-blocks

常见问题

  • ERROR 1273 (HY000): Unknown collation: 'utf8mb4_0900_ai_ci'
    MySql 8.0才支持的默认字符集
  • 查看Ubuntu版本
    cat /etc/issue

参考链接

EOS 合约代码中的print 输出到终端

修改~/.local/share/eosio/nodeos/config/config.ini

contracts-console = true

安装EOS 数据获取API Server EOSTracker-API

获取源代码

git clone https://github.com/EOSEssentials/EOSTracker-API.git

开始编译

cd EOSTracker-API
composer install

开始运行

php bin/console server:run


常见问题

  • ubuntu add-apt-repository command not found解决方法
    apt-get install software-properties-common
  • Ubuntu PHP升级到最新版本
    add-apt-repository ppa:ondrej/php
    apt-get update
    apt-get upgrade php
  • The requested PHP extension ext-apcu * is missing from your system. Install or enable PHP's apcu extension
    apt-get install php7.2-apcu //修改为当前版本  php -v
  • The requested PHP extension ext-intl * is missing from your system. Install or enable PHP's intl extension
    apt-get install php7.2-intl //修改为当前版本  php -v
  • The requested PHP extension ext-mbstring * is missing from your system. Install or enable PHP's mbstring extension
    apt-get install php7.2-mbstring //修改为本地版本  php -v
  • The requested PHP extension ext-redis * is missing from your system. Install or enable PHP's redis extension
    apt-get install php7.2-redis //修改为本地版本  php -v
  • Installation request for symfony/symfony v3.4.11 -> satisfiable by symfony/symfony[v3.4.11].
    symfony/symfony v3.4.11 requires ext-xml * -> the requested PHP extension xml is missing from your system.
       apt-get install php7.2-xml //修改为本地版本 PHP -v

EOSIO各版本含义

  • Dawn = Alpha
  • V1.. = Beta
  • EOS Mainnet 版本 = Stable

Alpha:是内部测试版,一般不向外部发布,会有很多Bug.一般只有测试人员使用。Beta:也是测试版,这个阶段的版本会一直加入新的功能。在Alpha版之后推出。Stable:稳定版;来自预览版本释出使用与改善而修正完成。