0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

RPCエンドポイントを利用してウォレットを実装してみる #5ネット情報取得

Posted at

テーマ設定

RPCエンドポイントを利用してネットワークIDやチェーンIDの取得する。

前提環境

web3のインストール

npm install web3

実装

以下の内容でgetNetworkAndChainId.jsファイルを作成してください。

const { Web3 } = require('web3');

// RPCエンドポイントを設定
const web3 = new Web3('https://gateway-api.cabinet-node.com/{access_token}');

async function getNetworkAndChainId() {
   try {
       // ネットワークIDを取得
       const networkId = await web3.eth.net.getId();

       // チェーンIDを取得
       const chainId = await web3.eth.getChainId();

       console.log(`Network ID: ${networkId}`);
       console.log(`Chain ID: ${chainId}`);
   } catch (error) {
       console.error('Error fetching network or chain ID:', error);
   }
}

getNetworkAndChainId();

実行

node getNetworkAndChainId.js

結果の例

Network ID: 1
Chain ID: 1
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?