mirror of
https://github.com/LBRYFoundation/pool.git
synced 2025-08-23 09:27:25 +00:00
sql: settings table for exchange variables
only applied to bittrex as sample, default values are kept if unset.
This commit is contained in:
parent
9d20eb01b3
commit
7e0c9f69ce
6 changed files with 237 additions and 13 deletions
10
sql/2016-04-27-settings.sql
Normal file
10
sql/2016-04-27-settings.sql
Normal file
|
@ -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;
|
||||||
|
|
|
@ -30,14 +30,27 @@ class ExchangeCommand extends CConsoleCommand
|
||||||
if (!isset($args[0])) {
|
if (!isset($args[0])) {
|
||||||
|
|
||||||
echo "Yiimp exchange command\n";
|
echo "Yiimp exchange command\n";
|
||||||
echo "Usage: yiimp exchange test\n";
|
echo "Usage: yiimp exchange apitest\n";
|
||||||
|
echo " yiimp exchange get <exchange> <key>\n";
|
||||||
|
echo " yiimp exchange set <exchange> <key> <value>\n";
|
||||||
|
echo " yiimp exchange unset <exchange> <key>\n";
|
||||||
|
echo " yiimp exchange settings <exchange>\n";
|
||||||
return 1;
|
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();
|
$this->testApiKeys();
|
||||||
|
|
||||||
echo "ok\n";
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,6 +64,62 @@ class ExchangeCommand extends CConsoleCommand
|
||||||
return parent::getHelp().'exchange';
|
return parent::getHelp().'exchange';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getExchangeSetting($args)
|
||||||
|
{
|
||||||
|
if (count($args) < 3)
|
||||||
|
die("usage: yiimp exchange get <exchange> <key>\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 <exchange> <key> <value>\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 <exchange> <key>\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 <exchange>\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()
|
public function testApiKeys()
|
||||||
{
|
{
|
||||||
require_once($this->basePath.'/yaamp/core/core.php');
|
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");
|
else echo("cryptomic all: ".json_encode($balance->result)."\n");
|
||||||
}
|
}
|
||||||
if (!empty(EXCH_BITSTAMP_KEY)) {
|
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";
|
if (!is_object($balance)) echo "bitstamp error ".json_encode($balance)."\n";
|
||||||
else echo("bitstamp: ".json_encode($balance->result)."\n");
|
else echo("bitstamp: ".json_encode($balance->result)."\n");
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,5 +7,4 @@ require_once("easybitcoin.php");
|
||||||
|
|
||||||
require_once("yaamp.php");
|
require_once("yaamp.php");
|
||||||
require_once("memcache.php");
|
require_once("memcache.php");
|
||||||
|
require_once('settings.php');
|
||||||
|
|
||||||
|
|
101
web/yaamp/core/functions/settings.php
Normal file
101
web/yaamp/core/functions/settings.php
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// DB Settings, initially made for trade settings on exchanges
|
||||||
|
|
||||||
|
function settings_key_type($key)
|
||||||
|
{
|
||||||
|
if (strpos($key, 'enabled') !== false) return 'bool';
|
||||||
|
if (strpos($key, 'disabled') !== false) return 'bool';
|
||||||
|
if (substr($key, -3) == 'pct') return 'percent';
|
||||||
|
if (substr($key, -3) == 'btc') return 'price';
|
||||||
|
if (substr($key, -5) == 'price') return 'price';
|
||||||
|
return 'string';
|
||||||
|
}
|
||||||
|
|
||||||
|
function settings_get($key, $default=null)
|
||||||
|
{
|
||||||
|
$row = dborow("SELECT value, type FROM settings WHERE param=:key", array(':key'=>$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',
|
||||||
|
);
|
||||||
|
}
|
|
@ -62,9 +62,12 @@ function doBittrexTrading($quick=false)
|
||||||
$flushall = rand(0, 8) == 0;
|
$flushall = rand(0, 8) == 0;
|
||||||
if($quick) $flushall = false;
|
if($quick) $flushall = false;
|
||||||
|
|
||||||
$min_btc_trade = 0.00050000; // minimum allowed by the exchange
|
// minimum order allowed by the exchange
|
||||||
$sell_ask_pct = 1.05; // sell on ask price + 5%
|
$min_btc_trade = exchange_get($exchange, 'trade_min_btc', 0.00050000);
|
||||||
$cancel_ask_pct = 1.20; // cancel order if our price is more than ask price + 20%
|
// 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);
|
sleep(1);
|
||||||
$orders = bittrex_api_query('market/getopenorders');
|
$orders = bittrex_api_query('market/getopenorders');
|
||||||
|
@ -234,10 +237,12 @@ function doBittrexTrading($quick=false)
|
||||||
$db_order->save();
|
$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;
|
$btcaddr = exchange_get($exchange, 'withdraw_btc_address', YAAMP_BTCADDRESS);
|
||||||
$amount = $savebalance->balance - 0.0002;
|
$amount = $savebalance->balance - $withdraw_fee;
|
||||||
debuglog("bittrex withdraw $amount to $btcaddr");
|
debuglog("bittrex withdraw $amount to $btcaddr");
|
||||||
|
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
|
40
web/yaamp/models/db_settingsModel.php
Normal file
40
web/yaamp/models/db_settingsModel.php
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class db_settings extends CActiveRecord
|
||||||
|
{
|
||||||
|
public static function model($className=__CLASS__)
|
||||||
|
{
|
||||||
|
return parent::model($className);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tableName()
|
||||||
|
{
|
||||||
|
return 'settings';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rules()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('param', 'required'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function relations()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function attributeLabels()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function save($runValidation=true,$attributes=null)
|
||||||
|
{
|
||||||
|
if (empty($this->type)) $this->type = null;
|
||||||
|
|
||||||
|
return parent::save($runValidation, $attributes);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue