EOS開發入門之HelloWorld
EOS的智能合約是基于C++語言的,本篇將進入智能合約的開發階段,寫一個HelloWorld之類的合約。
話不多說,開始敲大??。
新建一個"hello_eos"的文件夾,然后創建一個"hello_eos.cpp"的文件,文件夾和文件名一樣,內容如下:
1、編寫智能合約
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
using namespace eosio;
class hello_eos : public eosio::contract {
public:
using contract::contract;
/// @abi action
void test(account_name user) {
print("Hello ,", name{user});
}
};
EOSIO_ABI(hello_eos, (test))
2、編譯智能合約
wenzildeiMac:hello_eos wenzil$ pwd
/Users/wenzil/Desktop/study/hello_eos
wenzildeiMac:hello_eos wenzil$ eosiocpp -o hello_eos.wast hello_eos.cpp
-bash: eosiocpp: command not found
找不到智能合約編譯工具eosiocpp,坑啊。
進入到EOS源碼的"build"目錄,然后執行如下命令生成eosiocpp工具。
wenzildeiMac:build wenzil$ pwd
/Users/wenzil/Documents/Study/eos/build
wenzildeiMac:build wenzil$ sudo make install
Password:
[ 0%] Built target binaryen
[ 0%] Built target wasm
##########此處省略多行輸出內容##########
-- Installing: /usr/local/bin/eosio-abigen
-- Installing: /usr/local/bin/eosiocpp
如果失敗了,再次編譯即可,編譯成功后發現多了兩個文件。
wenzildeiMac:hello_eos wenzil$ ls
hello_eos.cpp hello_eos.wasm hello_eos.wast
3、生成abi文件
wenzildeiMac:hello_eos wenzil$ eosiocpp -g hello_eos.abi hello_eos.cpp
3376922ms thread-0 abi_generator.hpp:68 ricardian_contracts ] Warning, no ricardian clauses found for hello_eos
3376922ms thread-0 abi_generator.hpp:75 ricardian_contracts ] Warning, no ricardian contract found for test
Generated hello_eos.abi ...
再次查看,發現多了一個abi文件。
wenzildeiMac:hello_eos wenzil$ ls
hello_eos.abi hello_eos.cpp hello_eos.wasm hello_eos.wast
4、為智能合約創建新賬號
智能合約附著在賬號上的,因此需要為合約準備一個賬號。
于是,先創建key
wenzildeiMac:study wenzil$ cleos create key
Private key: 5KcUM9PkRLqRq6KtrGT4ek8dy9mLfPh4cTGR6Pt8fU5tH6Q7y4c
Public key: EOS7caVJ2iDpM7edncLbTGtQqXtTZqyMYxhUP8e1xPzv3mVyL7Gxu
然后,導入到錢包
wenzildeiMac:study wenzil$ cleos wallet import 5KcUM9PkRLqRq6KtrGT4ek8dy9mLfPh4cTGR6Pt8fU5tH6Q7y4c
imported private key for: EOS7caVJ2iDpM7edncLbTGtQqXtTZqyMYxhUP8e1xPzv3mVyL7Gxu
創建賬號
wenzildeiMac:study wenzil$ cleos create account eosio hello.eos EOS7caVJ2iDpM7edncLbTGtQqXtTZqyMYxhUP8e1xPzv3mVyL7Gxu EOS7caVJ2iDpM7edncLbTGtQqXtTZqyMYxhUP8e1xPzv3mVyL7Gxu
executed transaction: f9f0317694b27749c7eed78a27babe0ea0d92987213f2f6f561c3237ce8e100b 200 bytes 265 us
# eosio <= eosio::newaccount {"creator":"eosio","name":"hello.eos","owner":{"threshold":1,"keys":[{"key":"EOS7caVJ2iDpM7edncLbTGt...
warning: transaction executed locally, but may not be confirmed by the network yet
5、部署智能合約
部署智能合約需要將合約綁定到賬號,先返回到上一個目錄
wenzildeiMac:hello_eos wenzil$ pwd
/Users/wenzil/Desktop/study/hello_eos
wenzildeiMac:hello_eos wenzil$ cd ..
wenzildeiMac:study wenzil$ pwd
/Users/wenzil/Desktop/study
wenzildeiMac:study wenzil$ cleos set contract hello.eos ./hello_eos -p hello.eos
Reading WAST/WASM from ./hello_eos/hello_eos.wasm...
Using already assembled WASM...
Publishing contract...
executed transaction: 2820024b8513f5c05c82a12444765aaf70f2c9fa717c68993a93a7739f108a8c 1808 bytes 439 us
# eosio <= eosio::setcode {"account":"hello.eos","vmtype":0,"vmversion":0,"code":"0061736d01000000013b0c60027f7e006000017e6002...
# eosio <= eosio::setabi {"account":"hello.eos","abi":"0e656f73696f3a3a6162692f312e300001047465737400010475736572046e616d6501...
warning: transaction executed locally, but may not be confirmed by the network yet
6、調用智能合約
wenzildeiMac:study wenzil$ cleos push action hello.eos test '{"user":"hello.eos"}' -p hello.eos
executed transaction: 23ba97ec481f4b14e533c164fe044aa4d539b413f29d6d608644f862a5414c7c 104 bytes 242 us
# hello.eos <= hello.eos::test {"user":"hello.eos"}
warning: transaction executed locally, but may not be confirmed by the network yet
奇怪了,居然沒打印出test方法中print的內容。
PS:剛入坑的小白,很多不懂,還請各位大佬多賜教,謝謝!