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_getBalanceメソッドを呼び出す

Posted at

テーマ設定

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

前提環境

axiosのインストール

npm install axios

実装

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

const axios = require('axios');

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

// eth_getBalanceメソッドを使って指定アドレスの残高を取得する関数
async function getBalance() {
   const data = {
       jsonrpc: '2.0',
       method: 'eth_getBalance',
       params: ['WALLET_ADDRESS','latest'], // set target wallet address
       id: 1
   };

   try {
       // axiosを使ってRPCリクエストを送信
       const response = await axios.post(rpcUrl, data);
       
       // 残高を取得(Wei単位で返される)
       const balance = response.data.result;

       console.log(`Current Balance: ${balance}`);
   } catch (error) {
       console.error('Error fetching balance:', error);
   }
}

getBalance();

実行

node eth_getBalance.js

結果の例

Current Balance: ***
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?