User avatar
coinableS
Nickel Bitcoiner
Nickel Bitcoiner
Posts: 65
Joined: Wed Sep 30, 2015 6:06 am

Donate BTC of your choice to 1J9ikqFuwrzPbczsDkquA9uVYeq6dEehsj

Contact: Website Twitter

Let's Code! Using API to calculate the amount of time left until the block reward halving

Thu Oct 15, 2015 6:24 am

This tutorial will show you how you can get the time left until the upcoming block halving. Even if you don't know how to code, this step-by-step tutorial will show you how ;)

1) Goto PHPfiddle.org so you can execute PHP code without having a server or downloading anything. Unlike HTML, with PHP you can't just run it on your computer.

2) Click on the "Code Space" tab on the top left to get to the editor portion.

3) The block rewards half after every 210,000 blocks. So in order to determine the time we'll first need to know which block we are on currently. We can get this with blockchain.info's API. The useful stats API request (https://blockchain.info/stats?format=json) will provide us with all the information we need.

4) In the Code Space section at PHPfiddle, type the following between the php tags <?php ?>
$url = "https://blockchain.info/stats?format=json";
5) The $ defines a variable, and we set it equal to blockchain's stats URL.
6) This URL is in JSON format, we are going to tell PHP to get the information in JSON format. Add the following below your first line
$json = json_decode(file_get_contents($url), true);
7) Now we need to tell PHP which information from the JSON output that we want. JSON puts all the results into arrays, and we can tell PHP which data we want using straight brakets. ][ The current block height is stored under "n_blocks_total"
$currentBlock = $json['n_blocks_total'];
8) You can test this to make sure it works by echoing it out. Let's combine it all and test if it provides the current block height.
$url = "https://blockchain.info/stats?format=json";
$json = json_decode(file_get_contents($url), true);
$currentBlock = $json['n_blocks_total'];
echo $currentBlock;
9) Now that we have the current block height we can start the calculations. The next block halving will happen at block height 420,000. So we can figure out how many blocks are left until the halving by subtracting.
$blocksLeft = 420000 - $currentBlock;
10) Another useful figure with blockchain's stats request is "minutes_between_blocks". This tells us how long it takes to solve a block, on average. A few more simple calculations can tell us how many more minutes, hours, days etc until the block halving.
$minutesBetween = $json['minutes_between_blocks'];
$minutesLeft = $blocksLeft * $minutesBetween; 
$hoursLeft = $minutesLeft / 60;
$daysLeft = $hoursLeft / 24;
11) And that's it! From here you should be able to see how to create your own calculations! Put it all together
$url = "https://blockchain.info/stats?format=json";
$json = json_decode(file_get_contents($url), true);
$currentBlock = $json['n_blocks_total'];

$blocksLeft = 420000 - $currentBlock;

$minutesBetween = $json['minutes_between_blocks'];
$minutesLeft = $blocksLeft * $minutesBetween; 
$hoursLeft = $minutesLeft / 60;
$daysLeft = $hoursLeft / 24;

echo "Blocks Remaining Until Halving: ".number_format($blocksLeft);
echo "<br>Approx Minutes Until Halving: ".number_format($minutesLeft);
echo "<br>Approx Hours Until Halving: ".number_format($hoursLeft);
echo "<br>Approx Days Until Halving: ".number_format($daysLeft);
http://phpfiddle.org/lite/code/exjg-sb08

User avatar
LiteCoinGuy
Gold Bitcoiner
Gold Bitcoiner
Posts: 2505
Joined: Mon Sep 21, 2015 9:00 am

Donate BTC of your choice to 1Dbo5TtxG9cWoyw49GM8vbD7HgQhr1KVi6

Re: Let's Code! Using API to calculate the amount of time left until the block reward halving

Sun Oct 18, 2015 10:40 am

i cant code but iam always impressed by people who can do that. thanks for the little introduction :)
********************************************
More informations about Bitcoin and scaling BTC on

bitcoin.org/en/

https://bitcoincore.org/en/2015/12/23/c ... reases-faq

&
reddit.com/r/Bitcoin/

BScotton
Posts: 2
Joined: Thu Oct 15, 2015 7:54 pm

Re: Let's Code! Using API to calculate the amount of time left until the block reward halving

Thu Oct 29, 2015 3:29 pm

Hello I have been researching Bitcoin in general for about a couple of months now. I would like to learn more about cryptography, especially in reference to Bitcoin. Does anyone have suggestions on where to start with the basics of learning the code behind Bitcoin or the actual mining efforts done? Any help is greatly appreciated :)

User avatar
coinableS
Nickel Bitcoiner
Nickel Bitcoiner
Posts: 65
Joined: Wed Sep 30, 2015 6:06 am

Donate BTC of your choice to 1J9ikqFuwrzPbczsDkquA9uVYeq6dEehsj

Contact: Website Twitter

Re: Let's Code! Using API to calculate the amount of time left until the block reward halving

Sat Oct 31, 2015 5:09 am

Hello I have been researching Bitcoin in general for about a couple of months now. I would like to learn more about cryptography, especially in reference to Bitcoin. Does anyone have suggestions on where to start with the basics of learning the code behind Bitcoin or the actual mining efforts done? Any help is greatly appreciated :)
Are you familiar with coding in C++?

Bitcoin is open source, so you can look at the C++ code behind bitcoin on Github https://github.com/bitcoin/bitcoin/tree/master/src

Mastering Bitcoin by Andreas Antonopolus goes over some of the code that could be a good starting point http://shop.oreilly.com/product/0636920032281.do

Chris
Bronze Bitcoiner
Bronze Bitcoiner
Posts: 258
Joined: Tue Sep 22, 2015 10:57 am

Donate BTC of your choice to 16ByPxM5KqHUigBG1N3jm7cRVbFntH4yA9

Contact: Twitter

Re: Let's Code! Using API to calculate the amount of time left until the block reward halving

Sat Oct 31, 2015 7:11 pm

In python

Code: Select all

import requests import urllib3 urllib3.disable_warnings() bitcoin = 'https://blockchain.info/stats?format=json' r = requests.get(bitcoin, headers={'Accept': 'application/json'}) currentBlock = r.json()['n_blocks_total'] blocksLeft = 420000 - currentBlock minutesBetween = r.json()['minutes_between_blocks'] minutesLeft = blocksLeft * minutesBetween hoursLeft = minutesLeft / 60 daysLeft = hoursLeft / 24 print "Blocks Remaining Until Halving:%s" %blocksLeft print "Approx Minutes Until Halving:%s" %minutesLeft print "Approx Hours Until Halving:%s" % hoursLeft print "Approx Days Until Halving:%s" %daysLeft
Image pgp
https://wallet.bitcoin.com [Choose your wallet]
https://buy.bitcoin.com [Buy bitcoin]


Return to “Beginners & Help”

Who is online

Users browsing this forum: No registered users and 3 guests