<?php

$api_key = 'ZwEzFeiqnVo356Our9';
$api_secret = 'pKpGWLUSjwnXP3A2QXEhTuc5O30cSvXwoSMp';
$accountType = 'UNIFIED';
$coin = 'USDT';


function createSignature($params, $api_secret) {
    ksort($params); // Sort parameters by key
    $query_string = http_build_query($params); // Build query string
    return hash_hmac('sha256', $query_string, $api_secret); // Generate signature
}

// Function to get wallet balance for Unified Account
function getWalletBalance($api_key, $api_secret) {
    //$endpoint = 'https://api.bybit.com/v5/account/wallet-balance';
    $endpoint='https://api.bybit.com/v5/asset/transfer/query-account-coins-balance';
    $timestamp = time() * 1000; // Current time in seconds0

    // Parameters
    $params = [
        'api_key' => $api_key,
        'timestamp' => $timestamp,
        'accountType'=>'UNIFIED'
    ];

    // Create a signature
    $params['sign'] = createSignature($params, $api_secret);

    // Build the query string for the GET request
    $query_string = http_build_query($params);
    $url = $endpoint . '?' . $query_string;

    // Make the API request using cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    //print_r($response);
    curl_close($ch);

    return json_decode($response, true); // Return response as an associative array
}

// Execute the function
$walletBalance = getWalletBalance($api_key, $api_secret);

// Output the result
if (isset($walletBalance['result'])) {
    echo '<pre>';
    print_r($walletBalance['result']);
    echo '</pre>';
    // Display the wallet balance
} else {
    echo "Error fetching wallet balance: " . $walletBalance['ret_msg'];
}


?>
