查看主要代码
//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%手续费)。
参考: