作者 karlet

feat:币安的broker

<?php
require_once __DIR__ . '/../trader/CmBroker.php';
require_once __DIR__ . '/../trader/struct/ApiInfo.php';
require_once __DIR__ . '/../jytools/BinanceFutures.php';
use trader\struct\ApiInfo;
use trader\CmBroker;
$key = "nQMzzJlhYBkguxwtx5o4MbuiGsDfBIsrOCOUSiqCwR3wmM9gSkQnY1wczW2sVuTP";
$secret = "GUES7aaM54voJHKrrbXueWXfrJFqmqroVXCF430dolQ5uPK3i7t3Zy4nUgo2W0ec";
$apiInfo = new ApiInfo($key, $secret, "");
$broker = new CmBroker(CmBroker::PLAT_BINANCE, $apiInfo);
$broker->setWsHost("ws://bnws.a.indigo888.com/futures");
$broker->setRestHost("http://bnfapi.a.indigo888.com");
$broker->accListen(function ($data) {
var_dump($data);
});
// use jytools\BinanceFutures;
// $api = new BinanceFutures($key, $secret);
// $res = $api->listenKey();
// var_dump($res);
... ...
... ... @@ -47,7 +47,7 @@ class CmBroker
* @see PLAT_BITGET
*/
public $plat;
private ?OkxBroker $exBroker;
private OkxBroker|BinanceBroker $exBroker;
/** @var SymbolInfo[] $symbolInfos */
public $symbolInfos = [];
/** @var Pos[] $positions */
... ...
... ... @@ -3,8 +3,11 @@
namespace trader\exchange\binance;
require_once __DIR__ . '/../../struct/ApiInfo.php';
require_once __DIR__ . '/../../../jytools/func.php';
use trader\struct\ApiInfo;
use jytools\Curl;
use function jytools\getMicrotime;
class Api
{
... ... @@ -14,10 +17,61 @@ class Api
{
$this->apiInfo = $apiInfo;
}
public function setHost($host)
{
$this->host = $host;
}
public function getPremiumIndex()
//-----------public interface ------------
public function getPremiumIndex($params)
{
$url = "/fapi/v1/premiumIndex";
$method = "GET";
return $this->request($method, $url, $params);
}
//-------private interface ------------
public function getListenKey($params)
{
$url = "/fapi/v1/listenKey";
$method = "POST";
return $this->request($method, $url, [], $this->apiInfo);
}
//------------------------------------
private function createSign($secret, $param)
{
$len = count($param);
if ($len == 0) {
return $param;
}
$paramStr = http_build_query($param);
return hash_hmac('sha256', $paramStr, $secret);
}
public function request($method, $path, $param, ?ApiInfo $apiInfo = null)
{
$url = $this->host . $path;
if (!in_array(strtoupper($method), ['GET', 'POST', 'DELETE'])) {
return 0;
}
$header = [];
if ($apiInfo != null) {
$param['timestamp'] = getMicrotime();
$param['signature'] = $this->createSign($apiInfo->secret, $param);
$header[] = "X-MBX-APIKEY:" . $apiInfo->key;
}
$data = json_encode([]);
if (strtoupper($method) == 'POST') {
$data = Curl::httpPost($url, $param, $header);
}
if (strtoupper($method) == 'GET') {
$data = Curl::httpGet($url, $param, $header);
}
if (strtoupper($method) == 'DELETE') {
$data = Curl::httpDelete($url, $param, $header);
}
return json_decode($data, true);
}
}
... ...
... ... @@ -5,18 +5,17 @@ namespace trader\exchange\binance;
require_once __DIR__ . '/../../struct/ApiInfo.php';
require_once __DIR__ . '/Api.php';
require_once __DIR__ . '/../../../jytools/Websocket.php';
require_once __DIR__ . '/../../../jytools/func.php';
use trader\struct\ApiInfo;
use trader\exchange\binance\Api as BnApi;
use jytools\Websocket;
use function jytools\output;
class ExBroker
{
// private $host = 'wss://ws.okx.com:8443';
private $host = 'ws://okws.keetu.com';
private $pathPrivate = '/ws/v5/private';
private $pathPublic = '/ws/v5/public';
private $pathBusiness = '/ws/v5/business';
private $host = 'wss://fstream.binance.com';
private $path = '/stream';
private ApiInfo $apiInfo;
private BnApi $api;
private ?Websocket $wsAcc;
... ... @@ -28,9 +27,41 @@ class ExBroker
$this->api = new BnApi($apiInfo);
}
public function setWsHost($host)
{
$this->host = $host;
}
public function setRestHost($host)
{
$this->api->setHost($host);
}
public function accListen()
{
$listeneKey = $this->getListenKey();
var_dump($this->host . $this->path);
$this->wsAcc = new Websocket($this->host . $this->path);
$this->wsAcc->connect(
$onOpen = function () use ($listeneKey) {
$this->wsAcc->push(json_encode(['method' => 'SUBSCRIBE', 'params' => [$listeneKey], 'id' => 1]));
},
$onMessage = function ($msg) {
output($msg);
// $data = json_decode($msg, true);
},
$onClose = null
);
}
private function getListenKey()
{
$res = $this->api->getListenKey([]);
return $res['listenKey'];
}
//获取所有品种资金费率
public function getAllPremium()
{
$res = $this->api->getPremiumIndex();
$res = $this->api->getPremiumIndex([]);
}
}
... ...