Current versions of Abe support the getdifficulty API call. For instance, http://yacexplorer.tk/chain/Yacoin/q/getdifficulty returns the current Yacoin difficulty. The problem with Litecoin is that both explorer.litecoin.net and litecoinscout.com are running outdated versions of Abe.
For these older versions of Abe, difficulty at the time a block is solved is available; it should usually be close enough to the current difficulty. Here's an alternate approach I implemented to get Litecoin difficulty:
- get the blockchain height (/chain/name/q/getblockcount)
- get block info (/search?q=blocknum)
- get difficulty
Step 2 returns HTML which you'll have to scrape. Here's some C# I knocked together:
private double GetDifficultyAbeAlt(string url_prefix, string chain_name)
{
WebClient wc = new WebClient();
int blockcount = Convert.ToInt32(wc.DownloadString(url_prefix + "/chain/" + chain_name + "/q/getblockcount"));
string blockinfo = wc.DownloadString(url_prefix + "/search?q=" + blockcount.ToString());
double difficulty = 0;
foreach (string line in blockinfo.Split(new char[] { '\n' }))
if (line.Contains("Difficulty") && !line.Contains("Cumulative"))
difficulty = Convert.ToDouble(line.Split(new char[] { ' ' })[1]);
return difficulty;
}
For the block reward, this is the approach I took that should work with any version of Abe:
- get the blockchain height (/chain/name/q/getblockcount)
- get block info (/search?q=blocknum)
- get the value of the generation transaction; subtract from it the fees paid by the other transactions in the block
As above, step 2 returns HTML which you'll have to scrape. Here's some C# I knocked together:
private decimal GetRewardAbe(string url_prefix, string chain_name)
{
WebClient wc = new WebClient();
int blockcount = Convert.ToInt32(wc.DownloadString(url_prefix + "/chain/" + chain_name + "/q/getblockcount"));
string blockinfo = wc.DownloadString(url_prefix + "/search?q=" + blockcount.ToString());
int tx_index = 0;
decimal reward = 0;
foreach (string line in blockinfo.Split(new char[] { '\n' }))
if (line.Contains("<tr>") && !line.Contains("<table>"))
{
string[] fields = line.Split(new string[] { "<td>", "</td>", "<tr>", "</tr>" }, StringSplitOptions.RemoveEmptyEntries);
if (tx_index == 0)
{
reward = Convert.ToDecimal(fields[3].Split(new char[] { ' ' })[1]);
if (fields[3].Contains("+"))
break;
}
else
reward -= Convert.ToDecimal(fields[1]);
tx_index++;
}
return reward * (decimal)100000000;
}
The value returned is in satoshis, consistent with what blockexplorer.com or blockchain.info return for Bitcoin.
Thank but I don't understand what parameter I should pass to get current difficulty I tried this http://explorer.litecoin.net/q/nethash
– working4coins – 2013-04-05T05:25:35.170Both http://explorer.litecoin.net/ and http://litecoinscout.com/ rely on the Abe API. To get the current difficulty query this URL: http://explorer.litecoin.net/chain/Litecoin/q/nethash/1/-1 The 'CHAIN' is 'Litecoin', the 'INTERVAL' is '1'(every block), and the START is '-1'(last block). Your value(currently about 150) is between the second and third last comma. You will have to find a way to extract that in your calculator. There does not seem to be a http://explorer.litecoin.net/chain/Litecoin/q/difficulty
– ix5 – 2013-04-05T07:31:26.983It will be a pain to parse that !!! why are not they using a JSON API ??? or YAML ? – working4coins – 2013-04-05T09:23:09.870
@working4coins I have no idea, but you can contact the creators of the explorer sites. It should be easy to implement a ´difficulty´ API call. Otherwise you could run abe by yourself.
– ix5 – 2013-04-05T11:02:03.963I'm still looking for a way to know block reward (now it's 25 LTC) It was 50 LTC previously – working4coins – 2013-04-06T20:21:00.813
The block reward will not change for a while. The current block count is around 330,000, and the reward is supposed to halve at 840,00. You should be good with setting 25 LTC as a static value for now. – ix5 – 2013-04-07T16:25:17.230