這篇文章主要介紹“怎么為EOSIO平臺開發(fā)智能合約”,在日常操作中,相信很多人在怎么為EOSIO平臺開發(fā)智能合約問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么為EOSIO平臺開發(fā)智能合約”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比新城網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式新城網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋新城地區(qū)。費用合理售后完善,十載實體公司更值得信賴。
示例智能合約的目的是模擬選舉。我創(chuàng)建了一個EOSIO用戶來托管智能合約。創(chuàng)建了兩個公民用戶來投票給候選人。投票記錄保存在EOSIO區(qū)塊鏈中。在此示例中,所有操作都在命令模式下運行。讓我們開始吧。
EOSIO執(zhí)行以WebAssembly標準開發(fā)的智能合約。所以我用C++開發(fā)了選舉智能合約。以下是election.cpp
的完整源代碼:
#include <eosiolib/eosio.hpp> using namespace eosio; class election : public contract { private: // create the multi index tables to store the data /// @abi table struct candidate { uint64_t _key; // primary key std::string _name; // candidate name uint32_t _count = 0; // voted count uint64_t primary_key() const { return _key; } }; typedef eosio::multi_index<N(candidate), candidate> candidates; /// @abi table struct voter { uint64_t _key; uint64_t _candidate_key; // name of poll account_name _account; // this account has voted, avoid duplicate voter uint64_t primary_key() const { return _key; } uint64_t candidate_key() const { return _candidate_key; } }; typedef eosio::multi_index<N(voter), voter, indexed_by<N(_candidate_key), const_mem_fun<voter, uint64_t, &voter::candidate_key>>> voters; // local instances of the multi indexes candidates _candidates; voters _voters; uint64_t _candidates_count; public: election(account_name s) : contract(s), _candidates(s, s), _voters(s, s), _candidates_count(0) {} // public methods exposed via the ABI // on candidates /// @abi action void version() { print("Election Smart Contract version 0.0.1\n"); }; /// @abi action void addc(std::string name) { print("Adding candidate ", name, "\n"); uint64_t key = _candidates.available_primary_key(); // update the table to include a new candidate _candidates.emplace(get_self(), [&](auto &p) { p._key = key; p._name = name; p._count = 0; }); print("Candidate added successfully. candidate_key = ", key, "\n"); }; /// @abi action void reset() { // Get all keys of _candidates std::vector<uint64_t> keysForDeletion; for (auto &itr : _candidates) { keysForDeletion.push_back(itr.primary_key()); } // now delete each item for that poll for (uint64_t key : keysForDeletion) { auto itr = _candidates.find(key); if (itr != _candidates.end()) { _candidates.erase(itr); } } // Get all keys of _voters keysForDeletion.empty(); for (auto &itr : _voters) { keysForDeletion.push_back(itr.primary_key()); } // now delete each item for that poll for (uint64_t key : keysForDeletion) { auto itr = _voters.find(key); if (itr != _voters.end()) { _voters.erase(itr); } } print("candidates and voters reset successfully.\n"); }; /// @abi action void results() { print("Start listing voted results\n"); for (auto& item : _candidates) { print("Candidate ", item._name, " has voted count: ", item._count, "\n"); } }; /// @abi action void vote(account_name s, uint64_t candidate_key) { require_auth(s); bool found = false; // Did the voter vote before? for (auto& item : _voters) { if (item._account == s) { found = true; break; } } eosio_assert(!found, "You're voted already!"); // Findout the candidate by id std::vector<uint64_t> keysForModify; for (auto& item : _candidates) { if (item.primary_key() == candidate_key) { keysForModify.push_back(item.primary_key()); break; } } if (keysForModify.size() == 0) { eosio_assert(found, "Invalid candidate id!"); return; } // Update the voted count inside the candidate for (uint64_t key : keysForModify) { auto itr = _candidates.find(key); auto candidate = _candidates.get(key); if (itr != _candidates.end()) { _candidates.modify(itr, get_self(), [&](auto& p) { p._count++; }); print("Voted candidate: ", candidate._name, " successfully\n"); } } // Add this user to voters array _voters.emplace(get_self(), [&](auto& p) { p._key = _voters.available_primary_key(); p._candidate_key = candidate_key; p._account = s; }); }; }; EOSIO_ABI(election, (version)(reset)(addc)(results)(vote))
注意最后一行EOSIO_ABI()
是一個宏語句,用于自動生成ABI文件而不是手動編寫。ABI文件用于定義提交動作處理程序。這告訴了EOSIO智能合約中處理程序的定義。
EOSIO為我們提供了多索引數(shù)據(jù)庫API,可以將數(shù)據(jù)保存到區(qū)塊鏈中。在上面的選舉智能合約中,我定義了兩個multi_index
(類似于SQL表):候選人和選民。實際上是兩個數(shù)組存儲兩個結構:候選者和選民。我使用C++ STL來操作multi_index
,例如add
,update
,delete
。
請注意,兩個結構在開頭標有/// @abi table
。這是告訴EOSIO abi生成器在election.abi
文件中生成ABI表。這很方便。
編譯選舉智能合約:
$ eosiocpp -o election.wast election.cpp
分別生成WAST和WASM文件。但這對EOSIO來說還不夠。我們還需要生成ABI文件:
$ eosiocpp -g election.abi election.cpp
為了增強開發(fā)體驗,我為Visual Studio Code(VSCode)創(chuàng)建了一個屬性文件c_cpp_properties.json
,告訴它如何查找頭文件。該文件需要存儲在.vscode
目錄中,如下所示:
.vscode/c_cpp_properties
文件內容如下:
{ "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "~/eos/contracts", "~/opt/boost/include" ], "defines": [], "compilerPath": "/usr/bin/clang++-4.0", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "clang-x64" } ], "version": 4 }
一直在使用配置良好的虛擬機(在第1部分中提到)。要啟動單節(jié)點Testnet服務器:
$ nodeos -e -p eosio --plugin eosio::wallet_api_plugin --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --access-control-allow-origin=* --contracts-console
單擊此處獲取nodeos參數(shù)的更多信息。
下一個任務是解鎖默認錢包。EOSIO將密鑰對存儲在錢包中。每次服務器重啟或每15分鐘需要解鎖一次。解鎖錢包:
$ cleos wallet unlock --password ${wallet_password}
我們需要分別創(chuàng)建一個所有者密鑰對和活動密鑰對。然后將該私鑰導入錢包。鍵入以下命令:
$ cleos create key # Create an owner key $ cleos create key # Create an active key $ cleos wallet import ${private_owner_key} $ cleos wallet import ${private_active_key}
不要忘記在某個地方記錄這些密鑰對。
接下來的任務是創(chuàng)建一個新的帳戶來保存選舉智能合約。 鍵入以下命令:
$ cleos create account eosio election ${public_owner_key} ${public_active_key}
此外,為投票模擬創(chuàng)建兩個公民:
$ cleos create account eosio voter1 ${public_owner_key} ${public_active_key} $ cleos create account eosio voter2 ${public_owner_key} ${public_active_key}
輸入以下命令上傳選舉智能合約:
$ cleos set contract election ../election -p election
結果類似下圖:
我們可以嘗試運行合約。
1.運行version
操作
$ cleos push action election version '' -p election
我們可以從nodeos檢查控制臺輸出:
2.增加選舉候選人
$ cleos push action election addc '["Hillary Clinton"]' -p election $ cleos push action election addc '["Donald J. Trump"]' -p election
3.顯示存儲在區(qū)塊鏈中的候選數(shù)據(jù)庫
$ cleos get table election election candidate
結果如圖所示:
4.模擬投票(兩位選民都被投票給唐納德·J·特朗普)
$ cleos push action election vote '["voter1", 1]' -p voter1 $ cleos push action election vote '["voter2", 1]' -p voter2
如果voter1再次投票:
$ cleos push action election vote '["voter1", 0]' -p voter1
EOSIO 將返回一個例外:
5.查看投票結果
$ cleos get table election election candidate
如你所見,候選人“Donald J. Trump”的投票數(shù)為2.這意味著選舉智能合約正在工作!
到此,關于“怎么為EOSIO平臺開發(fā)智能合約”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
分享名稱:怎么為EOSIO平臺開發(fā)智能合約
新聞來源:http://chinadenli.net/article8/gjeoip.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供域名注冊、全網(wǎng)營銷推廣、外貿網(wǎng)站建設、網(wǎng)站設計公司、網(wǎng)站營銷、App設計
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)