Skip to content

Commit

Permalink
Calculate blocktime and block rewards dynamically, other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
iTiamo committed Mar 22, 2018
1 parent e2500b4 commit ed57177
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 92 deletions.
24 changes: 8 additions & 16 deletions calculator.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
<?php
class calculator
{
function averageArray($array) {
$sum=0;
foreach ($array as $value) {
$sum += $value;
}
return $sum/count($array);
}

function calculatePoWCoinsByDifficulty($hashrate, $difficulty) {
{
function calculatePoWCoinsByDifficulty($hashrate, $difficulty, $blockreward, $timeperiod=86400) {
if ($hashrate == 0) {
return 0;
} else {
return (86400/($difficulty*2**32/$hashrate))*10; //based off my generic formula coins_day=(seconds_day/(d*2^32/hashrate))*block_reward, where 2^32 is the average number of shares needed to find a block at a difficulty of 1
return ($timeperiod/($difficulty*2**32/$hashrate))*$blockreward; //based off my generic formula coins_day=(seconds_day/(d*2^32/hashrate))*block_reward, where 2^32 is the average number of shares needed to find a block at a difficulty of 1
}
}

function calculatePoWCoinsByNetworkHashPs($hashrate, $networkHashPs) {
function calculatePoWCoinsByNetworkHashPs($hashrate, $networkHashPs, $blockreward, $blocktime, $timeperiod=86400) {
if ($hashrate == 0) {
return 0;
} else {
return ($hashrate/$networkHashPs)*((86400/120)*10); //based off my generic formula coins_day=(hashrate/nethash)*((seconds_day/block_time)*block_reward)
return ($hashrate/$networkHashPs)*(($timeperiod/$blocktime)*$blockreward); //based off my generic formula coins_day=(hashrate/nethash)*((seconds_day/block_time)*block_reward
}
}

function calculateMasternodeCoins($multiplier, $masternode_count) {
return $multiplier * (((86400/120)*10)/$masternode_count); //based off my generic formula for one masternode coins_day = (((seconds_day/block_time)*block_rewards)/masternode_count)
function calculateMasternodeCoins($multiplier, $masternode_count, $blockreward, $blocktime, $timeperiod=86400) {
return $multiplier * ((($timeperiod/$blocktime)*$blockreward)/$masternode_count); //based off my generic formula for one masternode coins_day = (((seconds_day/block_time)*block_rewards)/masternode_count)
}
}
?>
?>
23 changes: 0 additions & 23 deletions explorer.php

This file was deleted.

58 changes: 22 additions & 36 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<!DOCTYPE html>

<?php
require_once("explorer.php");
require_once("calculator.php");
require_once("rpcclient.php");
require_once("ticker.php");
?>
<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8"/>
Expand All @@ -17,8 +17,8 @@
<h3>A <a href="https://gincoin.io/">GINcoin</a> income calculator made by @Tiamo#1675 on GINcoin Discord, written in PHP.</h3>

<p>Insert your hashrate, and amount of masternodes owned and click "submit"; the program will output an estimated amount of daily coins earned.
Decimal values are supported, for example if you own 25% of a shared Masternode, input 0.25 under "Amount of Masternodes".
Both fields are optional.</p>
Decimal values are supported, for example if you own 25% of a shared Masternode, input 0.25 under "Amount of Masternodes". Both fields are optional.
This calculator assumes the network hash over the past 1 hour, and dynamically calculates blocktime based on the last 101 blocks.</p>

<p>The <a href="https://github.com/iTiamo/GINcoin-Income-Calculator">source code is available on Github</a>.</p>

Expand All @@ -31,7 +31,8 @@
</form>

<?php
if ($_GET) {
if ($_GET)
{
if ($_GET["hashrate"]) {
$hashrate = $_GET["hashrate"] * 1000000;
} else {
Expand All @@ -43,47 +44,32 @@
$masternode_multiplier = 0;
}

//$explorer = new explorer();
$rpcclient = new rpcclient(user, password, ip, port);
$calculator = new calculator();
$ticker = new ticker();
$gincoin = new coin("gincoin", "gincoin", "127.0.0.1", "10112"); //instantiates a class to interact with gincoind, assumes you have set up rpc with username gincoin and password gincoin on port 10112.
$networkHashPs = $gincoin->getNetworkHashPs(30);
$masternode_count = $gincoin->getMasternodeCount();
$difficulty = $gincoin->getDifficulty();

/* OLD WAY OF CALCULATING COINS/DAY, SLOW AND LOTS OF CALLS TO GINCOIN BLOCK EXPLORER
* $blockHeight = $explorer->getBlockHeight(); //get current blockheight
* for ($i = $blockHeight; $i > $blockHeight - 30; $i--) { //get the difficulties of the current block, and the previous 29 blocks
* $block = $explorer->getBlock($explorer->getBlockHash($i));
* $difficulty = $explorer->getDifficulty($block);
* $difficulties[$i] = $difficulty;
* }
* $avg_difficulty = $calculator->averageArray($difficulties); //take the average of the current block, and the previous 29 blocks */

$difficulty = round($rpcclient->getDifficulty(), 0);
//NEW WAY TO CALCULATE COINS/DAY WITH GINCOIND getnetworkhashps
$networkHashPs = $rpcclient->getNetworkHashPs(30);
$masternode_count = $rpcclient->getMasternodeCount();

$pow_coins = round(($calculator->calculatePoWCoinsByNetworkHashPs($hashrate, $networkHashPs)), 8);
$masternode_coins = round(($calculator->calculateMasternodeCoins($masternode_multiplier, $masternode_count)), 8);
$totalCoins = round(($pow_coins + $masternode_coins), 8);
$calculator = new calculator();
$pow_coins = round(($calculator->calculatePoWCoinsByNetworkHashPs($hashrate, $networkHashPs, $gincoin->powReward, $gincoin->blocktime)), 2);
$masternode_coins = round(($calculator->calculateMasternodeCoins($masternode_multiplier, $masternode_count, $gincoin->mnReward, $gincoin->blocktime)), 2);
$totalCoins = round(($pow_coins + $masternode_coins), 2);

$ticker = new ticker();
$GINprice = $ticker->getGIN()->last;
$pow_coins_worth = round(($pow_coins * $GINprice), 8);
$masternode_coins_worth = round(($masternode_coins * $GINprice), 8);
$total_coins_worth = round(($pow_coins_worth + $masternode_coins_worth), 8);

echo "<p>You will make an average of <b>$pow_coins</b> GIN per day by Proof of Work, equal to <b>$pow_coins_worth BTC</b>.<br>";
echo "You will make an average of <b>$masternode_coins</b> GIN per day by Masternodes, equal to <b>$masternode_coins_worth BTC</b>.<br>";
echo "You will make a total of <b>$totalCoins</b> GIN per day, equal to <b>$total_coins_worth BTC</b>.</p>";
echo "<p>You will make an average of <b>$pow_coins GIN</b> per day by Proof of Work, equal to <b>$pow_coins_worth BTC</b>.<br>";
echo "You will make an average of <b>$masternode_coins GIN</b> per day by Masternodes, equal to <b>$masternode_coins_worth BTC</b>.<br>";
echo "You will make a total of <b>$totalCoins GIN</b> per day, equal to <b>$total_coins_worth BTC</b>.</p>";

echo "<p>The current difficulty is $difficulty.<br>";
echo "The current amount of Masternodes is $masternode_count.</p>";
echo "<p>The calculator assumed a nethash of <b>" . round($networkHashPs/1000000000, 3) . " GHs</b> over the past hour.<br>";
echo "The average blocktime over the past 101 blocks was <b>" . round($gincoin->blocktime) . " seconds</b>.<br>";
echo "The current amount of Masternodes is <b>$masternode_count</b>.</p>";
}
?>

<p>Generic formula for calculating Proof-of-Work coins by difficulty: coins_day=(seconds_day/(d*2^32/hashrate))*block_reward, where 2^32 is the average number of shares needed to find a block at a difficulty of 1<br>
Generic formula for calculating Proof-of-Work coins by nethash: coins_day=(hashrate/nethash)*((seconds_day/block_time)*block_reward)<br>
Generic formula for calculating Masternode coins: coins_day=(((seconds_day/block_time)*block_rewards)/masternode_count)</p>

<p>Tipjar (GIN): GXUQQXBr5i2gKcPaa5SJHqQ9M9G9SgL1X1</p>
</body>
</html>
</html>
63 changes: 55 additions & 8 deletions rpcclient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,69 @@
* https://en.bitcoin.it/wiki/Bitcoind
*/

class rpcclient
class coin //class to communicate with a coin's daemon
{
function __construct($rpcuser, $rpcpassword, $url, $port) { //url or ip address
$this->gincoin_client = new RPC\Client("http://".$rpcuser.":".$rpcpassword."@".$url.":".$port);
private $client;
public $blocktime;
public $powReward;
public $mnReward;

function __construct($rpcuser, $rpcpassword, $ip, $port) { //url or ip address
$this->client = new RPC\Client("http://".$rpcuser.":".$rpcpassword."@".$ip.":".$port);
$this->blocktime = $this->getBlockTime();
$this->mnReward = $this->getMNReward();
$this->powReward = $this->getPoWReward();
}

function getMasternodeCount() {
return $this->gincoin_client->masternode("count");
return $this->client->masternode("count", "enabled");
}

function getNetworkHashPs($blocks) {
return $this->gincoin_client->getnetworkhashps($blocks);
function getNetworkHashPs($blocks) { //returns the network's hashrate over the past n blocks
return $this->client->getnetworkhashps($blocks);
}

function getDifficulty() {
return $this->gincoin_client->getdifficulty();
return $this->client->getdifficulty();
}

function getBlock($hash) {
return $this->client->getblock($hash);
}

function getBestBlock() {
return $this->client->getblock($this->client->getbestblockhash());
}

function getBlockCount() { //returns the latest block index
return $this->client->getblockcount();
}

function getBlockHash($index) {
return $this->client->getblockhash($index);
}

function getRawTransaction($hash) {
return $this->client->getrawtransaction($hash, 1);
}

private function getBlockTime() {
$bestblock = $this->getBestBlock();
$block2 = $this->getblock($this->getBlockHash($this->getBlockCount() - 100));
$timedelta = $bestblock["mediantime"] - $block2["mediantime"]; //the time in seconds that has passed between the best block and the block 100 blocks before the best block
return $timedelta / 101; //returns the average blocktime over the past 101 blocks
}

private function getMNReward() {
$bestblock = $this->getBestBlock();
$coinbase = $this->getRawTransaction($bestblock["tx"][0]); //coinbase transaction of the best block
return $coinbase["vout"][0]["value"]; //the 1st output in a coinbase transaction is always the MN reward, here we return the value of that output
}

private function getPoWReward() {
$bestblock = $this->getBestBlock();
$coinbase = $this->getRawTransaction($bestblock["tx"][0]); //coinbase transaction of the best block
return $coinbase["vout"][1]["value"]; //the 2nd output in a coinbase transaction is always the PoW reward, here we return the value of that output
}
}
?>
?>
20 changes: 11 additions & 9 deletions ticker.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<?php
class ticker
class ticker //class to interact with CryptoBridge ticker API
{
private $ticker = "https://api.crypto-bridge.org/api/v1/ticker";
private $tickerurl = "https://api.crypto-bridge.org/api/v1/ticker";
private $coins;

function __construct() {
$this->coins = json_decode(file_get_contents($this->tickerurl));
}

function getGIN() {
$arr = json_decode(file_get_contents($this->ticker));

for($i = 0; $i < count($arr); $i++) {
if($arr[$i]->id == "GIN_BTC") {
return $arr[$i];
for($i = 0; $i <= count($this->coins); $i++) {
if($this->coins[$i]->id == "GIN_BTC") {
return $this->coins[$i];
}
}
return null;
}
}
?>
?>

0 comments on commit ed57177

Please sign in to comment.