10

How do I get the block confirmations for a specific transaction using the JSON RPC?

This is information that would come inside the transaction JSON in bitcoin but it doesn't seem to come in ethereum.

What am I missing?

11

When you send a transaction, you will receive back a transaction hash.

Use the command getTransactionByHash({transaction hash}) to retrieve the transaction details. Your blockNumber should be non-null if the transaction has been mined and included into a block.

The call is documented in https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_gettransactionbyhash with the following example:

// Request
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getTransactionByHash","params":["0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"],"id":1}'

// Result
{
  "id":1,
  "jsonrpc":"2.0",
  "result": {
    "hash":"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b",
    "nonce":"0x",
    "blockHash": "0xbeab0aa2411b7ab17f30a99d3cb9c6ef2fc5426d6ad6fd9e2a26a6aed1d1055b",
    "blockNumber": "0x15df", // 5599
    "transactionIndex":  "0x1", // 1
    "from":"0x407d73d8a49eeb85d32cf465507dd71d507100c1",
    "to":"0x85h43d8a49eeb85d32cf465507dd71d507100c1",
    "value":"0x7f110" // 520464
    "gas": "0x7f110" // 520464
    "gasPrice":"0x09184e72a000",
    "input":"0x603880600c6000396000f300603880600c6000396000f3603880600c6000396000f360",
  }
}

Then call eth_blockNumber (https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_blocknumber) to get the current block height. Your number of confirmations is the eth_blockNumber result minus the eth_getTransaction blockNumber result.

  • Ok, but how does that translate to confirmations? How is the blocknumber related to the confirmations of the transaction? – Nico Apr 11 '16 at 17:17
  • I get that, but how does that relate to the confirmations? @BokkyPooBah – Nico Apr 11 '16 at 17:20
  • See eth_blockNumber and difference calc in updated answer – The Officious BokkyPooBah Apr 11 '16 at 17:24
  • So, when transaction is first included in the block, does it has one confirmation? Or confirmations are counted from the next block only? – Slava Fomin II Dec 9 '17 at 12:13
1

The answer given by BokkyPooBah is perfect and as for how does that relate to the confirmations, I raised the same question here: How does (current block number - block number of a given transaction) gives the confirmation count?.

You might also refer to the comments for the question: Ethereum network transaction confirmation coverage. The idea is that, if your transaction was included in block number 100 (just assume) and the current block number is 160, it means 60 random miners accepted the block 100 as valid and added to that chain and so you can say that you have 60 confirmations. This is all about consensus.

  • So, when transaction is first included in the block, does it has one confirmation? Or confirmations are counted from the next block? – Slava Fomin II Dec 9 '17 at 12:13
  • @SlavaFominII When a transaction is first included in a block (say X), that cannot be counted as first confirmation. For all you know, I could be trying to break the chain. If another miner place a block over X, then that is a confirmation that X could be correct and you can say that the transaction has one confirmation. – devsubhro Dec 12 '17 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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