Compare commits
1 Commits
main
...
dependabot
| Author | SHA1 | Date |
|---|---|---|
|
|
cd51c513d7 |
|
|
@ -14,7 +14,7 @@ jobs:
|
||||||
|
|
||||||
- name: Dependabot metadata
|
- name: Dependabot metadata
|
||||||
id: metadata
|
id: metadata
|
||||||
uses: dependabot/fetch-metadata@v2.5.0
|
uses: dependabot/fetch-metadata@v2.4.0
|
||||||
with:
|
with:
|
||||||
github-token: "${{ secrets.GITHUB_TOKEN }}"
|
github-token: "${{ secrets.GITHUB_TOKEN }}"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ jobs:
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v5
|
uses: actions/checkout@v6
|
||||||
with:
|
with:
|
||||||
ref: ${{ github.head_ref }}
|
ref: ${{ github.head_ref }}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 5
|
timeout-minutes: 5
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v6
|
||||||
|
|
||||||
- name: Setup PHP
|
- name: Setup PHP
|
||||||
uses: shivammathur/setup-php@v2
|
uses: shivammathur/setup-php@v2
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ jobs:
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v5
|
uses: actions/checkout@v6
|
||||||
|
|
||||||
- name: Setup PHP
|
- name: Setup PHP
|
||||||
uses: shivammathur/setup-php@v2
|
uses: shivammathur/setup-php@v2
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"name": "moltox/laravel-bitunix-api",
|
"name": "mahdimsr/laravel-bitunix-api",
|
||||||
"description": "composer package for using bitunix api trading",
|
"description": "composer package for using bitunix api trading",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"msr",
|
"msr",
|
||||||
|
|
@ -13,22 +13,17 @@
|
||||||
"name": "mahdi mansouri",
|
"name": "mahdi mansouri",
|
||||||
"email": "mahdi.msr4@gmail.com",
|
"email": "mahdi.msr4@gmail.com",
|
||||||
"role": "Developer"
|
"role": "Developer"
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "Maik Mueller ",
|
|
||||||
"email": "mueller.maik1@gmail.com",
|
|
||||||
"role": "Developer"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^8.4",
|
"php": "^8.4",
|
||||||
"spatie/laravel-package-tools": "^1.16",
|
"spatie/laravel-package-tools": "^1.16",
|
||||||
"illuminate/contracts": "^10.0||^11.0||^12.0||^13.0"
|
"illuminate/contracts": "^11.0||^12.0||^10.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"larastan/larastan": "^3.0",
|
"laravel/pint": "^1.14",
|
||||||
"laravel/pint": "^1.29",
|
|
||||||
"nunomaduro/collision": "^8.8",
|
"nunomaduro/collision": "^8.8",
|
||||||
|
"larastan/larastan": "^3.0",
|
||||||
"orchestra/testbench": "^10.0.0||^9.0.0",
|
"orchestra/testbench": "^10.0.0||^9.0.0",
|
||||||
"pestphp/pest": "^4.0",
|
"pestphp/pest": "^4.0",
|
||||||
"pestphp/pest-plugin-arch": "^4.0",
|
"pestphp/pest-plugin-arch": "^4.0",
|
||||||
|
|
@ -55,8 +50,7 @@
|
||||||
"analyse": "vendor/bin/phpstan analyse",
|
"analyse": "vendor/bin/phpstan analyse",
|
||||||
"test": "vendor/bin/pest",
|
"test": "vendor/bin/pest",
|
||||||
"test-coverage": "vendor/bin/pest --coverage",
|
"test-coverage": "vendor/bin/pest --coverage",
|
||||||
"format": "vendor/bin/pint",
|
"format": "vendor/bin/pint"
|
||||||
"pint": "vendor/bin/pint --ansi"
|
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"sort-packages": true,
|
"sort-packages": true,
|
||||||
|
|
@ -75,7 +69,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"version": "1.2.5",
|
"version": "1.1.0",
|
||||||
"minimum-stability": "stable",
|
"minimum-stability": "stable",
|
||||||
"prefer-stable": true
|
"prefer-stable": true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Msr\LaravelBitunixApi\Contracts;
|
|
||||||
|
|
||||||
interface RateLimiterContract
|
|
||||||
{
|
|
||||||
public function throttle(
|
|
||||||
string $bucket,
|
|
||||||
string $identity,
|
|
||||||
int $maxRequests,
|
|
||||||
float $perSeconds
|
|
||||||
): void;
|
|
||||||
}
|
|
||||||
|
|
@ -3,14 +3,10 @@
|
||||||
namespace Msr\LaravelBitunixApi;
|
namespace Msr\LaravelBitunixApi;
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
use GuzzleHttp\Exception\GuzzleException;
|
|
||||||
use Msr\LaravelBitunixApi\Contracts\RateLimiterContract as RateLimiter;
|
|
||||||
use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract;
|
use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract;
|
||||||
use Msr\LaravelBitunixApi\Requests\ChangeMarginModeRequestContract;
|
use Msr\LaravelBitunixApi\Requests\ChangeMarginModeRequestContract;
|
||||||
use Msr\LaravelBitunixApi\Requests\FlashClosePositionRequestContract;
|
use Msr\LaravelBitunixApi\Requests\FlashClosePositionRequestContract;
|
||||||
use Msr\LaravelBitunixApi\Requests\FutureKLineRequestContract;
|
use Msr\LaravelBitunixApi\Requests\FutureKLineRequestContract;
|
||||||
use Msr\LaravelBitunixApi\Requests\GetHistoryPositionsRequestContract;
|
|
||||||
use Msr\LaravelBitunixApi\Requests\GetHistoryTradesRequestContract;
|
|
||||||
use Msr\LaravelBitunixApi\Requests\GetPendingPositionsRequestContract;
|
use Msr\LaravelBitunixApi\Requests\GetPendingPositionsRequestContract;
|
||||||
use Msr\LaravelBitunixApi\Requests\GetSingleAccountRequestContract;
|
use Msr\LaravelBitunixApi\Requests\GetSingleAccountRequestContract;
|
||||||
use Msr\LaravelBitunixApi\Requests\GetTradingPairsRequestContract;
|
use Msr\LaravelBitunixApi\Requests\GetTradingPairsRequestContract;
|
||||||
|
|
@ -18,50 +14,32 @@ 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, GetPendingPositionsRequestContract, GetSingleAccountRequestContract, GetTradingPairsRequestContract, PlaceOrderRequestContract, PlacePositionTpSlOrderRequestContract, PlaceTpSlOrderRequestContract
|
||||||
{
|
{
|
||||||
private Client $publicFutureClient;
|
private Client $publicFutureClient;
|
||||||
|
|
||||||
protected BitunixAccountConfig $config;
|
public function __construct()
|
||||||
|
|
||||||
public function __construct(protected RateLimiter $rateLimiter)
|
|
||||||
{
|
{
|
||||||
$this->config = BitunixAccountConfig::fromConfig();
|
|
||||||
|
|
||||||
$this->publicFutureClient = new Client([
|
$this->publicFutureClient = new Client([
|
||||||
'base_uri' => ($this->config->baseUrl ?? config('bitunix-api.future_base_uri')).'/api/v1/futures/market/',
|
'base_uri' => 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, $this->config);
|
$headers = Header::generateHeaders($queryParams, $bodyString);
|
||||||
|
|
||||||
return new Client([
|
return new Client([
|
||||||
'base_uri' => ($this->config->baseUrl ?? config('bitunix-api.future_base_uri')).'/api/v1/futures/',
|
'base_uri' => config('bitunix-api.future_base_uri').'/api/v1/futures/',
|
||||||
'headers' => $headers,
|
'headers' => $headers,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFutureKline(
|
public function getFutureKline(string $symbol, string $interval, int $limit = 100, ?int $startTime = null, ?int $endTime = null, string $type = 'LAST_PRICE'): ResponseInterface
|
||||||
string $symbol,
|
{
|
||||||
string $interval,
|
|
||||||
int $limit = 100,
|
|
||||||
?int $startTime = null,
|
|
||||||
?int $endTime = null,
|
|
||||||
string $type = 'LAST_PRICE'
|
|
||||||
): ResponseInterface {
|
|
||||||
$response = $this->publicFutureClient->get('kline', [
|
$response = $this->publicFutureClient->get('kline', [
|
||||||
'query' => [
|
'query' => [
|
||||||
'symbol' => $symbol,
|
'symbol' => $symbol,
|
||||||
|
|
@ -210,54 +188,6 @@ class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginMo
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getHistoryTrades(
|
|
||||||
?string $symbol = null,
|
|
||||||
?string $orderId = null,
|
|
||||||
?string $positionId = null,
|
|
||||||
?int $startTime = null,
|
|
||||||
?int $endTime = null,
|
|
||||||
int $skip = 0,
|
|
||||||
int $limit = 100
|
|
||||||
): ResponseInterface {
|
|
||||||
$this->rateLimiter->throttle(
|
|
||||||
'futures::get_history_trades',
|
|
||||||
$this->resolveRateLimitIdentity(),
|
|
||||||
maxRequests: 8,
|
|
||||||
perSeconds: 1
|
|
||||||
);
|
|
||||||
|
|
||||||
$queryParams = [
|
|
||||||
'skip' => $skip,
|
|
||||||
'limit' => $limit,
|
|
||||||
];
|
|
||||||
|
|
||||||
if ($symbol !== null) {
|
|
||||||
$queryParams['symbol'] = $symbol;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($orderId !== null) {
|
|
||||||
$queryParams['orderId'] = $orderId;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($positionId !== null) {
|
|
||||||
$queryParams['positionId'] = $positionId;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($startTime !== null) {
|
|
||||||
$queryParams['startTime'] = $startTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($endTime !== null) {
|
|
||||||
$queryParams['endTime'] = $endTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
$response = $this->getPrivateFutureClient($queryParams, [])->get('trade/get_history_trades', [
|
|
||||||
'query' => $queryParams,
|
|
||||||
]);
|
|
||||||
|
|
||||||
return $response;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getPendingPositions(?string $symbol = null, ?string $positionId = null): ResponseInterface
|
public function getPendingPositions(?string $symbol = null, ?string $positionId = null): ResponseInterface
|
||||||
{
|
{
|
||||||
$queryParams = [];
|
$queryParams = [];
|
||||||
|
|
@ -277,45 +207,6 @@ class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginMo
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws GuzzleException
|
|
||||||
*/
|
|
||||||
public function getHistoryPositions(
|
|
||||||
?string $symbol = null,
|
|
||||||
?int $startTime = null,
|
|
||||||
?int $endTime = null,
|
|
||||||
int $skip = 0,
|
|
||||||
int $limit = 100
|
|
||||||
): ResponseInterface {
|
|
||||||
$this->rateLimiter->throttle(
|
|
||||||
'futures::get_history_positions',
|
|
||||||
$this->resolveRateLimitIdentity(),
|
|
||||||
maxRequests: 8,
|
|
||||||
perSeconds: 1
|
|
||||||
);
|
|
||||||
|
|
||||||
$queryParams = [
|
|
||||||
'skip' => $skip,
|
|
||||||
'limit' => $limit,
|
|
||||||
];
|
|
||||||
|
|
||||||
if ($symbol !== null) {
|
|
||||||
$queryParams['symbol'] = $symbol;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($startTime !== null) {
|
|
||||||
$queryParams['startTime'] = $startTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($endTime !== null) {
|
|
||||||
$queryParams['endTime'] = $endTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->getPrivateFutureClient($queryParams, [])->get('position/get_history_positions', [
|
|
||||||
'query' => $queryParams,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getSingleAccount(string $marginCoin): ResponseInterface
|
public function getSingleAccount(string $marginCoin): ResponseInterface
|
||||||
{
|
{
|
||||||
$queryParams = [
|
$queryParams = [
|
||||||
|
|
@ -414,13 +305,10 @@ class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginMo
|
||||||
$body['slStopType'] = $slStopType;
|
$body['slStopType'] = $slStopType;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->getPrivateFutureClient([], $body)->post('tpsl/position/place_order', [
|
$response = $this->getPrivateFutureClient([], $body)->post('tpsl/position/place_order', [
|
||||||
'json' => $body,
|
'json' => $body,
|
||||||
]);
|
]);
|
||||||
}
|
|
||||||
|
|
||||||
protected function resolveRateLimitIdentity(): string
|
return $response;
|
||||||
{
|
|
||||||
return (string) ($this->config->accountKey ?? $this->config->apiKey);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,22 +2,17 @@
|
||||||
|
|
||||||
namespace Msr\LaravelBitunixApi;
|
namespace Msr\LaravelBitunixApi;
|
||||||
|
|
||||||
use Illuminate\Contracts\Cache\Factory as CacheFactory;
|
|
||||||
use Msr\LaravelBitunixApi\Commands\LaravelBitunixApiCommand;
|
use Msr\LaravelBitunixApi\Commands\LaravelBitunixApiCommand;
|
||||||
use Msr\LaravelBitunixApi\Contracts\RateLimiterContract;
|
|
||||||
use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract;
|
use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract;
|
||||||
use Msr\LaravelBitunixApi\Requests\ChangeMarginModeRequestContract;
|
use Msr\LaravelBitunixApi\Requests\ChangeMarginModeRequestContract;
|
||||||
use Msr\LaravelBitunixApi\Requests\FlashClosePositionRequestContract;
|
use Msr\LaravelBitunixApi\Requests\FlashClosePositionRequestContract;
|
||||||
use Msr\LaravelBitunixApi\Requests\FutureKLineRequestContract;
|
use Msr\LaravelBitunixApi\Requests\FutureKLineRequestContract;
|
||||||
use Msr\LaravelBitunixApi\Requests\GetHistoryPositionsRequestContract;
|
|
||||||
use Msr\LaravelBitunixApi\Requests\GetHistoryTradesRequestContract;
|
|
||||||
use Msr\LaravelBitunixApi\Requests\GetPendingPositionsRequestContract;
|
use Msr\LaravelBitunixApi\Requests\GetPendingPositionsRequestContract;
|
||||||
use Msr\LaravelBitunixApi\Requests\GetSingleAccountRequestContract;
|
use Msr\LaravelBitunixApi\Requests\GetSingleAccountRequestContract;
|
||||||
use Msr\LaravelBitunixApi\Requests\GetTradingPairsRequestContract;
|
use Msr\LaravelBitunixApi\Requests\GetTradingPairsRequestContract;
|
||||||
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\CacheRateLimiter;
|
|
||||||
use Spatie\LaravelPackageTools\Package;
|
use Spatie\LaravelPackageTools\Package;
|
||||||
use Spatie\LaravelPackageTools\PackageServiceProvider;
|
use Spatie\LaravelPackageTools\PackageServiceProvider;
|
||||||
|
|
||||||
|
|
@ -52,13 +47,5 @@ class LaravelBitunixApiServiceProvider extends PackageServiceProvider
|
||||||
$this->app->bind(PlaceTpSlOrderRequestContract::class, LaravelBitunixApi::class);
|
$this->app->bind(PlaceTpSlOrderRequestContract::class, LaravelBitunixApi::class);
|
||||||
$this->app->bind(PlacePositionTpSlOrderRequestContract::class, LaravelBitunixApi::class);
|
$this->app->bind(PlacePositionTpSlOrderRequestContract::class, LaravelBitunixApi::class);
|
||||||
$this->app->bind(GetTradingPairsRequestContract::class, LaravelBitunixApi::class);
|
$this->app->bind(GetTradingPairsRequestContract::class, LaravelBitunixApi::class);
|
||||||
$this->app->bind(GetHistoryTradesRequestContract::class, LaravelBitunixApi::class);
|
|
||||||
$this->app->bind(GetHistoryPositionsRequestContract::class, LaravelBitunixApi::class);
|
|
||||||
|
|
||||||
$this->app->bind(RateLimiterContract::class, function ($app) {
|
|
||||||
return new CacheRateLimiter(
|
|
||||||
$app->make(CacheFactory::class)->store()
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Msr\LaravelBitunixApi\Requests;
|
|
||||||
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
|
||||||
|
|
||||||
interface GetHistoryPositionsRequestContract
|
|
||||||
{
|
|
||||||
public function getHistoryPositions(
|
|
||||||
?string $symbol = null,
|
|
||||||
?int $startTime = null,
|
|
||||||
?int $endTime = null,
|
|
||||||
int $skip = 0,
|
|
||||||
int $limit = 100
|
|
||||||
): ResponseInterface;
|
|
||||||
}
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Msr\LaravelBitunixApi\Requests;
|
|
||||||
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
|
||||||
|
|
||||||
interface GetHistoryTradesRequestContract
|
|
||||||
{
|
|
||||||
public function getHistoryTrades(
|
|
||||||
?string $symbol = null,
|
|
||||||
?string $orderId = null,
|
|
||||||
?string $positionId = null,
|
|
||||||
?int $startTime = null,
|
|
||||||
?int $endTime = null,
|
|
||||||
int $skip = 0,
|
|
||||||
int $limit = 100
|
|
||||||
): ResponseInterface;
|
|
||||||
}
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
namespace Msr\LaravelBitunixApi\Requests;
|
namespace Msr\LaravelBitunixApi\Requests;
|
||||||
|
|
||||||
use Msr\LaravelBitunixApi\Support\BitunixAccountConfig;
|
|
||||||
|
|
||||||
class Header
|
class Header
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|
@ -66,13 +64,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, BitunixAccountConfig $config): string
|
public static function generateSignValue(array $queryParams = [], string $body = '', string $nonce = '', string $timestamp = ''): string
|
||||||
{
|
{
|
||||||
$apiKey = $config->apiKey;
|
$apiKey = config('bitunix-api.api_key');
|
||||||
$apiSecret = $config->apiSecret;
|
$apiSecret = config('bitunix-api.api_secret');
|
||||||
|
|
||||||
if (empty($apiKey) || empty($apiSecret)) {
|
if (empty($apiKey) || empty($apiSecret)) {
|
||||||
throw new \InvalidArgumentException('API key and secret must be provided in config');
|
throw new \InvalidArgumentException('API key and secret must be configured');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 1: Sort query parameters in ascending ASCII order
|
// Step 1: Sort query parameters in ascending ASCII order
|
||||||
|
|
@ -99,18 +97,18 @@ class Header
|
||||||
/**
|
/**
|
||||||
* Generate complete headers for authenticated requests
|
* Generate complete headers for authenticated requests
|
||||||
*/
|
*/
|
||||||
public static function generateHeaders(array $queryParams, string $body, BitunixAccountConfig $config): array
|
public static function generateHeaders(array $queryParams = [], string $body = ''): array
|
||||||
{
|
{
|
||||||
$nonce = self::generateNonce();
|
$nonce = self::generateNonce();
|
||||||
$timestamp = self::generateTimestamp();
|
$timestamp = self::generateTimestamp();
|
||||||
$sign = self::generateSignValue($queryParams, $body, $nonce, $timestamp, $config);
|
$sign = self::generateSignValue($queryParams, $body, $nonce, $timestamp);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'api-key' => $config->apiKey,
|
'api-key' => config('bitunix-api.api_key'),
|
||||||
'sign' => $sign,
|
'sign' => $sign,
|
||||||
'nonce' => $nonce,
|
'nonce' => $nonce,
|
||||||
'timestamp' => $timestamp,
|
'timestamp' => $timestamp,
|
||||||
'language' => $config->language, //config('bitunix-api.language', 'en-US'),
|
'language' => config('bitunix-api.language', 'en-US'),
|
||||||
'Content-Type' => 'application/json',
|
'Content-Type' => 'application/json',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,41 +0,0 @@
|
||||||
<?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'),
|
|
||||||
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,56 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Msr\LaravelBitunixApi\Support;
|
|
||||||
|
|
||||||
use Illuminate\Contracts\Cache\Repository;
|
|
||||||
use Msr\LaravelBitunixApi\Contracts\RateLimiterContract;
|
|
||||||
|
|
||||||
class CacheRateLimiter implements RateLimiterContract
|
|
||||||
{
|
|
||||||
public function __construct(
|
|
||||||
protected Repository $cache,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
public function throttle(
|
|
||||||
string $bucket,
|
|
||||||
string $identity,
|
|
||||||
int $maxRequests,
|
|
||||||
float $perSeconds
|
|
||||||
): void {
|
|
||||||
if ($maxRequests <= 0 || $perSeconds <= 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$cacheKey = $this->makeCacheKey($bucket, $identity);
|
|
||||||
$minIntervalMicroseconds = (int) (($perSeconds / $maxRequests) * 1_000_000);
|
|
||||||
|
|
||||||
$lastRequestAt = $this->cache->get($cacheKey);
|
|
||||||
|
|
||||||
if (is_numeric($lastRequestAt)) {
|
|
||||||
$elapsedMicroseconds = (int) ((microtime(true) - (float) $lastRequestAt) * 1_000_000);
|
|
||||||
|
|
||||||
if ($elapsedMicroseconds < $minIntervalMicroseconds) {
|
|
||||||
$sleepMicroseconds = $minIntervalMicroseconds - $elapsedMicroseconds;
|
|
||||||
|
|
||||||
if ($sleepMicroseconds > 0) {
|
|
||||||
usleep($sleepMicroseconds);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->cache->put(
|
|
||||||
$cacheKey,
|
|
||||||
microtime(true),
|
|
||||||
now()->addSeconds((int) max(1, ceil($perSeconds * 2)))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function makeCacheKey(string $bucket, string $identity): string
|
|
||||||
{
|
|
||||||
return sprintf(
|
|
||||||
'laravel_bitunix_api:rate_limit:%s:%s',
|
|
||||||
$bucket,
|
|
||||||
sha1($identity)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue