怎樣使用EOS.JS的API
在這篇文章中,我們將討論EOSJS上常用的API並對其進行測試。
我們將使所有程式碼可用。為了可用,需要做些準備工作,安裝 Node.js
和 EOSJS
並將以下程式碼內容放在javascript檔案的頂部。
const Eos = require('eosjs'); const config = { expireInSeconds: 60, broadcast: true, debug: false, sign: true, // mainNet bp endpoint httpEndpoint: 'https://api.eosnewyork.io', // mainNet chainId chainId: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906', }; const eos = Eos(config);
完成此操作後,編寫下面的api示例並使用node.js執行javascript以獲取相應的值。
注意:由於每個BP端點具有不同的速度及可靠性,建議使用最適合您的BP。
getBlock(blockNumOrId)
用於得到相應的區塊資訊。
引數:
程式碼:
// Promise eos.getBlock(1).then(result => console.log(result)).catch(error => console.error(error)); // callback eos.getBlock(1, (error, result) => console.log(error, result)); // Parameters object eos.getBlock({block_num_or_id: 1}).then(console.log);
結果:
{ timestamp: '2018-06-08T08:08:08.500', producer: '', confirmed: 1, previous: '0000000000000000000000000000000000000000000000000000000000000000', transaction_mroot: '0000000000000000000000000000000000000000000000000000000000000000', action_mroot: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906', schedule_version: 0, new_producers: null, header_extensions: [], producer_signature: 'SIG_K1_111111111111111111111111111111111111111111111111111111111111111116uk5ne', transactions: [], block_extensions: [], id: '00000001405147477ab2f5f51cda427b638191c66d2c59aa392d5c2c98076cb0', block_num: 1, ref_block_prefix: 4126519930 }
要檢視適用塊中發生的活動,可檢視交易。檢視交易,執行以下操作:
[ { status: 'executed', cpu_usage_us: 1170, net_usage_words: 40, trx: { id: '8a29bfa66850b7d4a2b0b62173a24c5dfe4dbd7b39c211df6309d02a85374960', signatures: [Array], compression: 'none', packed_context_free_data: '', context_free_data: [], packed_trx: '9051595bad38f016a289000000000100a6823403ea3055000000572d3ccdcd0110e0a53cab294d7600000000a8ed3232dd0110e0a53cab294d76a0986af64b96bc65010000000000000004454f5300000000bb01496e74726f647563696e67204954414d204e6574776f726b2c20616e20454f532d426173656420444150502050726f6a656374206f6e20426c6f636b636861696e2047616d696e6720506c6174666f726d20666f722061205472616e73706172656e742047616d696e672045636f73797374656d2e202d2d576562736974653a2068747470733a2f2f6974616d2e67616d65732f656e202d2d54656c656772616d3a2068747470733a2f2f742e6d652f6974616d6e6574776f726b00', transaction: [Object] } } ]
如果使用上述值檢視交易,則會有操作。通過檢視操作,可以更深入地瞭解發生的活動。
getAccount(accountName)
用於獲取帳戶的資訊。
引數:
程式碼:
// Promise eos.getAccount('itamnetwork1') .then(result => console.log(result)) .catch(error => console.error(error)); // callback eos.getAccount('itamnetwork1', (error, result) => console.log(error, result)); // Parameters object eos.getAccount({account_name: 'itamnetwork1'}) .then(result => console.log(result)) .catch(error => console.error(error));
結果:
{ account_name: 'itamnetwork1', head_block_num: 8516805, head_block_time: '2018-07-30T07:34:52.500', privileged: false, last_code_update: '1970-01-01T00:00:00.000', created: '2018-07-09T02:24:58.500', core_liquid_balance: '12.6131 EOS', ram_quota: 14976, net_weight: 201000, cpu_weight: 10401000, net_limit: { used: 1679786, available: 11108657, max: 12788443 }, cpu_limit: { used: 7950353, available: 6356380, max: 14306733 }, ram_usage: 10934, permissions: [ { perm_name: 'active', parent: 'owner', required_auth: [Object] }, { perm_name: 'owner', parent: '', required_auth: [Object] } ], total_resources: { owner: 'itamnetwork1', net_weight: '20.1000 EOS', cpu_weight: '1040.1000 EOS', ram_bytes: 14976 }, self_delegated_bandwidth: { from: 'itamnetwork1', to: 'itamnetwork1', net_weight: '0.1000 EOS', cpu_weight: '0.1000 EOS' }, refund_request: null, voter_info: { owner: 'itamnetwork1', proxy: '', producers: [], staked: 4000, last_vote_weight: '0.00000000000000000', proxied_vote_weight: '0.00000000000000000', is_proxy: 0 } }
我來解釋一些上述結果值。
staked
getKeyAccounts(publicKey)
用於獲取公鑰對應的帳戶。
引數:
程式碼:
// Promise eos.getKeyAccounts('EOS6S6C5ExCM7VHGdmG5h6VREVJEC33bpMJtLucwhyByPmzB58KW5') .then(result => console.log(result)) .catch(error => console.error(error)); // callback eos.getKeyAccounts('EOS6S6C5ExCM7VHGdmG5h6VREVJEC33bpMJtLucwhyByPmzB58KW5', (error, result) => console.log(error, result)); // Parameters object eos.getKeyAccounts({public_key: 'EOS6S6C5ExCM7VHGdmG5h6VREVJEC33bpMJtLucwhyByPmzB58KW5'}) .then(console.log);
結果:
{ account_names: [ 'itamnetwork1' ] }
可以使用一個EOS公鑰建立許多帳戶。這就是為什麼 account_name
的值是一個字串形式的陣列。
getCurrencyBalance(code, account, symbol)
獲取賬號餘額。
引數:
程式碼:
// Promise eos.getCurrencyBalance('eosio.token', 'itamnetwork1', 'EOS') .then(result => console.log(result)) .catch(error => console.error(error)); // callback eos.getCurrencyBalance('eosio.token', 'itamnetwork1', 'EOS', (error, result) => console.log(error, result)); // Parameters object eos.getCurrencyBalance({account: 'itamnetwork1', code: 'eosio.token', symbol: 'EOS'}) .then(console.log);
結果:
[ '12.6131 EOS' ]
如果檢視結果值,可以以字串的形式檢視陣列。這是因為帳戶中可能存在許多不同符號的token。在EOS testnet,Jungle Net上,如果你不包括搜尋eosio.token,你可以看到2個token。
getCurrencyStats(code, symbol)
獲取某種代幣的資訊。
引數:
程式碼:
// Promise eos.getCurrencyStats('eosio.token', 'EOS') .then(result => console.log(result)) .catch(error => console.error(error)); // callback eos.getCurrencyStats('eosio.token', 'EOS', (error, result) => console.log(error, result)); // Parameters object eos.getCurrencyStats({code: 'eosio.token', symbol: 'EOS'}) .then(console.log);
結果:
{ EOS: { supply: '1006148640.3388 EOS', max_supply: '10000000000.0000 EOS', issuer: 'eosio' } }
以下是結果值的說明。
- supply:當前提供的token數量。
- max_supply:token總數。
- issuer:發行人。
結論
我們瀏覽了5個最常用的EOSJS API。 我們還沒有涵蓋很多API,我打算在以後的帖子中繼續討論。謝謝。
======================================================================
分享一個互動式的線上程式設計實戰, EOS智慧合約與DApp開發入門 :
ofollow,noindex">EOS教程
本課程幫助你快速入門EOS區塊鏈去中心化應用的開發,內容涵蓋EOS工具鏈、賬戶與錢包、發行代幣、智慧合約開發與部署、使用程式碼與智慧合約互動等核心知識點,最後綜合運用各知識點完成一個便籤DApp的開發。
- web3j教程,主要是針對java和android程式員進行區塊鏈以太坊開發的web3j詳解。
- 以太坊教程,主要介紹智慧合約與dapp應用開發,適合入門。
- 以太坊開發,主要是介紹使用node.js、mongodb、區塊鏈、ipfs實現去中心化電商DApp實戰,適合進階。
- python以太坊,主要是針對python工程師使用web3.py進行區塊鏈以太坊開發的詳解。
- php以太坊,主要是介紹使用php進行智慧合約開發互動,進行賬號建立、交易、轉賬、代幣開發以及過濾器和事件等內容。
- C#以太坊,主要講解如何使用C#開發基於.Net的以太坊應用,包括賬戶管理、狀態與交易、智慧合約開發與互動、過濾器和事件等。
匯智網原創翻譯,轉載請標明出處。這裡是 原文