Compare commits
35 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
3845bf12c1 | |
|
|
f066040415 | |
|
|
8ad4765d5d | |
|
|
bf9bcd4f7e | |
|
|
6730ac8ada | |
|
|
a58ece7076 | |
|
|
289101215d | |
|
|
a24caddba5 | |
|
|
96ee83d67a | |
|
|
71c2d17fa3 | |
|
|
a4a5280bab | |
|
|
a1048d5f5c | |
|
|
21e94df5ff | |
|
|
ad9acbd410 | |
|
|
f04b685496 | |
|
|
8e5b631dc7 | |
|
|
4ce40bd4c8 | |
|
|
85b677ee0e | |
|
|
8ccede169c | |
|
|
82a0ac77f4 | |
|
|
5a6e9cca1d | |
|
|
b40852117f | |
|
|
67a9344d14 | |
|
|
0a8ef0280f | |
|
|
1c69e169f3 | |
|
|
8b416c09e0 | |
|
|
04ecce7bd7 | |
|
|
578fac70a0 | |
|
|
cf59fc1e59 | |
|
|
3846263eb6 | |
|
|
63a45260c9 | |
|
|
27550372df | |
|
|
db7f68ada8 | |
|
|
6059ef32e0 | |
|
|
60eb009213 |
|
|
@ -14,7 +14,7 @@ jobs:
|
||||||
|
|
||||||
- name: Dependabot metadata
|
- name: Dependabot metadata
|
||||||
id: metadata
|
id: metadata
|
||||||
uses: dependabot/fetch-metadata@v2.4.0
|
uses: dependabot/fetch-metadata@v2.5.0
|
||||||
with:
|
with:
|
||||||
github-token: "${{ secrets.GITHUB_TOKEN }}"
|
github-token: "${{ secrets.GITHUB_TOKEN }}"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,6 @@ jobs:
|
||||||
uses: aglipanci/laravel-pint-action@2.6
|
uses: aglipanci/laravel-pint-action@2.6
|
||||||
|
|
||||||
- name: Commit changes
|
- name: Commit changes
|
||||||
uses: stefanzweifel/git-auto-commit-action@v6
|
uses: stefanzweifel/git-auto-commit-action@v7
|
||||||
with:
|
with:
|
||||||
commit_message: Fix styling
|
commit_message: Fix styling
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,9 @@ name: run-tests
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
paths:
|
branches: [main, dev]
|
||||||
- '**.php'
|
pull_request:
|
||||||
- '.github/workflows/run-tests.yml'
|
branches: [main]
|
||||||
- 'phpunit.xml.dist'
|
|
||||||
- 'composer.json'
|
|
||||||
- 'composer.lock'
|
|
||||||
|
|
||||||
concurrency:
|
concurrency:
|
||||||
group: ${{ github.workflow }}-${{ github.ref }}
|
group: ${{ github.workflow }}-${{ github.ref }}
|
||||||
|
|
@ -15,19 +12,20 @@ concurrency:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 5
|
timeout-minutes: 5
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: true
|
fail-fast: true
|
||||||
matrix:
|
matrix:
|
||||||
os: [ubuntu-latest]
|
os: [ubuntu-latest]
|
||||||
php: [8.4, 8.3]
|
php: [8.4]
|
||||||
laravel: [12.*, 11.*]
|
laravel: [12.*, 11.*]
|
||||||
stability: [prefer-stable]
|
stability: [prefer-stable]
|
||||||
include:
|
include:
|
||||||
- laravel: 12.*
|
- laravel: 12.*
|
||||||
|
testbench: 10.*
|
||||||
- laravel: 11.*
|
- laravel: 11.*
|
||||||
- laravel: 10.*
|
testbench: 9.*
|
||||||
|
|
||||||
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
|
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
|
||||||
|
|
||||||
|
|
|
||||||
11
README.md
11
README.md
|
|
@ -2,10 +2,19 @@
|
||||||
|
|
||||||
A Laravel package for interacting with the Bitunix cryptocurrency exchange API.
|
A Laravel package for interacting with the Bitunix cryptocurrency exchange API.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
[](https://packagist.org/packages/msr/laravel-bitunix-api)
|
||||||
|
[](https://github.com/mahdimsr/laravel-bitunix-api/actions/workflows/fix-php-code-style-issues.yml)
|
||||||
|
[](https://github.com/mahdimsr/laravel-bitunix-api/actions/workflows/phpstan.yml)
|
||||||
|
[](https://github.com/mahdimsr/laravel-bitunix-api/actions/workflows/run-tests.yml)
|
||||||
|
[](https://packagist.org/packages/msr/laravel-bitunix-api)
|
||||||
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
composer require msr/laravel-bitunix-api
|
composer require mahdimsr/laravel-bitunix-api
|
||||||
```
|
```
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"name": "mahdimsr/laravel-bitunix-api",
|
"name": "moltox/laravel-bitunix-api",
|
||||||
"description": "composer package for using bitunix api trading",
|
"description": "composer package for using bitunix api trading",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"msr",
|
"msr",
|
||||||
|
|
@ -13,17 +13,22 @@
|
||||||
"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": "^11.0||^12.0||^10.0"
|
"illuminate/contracts": "^10.0||^11.0||^12.0||^13.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"laravel/pint": "^1.14",
|
|
||||||
"nunomaduro/collision": "^8.8",
|
|
||||||
"larastan/larastan": "^3.0",
|
"larastan/larastan": "^3.0",
|
||||||
|
"laravel/pint": "^1.29",
|
||||||
|
"nunomaduro/collision": "^8.8",
|
||||||
"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",
|
||||||
|
|
@ -50,7 +55,8 @@
|
||||||
"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,
|
||||||
|
|
@ -69,7 +75,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"version": "1.0.0",
|
"version": "1.2.3",
|
||||||
"minimum-stability": "stable",
|
"minimum-stability": "stable",
|
||||||
"prefer-stable": true
|
"prefer-stable": true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Msr\LaravelBitunixApi\Contracts;
|
||||||
|
|
||||||
|
interface RateLimiterContract
|
||||||
|
{
|
||||||
|
public function throttle(
|
||||||
|
string $bucket,
|
||||||
|
string $identity,
|
||||||
|
int $maxRequests,
|
||||||
|
float $perSeconds
|
||||||
|
): void;
|
||||||
|
}
|
||||||
|
|
@ -3,23 +3,28 @@
|
||||||
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\Header;
|
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 Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginModeRequestContract, FlashClosePositionRequestContract, FutureKLineRequestContract, GetPendingPositionsRequestContract, GetSingleAccountRequestContract, PlaceOrderRequestContract, PlacePositionTpSlOrderRequestContract, PlaceTpSlOrderRequestContract
|
class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginModeRequestContract, FlashClosePositionRequestContract, FutureKLineRequestContract, GetHistoryPositionsRequestContract, GetHistoryTradesRequestContract, GetPendingPositionsRequestContract, GetSingleAccountRequestContract, GetTradingPairsRequestContract, PlaceOrderRequestContract, PlacePositionTpSlOrderRequestContract, PlaceTpSlOrderRequestContract
|
||||||
{
|
{
|
||||||
private Client $publicFutureClient;
|
private Client $publicFutureClient;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct(protected RateLimiter $rateLimiter)
|
||||||
{
|
{
|
||||||
$this->publicFutureClient = new Client([
|
$this->publicFutureClient = new Client([
|
||||||
'base_uri' => config('bitunix-api.future_base_uri').'/api/v1/futures/market/',
|
'base_uri' => config('bitunix-api.future_base_uri').'/api/v1/futures/market/',
|
||||||
|
|
@ -37,8 +42,14 @@ class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginMo
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFutureKline(string $symbol, string $interval, int $limit = 100, ?int $startTime = null, ?int $endTime = null, string $type = 'LAST_PRICE'): ResponseInterface
|
public function getFutureKline(
|
||||||
{
|
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,
|
||||||
|
|
@ -53,6 +64,21 @@ class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginMo
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getTradingPairs(?string $symbols = null): ResponseInterface
|
||||||
|
{
|
||||||
|
$queryParams = [];
|
||||||
|
|
||||||
|
if ($symbols !== null) {
|
||||||
|
$queryParams['symbols'] = $symbols;
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = $this->publicFutureClient->get('trading_pairs', [
|
||||||
|
'query' => $queryParams,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
public function changeLeverage(string $symbol, string $marginCoin, int $leverage): ResponseInterface
|
public function changeLeverage(string $symbol, string $marginCoin, int $leverage): ResponseInterface
|
||||||
{
|
{
|
||||||
$body = [
|
$body = [
|
||||||
|
|
@ -172,6 +198,54 @@ 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 = [];
|
||||||
|
|
@ -191,6 +265,45 @@ 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 = [
|
||||||
|
|
@ -289,10 +402,13 @@ class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginMo
|
||||||
$body['slStopType'] = $slStopType;
|
$body['slStopType'] = $slStopType;
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = $this->getPrivateFutureClient([], $body)->post('tpsl/position/place_order', [
|
return $this->getPrivateFutureClient([], $body)->post('tpsl/position/place_order', [
|
||||||
'json' => $body,
|
'json' => $body,
|
||||||
]);
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
return $response;
|
protected function resolveRateLimitIdentity(): string
|
||||||
|
{
|
||||||
|
return (string) config('bitunix-api.api_key');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,16 +2,22 @@
|
||||||
|
|
||||||
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\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;
|
||||||
|
|
||||||
|
|
@ -45,5 +51,14 @@ class LaravelBitunixApiServiceProvider extends PackageServiceProvider
|
||||||
$this->app->bind(GetSingleAccountRequestContract::class, LaravelBitunixApi::class);
|
$this->app->bind(GetSingleAccountRequestContract::class, LaravelBitunixApi::class);
|
||||||
$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(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()
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
<?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;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
<?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;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Msr\LaravelBitunixApi\Requests;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
|
interface GetTradingPairsRequestContract
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Get future trading pair details
|
||||||
|
*
|
||||||
|
* @param string|null $symbols Trading pairs, comma-separated (e.g., "BTCUSDT,ETHUSDT,XRPUSDT")
|
||||||
|
*/
|
||||||
|
public function getTradingPairs(?string $symbols = null): ResponseInterface;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,56 @@
|
||||||
|
<?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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,108 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Msr\LaravelBitunixApi\Requests\GetTradingPairsRequestContract;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
config([
|
||||||
|
'bitunix-api.future_base_uri' => 'https://fapi.bitunix.com/',
|
||||||
|
'bitunix-api.api_key' => 'test-api-key',
|
||||||
|
'bitunix-api.api_secret' => 'test-secret-key',
|
||||||
|
'bitunix-api.language' => 'en-US',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('check trading pairs request response code', function () {
|
||||||
|
$bootedClass = app(GetTradingPairsRequestContract::class);
|
||||||
|
$response = $bootedClass->getTradingPairs();
|
||||||
|
expect($response->getStatusCode())
|
||||||
|
->toBe(200)
|
||||||
|
->and(json_decode($response->getBody()->getContents(), true))
|
||||||
|
->toHaveKeys(['code', 'data', 'msg']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can get trading pairs with specific symbols', function () {
|
||||||
|
$api = app(GetTradingPairsRequestContract::class);
|
||||||
|
|
||||||
|
expect(fn () => $api->getTradingPairs('BTCUSDT,ETHUSDT'))
|
||||||
|
->not->toThrow(Exception::class);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can get trading pairs without symbols parameter', function () {
|
||||||
|
$api = app(GetTradingPairsRequestContract::class);
|
||||||
|
|
||||||
|
expect(fn () => $api->getTradingPairs())
|
||||||
|
->not->toThrow(Exception::class);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('validates get trading pairs method exists', function () {
|
||||||
|
$api = app(GetTradingPairsRequestContract::class);
|
||||||
|
|
||||||
|
expect(method_exists($api, 'getTradingPairs'))->toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can handle single symbol', function () {
|
||||||
|
$api = app(GetTradingPairsRequestContract::class);
|
||||||
|
|
||||||
|
expect(fn () => $api->getTradingPairs('BTCUSDT'))
|
||||||
|
->not->toThrow(Exception::class);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can handle multiple symbols', function () {
|
||||||
|
$api = app(GetTradingPairsRequestContract::class);
|
||||||
|
|
||||||
|
$symbols = 'BTCUSDT,ETHUSDT,XRPUSDT';
|
||||||
|
expect(fn () => $api->getTradingPairs($symbols))
|
||||||
|
->not->toThrow(Exception::class);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('validates trading pairs response structure', function () {
|
||||||
|
$api = app(GetTradingPairsRequestContract::class);
|
||||||
|
|
||||||
|
$response = $api->getTradingPairs();
|
||||||
|
$data = json_decode($response->getBody()->getContents(), true);
|
||||||
|
|
||||||
|
expect($response->getStatusCode())->toBe(200)
|
||||||
|
->and($data)->toHaveKeys(['code', 'data', 'msg']);
|
||||||
|
|
||||||
|
if (isset($data['data']) && is_array($data['data']) && count($data['data']) > 0) {
|
||||||
|
$firstPair = $data['data'][0];
|
||||||
|
expect($firstPair)->toHaveKeys([
|
||||||
|
'symbol',
|
||||||
|
'base',
|
||||||
|
'quote',
|
||||||
|
'minTradeVolume',
|
||||||
|
'minBuyPriceOffset',
|
||||||
|
'maxSellPriceOffset',
|
||||||
|
'maxLimitOrderVolume',
|
||||||
|
'maxMarketOrderVolume',
|
||||||
|
'basePrecision',
|
||||||
|
'quotePrecision',
|
||||||
|
'maxLeverage',
|
||||||
|
'minLeverage',
|
||||||
|
'defaultLeverage',
|
||||||
|
'defaultMarginMode',
|
||||||
|
'priceProtectScope',
|
||||||
|
'symbolStatus',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can handle different symbol formats', function () {
|
||||||
|
$api = app(GetTradingPairsRequestContract::class);
|
||||||
|
|
||||||
|
// Test with uppercase symbols
|
||||||
|
expect(fn () => $api->getTradingPairs('BTCUSDT'))
|
||||||
|
->not->toThrow(Exception::class);
|
||||||
|
|
||||||
|
// Test with lowercase symbols
|
||||||
|
expect(fn () => $api->getTradingPairs('btcusdt'))
|
||||||
|
->not->toThrow(Exception::class);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can handle empty symbols parameter', function () {
|
||||||
|
$api = app(GetTradingPairsRequestContract::class);
|
||||||
|
|
||||||
|
// Test with null (should return all trading pairs)
|
||||||
|
expect(fn () => $api->getTradingPairs(null))
|
||||||
|
->not->toThrow(Exception::class);
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue