Skip to content

Commit

Permalink
add API
Browse files Browse the repository at this point in the history
  • Loading branch information
iTiamo committed May 27, 2018
1 parent 728e9b5 commit a8058ef
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 78 deletions.
12 changes: 12 additions & 0 deletions api/getIncome.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
require_once("../src/coin.php");
require_once("../src/income.php");

$coin = getCoin($_GET["coin"]);
$income = calculateIncome($coin, array(
"hashrate" => $_GET["hashrate"],
"masternodes" => $_GET["masternodes"]
));

echo json_encode($income);
?>
9 changes: 9 additions & 0 deletions api/getNetworkStats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
require_once("../src/coin.php");
require_once("../src/networkStats.php");

$coin = getCoin($_GET["coin"]);
$networkStats = getNetworkStats($coin);

echo json_encode($networkStats);
?>
30 changes: 26 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,34 @@
</form>

<?php
if (!empty($_GET) && !empty($_GET["coin"])) {
require_once("program.php");
program();
if (!empty($_GET) && !empty($_GET["coin"])) {
require_once("src/coin.php");
require_once("src/income.php");
require_once("src/networkStats.php");

$coinTicker = $_GET["coin"];
$coin = getCoin($coinTicker);

$income = calculateIncome($coin, array(
"hashrate" => $_GET["hashrate"],
"masternodes" => $_GET["masternodes"]
));

$networkStats = getNetworkStats($coin);

$totalCoins = number_format($income["proof-of-work"][$coinTicker] + $income["masternode-rewards"][$coinTicker], 8);
$totalCoinsWorth = number_format($income["proof-of-work"]["BTC"] + $income["masternode-rewards"]["BTC"], 8);

echo "<p>You will make an average of <b>{$income["proof-of-work"][$coinTicker]} {$coinTicker}</b> per day by Proof of Work, equal to <b>{$income["proof-of-work"]["BTC"]} BTC</b>.<br>";
echo "You will make an average of <b>{$income["masternode-rewards"][$coinTicker]} {$coinTicker}</b> per day by Masternodes, equal to <b>{$income["masternode-rewards"]["BTC"]} BTC</b>.<br>";
echo "You will make a total of <b>$totalCoins $coinTicker</b> per day, equal to <b>$totalCoinsWorth BTC</b>.</p>";

echo "<p>The average network hashrate is <b>" . round($networkStats["network-hashrate"]/1000000000, 3) . " GHs</b>.<br>";
echo "The average blocktime is <b>" . round($coin->blocktime) . " seconds</b>.<br>";
echo "The current amount of Masternodes is <b>{$networkStats["masternodes"]}</b>.</p>";
}
?>

<p>Tipjar (GIN): GXUQQXBr5i2gKcPaa5SJHqQ9M9G9SgL1X1</p>
</body>
</html>
</html>
63 changes: 0 additions & 63 deletions program.php

This file was deleted.

4 changes: 1 addition & 3 deletions calculator.php → src/calculator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

class calculator
{
function calculatePoWCoinsByDifficulty($hashrate, $difficulty, $blockreward, $timeperiod=86400) {
Expand All @@ -22,5 +21,4 @@ function calculateMasternodeCoins($multiplier, $masternode_count, $blockreward,
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)
}
}

?>
?>
19 changes: 15 additions & 4 deletions coin.php → src/coin.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

require_once 'vendor/autoload.php';
require_once '../vendor/autoload.php';
use JsonRPC\Client;

//credit to fguillot for JsonRPC, find it here: https://github.com/fguillot/JsonRPC

/*
Expand Down Expand Up @@ -80,4 +78,17 @@ private function getPoWReward() {
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
}
}
?>

function getCoin($coinTicker) {
switch($coinTicker) { //gets the coin and instantiates a class to interact with the coin's daemon
case "GIN":
return new coin("GIN", "gincoin", "gincoin", "127.0.0.1", "10112");

case "LUCKY":
return new coin("LUCKY", "luckybit", "luckybit", "127.0.0.1", "10113");

case "PROTON":
return new coin("PROTON", "protoncoin", "protoncoin", "127.0.0.1", "10114");
}
}
?>
48 changes: 48 additions & 0 deletions src/income.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
require_once "calculator.php";
require_once "coin.php";
require_once "ticker.php";

function calculateIncome($coin, $params) {
if ($params["hashrate"] === "") {
$params["hashrate"] = 0;
} else {
$params["hashrate"] *= 1000000;
}

if ($params["masternodes"] === "") {
$params["masternodes"] = 0;
}

$netHashrate = $coin->getNetworkHashPs(101);
$netMasternodes = $coin->getMasternodeCount();
$difficulty = $coin->getDifficulty();

$calculator = new calculator();
$pow_coins = number_format(($calculator->calculatePoWCoinsByNetworkHashPs($params["hashrate"], $netHashrate, $coin->powReward, $coin->blocktime)), 8);
$masternode_coins = number_format(($calculator->calculateMasternodeCoins($params["masternodes"], $netMasternodes, $coin->mnReward, $coin->blocktime)), 8);
$totalCoins = number_format(($pow_coins + $masternode_coins), 8);

$ticker = new ticker();
if($ticker->getCryptoBridgeCoin($coin->coinid)) {
$price = $ticker->getCryptoBridgeCoin($coin->coinid)->last;
} else {
$price = $ticker->getGraviexCoin($coin->coinid)->ticker->last;
}

$pow_coins_worth = number_format(($pow_coins * $price), 8);
$masternode_coins_worth = number_format(($masternode_coins * $price), 8);
$total_coins_worth = number_format(($pow_coins_worth + $masternode_coins_worth), 8);

return array(
"proof-of-work" => array(
$coin->coinid => $pow_coins,
"BTC" => $pow_coins_worth
),
"masternode-rewards" => array(
$coin->coinid => $masternode_coins,
"BTC" => $masternode_coins_worth
)
);
}
?>
13 changes: 13 additions & 0 deletions src/networkStats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
function getNetworkStats($coin) {
$netHashrate = $coin->getNetworkHashPs(101);
$masternodes = $coin->getMasternodeCount();
$difficulty = $coin->getDifficulty();

return array(
"network-hashrate" => $netHashrate,
"masternodes" => $masternodes,
"difficulty" => $difficulty
);
}
?>
6 changes: 2 additions & 4 deletions ticker.php → src/ticker.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

class ticker //class to interact with CryptoBridge ticker API
class ticker //class to interact with exchange API
{
private $cryptobridge = "https://api.crypto-bridge.org/api/v1/ticker";
private $graviex = "https://graviex.net/api/v2/tickers/";
Expand Down Expand Up @@ -29,5 +28,4 @@ function getGraviexCoin($id) {
return json_decode(file_get_contents(($this->graviex . strtolower($id) . "btc.json"), false, stream_context_create($arrContextOptions)));
}
}

?>
?>

0 comments on commit a8058ef

Please sign in to comment.