Fix styling

This commit is contained in:
mahdimsr 2025-09-28 11:53:47 +00:00 committed by github-actions[bot]
parent 0996c5fc27
commit 49c1633f52
6 changed files with 58 additions and 84 deletions

View File

@ -7,7 +7,7 @@
* to change leverage for a trading pair on Bitunix exchange. * to change leverage for a trading pair on Bitunix exchange.
*/ */
require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__.'/../vendor/autoload.php';
use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract; use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract;
@ -38,18 +38,18 @@ try {
if ($data['code'] === 0) { if ($data['code'] === 0) {
echo "✅ Leverage changed successfully!\n"; echo "✅ Leverage changed successfully!\n";
echo "Symbol: " . $data['data'][0]['symbol'] . "\n"; echo 'Symbol: '.$data['data'][0]['symbol']."\n";
echo "Margin Coin: " . $data['data'][0]['marginCoin'] . "\n"; echo 'Margin Coin: '.$data['data'][0]['marginCoin']."\n";
echo "New Leverage: " . $data['data'][0]['leverage'] . "\n"; echo 'New Leverage: '.$data['data'][0]['leverage']."\n";
} else { } else {
echo "❌ API Error: " . $data['msg'] . "\n"; echo '❌ API Error: '.$data['msg']."\n";
} }
} else { } else {
echo "❌ HTTP Error: " . $response->getStatusCode() . "\n"; echo '❌ HTTP Error: '.$response->getStatusCode()."\n";
} }
} catch (Exception $e) { } catch (Exception $e) {
echo "❌ Exception: " . $e->getMessage() . "\n"; echo '❌ Exception: '.$e->getMessage()."\n";
} }
/** /**

View File

@ -3,12 +3,12 @@
namespace Msr\LaravelBitunixApi; namespace Msr\LaravelBitunixApi;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Msr\LaravelBitunixApi\Requests\FutureKLineRequestContract;
use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract; use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract;
use Msr\LaravelBitunixApi\Requests\FutureKLineRequestContract;
use Msr\LaravelBitunixApi\Requests\Header; use Msr\LaravelBitunixApi\Requests\Header;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
class LaravelBitunixApi implements FutureKLineRequestContract, ChangeLeverageRequestContract class LaravelBitunixApi implements ChangeLeverageRequestContract, FutureKLineRequestContract
{ {
private Client $publicFutureClient; private Client $publicFutureClient;

View File

@ -12,8 +12,6 @@ interface ChangeLeverageRequestContract
* @param string $symbol Trading pair (e.g., 'BTCUSDT') * @param string $symbol Trading pair (e.g., 'BTCUSDT')
* @param string $marginCoin Margin coin (e.g., 'USDT') * @param string $marginCoin Margin coin (e.g., 'USDT')
* @param int $leverage Leverage value * @param int $leverage Leverage value
* @return ResponseInterface
*/ */
public function changeLeverage(string $symbol, string $marginCoin, int $leverage): ResponseInterface; public function changeLeverage(string $symbol, string $marginCoin, int $leverage): ResponseInterface;
} }

View File

