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.