Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mculp/chunky-mpos
Browse files Browse the repository at this point in the history
  • Loading branch information
mculp committed Jul 18, 2014
2 parents c198407 + e74d04d commit 6549909
Show file tree
Hide file tree
Showing 25 changed files with 581 additions and 111 deletions.
14 changes: 14 additions & 0 deletions cronjobs/change_coins.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

if [ -a /etc/haproxy/haproxy.$1$2$3.cfg ]
then
/etc/haproxy/change_haproxy.sh $1$2$3
echo "multi-scrypt,$1,0" > /chunky/data/multiport_coin.txt
echo "$1,$2,$3" > /mpos/public/multiport_coins.txt

echo "switched to $1, $2, $3"
else
echo "/etc/haproxy/haproxy.$1$2$3.cfg does not exist"
fi


131 changes: 102 additions & 29 deletions cronjobs/convertible_payout.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,49 @@
exit(0);
}

$db_names = array('WC' => 'wc', 'SUM' => 'summer');
$end_coin_exchange_rates = array('WC' => null, 'SUM' => null);
$process = true;
$script_settings_file = "/mpos/cronjobs/settings.json";
$script_settings = json_decode(file_get_contents($script_settings_file));

$process = $script_settings->process;
$payout_mode = $script_settings->payout_mode;

if ($process) {
echo "WE ARE RUNNING IN PAYOUT MODE." . PHP_EOL;
} else if ($payout_mode) {
echo "WE ARE RUNNING IN SEND TO EXCHANGE MODE." . PHP_EOL;
} else {
echo "WE ARE RUNNING IN STATS MODE." . PHP_EOL;
}

$db_names = array('WC' => 'wc', 'SUM' => 'summer', 'BNS' => 'bonus', 'UVC' => 'uni', 'HYPER' => 'hyper');
$end_coin_exchange_rates = array('WC' => null, 'SUM' => null, 'BNS' => null, 'UVC' => null, 'HYPER' => null);

//$from_coin_rate = array('TRC' => 0.00007, 'RZR' => '0.001', 'CRYPT' => '0.000616', 'POT' => 0.00001675, 'MUN' => 0.00000049, 'LGC' => 0.00000700, 'KARM' => 0.000000027, 'RDD' => 0.00000005);
//$end_coin_rate = array('WC' => 0.00000102, 'SUM' => 0.000008, 'BNS' => 0.00000003, 'UVC' => 0.00000151, 'HYPER' => 0.00002100);

require_once('prices.inc.php');

echo "retrieved latest prices." . PHP_EOL;

$tax_rate = 0.02;

$original_coin = $currency; // let's say POT

$from_coin_exchange_rate = json_decode(file_get_contents("http://chunkypools.com/api/coin/exchange_rates/$original_coin"));
$end_coin_exchange_rate = null;
if ($payout_mode) {
echo 'loading exchange addresses.' . PHP_EOL;
require_once('exchange_payout.php');
}

if (!isset($from_coin_rate)) {
$from_coin_exchange_rate = json_decode(file_get_contents("http://chunkypools.com/api/coin/exchange_rates/$original_coin"));

// check that we have exchange rates, or die
if (!is_object($from_coin_exchange_rate) || !($from_coin_exchange_rate->exchange_rate > 0)) {
$log->logInfo("Error retrieving $original_coin exchange rates");
$monitoring->endCronjob($cron_name, 'E0083', 0, true, false);
}

// check that we have exchange rates, or die
if (!is_object($from_coin_exchange_rate) || !($from_coin_exchange_rate->exchange_rate > 0)) {
$log->logInfo("Error retrieving $original_coin exchange rates");
$monitoring->endCronjob($cron_name, 'E0083', 0, true, false);
$from_coin_rate = $from_coin_exchange_rate->exchange_rate;
}

$convertible_transactions = $transaction->getConvertibleQueue();
Expand All @@ -35,45 +65,55 @@
$monitoring->endCronjob($cron_name, 'E0085', 0, true, false);
}

