| Current Path : /var/www/html/aipa/backend/modules/cryptoaverage/exchange/ |
| Current File : /var/www/html/aipa/backend/modules/cryptoaverage/exchange/Binance.php |
<?php
namespace backend\modules\cryptoaverage\exchange;
require_once __DIR__ . '/vendor/autoload.php';
class Binance
{
public static $client, $config;
public function __construct($config)
{
//self::$config = json_decode(file_get_contents( __DIR__ . '/api.json'));
self::$config = $config;
self::$client = new \GuzzleHttp\Client([
'base_uri' => self::$config->binance->BASE_URL
]);
}
public function info()
{
try {
$res = self::$client->get('/api/v3/exchangeInfo');
return json_decode($res->getBody());
} catch (\Throwable $t) {
throw new \Exception($t->getMessage());
}
}
public function orderBook($data)
{
try {
self::paramReq($data, [
'symbol',
'limit'
]);
$res = self::$client->get('/api/v3/depth', [
'query' => [
'symbol' => $data->symbol,
'limit' => $data->limit
]
]);
return json_decode($res->getBody());
} catch (\Throwable $t) {
throw new \Exception($t->getMessage());
}
}
public function trades($data)
{
try {
self::paramReq($data, [
'symbol',
'limit'
]);
$res = self::$client->get('/api/v3/trades', [
'query' => [
'symbol' => $data->symbol,
'limit' => $data->limit
]
]);
return json_decode($res->getBody());
} catch (\Throwable $t) {
throw new \Exception($t->getMessage());
}
}
public function allOrders($data){
try {
$query = [
'timestamp' => round(microtime(true) * 1000),
'symbol' => $data->symbol,
'limit' => $data->limit,
'recvWindow' => '60000'
];
if(!empty($data->startTime) && $data->startTime != $data->endTime) {
$query['startTime'] = $data->startTime;
}
if(!empty($data->endTime) && $data->startTime != $data->endTime) {
$query['endTime'] = $data->endTime;
}
// append signature
$query['signature'] = self::sign($query);
$res = self::$client->request('GET', '/api/v3/allOrders', [
'headers' => [
'X-MBX-APIKEY' => self::$config->binance->API_KEY
],
'query' => $query
]);
return json_decode($res->getBody());
} catch (\Throwable $t) {
throw new \Exception($t->getMessage());
}
}
public function myTrades($data){
try {
$query = [
'timestamp' => round(microtime(true) * 1000),
'symbol' => $data->symbol,
'limit' => $data->limit,
'recvWindow' => '60000'
];
// if(!empty($data->startTime) && $data->startTime != $data->endTime) {
// $query['startTime'] = $data->startTime;
// }
// if(!empty($data->endTime) && $data->startTime != $data->endTime) {
// $query['endTime'] = $data->endTime;
// }
// append signature
$query['signature'] = self::sign($query);
$res = self::$client->request('GET', '/api/v3/myTrades', [
'headers' => [
'X-MBX-APIKEY' => self::$config->binance->API_KEY
],
'query' => $query
]);
return json_decode($res->getBody());
} catch (\Throwable $t) {
throw new \Exception($t->getMessage());
}
}
public function time()
{
try {
$res = self::$client->get('/api/v3/time');
return json_decode($res->getBody());
} catch (\Throwable $t) {
throw new \Exception($t->getMessage());
}
}
public function depositHistory($data)
{
try {
self::paramReq($data, [
'asset'
]);
$query = [
'timestamp' => round(microtime(true) * 1000),
'asset' => $data->asset,
'recvWindow' => '60000'
];
// append signature
$query['signature'] = self::sign($query);
$res = self::$client->request('GET', '/wapi/v3/depositHistory.html', [
'headers' => [
'X-MBX-APIKEY' => self::$config->binance->API_KEY
],
'query' => $query
]);
return json_decode($res->getBody());
} catch (\Throwable $t) {
throw new \Exception($t->getMessage());
}
}
public static function paramReq($pay, $arr)
{
foreach ($arr as $r) {
if (!in_array($r, array_keys((array) $pay))) {
throw new \Exception('Missing required params ' . implode(', ', $arr));
}
if (!is_object($pay) || empty($pay->{$r})) {
throw new \Exception('Params not existed, or not an object ' . implode(', ', $arr));
}
}
}
public static function sign($data)
{
// convert to pair =
$data = array_map(function ($v, $k) {
return $k . '=' . $v;
}, $data, array_keys($data));
// add ampersand
$data = implode('&', $data);
// hash
return hash_hmac('sha256', $data, self::$config->binance->API_SECRET);
}
}