Commit 5baf2b5c authored by rlgy's avatar rlgy

bitfinex api &Structural optimization

parent fcd3824d
......@@ -21,6 +21,7 @@ class ExchangeBusiness
private static $exchanges = [
0 => 'HuoBi',
1 => 'Hadax',
2 => 'Bitfinex',
];
/**
......
......@@ -42,5 +42,11 @@ return [
'exchange' => 3600 * 3 * 24,
'exchange_count' => 3600 * 3 * 24,
'quotation' => 60,
],
'http_proxy' => [
'host' => '127.0.0.1',
'port' => '1080',
'use' => true
]
];
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-27
* Time: 下午5:29
*/
namespace common\service\exchange;
use common\helpers\Curl;
use Yii;
class Bitfinex extends Exchange implements ExchangeInterface
{
protected $supported_symbol = 'supported_symbol_bitfinex';
protected $quotation_prefix = 'quotation_bitfinex_';
public function formatSymbol($tag = 'BTC', $aim = 'USD')
{
return 't' . strtoupper(trim($tag) . trim($aim));
}
public function setSupportedSymbol()
{
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
$ch = new Curl();
//http代理
if (Yii::$app->params['http_proxy']['use']) {
$ch->setOptions([
CURLOPT_PROXY => Yii::$app->params['http_proxy']['host'],
CURLOPT_PROXYPORT => Yii::$app->params['http_proxy']['port'],
]);
}
$result = $ch->get('https://api.bitfinex.com/v1/symbols', false);
if ($result) {
foreach ($result as $item) {
$redis->sadd($this->supported_symbol, 't' . strtoupper($item));
}
}
}
public function setQuotation()
{
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
$ch = new Curl();
//http代理
if (Yii::$app->params['http_proxy']['use']) {
$ch->setOptions([
CURLOPT_PROXY => Yii::$app->params['http_proxy']['host'],
CURLOPT_PROXYPORT => Yii::$app->params['http_proxy']['port'],
]);
}
$symbols = $redis->smembers($this->supported_symbol);
$query = '?symbols=' . implode(',', $symbols);
$res = $ch->get('https://api.bitfinex.com/v2/tickers' . $query, false);
foreach ($res as $item) {
$redis->hmset($this->quotation_prefix . $item[0], 'low', $item[10], 'high', $item['9'], 'last', $item[7]);
}
}
}
\ No newline at end of file
......@@ -8,6 +8,8 @@
namespace common\service\exchange;
use Yii;
/**
* Class Exchange
* 交易所抽象类
......@@ -17,4 +19,52 @@ abstract class Exchange
{
protected $supported_symbol = '';
protected $quotation_prefix = '';
/**
* 转化交易对为请求变量
*
* @param string $tag
* @param string $aim
* @return mixed
*/
abstract public function formatSymbol($tag = 'BTC', $aim = 'USD');
/**
* 获取支持查询行情的交易对
*
* @param string $tag
* @param string $aim
* @return bool
*/
public function symbolExists($tag = 'BTC', $aim = "USD")
{
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
$supported = $redis->smembers($this->supported_symbol);
if (is_array($supported) && in_array($this->formatSymbol($tag, $aim), $supported)) {
return true;
}
return false;
}
/**
* 获取交易对聚合行情信息
*
* @param string $tag
* @param string $aim
* @return mixed
*/
public function getTicker($tag = 'BTC', $aim = "USDT")
{
$symbol = $this->formatSymbol($tag, $aim);
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
$keys = $redis->hkeys($this->quotation_prefix . $symbol);
$values = $redis->hvals($this->quotation_prefix . $symbol);
return array_combine($keys, $values);
}
}
\ No newline at end of file
......@@ -17,6 +17,8 @@ use Prophecy\Exception\Doubler\ClassNotFoundException;
*/
class ExchangeFactory
{
public static $instances = [];
/**
* 创建交易所,默认为火币
* @param string $name
......@@ -27,7 +29,13 @@ class ExchangeFactory
{
$className = __NAMESPACE__ . '\\' . $name;
if (class_exists($className)) {
return new $className();
if (array_key_exists($name, self::$instances)) {
if (self::$instances[$name] instanceof $className) {
return self::$instances[$name];
}
}
self::$instances[$name] = new $className();
return self::$instances[$name];
}
throw new ClassNotFoundException('class ' . $className . ' not found!', $className);
}
......
......@@ -11,32 +11,6 @@ namespace common\service\exchange;
interface ExchangeInterface
{
/**
* 获取交易对聚合行情信息
*
* @param string $tag
* @param string $aim
* @return mixed
*/
public function getTicker($tag = 'BTC', $aim = "USD");
/**
* 获取支持查询行情的交易对
*
* @param string $tag
* @param string $aim
* @return bool
*/
public function symbolExists($tag = 'BTC', $aim = "USD");
/**
* 转化交易对为请求变量
* @param string $tag
* @param string $aim
* @return mixed
*/
public static function formatSymbol($tag = 'BTC', $aim = 'USD');
/**
* 保存支持的交易对到redis数据库,使用crontab定时更新
* @return mixed|void
*/
......
......@@ -17,33 +17,7 @@ class HuoBi extends Exchange implements ExchangeInterface
protected $quotation_prefix = 'quotation_huobi_';
protected $base_url = 'https://api.huobi.pro';
public function getTicker($tag = 'BTC', $aim = "USDT")
{
$symbol = self::formatSymbol($tag, $aim);
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
$keys = $redis->hkeys($this->quotation_prefix . $symbol);
$values = $redis->hvals($this->quotation_prefix . $symbol);
return array_combine($keys, $values);
}
public function symbolExists($tag = 'BTC', $aim = "USDT")
{
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
$supported = $redis->smembers($this->supported_symbol);
if (is_array($supported) && in_array(self::formatSymbol($tag, $aim), $supported)) {
return true;
}
return false;
}
public static function formatSymbol($tag = 'BTC', $aim = 'USDT')
public function formatSymbol($tag = 'BTC', $aim = 'USDT')
{
return strtolower(trim($tag) . trim($aim));
}
......
......@@ -34,4 +34,12 @@ class ExchangeController extends Controller
{
ExchangeBusiness::setQuotation();
}
public function actionTest()
{
$a = new \common\service\exchange\Bitfinex();
// $a->setSupportedSymbol();
$a->setQuotation();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment