From 7e0c9f69ce507cc702b2d67b1ddf39ce67bb5fb9 Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Wed, 27 Apr 2016 08:42:08 +0200 Subject: [PATCH] sql: settings table for exchange variables only applied to bittrex as sample, default values are kept if unset. --- sql/2016-04-27-settings.sql | 10 ++ web/yaamp/commands/ExchangeCommand.php | 79 +++++++++++++++- web/yaamp/core/functions/functions.php | 3 +- web/yaamp/core/functions/settings.php | 101 +++++++++++++++++++++ web/yaamp/core/trading/bittrex_trading.php | 17 ++-- web/yaamp/models/db_settingsModel.php | 40 ++++++++ 6 files changed, 237 insertions(+), 13 deletions(-) create mode 100644 sql/2016-04-27-settings.sql create mode 100644 web/yaamp/core/functions/settings.php create mode 100644 web/yaamp/models/db_settingsModel.php diff --git a/sql/2016-04-27-settings.sql b/sql/2016-04-27-settings.sql new file mode 100644 index 0000000..faf500b --- /dev/null +++ b/sql/2016-04-27-settings.sql @@ -0,0 +1,10 @@ +-- Recent additions to add after db init (.gz) +-- mysql yaamp -p < file.sql + + +CREATE TABLE `settings` ( + `param` varchar(128) NOT NULL PRIMARY KEY, + `value` varchar(255) NULL, + `type` varchar(8) NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + diff --git a/web/yaamp/commands/ExchangeCommand.php b/web/yaamp/commands/ExchangeCommand.php index 8721735..bd1a7a8 100644 --- a/web/yaamp/commands/ExchangeCommand.php +++ b/web/yaamp/commands/ExchangeCommand.php @@ -30,14 +30,27 @@ class ExchangeCommand extends CConsoleCommand if (!isset($args[0])) { echo "Yiimp exchange command\n"; - echo "Usage: yiimp exchange test\n"; + echo "Usage: yiimp exchange apitest\n"; + echo " yiimp exchange get \n"; + echo " yiimp exchange set \n"; + echo " yiimp exchange unset \n"; + echo " yiimp exchange settings \n"; return 1; - } else if ($args[0] == 'test') { + } else if ($args[0] == 'get') { + return $this->getExchangeSetting($args); + } else if ($args[0] == 'set') { + return $this->setExchangeSetting($args); + + } else if ($args[0] == 'unset') { + return $this->unsetExchangeSetting($args); + + } else if ($args[0] == 'settings') { + return $this->listExchangeSettings($args); + + } else if ($args[0] == 'apitest') { $this->testApiKeys(); - - echo "ok\n"; return 0; } } @@ -51,6 +64,62 @@ class ExchangeCommand extends CConsoleCommand return parent::getHelp().'exchange'; } + public function getExchangeSetting($args) + { + if (count($args) < 3) + die("usage: yiimp exchange get \n"); + $exchange = $args[1]; + $key = $args[2]; + $value = exchange_get($exchange, $key); + echo "$value\n"; + return 0; + } + + public function setExchangeSetting($args) + { + if (count($args) < 4) + die("usage: yiimp exchange set \n"); + $exchange = $args[1]; + $key = $args[2]; + $value = $args[3]; + $keys = exchange_valid_keys($exchange); + if (!isset($keys[$key])) { + echo "error: key '$key' is not handled!\n"; + return 1; + } + $res = exchange_set($exchange, $key, $value); + $val = exchange_get($exchange, $key); + echo ($res ? "$exchange $key ".json_encode($val) : "error") . "\n"; + return 0; + } + + public function unsetExchangeSetting($args) + { + if (count($args) < 3) + die("usage: yiimp exchange unset \n"); + $exchange = $args[1]; + $key = $args[2]; + exchange_unset($exchange, $key); + echo "ok\n"; + return 0; + } + + public function listExchangeSettings($args) + { + if (count($args) < 2) + die("usage: yiimp exchange settings \n"); + $exchange = $args[1]; + $keys = exchange_valid_keys($exchange); + foreach ($keys as $key => $desc) { + $val = exchange_get($exchange, $key); + if ($val !== null) { + //echo "$desc\n"; + echo "$exchange $key ".json_encode($val)."\n"; + } + } + return 0; + } + public function testApiKeys() { require_once($this->basePath.'/yaamp/core/core.php'); @@ -116,7 +185,7 @@ class ExchangeCommand extends CConsoleCommand else echo("cryptomic all: ".json_encode($balance->result)."\n"); } if (!empty(EXCH_BITSTAMP_KEY)) { - $balance = bitstamp_api_user(); + $balance = bitstamp_api_user('balance'); if (!is_object($balance)) echo "bitstamp error ".json_encode($balance)."\n"; else echo("bitstamp: ".json_encode($balance->result)."\n"); } diff --git a/web/yaamp/core/functions/functions.php b/web/yaamp/core/functions/functions.php index d37cdc5..b462092 100644 --- a/web/yaamp/core/functions/functions.php +++ b/web/yaamp/core/functions/functions.php @@ -7,5 +7,4 @@ require_once("easybitcoin.php"); require_once("yaamp.php"); require_once("memcache.php"); - - +require_once('settings.php'); diff --git a/web/yaamp/core/functions/settings.php b/web/yaamp/core/functions/settings.php new file mode 100644 index 0000000..f12dafa --- /dev/null +++ b/web/yaamp/core/functions/settings.php @@ -0,0 +1,101 @@ +$key)); + if (!$row) return $default; + + $type = arraySafeVal($row, 'type', settings_key_type($key)); + $value = $row['value']; + switch ($type) { + case 'bool': + return (bool) $value; + case 'int': + return intval($value); + case 'percent': + return ((double) $value) / 100.0; + case 'price': + case 'real': + return (double) $value; + case 'json': + return json_decode($value, true); + } + return $value; +} + +function settings_set($key, $value) +{ + $type = settings_key_type($key); + if ($type == 'json') { + $value = json_encode($value); + if (strlen($value) > 255) { + debuglog("warning: settings_set($key) value is too long!"); + return false; + } + } + + if ($type == 'bool' && strcasecmp($value,'true') == 0) $value = 1; + if ($type == 'bool' && strcasecmp($value,'false')== 0) $value = 0; + if ($type == 'percent' && strpos($value, '%') === false) $value = floatval($value) * 100; + + dborun("INSERT IGNORE INTO settings(param,value) VALUES (:key,:val)", array( + ':key'=>$key,':val'=>$value + )); + dborun("UPDATE settings SET value=:val, type=:type WHERE param=:key", array( + ':key'=>$key,':val'=>$value,':type'=>$type + )); + return true; +} + +function settings_unset($key) +{ + dborun("DELETE FROM settings WHERE param=:key", array(':key'=>$key)); + return 0; +} + +function exchange_get($exchange, $key, $default=null) +{ + $value = settings_get("$exchange-$key", $default); + return $value; +} + +function exchange_set($exchange, $key, $value) +{ + return settings_set("$exchange-$key", $value); +} + +function exchange_unset($exchange, $key) +{ + // reset to default value + return settings_unset("$exchange-$key"); +} + +/** + * Returns the list of valid keys, with a description + */ +function exchange_valid_keys() +{ + return array( + 'disabled' => 'Fully disable the exchange', + + 'trade_min_btc' => 'Minimum order on the exchange', + 'trade_sell_ask_pct' => 'Initial order ask price related to the lowest ask (in %)', + 'trade_cancel_ask_pct' => 'Cancel orders if the lowest ask reach this % of your order', + + 'withdraw_btc_address' => 'Custom withdraw BTC address for the exchange', + 'withdraw_min_btc' => 'Auto withdraw when your BTC balance reach this amount (0=disabled)', + 'withdraw_fee_btc' => 'Fees in BTC required to withdraw BTCs on the exchange', + ); +} diff --git a/web/yaamp/core/trading/bittrex_trading.php b/web/yaamp/core/trading/bittrex_trading.php index 82d7a57..8348c21 100644 --- a/web/yaamp/core/trading/bittrex_trading.php +++ b/web/yaamp/core/trading/bittrex_trading.php @@ -62,9 +62,12 @@ function doBittrexTrading($quick=false) $flushall = rand(0, 8) == 0; if($quick) $flushall = false; - $min_btc_trade = 0.00050000; // minimum allowed by the exchange - $sell_ask_pct = 1.05; // sell on ask price + 5% - $cancel_ask_pct = 1.20; // cancel order if our price is more than ask price + 20% + // minimum order allowed by the exchange + $min_btc_trade = exchange_get($exchange, 'trade_min_btc', 0.00050000); + // sell on ask price + 5% + $sell_ask_pct = exchange_get($exchange, 'trade_sell_ask_pct', 1.05); + // cancel order if our price is more than ask price + 20% + $cancel_ask_pct = exchange_get($exchange, 'trade_cancel_ask_pct', 1.20); sleep(1); $orders = bittrex_api_query('market/getopenorders'); @@ -234,10 +237,12 @@ function doBittrexTrading($quick=false) $db_order->save(); } - if(floatval(EXCH_AUTO_WITHDRAW) > 0 && $savebalance->balance >= (EXCH_AUTO_WITHDRAW + 0.0002)) + $withdraw_min = exchange_get($exchange, 'withdraw_min_btc', EXCH_AUTO_WITHDRAW); + $withdraw_fee = exchange_get($exchange, 'withdraw_fee_btc', 0.0002); + if($withdraw_min > 0 && $savebalance->balance >= ($withdraw_min + $withdraw_fee)) { - $btcaddr = YAAMP_BTCADDRESS; - $amount = $savebalance->balance - 0.0002; + $btcaddr = exchange_get($exchange, 'withdraw_btc_address', YAAMP_BTCADDRESS); + $amount = $savebalance->balance - $withdraw_fee; debuglog("bittrex withdraw $amount to $btcaddr"); sleep(1); diff --git a/web/yaamp/models/db_settingsModel.php b/web/yaamp/models/db_settingsModel.php new file mode 100644 index 0000000..d5cd242 --- /dev/null +++ b/web/yaamp/models/db_settingsModel.php @@ -0,0 +1,40 @@ +type)) $this->type = null; + + return parent::save($runValidation, $attributes); + } +}