From 0996c5fc271f139d3d3613c03d40b5228009da3b Mon Sep 17 00:00:00 2001 From: mahdi msr Date: Sun, 28 Sep 2025 15:22:55 +0330 Subject: [PATCH] future-public: add set leverage request --- .gitignore | 1 + config/bitunix-api.php | 4 +- examples/ChangeLeverageExample.php | 61 ++++++++ src/LaravelBitunixApi.php | 30 +++- src/LaravelBitunixApiServiceProvider.php | 2 + .../ChangeLeverageRequestContract.php | 19 +++ src/Requests/Header.php | 125 +++++++++++++--- tests/ChangeLeverageTest.php | 25 ++++ tests/HeaderTest.php | 135 ++++++++++++++++++ 9 files changed, 377 insertions(+), 25 deletions(-) create mode 100644 examples/ChangeLeverageExample.php create mode 100644 src/Requests/ChangeLeverageRequestContract.php create mode 100644 tests/ChangeLeverageTest.php create mode 100644 tests/HeaderTest.php diff --git a/.gitignore b/.gitignore index b60507f..4b1d8c2 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ phpstan.neon testbench.yaml /docs /coverage +.env diff --git a/config/bitunix-api.php b/config/bitunix-api.php index b0bbf2a..056ba9b 100644 --- a/config/bitunix-api.php +++ b/config/bitunix-api.php @@ -1,7 +1,9 @@ 'https://fapi.bitunix.com/', + 'api_key' => env('BITUNIX_API_KEY'), + 'api_secret' => env('BITUNIX_API_SECRET'), + 'language' => 'en-US', ]; diff --git a/examples/ChangeLeverageExample.php b/examples/ChangeLeverageExample.php new file mode 100644 index 0000000..4129e60 --- /dev/null +++ b/examples/ChangeLeverageExample.php @@ -0,0 +1,61 @@ + 'https://fapi.bitunix.com/', + 'bitunix-api.api_key' => 'your-api-key-here', + 'bitunix-api.api_secret' => 'your-api-secret-here', + 'bitunix-api.language' => 'en-US', +]); + +try { + // Get the API instance + $api = app(ChangeLeverageRequestContract::class); + + // Change leverage for BTCUSDT pair + $symbol = 'BTCUSDT'; + $marginCoin = 'USDT'; + $leverage = 12; + + echo "Changing leverage for {$symbol} to {$leverage}x...\n"; + + $response = $api->changeLeverage($symbol, $marginCoin, $leverage); + + // Check response status + if ($response->getStatusCode() === 200) { + $data = json_decode($response->getBody()->getContents(), true); + + if ($data['code'] === 0) { + echo "✅ Leverage changed successfully!\n"; + echo "Symbol: " . $data['data'][0]['symbol'] . "\n"; + echo "Margin Coin: " . $data['data'][0]['marginCoin'] . "\n"; + echo "New Leverage: " . $data['data'][0]['leverage'] . "\n"; + } else { + echo "❌ API Error: " . $data['msg'] . "\n"; + } + } else { + echo "❌ HTTP Error: " . $response->getStatusCode() . "\n"; + } + +} catch (Exception $e) { + echo "❌ Exception: " . $e->getMessage() . "\n"; +} + +/** + * Environment Variables Required: + * + * BITUNIX_API_KEY=your-api-key + * BITUNIX_API_SECRET=your-api-secret + * BITUNIX_LANGUAGE=en-US + */ diff --git a/src/LaravelBitunixApi.php b/src/LaravelBitunixApi.php index 5b73040..4b5b2b9 100644 --- a/src/LaravelBitunixApi.php +++ b/src/LaravelBitunixApi.php @@ -4,9 +4,11 @@ namespace Msr\LaravelBitunixApi; use GuzzleHttp\Client; use Msr\LaravelBitunixApi\Requests\FutureKLineRequestContract; +use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract; +use Msr\LaravelBitunixApi\Requests\Header; use Psr\Http\Message\ResponseInterface; -class LaravelBitunixApi implements FutureKLineRequestContract +class LaravelBitunixApi implements FutureKLineRequestContract, ChangeLeverageRequestContract { private Client $publicFutureClient; @@ -17,6 +19,17 @@ class LaravelBitunixApi implements FutureKLineRequestContract ]); } + protected function getPrivateFutureClient(array $queryParams = [], array $body = []): Client + { + $bodyString = json_encode($body); + $headers = Header::generateHeaders($queryParams, $bodyString); + + return new Client([ + 'base_uri' => config('bitunix-api.future_base_uri').'/api/v1/futures/', + 'headers' => $headers, + ]); + } + 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', [ @@ -32,4 +45,19 @@ class LaravelBitunixApi implements FutureKLineRequestContract return $response; } + + public function changeLeverage(string $symbol, string $marginCoin, int $leverage): ResponseInterface + { + $body = [ + 'symbol' => $symbol, + 'marginCoin' => $marginCoin, + 'leverage' => $leverage, + ]; + + $response = $this->getPrivateFutureClient([], $body)->post('account/change_leverage', [ + 'json' => $body, + ]); + + return $response; + } } diff --git a/src/LaravelBitunixApiServiceProvider.php b/src/LaravelBitunixApiServiceProvider.php index a984a54..589a68b 100644 --- a/src/LaravelBitunixApiServiceProvider.php +++ b/src/LaravelBitunixApiServiceProvider.php @@ -3,6 +3,7 @@ namespace Msr\LaravelBitunixApi; use Msr\LaravelBitunixApi\Commands\LaravelBitunixApiCommand; +use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract; use Msr\LaravelBitunixApi\Requests\FutureKLineRequestContract; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; @@ -29,5 +30,6 @@ class LaravelBitunixApiServiceProvider extends PackageServiceProvider parent::packageRegistered(); $this->app->bind(FutureKLineRequestContract::class, LaravelBitunixApi::class); + $this->app->bind(ChangeLeverageRequestContract::class, LaravelBitunixApi::class); } } diff --git a/src/Requests/ChangeLeverageRequestContract.php b/src/Requests/ChangeLeverageRequestContract.php new file mode 100644 index 0000000..83e91d1 --- /dev/null +++ b/src/Requests/ChangeLeverageRequestContract.php @@ -0,0 +1,19 @@ + "1", "uid" => "200"] becomes "id1uid200" + * + * @param array $parameters + * @return string + */ + public static function digestQueryParameters(array $parameters): string { - if (!count($parameters)) { - return null; + if (empty($parameters)) { + return ''; } $sortedParameters = self::sortQueryParameters($parameters); - return implode('', array_map( - fn($k, $v) => $k . $v, - array_keys($sortedParameters), - array_values($sortedParameters) - )); + $result = ''; + + foreach ($sortedParameters as $key => $value) { + $result .= $key . $value; + } + + return $result; } - public static function generateNonce(): ?string + /** + * Generate a random 32-bit nonce string + * + * @return string + */ + public static function generateNonce(): string { - return md5(uniqid(mt_rand(), true)); + return bin2hex(random_bytes(16)); // 32 characters } - public static function generateSignValue(array $queryParams = [], string $body = '', string $randomNonce = ''): string + /** + * Generate current timestamp in milliseconds + * + * @return string + */ + public static function generateTimestamp(): string + { + return (string) round(microtime(true) * 1000); + } + + /** + * Generate signature according to Bitunix API documentation + * + * Steps: + * 1. Sort all queryParams in ascending ASCII order by Key + * 2. Remove all spaces from body string + * 3. Create digest: SHA256(nonce + timestamp + api-key + queryParams + body) + * 4. Create sign: SHA256(digest + secretKey) + * + * @param array $queryParams + * @param string $body + * @param string $nonce + * @param string $timestamp + * @return string + */ + public static function generateSignValue(array $queryParams = [], string $body = '', string $nonce = '', string $timestamp = ''): string { $apiKey = config('bitunix-api.api_key'); $apiSecret = config('bitunix-api.api_secret'); - $timeStamp = (string)round(microtime(true) * 1000); - $digestedQueryParam = self::digestQueryParameters($queryParams); + + if (empty($apiKey) || empty($apiSecret)) { + throw new \InvalidArgumentException('API key and secret must be configured'); + } - $digestedHeader = $randomNonce . $timeStamp . $apiKey . $digestedQueryParam . $body; - $hash = hash('sha256', $digestedHeader); - $sign = $hash . $apiSecret; - return hash('sha256', $sign); + // Step 1: Sort query parameters in ascending ASCII order + $queryParamsString = self::digestQueryParameters($queryParams); + + // Step 2: Remove all spaces from body (already done if JSON encoded properly) + $bodyString = trim($body); + + // Step 3: Create digest: SHA256(nonce + timestamp + api-key + queryParams + body) + $digestInput = $nonce . $timestamp . $apiKey . $queryParamsString . $bodyString; + $digest = hash('sha256', $digestInput); + + // Step 4: Create sign: SHA256(digest + secretKey) + $signInput = $digest . $apiSecret; + $sign = hash('sha256', $signInput); + + return $sign; + } + + /** + * Generate complete headers for authenticated requests + * + * @param array $queryParams + * @param string $body + * @return array + */ + public static function generateHeaders(array $queryParams = [], string $body = ''): array + { + $nonce = self::generateNonce(); + $timestamp = self::generateTimestamp(); + $sign = self::generateSignValue($queryParams, $body, $nonce, $timestamp); + + return [ + 'api-key' => config('bitunix-api.api_key'), + 'sign' => $sign, + 'nonce' => $nonce, + 'timestamp' => $timestamp, + 'language' => config('bitunix-api.language', 'en-US'), + 'Content-Type' => 'application/json', + ]; } } diff --git a/tests/ChangeLeverageTest.php b/tests/ChangeLeverageTest.php new file mode 100644 index 0000000..ced3d13 --- /dev/null +++ b/tests/ChangeLeverageTest.php @@ -0,0 +1,25 @@ + $api->changeLeverage('BTCUSDT', 'USDT', 12)) + ->not->toThrow(Exception::class); +}); + +it('validates required parameters for change leverage', function () { + $api = app(ChangeLeverageRequestContract::class); + + expect(fn() => $api->changeLeverage('BTCUSDT', 'USDT', 10)) + ->not->toThrow(Exception::class) + ->and(fn() => $api->changeLeverage('BTCUSDT', 'USDT', 0)) + ->not->toThrow(Exception::class); +}); + diff --git a/tests/HeaderTest.php b/tests/HeaderTest.php new file mode 100644 index 0000000..f680c3b --- /dev/null +++ b/tests/HeaderTest.php @@ -0,0 +1,135 @@ + 'test-api-key', + 'bitunix-api.api_secret' => 'test-secret-key', + 'bitunix-api.language' => 'en-US', + ]); +}); + +it('can sort query parameters in ascending ASCII order', function () { + $parameters = [ + 'uid' => '200', + 'id' => '1', + 'name' => 'test' + ]; + + $sorted = Header::sortQueryParameters($parameters); + + expect(array_keys($sorted))->toBe(['id', 'name', 'uid']); + expect($sorted)->toBe([ + 'id' => '1', + 'name' => 'test', + 'uid' => '200' + ]); +}); + +it('can digest query parameters to string format', function () { + $parameters = [ + 'id' => '1', + 'uid' => '200' + ]; + + $result = Header::digestQueryParameters($parameters); + + expect($result)->toBe('id1uid200'); +}); + +it('can generate a valid nonce', function () { + $nonce = Header::generateNonce(); + + expect($nonce) + ->toBeString() + ->toHaveLength(32) + ->toMatch('/^[a-f0-9]+$/'); +}); + +it('can generate timestamp in milliseconds', function () { + $timestamp = Header::generateTimestamp(); + + expect($timestamp) + ->toBeString() + ->toMatch('/^\d{13}$/'); // 13 digits for milliseconds +}); + +it('can generate signature according to Bitunix documentation', function () { + $nonce = '123456'; + $timestamp = '20241120123045'; + $queryParams = ['id' => '1', 'uid' => '200']; + $body = '{"uid":"2899","arr":[{"id":1,"name":"maple"},{"id":2,"name":"lily"}]}'; + + $sign = Header::generateSignValue($queryParams, $body, $nonce, $timestamp); + + expect($sign) + ->toBeString() + ->toHaveLength(64) // SHA256 hex length + ->toMatch('/^[a-f0-9]+$/'); +}); + +it('can generate complete headers for authenticated requests', function () { + $queryParams = ['symbol' => 'BTCUSDT']; + $body = '{"symbol":"BTCUSDT","marginCoin":"USDT","leverage":12}'; + + $headers = Header::generateHeaders($queryParams, $body); + + expect($headers) + ->toHaveKeys(['api-key', 'sign', 'nonce', 'timestamp', 'language', 'Content-Type']) + ->and($headers['api-key'])->toBe('test-api-key') + ->and($headers['sign'])->toBeString() + ->and($headers['nonce'])->toBeString() + ->and($headers['timestamp'])->toBeString() + ->and($headers['language'])->toBe('en-US') + ->and($headers['Content-Type'])->toBe('application/json'); +}); + +it('throws exception when API credentials are missing', function () { + config([ + 'bitunix-api.api_key' => '', + 'bitunix-api.api_secret' => '', + ]); + + expect(fn() => Header::generateSignValue([], '', 'nonce', 'timestamp')) + ->toThrow(InvalidArgumentException::class, 'API key and secret must be configured'); +}); + +it('handles empty query parameters correctly', function () { + $result = Header::digestQueryParameters([]); + + expect($result)->toBe(''); +}); + +it('handles empty body correctly', function () { + $nonce = '123456'; + $timestamp = '20241120123045'; + + $sign = Header::generateSignValue([], '', $nonce, $timestamp); + + expect($sign) + ->toBeString() + ->toHaveLength(64); +}); + +it('generates consistent signature for same inputs', function () { + $nonce = '123456'; + $timestamp = '20241120123045'; + $queryParams = ['id' => '1']; + $body = '{"test":"value"}'; + + $sign1 = Header::generateSignValue($queryParams, $body, $nonce, $timestamp); + $sign2 = Header::generateSignValue($queryParams, $body, $nonce, $timestamp); + + expect($sign1)->toBe($sign2); +}); + +it('generates different signatures for different inputs', function () { + $nonce = '123456'; + $timestamp = '20241120123045'; + + $sign1 = Header::generateSignValue(['id' => '1'], '{"test":"value1"}', $nonce, $timestamp); + $sign2 = Header::generateSignValue(['id' => '2'], '{"test":"value2"}', $nonce, $timestamp); + + expect($sign1)->not->toBe($sign2); +});