3

I use web3.eth.getBalance to get balance of a specific address. I want to get how amount of unconfirmed ETH the address has.

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

Update 1

Thanks to @niksmac. I tried to use web3.eth.filter. It seems that pending txes are showed. But it is a little bit strange. I set 0x11fd3ffc4243cac293ee3d349ec906a2786087fe as a specific address, however the address was not showed in the result. And I may have to get the amount of ETH by the transactionHash.

filter.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); });

result

[ { 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 } 
]
2
  • 1
    Post your finding/solution as an answer rather than updating the question.
    – niksmac
    Commented Sep 6, 2017 at 10:01
  • You are right but I'm still looking into how to get the amount of balance from the result of filter. I will post it as an answer it if I can do it.
    – zono
    Commented Sep 6, 2017 at 10:15

3 Answers 3

3

I dont see any direct way to get this. But you can try web3.eth.filter with specifying the account address.

Parameters

  1. String|Object - The string "latest" or "pending" to watch for changes in the latest block or pending transactions respectively. Or a filter options object as follows:
  2. fromBlock: Number|String - The number of the earliest block (latest may be given to mean the most recent and pending currently mining, block).
  3. toBlock: Number|String - The number of the latest block (latest may be given to mean the most recent and pending currently mining, block).
  4. address: String - An address or a list of addresses to only get logs from particular account(s).
  5. topics: Array of Strings - An array of values which must each appear in the log entries. The order is important, if you want to leave topics use null, e.g. [null, '0x00...'].
2
  • Thanks. I'm trying it right now. I posted the result of filter.
    – zono
    Commented Sep 6, 2017 at 6:05
  • This would only return events. It would not work if the account has an incoming ETH transfer with no accompanying events. I posted a simple and effective answer below — I guess this wasn't available at the time the question was posted.
    – FRAGA
    Commented Sep 29, 2021 at 16:47
0

I'm not sure this is ideal or not but the following way might be possible. This way can be used not only ETH transaction but also token txes if I can parse the input data.

(1) run watch daemon and save the txes.

(2) clients are allowed to access the saved data.

filter-daemon.js

web3.eth.filter("pending").watch(
    function(error,result){
        if (!error) {
            var transaction = web3.eth.getTransaction(result);
            console.log(transaction);
            // save the transaction somewhere (e.g. DB)
        }
    }
)

output

{ blockHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
  blockNumber: null,
  from: '0xa3ac96fbe4b0dce5f6f89a715ca00934d68f6c37',
  gas: 4712388,
  gasPrice: { [String: '100000000000'] s: 1, e: 11, c: [ 100000000000 ] },
  hash: '0x1ba953d7a009af9b65763f43ee4e14e11ed7ba1051c4a75bd27faf706cb5c0f6',
  input: '0x',
  nonce: 83,
  to: '0x4f7d630814447f1b4c0719bb7a259c2bfd63f174',
  transactionIndex: 0,
  value: { [String: '410000000000000000'] s: 1, e: 17, c: [ 4100 ] },
  v: '0x29',
  r: '0x89d0370711ab688b0a46c1bbd512ce523f2c239186a3b84c87ea277dd73489ee',
  s: '0x2dfe2dc4b0719883f683199c35690189d1aebdb33eb394ba7f8392069fe3bd00' }

I hope there are another easier ways.

0

You can query the state from the "pending" block.

{
"id": 1,
"jsonrpc": 2.0,
"method": "eth_getBalance",
"params": ["0x<address>", "pending"]
}

https://eth.wiki/json-rpc/API#eth_getBalance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.