OLDBOY/skin/board/outline_target/list/realtime.asset.php
<?php
include_once('../_common.php');

header('Content-Type: application/json; charset=utf-8');

if (!function_exists('x2_base64url_encode')) {
    function x2_base64url_encode($data)
    {
        return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
    }
}

if (!function_exists('x2_jwt_hs256')) {
    function x2_jwt_hs256(array $payload, $secret)
    {
        $header = ['alg' => 'HS256', 'typ' => 'JWT'];
        $h = x2_base64url_encode(json_encode($header));
        $p = x2_base64url_encode(json_encode($payload));
        $s = hash_hmac('sha256', $h.'.'.$p, $secret, true);
        return $h.'.'.$p.'.'.x2_base64url_encode($s);
    }
}

if (!function_exists('x2_http_get_json')) {
    function x2_http_get_json($url, array $headers = [])
    {
        if (!function_exists('curl_init')) {
            return null;
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt($ch, CURLOPT_TIMEOUT, 8);
        if (!empty($headers)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }

        $response = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($response === false || $http_code < 200 || $http_code >= 300) {
            return null;
        }

        $decoded = json_decode($response, true);
        return is_array($decoded) ? $decoded : null;
    }
}

if (!function_exists('x2_fetch_upbit_current_asset')) {
    function x2_fetch_upbit_current_asset()
    {
        $key_candidates = [
            '/home/www/DB/key_upbit_trade.php',
            isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'].'/DB/key_upbit_trade.php' : ''
        ];

        $UPBIT_ACCESS_KEY = '';
        $UPBIT_SECRET_KEY = '';

        foreach ($key_candidates as $key_path) {
            if ($key_path && file_exists($key_path)) {
                include $key_path;
                if (!empty($UPBIT_ACCESS_KEY) && !empty($UPBIT_SECRET_KEY)) {
                    break;
                }
            }
        }

        if (empty($UPBIT_ACCESS_KEY) || empty($UPBIT_SECRET_KEY)) {
            return null;
        }

        $payload = [
            'access_key' => $UPBIT_ACCESS_KEY,
            'nonce' => md5(uniqid('', true))
        ];

        $jwt = x2_jwt_hs256($payload, $UPBIT_SECRET_KEY);
        $accounts = x2_http_get_json('https://api.upbit.com/v1/accounts', ['Authorization: Bearer '.$jwt]);

        if (!is_array($accounts)) {
            return null;
        }

        $total = 0.0;
        $need_markets = [];
        $holding_map = [];
        $avg_map = [];

        foreach ($accounts as $account) {
            $currency = strtoupper($account['currency'] ?? '');
            $balance = (float)($account['balance'] ?? 0);
            $locked = (float)($account['locked'] ?? 0);
            $amount = $balance + $locked;

            if ($amount <= 0) {
                continue;
            }

            if ($currency === 'KRW') {
                $total += $amount;
                continue;
            }

            $market = 'KRW-'.$currency;
            $holding_map[$market] = $amount;
            $avg_map[$market] = (float)($account['avg_buy_price'] ?? 0);
            $need_markets[] = $market;
        }

        if (!empty($need_markets)) {
            $price_map = [];
            $need_markets = array_values(array_unique($need_markets));
            $chunks = array_chunk($need_markets, 100);

            foreach ($chunks as $markets) {
                $url = 'https://api.upbit.com/v1/ticker?markets='.urlencode(implode(',', $markets));
                $ticker_rows = x2_http_get_json($url);
                if (!is_array($ticker_rows)) {
                    continue;
                }

                foreach ($ticker_rows as $ticker_row) {
                    $market = $ticker_row['market'] ?? '';
                    $trade_price = (float)($ticker_row['trade_price'] ?? 0);
                    if ($market !== '' && $trade_price > 0) {
                        $price_map[$market] = $trade_price;
                    }
                }
            }

            foreach ($holding_map as $market => $quantity) {
                $market_price = isset($price_map[$market]) ? (float)$price_map[$market] : 0;
                if ($market_price <= 0 && isset($avg_map[$market])) {
                    $market_price = (float)$avg_map[$market];
                }
                $total += $quantity * $market_price;
            }
        }

        return $total;
    }
}

$current_asset = x2_fetch_upbit_current_asset();
if ($current_asset === null) {
    echo json_encode([
        'ok' => false,
        'current_asset' => null,
        'ts' => time()
    ]);
    exit;
}

echo json_encode([
    'ok' => true,
    'current_asset' => (float)$current_asset,
    'ts' => time()
]);