commit
b3be7a1f22
|
|
@ -18,26 +18,38 @@ use Msr\LaravelBitunixApi\Requests\Header;
|
||||||
use Msr\LaravelBitunixApi\Requests\PlaceOrderRequestContract;
|
use Msr\LaravelBitunixApi\Requests\PlaceOrderRequestContract;
|
||||||
use Msr\LaravelBitunixApi\Requests\PlacePositionTpSlOrderRequestContract;
|
use Msr\LaravelBitunixApi\Requests\PlacePositionTpSlOrderRequestContract;
|
||||||
use Msr\LaravelBitunixApi\Requests\PlaceTpSlOrderRequestContract;
|
use Msr\LaravelBitunixApi\Requests\PlaceTpSlOrderRequestContract;
|
||||||
|
use Msr\LaravelBitunixApi\Support\BitunixAccountConfig;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginModeRequestContract, FlashClosePositionRequestContract, FutureKLineRequestContract, GetHistoryPositionsRequestContract, GetHistoryTradesRequestContract, GetPendingPositionsRequestContract, GetSingleAccountRequestContract, GetTradingPairsRequestContract, PlaceOrderRequestContract, PlacePositionTpSlOrderRequestContract, PlaceTpSlOrderRequestContract
|
class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginModeRequestContract, FlashClosePositionRequestContract, FutureKLineRequestContract, GetHistoryPositionsRequestContract, GetHistoryTradesRequestContract, GetPendingPositionsRequestContract, GetSingleAccountRequestContract, GetTradingPairsRequestContract, PlaceOrderRequestContract, PlacePositionTpSlOrderRequestContract, PlaceTpSlOrderRequestContract
|
||||||
{
|
{
|
||||||
private Client $publicFutureClient;
|
private Client $publicFutureClient;
|
||||||
|
|
||||||
|
protected BitunixAccountConfig $config;
|
||||||
|
|
||||||
public function __construct(protected RateLimiter $rateLimiter)
|
public function __construct(protected RateLimiter $rateLimiter)
|
||||||
{
|
{
|
||||||
|
$this->config = BitunixAccountConfig::fromConfig();
|
||||||
|
|
||||||
$this->publicFutureClient = new Client([
|
$this->publicFutureClient = new Client([
|
||||||
'base_uri' => config('bitunix-api.future_base_uri').'/api/v1/futures/market/',
|
'base_uri' => ($this->config->baseUrl ?? config('bitunix-api.future_base_uri')).'/api/v1/futures/market/',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function forAccount(BitunixAccountConfig $config): self
|
||||||
|
{
|
||||||
|
$this->config = $config;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
protected function getPrivateFutureClient(array $queryParams = [], array $body = []): Client
|
protected function getPrivateFutureClient(array $queryParams = [], array $body = []): Client
|
||||||
{
|
{
|
||||||
$bodyString = count($body) ? json_encode($body) : '';
|
$bodyString = count($body) ? json_encode($body) : '';
|
||||||
$headers = Header::generateHeaders($queryParams, $bodyString);
|
$headers = Header::generateHeaders($queryParams, $bodyString, $this->config);
|
||||||
|
|
||||||
return new Client([
|
return new Client([
|
||||||
'base_uri' => config('bitunix-api.future_base_uri').'/api/v1/futures/',
|
'base_uri' => ($this->config->baseUrl ?? config('bitunix-api.future_base_uri')).'/api/v1/futures/',
|
||||||
'headers' => $headers,
|
'headers' => $headers,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
@ -409,6 +421,6 @@ class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginMo
|
||||||
|
|
||||||
protected function resolveRateLimitIdentity(): string
|
protected function resolveRateLimitIdentity(): string
|
||||||
{
|
{
|
||||||
return (string) config('bitunix-api.api_key');
|
return (string) ($this->config->accountKey ?? $this->config->apiKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
namespace Msr\LaravelBitunixApi\Requests;
|
namespace Msr\LaravelBitunixApi\Requests;
|
||||||
|
|
||||||
|
use Msr\LaravelBitunixApi\Support\BitunixAccountConfig;
|
||||||
|
|
||||||
class Header
|
class Header
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|
@ -64,13 +66,13 @@ class Header
|
||||||
* 3. Create digest: SHA256(nonce + timestamp + api-key + queryParams + body)
|
* 3. Create digest: SHA256(nonce + timestamp + api-key + queryParams + body)
|
||||||
* 4. Create sign: SHA256(digest + secretKey)
|
* 4. Create sign: SHA256(digest + secretKey)
|
||||||
*/
|
*/
|
||||||
public static function generateSignValue(array $queryParams = [], string $body = '', string $nonce = '', string $timestamp = ''): string
|
public static function generateSignValue(array $queryParams, string $body, string $nonce, string $timestamp, BitunixAccountConfig $config): string
|
||||||
{
|
{
|
||||||
$apiKey = config('bitunix-api.api_key');
|
$apiKey = $config->apiKey;
|
||||||
$apiSecret = config('bitunix-api.api_secret');
|
$apiSecret = $config->apiSecret;
|
||||||
|
|
||||||
if (empty($apiKey) || empty($apiSecret)) {
|
if (empty($apiKey) || empty($apiSecret)) {
|
||||||
throw new \InvalidArgumentException('API key and secret must be configured');
|
throw new \InvalidArgumentException('API key and secret must be provided in config');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 1: Sort query parameters in ascending ASCII order
|
// Step 1: Sort query parameters in ascending ASCII order
|
||||||
|
|
@ -97,18 +99,18 @@ class Header
|
||||||
/**
|
/**
|
||||||
* Generate complete headers for authenticated requests
|
* Generate complete headers for authenticated requests
|
||||||
*/
|
*/
|
||||||
public static function generateHeaders(array $queryParams = [], string $body = ''): array
|
public static function generateHeaders(array $queryParams, string $body, BitunixAccountConfig $config): array
|
||||||
{
|
{
|
||||||
$nonce = self::generateNonce();
|
$nonce = self::generateNonce();
|
||||||
$timestamp = self::generateTimestamp();
|
$timestamp = self::generateTimestamp();
|
||||||
$sign = self::generateSignValue($queryParams, $body, $nonce, $timestamp);
|
$sign = self::generateSignValue($queryParams, $body, $nonce, $timestamp, $config);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'api-key' => config('bitunix-api.api_key'),
|
'api-key' => $config->apiKey,
|
||||||
'sign' => $sign,
|
'sign' => $sign,
|
||||||
'nonce' => $nonce,
|
'nonce' => $nonce,
|
||||||
'timestamp' => $timestamp,
|
'timestamp' => $timestamp,
|
||||||
'language' => config('bitunix-api.language', 'en-US'),
|
'language' => $config->language, //config('bitunix-api.language', 'en-US'),
|
||||||
'Content-Type' => 'application/json',
|
'Content-Type' => 'application/json',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Msr\LaravelBitunixApi\Support;
|
||||||
|
|
||||||
|
class BitunixAccountConfig
|
||||||
|
|
||||||
|
{
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
|
||||||
|
public readonly string $apiKey,
|
||||||
|
|
||||||
|
public readonly string $apiSecret,
|
||||||
|
|
||||||
|
public readonly ?string $accountKey = null,
|
||||||
|
|
||||||
|
public readonly ?string $baseUrl = null,
|
||||||
|
|
||||||
|
public readonly ?string $language = 'en-US',
|
||||||
|
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function fromConfig(): self
|
||||||
|
|
||||||
|
{
|
||||||
|
return new self(
|
||||||
|
|
||||||
|
apiKey: config('bitunix-api.api_key'),
|
||||||
|
|
||||||
|
apiSecret: config('bitunix-api.api_secret'),
|
||||||
|
|
||||||
|
accountKey: config('bitunix-api.account_key', 'default'),
|
||||||
|
|
||||||
|
baseUrl: config('bitunix-api.future_base_uri'),
|
||||||
|
|
||||||
|
language: config('bitunix-api.language', 'en-US'),
|
||||||
|
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue