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.
*/
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__.'/../vendor/autoload.php';
use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract;
@ -38,18 +38,18 @@ try {
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";
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";
echo '❌ API Error: '.$data['msg']."\n";
}
} else {
echo "❌ HTTP Error: " . $response->getStatusCode() . "\n";
echo '❌ HTTP Error: '.$response->getStatusCode()."\n";
}
} catch (Exception $e) {
echo "❌ Exception: " . $e->getMessage() . "\n";
echo '❌ Exception: '.$e->getMessage()."\n";
}
/**

View File

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

View File

@ -12,8 +12,6 @@ interface ChangeLeverageRequestContract
* @param string $symbol Trading pair (e.g., 'BTCUSDT')
* @param string $marginCoin Margin coin (e.g., 'USDT')
* @param int $leverage Leverage value
* @return 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
*
* @param array $parameters
* @return array
*/
public static function sortQueryParameters(array $parameters): array
{
@ -17,15 +14,13 @@ class Header
}
ksort($parameters, SORT_STRING);
return $parameters;
}
/**
* Convert sorted parameters to string format
* Example: ["id" => "1", "uid" => "200"] becomes "id1uid200"
*
* @param array $parameters
* @return string
*/
public static function digestQueryParameters(array $parameters): string
{
@ -37,7 +32,7 @@ class Header
$result = '';
foreach ($sortedParameters as $key => $value) {
$result .= $key . $value;
$result .= $key.$value;
}
return $result;
@ -45,8 +40,6 @@ class Header
/**
* Generate a random 32-bit nonce string
*
* @return string
*/
public static function generateNonce(): string
{
@ -55,8 +48,6 @@ class Header
/**
* Generate current timestamp in milliseconds
*
* @return string
*/
public static function generateTimestamp(): string
{
@ -71,12 +62,6 @@ class Header
* 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
{
@ -94,11 +79,11 @@ class Header
$bodyString = trim($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);
// Step 4: Create sign: SHA256(digest + secretKey)
$signInput = $digest . $apiSecret;
$signInput = $digest.$apiSecret;
$sign = hash('sha256', $signInput);
return $sign;
@ -106,10 +91,6 @@ class Header
/**
* Generate complete headers for authenticated requests
*
* @param array $queryParams
* @param string $body
* @return array
*/
public static function generateHeaders(array $queryParams = [], string $body = ''): array
{

View File

@ -1,25 +1,20 @@
<?php
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 () {
$api = app(ChangeLeverageRequestContract::class);
// 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
expect(fn() => $api->changeLeverage('BTCUSDT', 'USDT', 12))
expect(fn () => $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))
expect(fn () => $api->changeLeverage('BTCUSDT', 'USDT', 10))
->not->toThrow(Exception::class)
->and(fn() => $api->changeLeverage('BTCUSDT', 'USDT', 0))
->and(fn () => $api->changeLeverage('BTCUSDT', 'USDT', 0))
->not->toThrow(Exception::class);
});

View File

@ -14,7 +14,7 @@ it('can sort query parameters in ascending ASCII order', function () {
$parameters = [
'uid' => '200',
'id' => '1',
'name' => 'test'
'name' => 'test',
];
$sorted = Header::sortQueryParameters($parameters);
@ -23,14 +23,14 @@ it('can sort query parameters in ascending ASCII order', function () {
expect($sorted)->toBe([
'id' => '1',
'name' => 'test',
'uid' => '200'
'uid' => '200',
]);
});
it('can digest query parameters to string format', function () {
$parameters = [
'id' => '1',
'uid' => '200'
'uid' => '200',
];
$result = Header::digestQueryParameters($parameters);
@ -91,7 +91,7 @@ it('throws exception when API credentials are missing', function () {
'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');
});