怎样使用EOS.JS的API开发智能合约

在这篇文章中,我们将讨论EOSJS上常用的API并对其进行测试。

我们将使所有代码可用。为了可用,需要做些准备工作,安装Node.jsEOSJS并将以下代码内容放在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)

用于得到相应的区块信息。

参数:

image

代码:

// 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)

用于获取帐户的信息。

参数:

image

代码:

// 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 } }

我来解释一些上述结果值。

  • account_name:这是EOS帐户名。
  • ram_quota:持有的RAM量,单位字节。
  • net_limit:帐户的总额、可用额、已用额,单位字节。
  • cpu_limit:CPU总量、可用CPU和已用CPU的总量,单位us。
  • ram_usage:帐户使用的RAM量,单位字节。
  • total_resources:分配给自己的EOS资源。
  • self_delegated_bandwidth:自己的委托信息。
  • voter_info:有关投票的信息。留意staked部分,显示我押注的数量。更具体地说,它的价值包括我委托给自己以及其他人委托的内容。

getKeyAccounts(publicKey)

用于获取公钥对应的帐户。

参数:

image

代码:

// 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)

获取账号余额。

参数:

怎样使用EOS.JS的API开发智能合约_第1张图片
image

代码:

// 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)

获取某种代币的信息。

参数:

怎样使用EOS.JS的API开发智能合约_第2张图片
image

代码:

// 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开发入门

EOS教程

本课程帮助你快速入门EOS区块链去中心化应用的开发,内容涵盖EOS工具链、账户与钱包、发行代币、智能合约开发与部署、使用代码与智能合约交互等核心知识点,最后综合运用各知识点完成一个便签DApp的开发。

  • web3j教程,主要是针对java和android程序员进行区块链以太坊开发的web3j详解。
  • 以太坊教程,主要介绍智能合约与dapp应用开发,适合入门。
  • 以太坊开发,主要是介绍使用node.js、mongodb、区块链、ipfs实现去中心化电商DApp实战,适合进阶。
  • python以太坊,主要是针对python工程师使用web3.py进行区块链以太坊开发的详解。
  • php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。
  • C#以太坊,主要讲解如何使用C#开发基于.Net的以太坊应用,包括账户管理、状态与交易、智能合约开发与交互、过滤器和事件等。

汇智网原创翻译,转载请标明出处。这里是原文

你可能感兴趣的:(怎样使用EOS.JS的API开发智能合约)