摘要:啟動節(jié)點環(huán)境配置腳本創(chuàng)建文件夾,保存如下代碼至運行開發(fā)環(huán)境運行結(jié)果日志部分如果你看到了以上的消息,說明運行成功,已經(jīng)開始區(qū)塊生產(chǎn)。
搭建一個 FIBOS 開發(fā)環(huán)境需要安裝 fibos 和 fibos.js。
本章節(jié)示例代碼的目錄結(jié)構(gòu):
hello_fibos/ ├── fibos_client │ ├── call.js //調(diào)用合約接口腳本文件 │ ├── initClient.js //FIBOS連接文件 │ ├── deploy.js //加載、發(fā)布合約腳本文件 │ ├── hello │ │ ├── hello.abi //合約abi文件 │ │ └── hello.js //合約代碼文件 │ └── package.json └── start_fibos └── node.js
本章節(jié)示例代碼地址:https://github.com/FIBOSIO/sa... 下basic文件夾示例。
啟動節(jié)點環(huán)境配置腳本
創(chuàng)建 start_fibos 文件夾,保存如下代碼至 node.js
var fibos = require("fibos"); fibos.load("chain"); fibos.load("chain_api"); fibos.load("history_api"); fibos.load("producer", { "producer-name": "eosio", "enable-stale-production": true }); fibos.load("http", { "http-server-address": "0.0.0.0:8888" }); fibos.load("net", { "p2p-listen-endpoint": "0.0.0.0:9876" }); fibos.config_dir = "fibos_config_dir/"; fibos.data_dir = "fibos_data_dir/"; fibos.enableJSContract = true; fibos.start();
運行 FIBOS 開發(fā)環(huán)境
hello_fibos$ fibos start_fibos/node.js
運行結(jié)果日志(部分):
…… 2018-07-30T03:29:01.004 thread-1 producer_plugin.cpp:1194 produce_block ] Produced block 00000002e091c956... #2 @ 2018-07-30T03:29:01.000 signed by eosio [trxs: 0, lib: 0, confirmed: 0]
如果你看到了以上的消息,說明運行成功,fibos 已經(jīng)開始區(qū)塊生產(chǎn)。
JavaScript 合約代碼注意:需新建窗口,保證 node.js 節(jié)點正常運行
創(chuàng)建文件夾 fibos_client,保存如下代碼至 hello/hello.js:
exports.hi = user => console.error("in contract:", user);合約 ABI 文件
保存如下代碼至 hello/hello.abi:
{ "version": "eosio::abi/1.0", "structs": [{ "name": "player", "base": "", "fields": [{ "name": "title", "type": "string" }, { "name": "age", "type": "int64" }] }, { "name": "hi", "base": "", "fields": [{ "name": "user", "type": "name" }] }], "actions": [{ "name": "hi", "type": "hi", "ricardian_contract": "" }] }連接 FIBOS 節(jié)點
在文件夾fibos_client下,新建 initClient.js,保存如下代碼:
var FIBOS = require("fibos.js"); function initClient(_keyProvider) { return FIBOS({ chainId: "cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f", keyProvider: _keyProvider, httpEndpoint: "http://127.0.0.1:8888", logger: { log: null, error: null } }); } module.exports = initClient;加載、發(fā)布合約腳本
在文件夾fibos_client下,新建 deploy.js,保存如下代碼:
var FIBOS = require("./initClient.js") var fs = require("fs"); var config = { "public-key": "FO6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV", "private-key": "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3", "contractName": "hello" }; // new FIBOS client var fibos = FIBOS(config["private-key"]); // create account hello fibos.newaccountSync({ creator: "eosio", name: "hello", owner: config["public-key"], active: config["public-key"] }); //setcode var js_code = fs.readTextFile("./hello/hello.js"); fibos.setcodeSync(config["contractName"], 0, 0, fibos.compileCode(js_code)); //getcode var code = fibos.getCodeSync(config["contractName"], true); console.log("code:", code); //setabi var abi = JSON.parse(fs.readTextFile("./hello/hello.abi")); fibos.setabiSync(config["contractName"], abi);
運行腳本:
hello_fibos$ fibos fibos_client/deploy.js
輸出結(jié)果(部分):
code: { "account_name": "hello", "code_hash": "383a12daacaf124eea9afc529822d990853b5b99570401b8394534b746ea3977", "wast": "", "wasm": "504b03042d00000008002cadfe4c6a9400a2360000003900000008001400696e6465782e6a7301001000000000000000000000000000000000004bad28c82f2a29d6cbc854b055282d4e2d52b0b55348cecf2bcecf49d54b2d2aca2fd250cfcc0389941425269758a9eb8055695a0300504b010200001400000008002cadfe4c6a9400a23600000039000000080000000000000001000000000000000000696e6465782e6a73504b0506000000000100010036000000700000000000" }
備注:wasm 是一個低層級的、輕便式的字節(jié)碼,它致力于實現(xiàn)接近原生的執(zhí)行速度。
調(diào)用合約使用 fibos.js 中的 API 去調(diào)用合約。
在文件夾fibos_client下,新建 call.js,保存如下代碼:
var FIBOS = require("./initClient.js") var config = { "public-key": "FO6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV", "private-key": "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3", "contractName": "hello" }; // new FIBOS client var fibos = FIBOS(config["private-key"]); //call abi var ctx = fibos.contractSync(config["contractName"]); let i = ctx.hiSync("hello", { authorization: config["contractName"] }); console.log(i)
執(zhí)行腳本:
hello_fibos$ fibos fibos_client/call.js
控制臺打印結(jié)果如下:
{ "broadcast": true, "transaction": { "compression": "none", "transaction": { "expiration": "2018-10-29T09:54:38", "ref_block_num": 45, "ref_block_prefix": 552314262, "max_net_usage_words": 0, "max_cpu_usage_ms": 0, "delay_sec": 0, "context_free_actions": [], "actions": [ { "account": "hello", "name": "hi", "authorization": [ { "actor": "hello", "permission": "active" } ], "data": "00000000001aa36a" } ], "transaction_extensions": [] }, "signatures": [ "SIG_K1_JxKj8whii1gosSY6S5b3JcNxZ7x7xTUFkWg3fsjpiQ3K8eTfY6N8van5PqoUFYGJVRGNzdxapWLc6NAcieJgPCh8e8hkDA" ] }, "transaction_id": "5d7262907ab645168f6a1fdd1e17988ca7f50a07d2ef6bb90f5891a6c2cb8bb9", "processed": { "id": "5d7262907ab645168f6a1fdd1e17988ca7f50a07d2ef6bb90f5891a6c2cb8bb9", "block_num": 47, "block_time": "2018-10-29T09:53:39.000", "producer_block_id": null, "receipt": { "status": "executed", "cpu_usage_us": 3510, "net_usage_words": 13 }, "elapsed": 7273, "net_usage": 104, "scheduled": false, "action_traces": [ { "receipt": { "receiver": "hello", "act_digest": "35c685a9391997dfe62291f39ead19cde36bbfb29f411deb9019592ed6be5b4f", "global_sequence": 50, "recv_sequence": 1, "auth_sequence": [ [ "hello", 3 ] ], "code_sequence": 1, "abi_sequence": 1 }, "act": { "account": "hello", "name": "hi", "authorization": [ { "actor": "hello", "permission": "active" } ], "data": { "user": "hello" }, "hex_data": "00000000001aa36a" }, "context_free": false, "elapsed": 7011, "cpu_usage": 0, "console": "in contract: hello ", "total_cpu_usage": 0, "trx_id": "5d7262907ab645168f6a1fdd1e17988ca7f50a07d2ef6bb90f5891a6c2cb8bb9", "block_num": 47, "block_time": "2018-10-29T09:53:39.000", "producer_block_id": null, "account_ram_deltas": [], "inline_traces": [] } ], "except": null } }
同時在 FIBOS 節(jié)點服務(wù)控制臺顯示 trxs:1 顯示結(jié)果如下:
2018-8-30T14:28:22.005 thread-1 producer_plugin.cpp:1196 produce_block ] Produced block 00000e57c573a33b... #3671 @ 2018-07-30T14:28:22.000 signed by eosio [trxs: 1, lib: 3670, confirmed: 0]
文末附上官方地址~~~
https://fibos.io/ FIBOS 是一個創(chuàng)造和發(fā)展區(qū)塊鏈應(yīng)用生態(tài)的平臺,讓你輕松創(chuàng)建屬于自己的價值網(wǎng)絡(luò),幫助開發(fā)者和創(chuàng)業(yè)者一步進入?yún)^(qū)塊鏈世界。
https://dev.fo/ JavaScript 開發(fā) + BANCOR 協(xié)議智能通證 + 開發(fā)者服務(wù),F(xiàn)IBOS 平臺實現(xiàn)了快速開發(fā)、快速部署和穩(wěn)定且流動的通證體系,幫助開發(fā)者一步進入?yún)^(qū)塊鏈時代。
搜索關(guān)注公眾號「FIBOS社區(qū)」,第一時間獲取技術(shù)干貨!
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/24477.html
摘要:啟動節(jié)點環(huán)境配置腳本創(chuàng)建文件夾,保存如下代碼至運行開發(fā)環(huán)境運行結(jié)果日志部分如果你看到了以上的消息,說明運行成功,已經(jīng)開始區(qū)塊生產(chǎn)。 搭建一個 FIBOS 開發(fā)環(huán)境需要安裝 fibos 和 fibos.js。 本章節(jié)示例代碼的目錄結(jié)構(gòu): hello_fibos/ ├── fibos_client │ ├── call.js //調(diào)用合約接口腳本文件 │ ├── initCli...
閱讀 2609·2021-11-18 10:02
閱讀 2278·2021-09-30 09:47
閱讀 1745·2021-09-27 14:01
閱讀 3109·2021-08-16 11:00
閱讀 3163·2019-08-30 11:06
閱讀 2391·2019-08-29 17:29
閱讀 1531·2019-08-29 13:19
閱讀 445·2019-08-26 13:54