- Home
- イーサリアムの承認待ちトランザクションを取得する
イーサリアムの承認待ちトランザクションを取得する
ブロックチェーンの中からペンディング状態のトランザクションを取得する。
ペンディングトランザクションの取得
Ethereumでペンディングトランザクションを取得するためには、web3.js(v1.2.0)のweb3.eth.subscribe('pendingTransactions' [, callback]);
を用いて値を抜き出せばよい。
※web3.js のバージョンごとに実行可能なコマンドが異なっているため注意が必要。
1. geth 起動オプションの変更
Ethereumでsubscribeを使用するためにwebsoketの機能を有効化する。
こちらを参照し supervisorのgethプロセス起動オプションに以下を追加する。
--ws --wsaddr "127.0.0.1" --wsport "8546"
2. supervisor を再起動し設定を反映
$ sudo supervisorctl reload
3.node.js によるペンディングトランザクション一覧取得
websoket用のアドレス:ポートに対してコマンドを発行しペンディングトランザクションを取得する。
const Web3 = require("web3"); web3 = new Web3(); web3.setProvider(new Web3.providers.WebsocketProvider("ws://localhost:8546")); var subscription = web3.eth.subscribe('pendingTransactions', function(error, result){ if (!error) console.log(result); }) .on("data", function(transaction){ console.log(transaction); }); // unsubscribes the subscription subscription.unsubscribe(function(error, success){ if(success) console.log('Successfully unsubscribed!'); });
実行結果。ペンディングトランザクションが表示される。
0xe53ea58871e9fc745821fa19d8fa990711b18af15e0fcd16c68a2345ed026802 0x49b378509c40c3f4f3d98fea68b3d873753dafcc61a697ab781ff89d6b886e26 0xf92a79e05ae0760e196a1873f5a80bf8272e55b7b74294a662729d4e06d9783e …
websoketでリアルタイムにペンディングトランザクションを取得しているため、時間経過とともに承認されていく。
ご相談・お見積もり
03-5207-2689