(四)区块链开发之以太坊变成接口

1、web3.js API

https://github.com/ethereum/web3.js/tree/1.0

https://web3js.readthedocs.io/en/1.0/#

安装:

npm install web3

1.创建web3实例,connect.js

  1 var Web3 = require('web3');
  2
  3 //创建web3对象
  4 var web3 = new Web3();
  5 //链接到以太坊节点
  6 var conn = web3.setProvider(new web3.providers.HttpProvider("http://localhos    t:8545"));
  7 console.log(conn)

运行:node connect.js 输出true

2、账户相关API

var accounts = web3.eth.accounts;
console.info(accounts);

查看以太坊节点上所有可用的账户

nodejs node connect.js
true
Accounts {
  currentProvider: [Getter/Setter],
  _requestManager:
   RequestManager {
     provider:
      HttpProvider {
        host: 'http://localhost:8545',
        httpAgent: [Agent],
        timeout: 0,
        headers: undefined,
        connected: false },
     providers:
      { WebsocketProvider: [Function: WebsocketProvider],
        HttpProvider: [Function: HttpProvider],
        IpcProvider: [Function: IpcProvider] },
     subscriptions: {} },
  givenProvider: null,
  providers:
   { WebsocketProvider: [Function: WebsocketProvider],
     HttpProvider: [Function: HttpProvider],
     IpcProvider: [Function: IpcProvider] },
  _provider:
   HttpProvider {
     host: 'http://localhost:8545',
     httpAgent:
      Agent {
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        defaultPort: 80,
        protocol: 'http:',
        options: [Object],
        requests: {},
        sockets: {},
        freeSockets: {},
        keepAliveMsecs: 1000,
        keepAlive: true,
        maxSockets: Infinity,
        maxFreeSockets: 256 },
     timeout: 0,
     headers: undefined,
     connected: false },
  setProvider: [Function],
  _ethereumCall:
   { getId:
      { [Function: send]
        method: [Method],
        request: [Function: bound ],
        call: 'net_version' },
     getGasPrice:
      { [Function: send]
        method: [Method],
        request: [Function: bound ],
        call: 'eth_gasPrice' },
     getTransactionCount:
      { [Function: send]
        method: [Method],
        request: [Function: bound ],
        call: 'eth_getTransactionCount' } },
  wallet:
   Wallet {
     _accounts: [Circular],
     length: 0,
     defaultKeyName: 'web3js_wallet' } }

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值