(四)區塊鏈開發之以太坊變成接口

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

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章