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エンドポイントを使ってgethのeth_gasPriceメソッドを呼び出す

Posted at

テーマ設定

RPCノードを使ってgethのeth_gasPriceメソッドを呼び出す処理を実装してみる。

前提環境

axiosのインストール

npm install axios

実装

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

const axios = require('axios');

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

// eth_gasPriceメソッドを使ってガス価格を取得する関数
async function getGasPriceUsingRpc() {
   const data = {
       jsonrpc: '2.0',
       method: 'eth_gasPrice',
       params: [],
       id: 1
   };

   try {
       // axiosを使ってRPCリクエストを送信
       const response = await axios.post(rpcUrl, data);
       
       // ガス価格を取得(Wei単位で返される)
       const gasPriceInWei = response.data.result;
       
       // WeiをGweiに変換
       const gasPriceInGwei = parseInt(gasPriceInWei, 16) / 1e9;

       console.log(`Current Gas Price: ${gasPriceInGwei} Gwei`);
   } catch (error) {
       console.error('Error fetching gas price:', error);
   }
}

getGasPriceUsingRpc();

実行

node eth_gasPrice.js

結果の例

Current Gas Price: 4.298716428 Gwei
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?