Balances

如何通過待處理的交易獲得 ETH 餘額?

  • September 29, 2021

我使用 web3.eth.getBalance 來獲取特定地址的餘額。我想知道地址有多少未確認的 ETH。

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://xxx:xxxx"));

var balance = web3.eth.getBalance("0xbddf0bf3ac858d7fb8a2bdda55884d61779ba5a9");
console.log(balance.toNumber()); // Only confirmed balance is showed. I want to know unconfirmed balance

更新 1

感謝@niksmac。我嘗試使用 web3.eth.filter。似乎顯示了待處理的交易。但這有點奇怪。我將 0x11fd3ffc4243cac293ee3d349ec906a2786087fe 設置為特定地址,但是結果中沒有顯示該地址。而且我可能必須通過 transactionHash 獲取 ETH 的數量。

過濾器.js

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://xxx:xxx"));

var options = web3.eth.filter({address: "0x11fd3ffc4243cac293ee3d349ec906a2786087fe"});
var filter = web3.eth.filter(options);
var myResults = filter.get(function(error, logs){ console.log(logs); });

結果

[ { address: '0x2a98c5f40bfa3dee83431103c535f6fae9a8ad38',
   topics: 
    [ '0x5a690ecd0cb15c1c1fd6b6f8a32df0d4f56cb41a54fea7e94020f013595de796',
      '0x0000000000000000000000000000000000000000000000000000000000000002',
      '0x0000000000000000000000002e8520ef2806ea0c0c3f02b24dddf0d23e58b5f8',
      '0x0000000000000000000000000000000000000000000000000000000000000000' ],
   data: '0x58425455534400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
   blockNumber: 1620014,
   transactionHash: '0xfc93b988bc65686b7f01f5f522ce13cd80c800674de386a82512d77246721b07',
   transactionIndex: 0,
   blockHash: '0x1e932b4902300dd352e5a0f1a446c92ac1d6373c1551acfc83d4b458171009ee',
   logIndex: 0,
   removed: false },
 { address: '0x2a98c5f40bfa3dee83431103c535f6fae9a8ad38',
   topics: 
    [ '0xa9c6cbc4bd352a6940479f6d802a1001550581858b310d7f68f7bea51218cda6',
      '0x5842545553440000000000000000000000000000000000000000000000000000' ],
   data: '0x0000000000000000000000000000000000000000000000f53bab9c227ae40000',
   blockNumber: 1620014,
   transactionHash: '0xfc93b988bc65686b7f01f5f522ce13cd80c800674de386a82512d77246721b07',
   transactionIndex: 0,
   blockHash: '0x1e932b4902300dd352e5a0f1a446c92ac1d6373c1551acfc83d4b458171009ee',
   logIndex: 1,
   removed: false },
 { address: '0x2e8520ef2806ea0c0c3f02b24dddf0d23e58b5f8',
   topics: 
    [ '0xa609f6bd4ad0b4f419ddad4ac9f0d02c2b9295c5e6891469055cf73c2b568fff',
      '0x0000000000000000000000002e8520ef2806ea0c0c3f02b24dddf0d23e58b5f8' ],
   data: '0x0000000000000000000000000000000000000000000000f3f22f14ecd9910000',
   blockNumber: 1620014,
   transactionHash: '0xfc93b988bc65686b7f01f5f522ce13cd80c800674de386a82512d77246721b07',
   transactionIndex: 0,
   blockHash: '0x1e932b4902300dd352e5a0f1a446c92ac1d6373c1551acfc83d4b458171009ee',
   logIndex: 2,
   removed: false } 
]

我沒有看到任何直接的方法來獲得這個。但是您可以嘗試web3.eth.filter指定帳戶地址。

參數

  1. String|Object- 字元串“latest”或“pending”分別用於觀察最新區塊或待處理交易的變化。或者一個過濾器選項對象,如下所示:
  2. fromBlock:Number|String - 最早區塊的編號(可以給出最新的表示目前正在探勘的最新和待處理的區塊)。
  3. toBlock:Number|String - 最新區塊的編號(latest 可以表示最近和待定的目前正在探勘的區塊)。
  4. address:字元串 - 僅從特定帳戶獲取日誌的地址或地址列表。
  5. topics:字元串數組 - 每個值都必須出現在日誌條目中的數組。順序很重要,如果您想保留主題,請使用 null,例如$$ null, ‘0x00…’ $$.

引用自:https://ethereum.stackexchange.com/questions/25911