@ -6,9 +6,6 @@ class Header
{ {
/** /**
* Sort query parameters in ascending ASCII order by Key * Sort query parameters in ascending ASCII order by Key
*
* @param array $parameters
* @return array
*/ */
public static function sortQueryParameters(array $parameters): array public static function sortQueryParameters(array $parameters): array
{ {
@ -17,15 +14,13 @@ class Header
} }
ksort($parameters, SORT_STRING); ksort($parameters, SORT_STRING);
return $parameters; return $parameters;
} }
/** /**
* Convert sorted parameters to string format * Convert sorted parameters to string format
* Example: ["id" => "1", "uid" => "200"] becomes "id1uid200" * Example: ["id" => "1", "uid" => "200"] becomes "id1uid200"
*
* @param array $parameters
* @return string
*/ */
public static function digestQueryParameters(array $parameters): string public static function digestQueryParameters(array $parameters): string
{ {
@ -37,7 +32,7 @@ class Header
$result = ''; $result = '';
foreach ($sortedParameters as $key => $value) { foreach ($sortedParameters as $key => $value) {
$result .= $key . $value; $result .= $key.$value;
} }
return $result; return $result;
@ -45,8 +40,6 @@ class Header
/** /**
* Generate a random 32-bit nonce string * Generate a random 32-bit nonce string
*
* @return string
*/ */
public static function generateNonce(): string public static function generateNonce(): string
{ {
@ -55,8 +48,6 @@ class Header
/** /**
* Generate current timestamp in milliseconds * Generate current timestamp in milliseconds
*
* @return string
*/ */
public static function generateTimestamp(): string public static function generateTimestamp(): string
{ {
@ -71,12 +62,6 @@ class Header
* 2. Remove all spaces from body string * 2. Remove all spaces from body string
* 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)
*
* @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 public static function generateSignValue(array $queryParams = [], string $body = '', string $nonce = '', string $timestamp = ''): string
{ {
@ -94,11 +79,11 @@ class Header
$bodyString = trim($body); $bodyString = trim($body);
// Step 3: Create digest: SHA256(nonce + timestamp + api-key + queryParams + body) // Step 3: Create digest: SHA256(nonce + timestamp + api-key + queryParams + body)
$digestInput = $nonce . $timestamp . $apiKey . $queryParamsString . $bodyString; $digestInput = $nonce.$timestamp.$apiKey.$queryParamsString.$bodyString;
$digest = hash('sha256', $digestInput); $digest = hash('sha256', $digestInput);
// Step 4: Create sign: SHA256(digest + secretKey) // Step 4: Create sign: SHA256(digest + secretKey)
$signInput = $digest . $apiSecret; $signInput = $digest.$apiSecret;
$sign = hash('sha256', $signInput); $sign = hash('sha256', $signInput);
return $sign; return $sign;
@ -106,10 +91,6 @@ class Header
/** /**
* Generate complete headers for authenticated requests * Generate complete headers for authenticated requests
*
* @param array $queryParams
* @param string $body
* @return array
*/ */
public static function generateHeaders(array $queryParams = [], string $body = ''): array public static function generateHeaders(array $queryParams = [], string $body = ''): array
{ {

View File

@ -1,25 +1,20 @@
<?php <?php
use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract; use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
it('can change leverage successfully', function () { it('can change leverage successfully', function () {
$api = app(ChangeLeverageRequestContract::class); $api = app(ChangeLeverageRequestContract::class);
// This test will make a real API call if credentials are valid // This test will make a real API call if credentials are valid
// For testing purposes, we'll just verify the method exists and can be called // For testing purposes, we'll just verify the method exists and can be called
expect(fn() => $api->changeLeverage('BTCUSDT', 'USDT', 12)) expect(fn () => $api->changeLeverage('BTCUSDT', 'USDT', 12))
->not->toThrow(Exception::class); ->not->toThrow(Exception::class);
}); });
it('validates required parameters for change leverage', function () { it('validates required parameters for change leverage', function () {
$api = app(ChangeLeverageRequestContract::class); $api = app(ChangeLeverageRequestContract::class);
expect(fn() => $api->changeLeverage('BTCUSDT', 'USDT', 10)) expect(fn () => $api->changeLeverage('BTCUSDT', 'USDT', 10))
->not->toThrow(Exception::class) ->not->toThrow(Exception::class)
->and(fn() => $api->changeLeverage('BTCUSDT', 'USDT', 0)) ->and(fn () => $api->changeLeverage('BTCUSDT', 'USDT', 0))
->not->toThrow(Exception::class); ->not->toThrow(Exception::class);
}); });

View File

@ -14,7 +14,7 @@ it('can sort query parameters in ascending ASCII order', function () {
$parameters = [ $parameters = [
'uid' => '200', 'uid' => '200',
'id' => '1', 'id' => '1',
'name' => 'test' 'name' => 'test',
]; ];
$sorted = Header::sortQueryParameters($parameters); $sorted = Header::sortQueryParameters($parameters);
@ -23,14 +23,14 @@ it('can sort query parameters in ascending ASCII order', function () {
expect($sorted)->toBe([ expect($sorted)->toBe([
'id' => '1', 'id' => '1',
'name' => 'test', 'name' => 'test',
'uid' => '200' 'uid' => '200',
]); ]);
}); });
it('can digest query parameters to string format', function () { it('can digest query parameters to string format', function () {
$parameters = [ $parameters = [
'id' => '1', 'id' => '1',
'uid' => '200' 'uid' => '200',
]; ];
$result = Header::digestQueryParameters($parameters); $result = Header::digestQueryParameters($parameters);
@ -91,7 +91,7 @@ it('throws exception when API credentials are missing', function () {
'bitunix-api.api_secret' => '', 'bitunix-api.api_secret' => '',
]); ]);
expect(fn() => Header::generateSignValue([], '', 'nonce', 'timestamp')) expect(fn () => Header::generateSignValue([], '', 'nonce', 'timestamp'))
->toThrow(InvalidArgumentException::class, 'API key and secret must be configured'); ->toThrow(InvalidArgumentException::class, 'API key and secret must be configured');
}); });