📝 Share Your Feedback on Twitter and Claim 1M Free GetBlock Requests

/

如何使用 Web3js 連接到 GetBlock

# 如何使用 Web3.js 連接到 GetBlock

Web3.js 是一個為與以太坊區塊鏈交互而構建的 JavaScript 庫。 它用於通過 HTTP、IPC 或 WebSocket 連接向以太坊節點發送 JSON-RPC 調用,以便從區塊鏈讀取數據、進行交易或部署智能合約。

在本指南中,我們將向您展示如何開始使用 web3.js 連接到 GetBlock。 首先,您需要將 web3.js 庫添加到您的項目中。 這可以使用以下命令完成:

• Npm:
npm install web3
• Yarn:
yarn add web3
• 純js鏈接:鏈接
dist/web3.min.js

之後,您需要創建一個 web3 實例並設置一個提供程序。

// In case you are using Node.js
const Web3 = require('web3');
// Setting getblock node as HTTP provider
const provider = new Web3.providers.HttpProvider("https://eth.getblock.io/mainnet/?api_key=YOUR_API_KEY_HERE");
// or as WebSocket provider
const provider = new Web3.providers.WebsocketProvider("wss://eth.getblock.io/mainnet/?api_key=YOUR_API_KEY_HERE");
// Creating web3 instance with given provider
const web3 = new Web3(provider);
// Initializing web3.eth method
var block = web3.eth.getBlockNumber().then(console.log);
web3js screenshot 1

所有 API 參考都可以在項目文檔中找到 https://web3js.readthedocs.io.

方法響應:

web3js screenshot 2