$total_original = 0;
$total_btc = 0;
$total_end = 0;
$total_original = array('WC' => 0, 'SUM' => 0, 'BNS' => 0, 'UVC' => 0, 'HYPER' => 0);
$total_btc = array('WC' => 0, 'SUM' => 0, 'BNS' => 0, 'UVC' => 0, 'HYPER' => 0);
$total_end = array('WC' => 0, 'SUM' => 0, 'BNS' => 0, 'UVC' => 0, 'HYPER' => 0);

foreach ($convertible_transactions as $convertible_transaction) {
$account_id = $convertible_transaction['account_id'];
$amount_to_exchange = $convertible_transaction['amount']; // let's say 20000 (in POT)
$coin_to_credit = $convertible_transaction['convertible']; // let's say WC
$end_coin_db_name = $db_names[$coin_to_credit];

if (!isset($end_coin_exchange_rates[$coin_to_credit])) {
$json_data = file_get_contents("http://chunkypools.com/api/coin/exchange_rates/$coin_to_credit");
$end_coin_exchange_rate[$coin_to_credit] = json_decode($json_data);
echo "paying account id: $account_id $amount_to_exchange $original_coin in $coin_to_credit" . PHP_EOL;
if (!isset($coin_to_credit) || empty($coin_to_credit)) {
$log->logDebug("skipping $account_id because \$coin_to_credit is empty");
continue;
}

// check that we have good exchange rates, or die
if (!is_object($end_coin_exchange_rate[$coin_to_credit]) || !($end_coin_exchange_rate[$coin_to_credit]->exchange_rate > 0)) {
$log->logInfo("Error retrieving $coin_to_credit exchange rates");
$monitoring->endCronjob($cron_name, 'E0084', 0, true, false);
$end_coin_db_name = $db_names[$coin_to_credit];
if (!isset($end_coin_rate[$coin_to_credit])) {
if (!isset($end_coin_exchange_rates[$coin_to_credit])) {
$json_data = file_get_contents("http://chunkypools.com/api/coin/exchange_rates/$coin_to_credit");
$end_coin_exchange_rate[$coin_to_credit] = json_decode($json_data);
}

// check that we have good exchange rates, or die
if (!is_object($end_coin_exchange_rate[$coin_to_credit]) || !($end_coin_exchange_rate[$coin_to_credit]->exchange_rate > 0)) {
$log->logInfo("Error retrieving $coin_to_credit exchange rates");
$monitoring->endCronjob($cron_name, 'E0084', 0, true, false);
}

$end_coin_rate[$coin_to_credit] = $end_coin_exchange_rate[$coin_to_credit]->exchange_rate;
}

$end_rate = $end_coin_exchange_rate[$coin_to_credit]->exchange_rate;
$end_rate = $end_coin_rate[$coin_to_credit];

$log->logInfo("processing: account $account_id");

// 0.48 BTC = 0.00002400 POT/BTC * 20000 POT
$amount_in_bitcoin = $from_coin_exchange_rate->exchange_rate * $amount_to_exchange;
$total_btc += $amount_in_bitcoin;
$amount_in_bitcoin = $from_coin_rate[$original_coin] * $amount_to_exchange;
$total_btc[$coin_to_credit] += $amount_in_bitcoin;

$log->logInfo("amount in btc [$amount_in_bitcoin] = $original_coin rate [$from_coin_exchange_rate->exchange_rate] * amount to exchange [$amount_to_exchange]");
$log->logInfo("amount in btc [$amount_in_bitcoin] = $original_coin rate [" . $from_coin_rate[$original_coin] . "] * amount to exchange [$amount_to_exchange]");

// 50,526.315789 WC = 0.48 BTC / 0.00000950 WC/BTC
$amount_in_end_coin = $amount_in_bitcoin / $end_rate;

$log->logInfo("amount in end coin [$amount_in_end_coin] = amount in btc [$amount_in_bitcoin] / $coin_to_credit rate [$end_rate]");

$amount_in_end_coin_minus_fee = $amount_in_end_coin * 0.99;
$amount_in_end_coin_minus_fee = $amount_in_end_coin * (1 - $tax_rate);

$log->logInfo("amount in end coin minus fee [$amount_in_end_coin_minus_fee] = amount in end coin [$amount_in_end_coin] * 0.99");
$log->logInfo("amount in end coin minus fee [$amount_in_end_coin_minus_fee] = amount in end coin [$amount_in_end_coin] * (1 - $tax_rate)");

if ($process) {
if (!$transaction->addTransaction($account_id, $amount_in_end_coin_minus_fee, 'Credit_PPS', NULL, NULL, NULL, NULL, $end_coin_db_name)) {
Expand All @@ -82,7 +122,7 @@
}
}
$log->logInfo("added $amount_in_end_coin_minus_fee to $end_coin_db_name transaction table as Credit_PPS");
$total_end += $amount_in_end_coin_minus_fee;
$total_end[$coin_to_credit] += $amount_in_end_coin_minus_fee;

if ($process) {
if (!$transaction->addTransaction($account_id, $amount_to_exchange, 'Convertible_Transfer')) {
Expand All @@ -91,7 +131,7 @@
}

$log->logInfo("added Convertible_Transfer of $amount_to_exchange to $original_coin transaction table");
$total_original += $amount_to_exchange;
$total_original[$coin_to_credit] += $amount_to_exchange;

if ($process) {
if (!$transaction->setConvertibleArchived($account_id, null)) {
Expand All @@ -101,7 +141,40 @@
$log->logInfo("marked transaction as archived. done.");
}

$log->logInfo("totals: $original_coin [$total_original], $coin_to_credit [$total_end], BTC [$total_btc]");
$total_out = 0.0;

foreach ($total_original as $coin => $blah) {
$total_string = "totals: $original_coin [" .$total_original[$coin] ."], $coin [" . $total_end[$coin] ."], BTC [" . $total_btc[$coin] ."]";

$total_out += $total_original[$coin];

$log->logInfo($total_string);
print $total_string . PHP_EOL;
}

$daemon_balance = $bitcoin->getrealbalance();

echo "total $original_coin: $total_out" . PHP_EOL;
echo "balance of $original_coin: $daemon_balance" . PHP_EOL;

if ($payout_mode) {
echo "paying $total_out to " . $payout_addresses[$original_coin] . PHP_EOL;

if ($total_out > ($daemon_balance * 0.9)) {
$total_out = $daemon_balance * 0.9;
echo "total is higher than 90% of coin balance. only sending $total_out coins." . PHP_EOL;
}

try {
$tx_id = $bitcoin->sendtoaddress($payout_addresses[$original_coin], $total_out);
echo "sent $total_out $original_coin. tx id: $tx_id" . PHP_EOL;
} catch (Exception $e) {
$log->logError('error sending coins to exchange' . $e->getMessage());
// Remove this line below if RPC calls are failing but transactions are still added to it
// Don't blame MPOS if you run into issues after commenting this out!
$monitoring->endCronjob($cron_name, 'E0078', 1, true);
}
}

require_once('cron_end.inc.php');

11 changes: 11 additions & 0 deletions cronjobs/exchange_payout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?

$payout_addresses = array(
'POT' => 'PP4c9MGR59Vo6wwkE55RoJHttexffFaptG',
'MUN' => 'mHdNfaXPnNSU3joTdNK6zNCdcHWyvzqQUm',
'TRC' => '1QDdP2xBHJVYv7JKG3eCN85pu37dTypfuG',
'KARM' => 'KBufA4duPb5yzpMWXqaGYGZca2xa32cYtK',
'LGC' => 'LY4DRJ6wVsUE3JP17Ab3MUFTuWCTqpGYh4'
);

?>
2 changes: 1 addition & 1 deletion cronjobs/payouts.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
$log->logInfo(" payouts disabled via admin panel");
$monitoring->endCronjob($cron_name, 'E0009', 0, true, false);
}
$log->logInfo("Starting Payout...");
$log->logInfo("Starting Payout..." . $config['currency']);
if ($bitcoin->can_connect() !== true) {
$log->logFatal(" unable to connect to RPC server, exiting");
$monitoring->endCronjob($cron_name, 'E0006', 1, true);
Expand Down
4 changes: 4 additions & 0 deletions cronjobs/prices.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?
$from_coin_rate = array('TRC' => 0.00007000, 'RZR' => 0.00100000, 'CRYPT' => 0.00061648, 'POT' => 0.00001675, 'MUN' => 0.00000049, 'LGC' => 0.00000855, 'KARM' => 0.00000003792, 'RDD' => 0.00000009);
$end_coin_rate = array('WC' => 0.00000098, 'SUM' => 0.00001356, 'BNS' => 0.00000003, 'UVC' => 0.00000080, 'HYPER' => 0.00002000);
?>
8 changes: 6 additions & 2 deletions cronjobs/proportional_payout.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
// Include all settings and classes
require_once('shared.inc.php');

$log->logInfo("Running proportional payout for " . $config['currency']);

// Check if we are set as the payout system
if ($config['payout_system'] != 'prop') {
$log->logInfo("Please activate this cron in configuration via payout_system = prop");
Expand All @@ -44,7 +46,7 @@
$log->logInfo(sprintf($strLogMask, 'Block', 'ID', 'Username', 'Valid', 'Invalid', 'Percentage', 'Payout', 'Donation', 'Fee', 'Bonus'));
foreach ($aAllBlocks as $iIndex => $aBlock) {
// If we have unaccounted blocks without share_ids, they might not have been inserted yet
if (!$aBlock['share_id']) {
if (!$aBlock['share_id'] && !in_array($config['currency'], array('WC', 'SUM', 'BNS'))) {
$log->logError('E0062: Block has no share_id, not running payouts');
$monitoring->endCronjob($cron_name, 'E0062', 0, true);
}
Expand Down Expand Up @@ -158,7 +160,9 @@
$monitoring->endCronjob($cron_name, 'E0014', 1, true);
}
} else {
$log->logFatal('Potential double payout detected for block ' . $aBlock['id'] . '. Aborted.');
$log->logDebug("double payout dump: " . print_r($aAllBlocks, true) );
$log->logDebug("double payout index: " . $iIndex);
//$log->logFatal('Potential double payout detected for block ' . $aBlock['id'] . '. Aborted.');
$aMailData = array(
'email' => $setting->getValue('system_error_email'),
'subject' => 'Payout Failure: Double Payout',
Expand Down
23 changes: 0 additions & 23 deletions cronjobs/run-convertible.sh
Original file line number Diff line number Diff line change
Expand Up @@ -99,31 +99,8 @@ if [[ ! -e 'shared.inc.php' ]]; then
exit 1
fi

# Our PID of this shell
PID=$$

if [[ -e $PIDFILE ]]; then
echo "Cron seems to be running already"
RUNPID=$( cat $PIDFILE )
if ps fax | grep -q "^\<$RUNPID\>"; then
echo "Process found in process table, aborting"
exit 1
else
echo "Process $RUNPID not found. Plese remove $PIDFILE if process is indeed dead."
exit 1
fi
fi

# Write our PID file
echo $PID 2>/dev/null 1> $PIDFILE || {
echo 'Failed to create PID file, aborting';
exit 1
}

for cron in $CRONS; do
[[ $VERBOSE == 1 ]] && echo "Running $cron, check logfile for details"
$PHP_BIN $cron -d $SUBFOLDER $PHP_OPTS
done

# Remove pidfile
rm -f $PIDFILE
1 change: 0 additions & 1 deletion cronjobs/shared.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ function cfip() { return (@defined('SECURITY')) ? 1 : 0; }
// MODIFY THIS
// We need to find our include files so set this properly
define("BASEPATH", "../public/");

/*****************************************************
* No need to change beyond this point *
*****************************************************/
Expand Down
31 changes: 31 additions & 0 deletions cronjobs/update_price.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/ruby

require 'google_drive'

session = GoogleDrive.login("user", "password")
sheet = session.files.find { |f| f.title == "sheet" }.worksheets[0]

crypt_price = sheet[4,2]
karm_price = sheet[4,3]
lgc_price = sheet[4,4]
mun_price = sheet[4,5]
pot_price = sheet[4,6]
rdd_price = sheet[4,7]
rzr_price = sheet[4,8]
trc_price = sheet[4,9]

bns_price = sheet[6,2]
hyper_price = sheet[13,2]
sum_price = sheet[20,2]
uvc_price = sheet[27,2]
wc_price = sheet[34,2]

output = <<-DERP
<?
$from_coin_rate = array('TRC' => #{trc_price}, 'RZR' => #{rzr_price}, 'CRYPT' => #{crypt_price}, 'POT' => #{pot_price}, 'MUN' => #{mun_price}, 'LGC' => #{lgc_price}, 'KARM' => #{karm_price}, 'RDD' => #{rdd_price});
$end_coin_rate = array('WC' => #{wc_price}, 'SUM' => #{sum_price}, 'BNS' => #{bns_price}, 'UVC' => #{uvc_price}, 'HYPER' => #{hyper_price});
?>
DERP

puts output

4 changes: 4 additions & 0 deletions cronjobs/update_prices.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

/usr/local/bin/ruby /mpos/cronjobs/update_price.rb > /mpos/cronjobs/prices.inc.php
cat /mpos/cronjobs/prices.inc.php
6 changes: 4 additions & 2 deletions public/include/classes/bitcoinwrapper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ public function getblockcount() {
// Wrapper method to get the real main account balance
public function getrealbalance() {
$this->oDebug->append("STA " . __METHOD__, 4);
$aAccounts = parent::listaccounts();
$dBalance = parent::getbalance('');
// $aAccounts = parent::listaccounts();
$dBalance = parent::getbalance();

return $dBalance;
// Account checks
if (count($aAccounts) == 1) {
// We only have a single account so getbalance will be fine
Expand Down
4 changes: 2 additions & 2 deletions public/include/classes/coinaddress.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public function insertOrUpdateCoinAddress($account_id, $coin_address, $threshold
**/
public function updateCoinAddress($account_id, $old_address, $coin_address, $threshold) {
$this->debug->append("STA " . __METHOD__, 4);
$str = "UPDATE $this->table SET address = ?, ap_threshold = ? WHERE address = ?";
$str = "UPDATE $this->table SET address = ?, ap_threshold = ? WHERE address = ? AND account_id = ?";
$stmt = $this->mysqli->prepare($str);

return $this->checkStmt($stmt) && $stmt->bind_param('sds', $coin_address, $threshold, $old_address) && $stmt->execute();
return $this->checkStmt($stmt) && $stmt->bind_param('sdsd', $coin_address, $threshold, $old_address, $account_id) && $stmt->execute();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion public/include/classes/monitoring.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ public function endCronjob($cron_name, $msgCode, $exitCode=0, $fatal=false, $mai
'email' => $this->setting->getValue('system_error_email'),
'subject' => 'Cronjob Failure',
'Error Code' => $msgCode,
'Error Message' => $this->getErrorMsg($msgCode)
'Error Message' => $this->getErrorMsg($msgCode),
'coin' => $this->config['currency']
);
if (!$this->mail->sendMail('notifications/error', $aMailData))
$this->setErrorMessage('Failed to send mail notification');
Expand Down
4 changes: 2 additions & 2 deletions public/include/classes/transaction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function setArchived($account_id, $txid) {
SET t.archived = 1
WHERE t.archived = 0
AND (
( t.account_id = ? AND t.id <= ? AND b.confirmations >= ? )
( t.account_id = ? AND t.id <= ? AND b.confirmations >= ? AND t.type != 'Convertible')
OR ( t.account_id = ? AND t.id <= ? AND t.type IN ( 'Credit_PPS', 'Donation_PPS', 'Fee_PPS', 'TXFee', 'Debit_MP', 'Debit_AP' ) )
)");
if ($this->checkStmt($stmt) && $stmt->bind_param('iiiii', $account_id, $txid, $this->config['confirmations'], $account_id, $txid) && $stmt->execute())
Expand Down Expand Up @@ -395,7 +395,7 @@ public function getConvertibleQueue($limit=250) {
LEFT JOIN " . $this->user->getTableName() . " AS a
ON t.account_id = a.id
WHERE t.type in ('Convertible', 'Convertible_Transfer') AND t.archived = 0
GROUP BY t.account_id
GROUP BY t.account_id, t.convertible
HAVING amount > 0
LIMIT ?");
if ($this->checkStmt($stmt) && $stmt->bind_param('i', $limit) && $stmt->execute() && $result = $stmt->get_result())
Expand Down
Loading

0 comments on commit 6549909

Please sign in to comment.