The Stratumsphere API returns the hashrate in different forms whether it be the personal, rented, total, average or rejected hashrate on rentals, schedulers or workers. Given a few exceptions almost all endpoints in the Stratumsphere API have at-least one attribute that specifies hashrate.

Whether it be a GET or a POST call, the API expects the hashrate to be in hashes/second.
1 KH/s = 1024 H/s
The following code is written in javascript but the same login can be applied to any language. Following functions can be used to convert raw hashrate coming form the API to be displayed on the UI or asking the user for a hashrate with a unit and converting it to API readable raw hashrate:

Convert Raw hashrate to readable with unit Prefix (MH, GH, TH etc)

This function is to display the raw hashrate from the API data onto a UI
formattedHashrate (rawString) {
if(!rawString){
return '-';
}
if(typeof rawString != 'number')
{
return '-';
}
var hashrate = 0;
hashrate = rawString;
var i = 0;
var byteUnits = [' H', ' KH', ' MH', ' GH', ' TH', ' PH' ];
do {
hashrate = hashrate / 1024;
i++;
} while (hashrate > 1024);
return i > 0 ? hashrate.toFixed(2) + byteUnits[i]: hashrate.toFixed(0) + byteUnits[i];
}

Convert hashrate with unit prefix to raw hashrate for the API

This function is to convert user input with unit prefix (MH, GH, TH etc) to API compatible hashrate
switch(Unit){
case "TH":
value = threshold * 1024 * 1024 * 1024 * 1024;
break;
case "GH":
value = threshold * 1024 * 1024 * 1024;
break;
case "MH":
value = threshold * 1024 * 1024;
break;
case "KH":
value = threshold * 1024;
break;
case "H":
value = threshold;
break;
default:
value = threshold;
break;
}
Was this article helpful?
Thank you!