Compare commits

...

19 Commits
dev ... main

Author SHA1 Message Date
Maik Müller 3845bf12c1 Done 2026-04-12 19:59:00 +02:00
Maik Müller f066040415 Add CacheRateLimiter 2026-04-12 19:47:10 +02:00
Maik Müller 8ad4765d5d Merge branch 'refs/heads/1.2.2'
# Conflicts:
#	src/LaravelBitunixApiServiceProvider.php
2026-04-12 19:46:48 +02:00
Maik Müller bf9bcd4f7e Add CacheRateLimiter 2026-04-12 19:43:56 +02:00
Maik Müller 6730ac8ada Add getHistoryPositions and getHistoryTrades endpoints to Futures API 2026-04-12 19:32:14 +02:00
moltox a58ece7076 Merge pull request '1.2.2' (#1) from 1.2.2 into main
Reviewed-on: #1
2026-04-11 21:44:13 +00:00
Maik Müller 289101215d change version to 1.2.2 2026-04-11 23:43:37 +02:00
Maik Müller a24caddba5 change version to 1.2.1 2026-04-11 23:41:23 +02:00
Maik Müller 96ee83d67a change version to 1.2.0 2026-04-11 23:40:26 +02:00
Maik Müller 71c2d17fa3 Change structure. 2026-04-11 16:20:35 +02:00
github-actions[bot] a4a5280bab
Merge pull request #13 from mahdimsr/dependabot/github_actions/dependabot/fetch-metadata-2.5.0
build(deps): bump dependabot/fetch-metadata from 2.4.0 to 2.5.0
2026-01-05 21:36:58 +00:00
dependabot[bot] a1048d5f5c
build(deps): bump dependabot/fetch-metadata from 2.4.0 to 2.5.0
Bumps [dependabot/fetch-metadata](https://github.com/dependabot/fetch-metadata) from 2.4.0 to 2.5.0.
- [Release notes](https://github.com/dependabot/fetch-metadata/releases)
- [Commits](https://github.com/dependabot/fetch-metadata/compare/v2.4.0...v2.5.0)

---
updated-dependencies:
- dependency-name: dependabot/fetch-metadata
  dependency-version: 2.5.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-01-05 21:36:45 +00:00
mahdi mansouri 21e94df5ff
Merge pull request #11 from mahdimsr/dev
dev: match composer version with release tag
2025-11-23 16:07:53 +03:30
mahdi mansouri ad9acbd410
Merge pull request #8 from mahdimsr/dependabot/github_actions/stefanzweifel/git-auto-commit-action-7
build(deps): bump stefanzweifel/git-auto-commit-action from 6 to 7
2025-11-23 16:05:55 +03:30
mahdi msr 8e5b631dc7 main: match composer version with release tag 2025-11-23 16:03:04 +03:30
mahdi mansouri 4ce40bd4c8
Merge pull request #10 from mahdimsr/dev
Add get trading pair request
2025-11-23 15:54:23 +03:30
dependabot[bot] b40852117f
build(deps): bump stefanzweifel/git-auto-commit-action from 6 to 7
Bumps [stefanzweifel/git-auto-commit-action](https://github.com/stefanzweifel/git-auto-commit-action) from 6 to 7.
- [Release notes](https://github.com/stefanzweifel/git-auto-commit-action/releases)
- [Changelog](https://github.com/stefanzweifel/git-auto-commit-action/blob/master/CHANGELOG.md)
- [Commits](https://github.com/stefanzweifel/git-auto-commit-action/compare/v6...v7)

---
updated-dependencies:
- dependency-name: stefanzweifel/git-auto-commit-action
  dependency-version: '7'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-10-13 21:39:46 +00:00
mahdi mansouri 67a9344d14
Merge pull request #7 from mahdimsr/dev
Update README.md
2025-09-30 10:36:47 +03:30
mahdi mansouri 1c69e169f3
Update README.md 2025-09-29 04:41:47 +03:30
10 changed files with 245 additions and 14 deletions

View File

@ -14,7 +14,7 @@ jobs:
- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@v2.4.0
uses: dependabot/fetch-metadata@v2.5.0
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

View File

@ -23,6 +23,6 @@ jobs:
uses: aglipanci/laravel-pint-action@2.6
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v6
uses: stefanzweifel/git-auto-commit-action@v7
with:
commit_message: Fix styling

View File

@ -2,6 +2,15 @@
A Laravel package for interacting with the Bitunix cryptocurrency exchange API.
---
[![Latest Stable Version](https://poser.pugx.org/mahdimsr/laravel-bitunix-api/v/stable)](https://packagist.org/packages/msr/laravel-bitunix-api)
[![Fix PHP code style issues](https://github.com/mahdimsr/laravel-bitunix-api/actions/workflows/fix-php-code-style-issues.yml/badge.svg)](https://github.com/mahdimsr/laravel-bitunix-api/actions/workflows/fix-php-code-style-issues.yml)
[![PHPStan](https://github.com/mahdimsr/laravel-bitunix-api/actions/workflows/phpstan.yml/badge.svg)](https://github.com/mahdimsr/laravel-bitunix-api/actions/workflows/phpstan.yml)
[![run-tests](https://github.com/mahdimsr/laravel-bitunix-api/actions/workflows/run-tests.yml/badge.svg)](https://github.com/mahdimsr/laravel-bitunix-api/actions/workflows/run-tests.yml)
[![License](https://poser.pugx.org/mahdimsr/laravel-bitunix-api/license)](https://packagist.org/packages/msr/laravel-bitunix-api)
## Installation
```bash

View File

@ -1,5 +1,5 @@
{
"name": "mahdimsr/laravel-bitunix-api",
"name": "moltox/laravel-bitunix-api",
"description": "composer package for using bitunix api trading",
"keywords": [
"msr",
@ -13,17 +13,22 @@
"name": "mahdi mansouri",
"email": "mahdi.msr4@gmail.com",
"role": "Developer"
},
{
"name": "Maik Mueller ",
"email": "mueller.maik1@gmail.com",
"role": "Developer"
}
],
"require": {
"php": "^8.4",
"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": {
"laravel/pint": "^1.14",
"nunomaduro/collision": "^8.8",
"larastan/larastan": "^3.0",
"laravel/pint": "^1.29",
"nunomaduro/collision": "^8.8",
"orchestra/testbench": "^10.0.0||^9.0.0",
"pestphp/pest": "^4.0",
"pestphp/pest-plugin-arch": "^4.0",
@ -50,7 +55,8 @@
"analyse": "vendor/bin/phpstan analyse",
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage",
"format": "vendor/bin/pint"
"format": "vendor/bin/pint",
"pint": "vendor/bin/pint --ansi"
},
"config": {
"sort-packages": true,
@ -69,7 +75,7 @@
}
}
},
"version": "1.1.0",
"version": "1.2.3",
"minimum-stability": "stable",
"prefer-stable": true
}

View File

@ -0,0 +1,13 @@
<?php
namespace Msr\LaravelBitunixApi\Contracts;
interface RateLimiterContract
{
public function throttle(
string $bucket,
string $identity,
int $maxRequests,
float $perSeconds
): void;
}

View File

@ -3,10 +3,14 @@
namespace Msr\LaravelBitunixApi;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Msr\LaravelBitunixApi\Contracts\RateLimiterContract as RateLimiter;
use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract;
use Msr\LaravelBitunixApi\Requests\ChangeMarginModeRequestContract;
use Msr\LaravelBitunixApi\Requests\FlashClosePositionRequestContract;
use Msr\LaravelBitunixApi\Requests\FutureKLineRequestContract;
use Msr\LaravelBitunixApi\Requests\GetHistoryPositionsRequestContract;
use Msr\LaravelBitunixApi\Requests\GetHistoryTradesRequestContract;
use Msr\LaravelBitunixApi\Requests\GetPendingPositionsRequestContract;
use Msr\LaravelBitunixApi\Requests\GetSingleAccountRequestContract;
use Msr\LaravelBitunixApi\Requests\GetTradingPairsRequestContract;
@ -16,11 +20,11 @@ use Msr\LaravelBitunixApi\Requests\PlacePositionTpSlOrderRequestContract;
use Msr\LaravelBitunixApi\Requests\PlaceTpSlOrderRequestContract;
use Psr\Http\Message\ResponseInterface;
class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginModeRequestContract, FlashClosePositionRequestContract, FutureKLineRequestContract, GetPendingPositionsRequestContract, GetSingleAccountRequestContract, GetTradingPairsRequestContract, PlaceOrderRequestContract, PlacePositionTpSlOrderRequestContract, PlaceTpSlOrderRequestContract
class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginModeRequestContract, FlashClosePositionRequestContract, FutureKLineRequestContract, GetHistoryPositionsRequestContract, GetHistoryTradesRequestContract, GetPendingPositionsRequestContract, GetSingleAccountRequestContract, GetTradingPairsRequestContract, PlaceOrderRequestContract, PlacePositionTpSlOrderRequestContract, PlaceTpSlOrderRequestContract
{
private Client $publicFutureClient;
public function __construct()
public function __construct(protected RateLimiter $rateLimiter)
{
$this->publicFutureClient = new Client([
'base_uri' => config('bitunix-api.future_base_uri').'/api/v1/futures/market/',
@ -38,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', [
'query' => [
'symbol' => $symbol,
@ -188,6 +198,54 @@ class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginMo
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
{
$queryParams = [];
@ -207,6 +265,45 @@ class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginMo
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
{
$queryParams = [
@ -305,10 +402,13 @@ class LaravelBitunixApi implements ChangeLeverageRequestContract, ChangeMarginMo
$body['slStopType'] = $slStopType;
}
$response = $this->getPrivateFutureClient([], $body)->post('tpsl/position/place_order', [
return $this->getPrivateFutureClient([], $body)->post('tpsl/position/place_order', [
'json' => $body,
]);
}
return $response;
protected function resolveRateLimitIdentity(): string
{
return (string) config('bitunix-api.api_key');
}
}

View File

@ -2,17 +2,22 @@
namespace Msr\LaravelBitunixApi;
use Illuminate\Contracts\Cache\Factory as CacheFactory;
use Msr\LaravelBitunixApi\Commands\LaravelBitunixApiCommand;
use Msr\LaravelBitunixApi\Contracts\RateLimiterContract;
use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract;
use Msr\LaravelBitunixApi\Requests\ChangeMarginModeRequestContract;
use Msr\LaravelBitunixApi\Requests\FlashClosePositionRequestContract;
use Msr\LaravelBitunixApi\Requests\FutureKLineRequestContract;
use Msr\LaravelBitunixApi\Requests\GetHistoryPositionsRequestContract;
use Msr\LaravelBitunixApi\Requests\GetHistoryTradesRequestContract;
use Msr\LaravelBitunixApi\Requests\GetPendingPositionsRequestContract;
use Msr\LaravelBitunixApi\Requests\GetSingleAccountRequestContract;
use Msr\LaravelBitunixApi\Requests\GetTradingPairsRequestContract;
use Msr\LaravelBitunixApi\Requests\PlaceOrderRequestContract;
use Msr\LaravelBitunixApi\Requests\PlacePositionTpSlOrderRequestContract;
use Msr\LaravelBitunixApi\Requests\PlaceTpSlOrderRequestContract;
use Msr\LaravelBitunixApi\Support\CacheRateLimiter;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
@ -47,5 +52,13 @@ class LaravelBitunixApiServiceProvider extends PackageServiceProvider
$this->app->bind(PlaceTpSlOrderRequestContract::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()
);
});
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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)
);
}
}