1. 安裝eosio.cdt
wget https://github.com/eosio/eosio.cdt/releases/download/v1.4.1/eosio.cdt-1.4.1.x86_64.deb
sudo apt install ./eosio.cdt-1.4.1.x86_64.deb
sudo apt remove eosio.cdt
2. 編譯合約
eosio-cpp test.cpp -o test.wasm --abigen
3. 創建錢包
cleos wallet create --to-console
Creating wallet: default
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5H......"
4. 導入私鑰進錢包
a. eosio(EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV=KEY:5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3)
root@163be9123bc2:/work# cleos wallet import
private key: imported private key for: EOS7gaWGNfUHpECQ2SyyhoMJD7gBPcPezFLWH9SghV9LjwUn8byzC
b. 測試賬號
root@163be9123bc2:/work# cleos wallet import
private key: imported private key for: EOS6MRyAj.......
5. 創建賬號
root@163be9123bc2:/work# cleos create account eosio kylin1111111 EOS7gaW...... EOS7gaWGNf......
executed transaction: 98d04e953cc2b26b5b8741d63a839e1f07e88ce110826697c7453f82ee8b63dc 200 bytes 200 us
# eosio <= eosio::newaccount {"creator":"eosio","name":"kylin1111111","owner":{"threshold":1,"keys": [{"key":"EOS7gaWGNfUHpECQ2Syy
warning: transaction executed locally, but may not be confirmed by the network yet ]
6. 部署合約
root@163be9123bc2:/work# cleos set contract kylin1111111 ./test -p kylin1111111
Reading WASM from ./test/test.wasm...
Publishing contract...
executed transaction: 854b1ea4336a01bc9bb6cc3a0d2631c9419b967a1bab5db5663468bca3ecc633 5376 bytes 32954 us
# eosio <= eosio::setcode {"account":"kylin1111111","vmtype":0,"vmversion":0,"code":"0061736d01000000019a011960027f7e0060047f7...
# eosio <= eosio::setabi {"account":"kylin1111111","abi":"0e656f73696f3a3a6162692f312e310004036164640003076163636f756e74046e6...
warning: transaction executed locally, but may not be confirmed by the network yet ]
7. 執行合約
root@163be9123bc2:/work# cleos push action kylin1111111 add '["kylin1111111", 1, "1"]' -p kylin1111111
executed transaction: a738e7d0bb2946091ad50ee60c3832339ad61569c564cd866f6c8c02faf9b129 112 bytes 395 us
# kylin1111111 <= kylin1111111::add {"account":"kylin1111111","value":1,"desc":"1"}
warning: transaction executed locally, but may not be confirmed by the network yet ]
8. 執行合約
root@163be9123bc2:/work# cleos get transaction a738e7d0bb2946091ad50ee60c3832339ad61569c564cd866f6c8c02faf9b129
{
"id": "a738e7d0bb2946091ad50ee60c3832339ad61569c564cd866f6c8c02faf9b129",
"trx": {
"receipt": {
"status": "executed",
"cpu_usage_us": 395,
"net_usage_words": 14,
"trx": [
1,{
"signatures": [
"SIG_K1_JvNd4ysZPJNuZPqE4ZmLMWBS3AHciQXVw2aPfxg7vumwfexHeWUU8f8zrLXaVhCvP3EjNsvnTdt7z8w4iMGNL6mGPgufjb"
],
"compression": "none",
"packed_context_free_data": "",
"packed_trx": "281e0a5caf62a53fec8400000000011042082184e9a2870000000000005232011042082184e9a28700000000a8ed3232121042082184e9a2870100000000000000013100"
}
]
},
"trx": {
"expiration": "2018-12-07T07:15:52",
"ref_block_num": 25263,
"ref_block_prefix": 2230075301,
"max_net_usage_words": 0,
"max_cpu_usage_ms": 0,
"delay_sec": 0,
"context_free_actions": [],
"actions": [{
"account": "kylin1111111",
"name": "add",
"authorization": [{
"actor": "kylin1111111",
"permission": "active"
}
],
"data": {
"account": "kylin1111111",
"value": 1,
"desc": "1"
},
"hex_data": "1042082184e9a28701000000000000000131"
}
],
"transaction_extensions": [],
"signatures": [
"SIG_K1_JvNd4ysZPJNuZPqE4ZmLMWBS3AHciQXVw2aPfxg7vumwfexHeWUU8f8zrLXaVhCvP3EjNsvnTdt7z8w4iMGNL6mGPgufjb"
],
"context_free_data": []
}
},
"block_time": "2018-12-07T07:15:23.000",
"block_num": 25265,
"last_irreversible_block": 25346,
"traces": [{
"receipt": {
"receiver": "kylin1111111",
"act_digest": "45957708225bdbea768d7d680ad538bbaa484b915c3f7a6c8515f58bb41e0e32",
"global_sequence": 25268,
"recv_sequence": 1,
"auth_sequence": [[
"kylin1111111",
3
]
],
"code_sequence": 1,
"abi_sequence": 1
},
"act": {
"account": "kylin1111111",
"name": "add",
"authorization": [{
"actor": "kylin1111111",
"permission": "active"
}
],
"data": {
"account": "kylin1111111",
"value": 1,
"desc": "1"
},
"hex_data": "1042082184e9a28701000000000000000131"
},
"context_free": false,
"elapsed": 244,
"cpu_usage": 0,
"console": "",
"total_cpu_usage": 0,
"trx_id": "a738e7d0bb2946091ad50ee60c3832339ad61569c564cd866f6c8c02faf9b129",
"block_num": 25265,
"block_time": "2018-12-07T07:15:23.000",
"producer_block_id": null,
"account_ram_deltas": [{
"account": "kylin1111111",
"delta": 242
}
],
"inline_traces": []
}
]
}
9. 查詢數據表
root@163be9123bc2:/work# cleos get table kylin1111111 kylin1111111 useritem
{
"rows": [{
"account": "9773630895046541840",
"value": 1,
"desc": "1"
}
],
"more": false
}
10. 智能合約
#include<string>
#include<eosiolib/eosio.hpp>
using namespace std;
using namespace eosio;
CONTRACT test : public eosio::contract
{
private:
TABLE useritem
{
uint64_t account;
uint64_t value;
string desc;
auto primary_key() const { return account; }
};
typedef eosio::multi_index<"useritem"_n, useritem> user_table;
user_table _users;
public:
using contract::contract;
test(name receiver, name code, datastream<const char*> ds) : contract(receiver, code, ds), _users(receiver, receiver.value)
{
}
ACTION add(name account, uint64_t value, string desc)
{
require_auth(account);
auto itr = _users.find(account.value);
eosio_assert(itr == _users.end(), "account already exist!");
_users.emplace(account, [&](auto& newuser){
newuser.account = account.value;
newuser.value = value;
newuser.desc = desc;
});
}
ACTION modify(name account, uint64_t value, string desc)
{
require_auth(account);
auto itr = _users.find(account.value);
eosio_assert(itr != _users.end(), "account do not exist!");
_users.modify(itr, account, [&](auto& newuser){
newuser.account = account.value;
newuser.value = value;
newuser.desc = desc;
});
}
ACTION del(name account)
{
require_auth(account);
auto itr = _users.find(account.value);
eosio_assert(itr != _users.end(), "account do not exist!");
_users.erase(itr);
}
using add_action = action_wrapper<"add"_n, &test::add>;
using modify_action = action_wrapper<"modify"_n, &test::modify>;
using del_action = action_wrapper<"del"_n, &test::del>;
};
EOSIO_DISPATCH(test, (add)(modify)(del))