Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
token
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wallet
token
Commits
5baf2b5c
Commit
5baf2b5c
authored
Jun 27, 2018
by
rlgy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bitfinex api &Structural optimization
parent
fcd3824d
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
145 additions
and
54 deletions
+145
-54
ExchangeBusiness.php
common/business/ExchangeBusiness.php
+1
-0
params.php
common/config/params.php
+6
-0
Bitfinex.php
common/service/exchange/Bitfinex.php
+69
-0
Exchange.php
common/service/exchange/Exchange.php
+51
-0
ExchangeFactory.php
common/service/exchange/ExchangeFactory.php
+9
-1
ExchangeInterface.php
common/service/exchange/ExchangeInterface.php
+0
-26
HuoBi.php
common/service/exchange/HuoBi.php
+1
-27
ExchangeController.php
console/controllers/ExchangeController.php
+8
-0
No files found.
common/business/ExchangeBusiness.php
View file @
5baf2b5c
...
...
@@ -21,6 +21,7 @@ class ExchangeBusiness
private
static
$exchanges
=
[
0
=>
'HuoBi'
,
1
=>
'Hadax'
,
2
=>
'Bitfinex'
,
];
/**
...
...
common/config/params.php
View file @
5baf2b5c
...
...
@@ -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
]
];
common/service/exchange/Bitfinex.php
0 → 100644
View file @
5baf2b5c
<?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
common/service/exchange/Exchange.php
View file @
5baf2b5c
...
...
@@ -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
common/service/exchange/ExchangeFactory.php
View file @
5baf2b5c
...
...
@@ -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
);
}
...
...
common/service/exchange/ExchangeInterface.php
View file @
5baf2b5c
...
...
@@ -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
*/
...
...
common/service/exchange/HuoBi.php
View file @
5baf2b5c
...
...
@@ -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
));
}
...
...
console/controllers/ExchangeController.php
View file @
5baf2b5c
...
...
@@ -34,4 +34,12 @@ class ExchangeController extends Controller
{
ExchangeBusiness
::
setQuotation
();
}
public
function
actionTest
()
{
$a
=
new
\common\service\exchange\Bitfinex
();
// $a->setSupportedSymbol();
$a
->
setQuotation
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment