3

How can I check confirmations number for some bitcoin address programmatically? (I have eCommerce shop where each order has separate address). I want to use blockchain.info API.

Here is an example of the API call, but I can't figure out what numbers should I check in API response:

https://blockchain.info/rawaddr/1JbBiCzQqzE2dW9TCzA6w7N27jVDXxwreD

2 Answers 2

4

Confirmations for an address aren't really a thing. Addresses are not confirmed; transactions are. A single address is typically used in multiple transactions: one where the address is used as an output, and one where the first transaction was used as an input. Although it's not a best practice, addresses can even be re-used multiple times.

It looks like this particular address has been used in two different transactions. Once as an output in 00c83c7677d414053488d0b51067feee157a5b82d5acec5bfe8e9c540c11c624, and once as in input in 378f829160cb9c99cc7a8fb1152472b80f0e1ea9c9cc24118dbccc2da72dedcf.

Each of these two transactions returned in the API response has a block_height value (489353 and 489355). That is the block at which the transaction was included. To find how many confirmations each transaction has, subtract the block_height by the height of the most recently confirmed block, and add 1. So if the current block is 489355, one transaction has 1 confirmation, and the other has 3.

1

If you are using php, then try this :

function response ($result) {   
      header("Content-Type:application/json");
      $response['confirmation'] = $result;
      $json_response = json_encode($response);
      echo $json_response;
  }


$tx_hash  = '5e910a5b61e3ccdb7...'; // try to get a new tx hash from blockchain.info/.com

$bcinfo = json_decode(file_get_contents("https://blockchain.info/rawtx/".$tx_hash), true);
$latest = json_decode(file_get_contents("https://blockchain.info/latestblock"), true);

if(isset($bcinfo['block_height'])) {

   $block_height =  $bcinfo['block_height'];
   $height       =  $latest['height'];

   response($height - $block_height + 1);

}else{

   response(0); // not yet has any confirmations

}

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.