[UPDATE] Store coin auto payout in coin_addresses table per coin

This commit is contained in:
Sebastian Grewe 2015-04-16 12:16:45 +02:00
parent 8826d0301c
commit 1613355c04
5 changed files with 71 additions and 14 deletions

View File

@ -27,6 +27,29 @@ class CoinAddress extends Base {
return $this->sqlError();
}
/**
* Fetch users Auto Payout Threshold for a currency
* @param UserID int UserID
* @return mixed Float value for threshold, false on error
**/
public function getAPThreshold($userID, $currency=NULL) {
if ($currency === NULL) $currency = $this->config['currency'];
$this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare("
SELECT ap_threshold
FROM " . $this->getTableName() . "
WHERE account_id = ? AND currency = ?
");
if ( $this->checkStmt($stmt) && $stmt->bind_param('is', $userID, $currency) && $stmt->execute() && $result = $stmt->get_result()) {
if ($result->num_rows == 1) {
return $result->fetch_object()->ap_threshold;
}
}
$this->debug->append("Unable to fetch users auto payout threshold for " . $currency);
return $this->sqlError();
}
/**
* Check if a coin address is already set
* @param address string Coin Address to check for
@ -76,23 +99,24 @@ class CoinAddress extends Base {
* Update a coin address record for a user and a currency
* @param userID int Account ID
* @param address string Coin Address
* @param ap_threshold float Threshold for auto payouts for this currency
* @param currency string Currency short handle, defaults to config option
* @return bool true or false
**/
public function update($userID, $address, $currency=NULL) {
public function update($userID, $address, $ap_threshold, $currency=NULL) {
if ($currency === NULL) $currency = $this->config['currency'];
if ($address != $this->getCoinAddress($userID) && $this->existsCoinAddress($address)) {
$this->setErrorMessage('Unable to update coin address, address already exists');
return false;
}
if ($this->getCoinAddress($userID) != NULL) {
$stmt = $this->mysqli->prepare("UPDATE " . $this->getTableName() . " SET coin_address = ? WHERE account_id = ? AND currency = ?");
if ( $this->checkStmt($stmt) && $stmt->bind_param('sis', $address, $userID, $currency) && $stmt->execute()) {
$stmt = $this->mysqli->prepare("UPDATE " . $this->getTableName() . " SET coin_address = ?, ap_threshold = ? WHERE account_id = ? AND currency = ?");
if ( $this->checkStmt($stmt) && $stmt->bind_param('sdis', $address, $ap_threshold, $userID, $currency) && $stmt->execute()) {
return true;
}
} else {
$stmt = $this->mysqli->prepare("INSERT INTO " . $this->getTableName() . " (coin_address, account_id, currency) VALUES (?, ?, ?)");
if ( $this->checkStmt($stmt) && $stmt->bind_param('sis', $address, $userID, $currency) && $stmt->execute()) {
$stmt = $this->mysqli->prepare("INSERT INTO " . $this->getTableName() . " (coin_address, ap_threshold, account_id, currency) VALUES (?, ?, ?, ?)");
if ( $this->checkStmt($stmt) && $stmt->bind_param('sdis', $address, $ap_threshold, $userID, $currency) && $stmt->execute()) {
return true;
}
}

View File

@ -340,11 +340,11 @@ class User extends Base {
$this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare("
SELECT
a.id, a.username, ca.coin_address AS coin_address, a.ap_threshold
a.id, a.username, ca.coin_address AS coin_address, ca.ap_threshold
FROM " . $this->getTableName() . " AS a
LEFT JOIN " . $this->coin_address->getTableName() . " AS ca
ON a.id = ca.account_id
WHERE ap_threshold > 0 AND ca.currency = ?
WHERE ca.ap_threshold > 0 AND ca.currency = ?
AND ca.coin_address IS NOT NULL
");
if ( $this->checkStmt($stmt) && $stmt->bind_param('s', $this->config['currency']) && $stmt->execute() && $result = $stmt->get_result()) {
@ -544,12 +544,12 @@ class User extends Base {
if ($email == 'hidden' || $email == NULL)
$email = $this->getUserEmailById($userID);
// We passed all validation checks so update the account
$stmt = $this->mysqli->prepare("UPDATE $this->table SET ap_threshold = ?, donate_percent = ?, email = ?, timezone = ?, is_anonymous = ? WHERE id = ?");
if ($this->checkStmt($stmt) && $stmt->bind_param('ddssii', $threshold, $donate, $email, $timezone, $is_anonymous, $userID) && $stmt->execute()) {
$stmt = $this->mysqli->prepare("UPDATE $this->table SET donate_percent = ?, email = ?, timezone = ?, is_anonymous = ? WHERE id = ?");
if ($this->checkStmt($stmt) && $stmt->bind_param('dssii', $donate, $email, $timezone, $is_anonymous, $userID) && $stmt->execute()) {
$this->log->log("info", $this->getUserName($userID)." updated their account details");
// Update coin address too
// Update coin address and ap_threshold if coin_address is set
if ($address) {
if ($this->coin_address->update($userID, $address)) {
if ($this->coin_address->update($userID, $address, $threshold)) {
return true;
}
} else {
@ -698,12 +698,13 @@ class User extends Base {
$stmt = $this->mysqli->prepare("
SELECT
id AS id, username, pin, api_key, is_admin, is_anonymous, email, timezone, no_fees,
IFNULL(donate_percent, '0') as donate_percent, ap_threshold
IFNULL(donate_percent, '0') as donate_percent
FROM " . $this->getTableName() . "
WHERE id = ? LIMIT 0,1");
if ($this->checkStmt($stmt) && $stmt->bind_param('i', $userID) && $stmt->execute() && $result = $stmt->get_result()) {
$aData = $result->fetch_assoc();
$aData['coin_address'] = $this->coin_address->getCoinAddress($userID);
$aData['ap_threshold'] = $this->coin_address->getAPThreshold($userID);
$stmt->close();
return $aData;
}

View File

@ -2,7 +2,7 @@
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
define('MPOS_VERSION', '1.0.2');
define('DB_VERSION', '1.0.0');
define('DB_VERSION', '1.0.1');
define('CONFIG_VERSION', '1.0.0');
define('HASH_VERSION', 1);

View File

@ -27,7 +27,6 @@ CREATE TABLE IF NOT EXISTS `accounts` (
`api_key` varchar(255) DEFAULT NULL,
`token` varchar(65) DEFAULT NULL,
`donate_percent` float DEFAULT '0',
`ap_threshold` float DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`)
@ -56,6 +55,7 @@ CREATE TABLE IF NOT EXISTS `coin_addresses` (
`account_id` int(11) NOT NULL,
`currency` varchar(5) NOT NULL,
`coin_address` varchar(255) NOT NULL,
`ap_threshold` float DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `coin_address` (`coin_address`),
KEY `account_id` (`account_id`)

View File

@ -0,0 +1,32 @@
<?php
function run_101() {
// Ugly but haven't found a better way
global $setting, $config, $coin_address, $user, $mysqli;
// Version information
$db_version_old = '1.0.0'; // What version do we expect
$db_version_new = '1.0.1'; // What is the new version we wish to upgrade to
$db_version_now = $setting->getValue('DB_VERSION'); // Our actual version installed
// Upgrade specific variables
$aSql[] = "ALTER TABLE `" . $coin_address->getTableName() . "` ADD ap_threshold float DEFAULT '0'";
$aSql[] = "UPDATE " . $coin_address->getTableName() . " AS ca LEFT JOIN " . $user->getTableName() . " AS a ON a.id = ca.account_id SET ca.coin_address = a.coin_address";
$aSql[] = "ALTER TABLE `" . $user->getTableName() . "` DROP `ap_threshold`";
$aSql[] = "UPDATE " . $setting->getTableName() . " SET value = '1.0.1' WHERE name = 'DB_VERSION'";
if ($db_version_now == $db_version_old && version_compare($db_version_now, DB_VERSION, '<')) {
// Run the upgrade
echo '- Starting database migration to version ' . $db_version_new . PHP_EOL;
foreach ($aSql as $sql) {
echo '- Preparing: ' . $sql . PHP_EOL;
$stmt = $mysqli->prepare($sql);
if ($stmt && $stmt->execute()) {
echo '- success' . PHP_EOL;
} else {
echo '- failed: ' . $mysqli->error . PHP_EOL;
exit(1);
}
}
}
}
?>