1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Mokka\Exchange\Market; |
5
|
|
|
|
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use Mokka\Action\BuyAction; |
8
|
|
|
use Mokka\Action\SellAction; |
9
|
|
|
use Mokka\Exchange\ExchangeInterface; |
10
|
|
|
|
11
|
|
|
class Binance implements ExchangeInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var |
16
|
|
|
*/ |
17
|
|
|
private static $apiService; |
18
|
|
|
|
19
|
|
|
public function __construct($config) |
20
|
|
|
{ |
21
|
|
|
self::$apiService = new BinanceApiService($config['api_key'], $config['api_secret']); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get symbol price from source |
26
|
|
|
* @param $symbol |
27
|
|
|
* @return float |
28
|
|
|
*/ |
29
|
|
|
public function getPrice($symbol) : float |
30
|
|
|
{ |
31
|
|
|
$client = new Client(); |
32
|
|
|
|
33
|
|
|
$response = $client->get("https://api.binance.com/api/v3/ticker/price?symbol={$symbol}"); |
34
|
|
|
|
35
|
|
|
$response = json_decode($response->getBody()->getContents(), TRUE); |
36
|
|
|
|
37
|
|
|
return (float) $response['price']; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get historical prices |
42
|
|
|
* @param $symbol |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function getHistoricalPrices($symbol): array |
46
|
|
|
{ |
47
|
|
|
// TODO: Implement getHistoricalPrices() method. |
48
|
|
|
} |
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Put buy order |
52
|
|
|
* @param BuyAction $action |
53
|
|
|
* @return mixed|\Psr\Http\Message\ResponseInterface |
54
|
|
|
*/ |
55
|
|
|
public function buyOrder(BuyAction $action) |
56
|
|
|
{ |
57
|
|
|
|
58
|
|
|
$getServerTime = self::$apiService->getServerTime()->getBody()->getContents(); |
59
|
|
|
$getServerTime = json_decode($getServerTime, true); |
60
|
|
|
|
61
|
|
|
$params = [ |
62
|
|
|
'symbol' => $action->getSymbol(), |
63
|
|
|
'price' => $action->getActionPrice(), |
64
|
|
|
'quantity' => round($action->getQuantity(), 2), |
65
|
|
|
'side' => 'BUY', |
66
|
|
|
'type' => 'LIMIT', |
67
|
|
|
'timeInForce' => 'GTC', |
68
|
|
|
'recvWindow' => 5000, |
69
|
|
|
'timestamp' => $getServerTime['serverTime'] |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
return self::$apiService->postOrderTest($params); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Put sell order |
77
|
|
|
* @param SellAction $action |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
|
|
public function sellOrder(SellAction $action) |
81
|
|
|
{ |
82
|
|
|
|
83
|
|
|
$getServerTime = self::$apiService->getServerTime()->getBody()->getContents(); |
84
|
|
|
$getServerTime = json_decode($getServerTime, true); |
85
|
|
|
|
86
|
|
|
$params = [ |
87
|
|
|
'symbol' => $action->getSymbol(), |
88
|
|
|
'price' => $action->getActionPrice(), |
89
|
|
|
'quantity' => $action->getQuantity(), |
90
|
|
|
'side' => 'SELL', |
91
|
|
|
'type' => 'LIMIT', |
92
|
|
|
'timeInForce' => 'GTC', |
93
|
|
|
'recvWindow' => 10000000, |
94
|
|
|
'timestamp' => $getServerTime['serverTime'] |
95
|
|
|
|
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
return self::$apiService->postOrderTest($params); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getName() |
105
|
|
|
{ |
106
|
|
|
return 'binance'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
public function getBalance() |
114
|
|
|
{ |
115
|
|
|
//get server time. dont use user local machine time |
116
|
|
|
$client = new Client(); |
117
|
|
|
$response = $client->get("https://api.binance.com/api/v1/time"); |
118
|
|
|
$response = json_decode($response->getBody()->getContents(), TRUE); |
119
|
|
|
|
120
|
|
|
$account = self::$apiService->getAccount(['timestamp' => $response['serverTime']]); |
121
|
|
|
$account = json_decode($account->getBody()->getContents(), TRUE); |
122
|
|
|
|
123
|
|
|
$balance = null; |
124
|
|
|
//catch BTC balance |
125
|
|
|
foreach ($account['balances'] as $item) { |
126
|
|
|
if ($item['asset'] == 'BTC') { |
127
|
|
|
$balance = $item['free']; |
128
|
|
|
break; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $balance; |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
} |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: