#Cannot access offset of type string on string

24 messages · Page 1 of 1 (latest)

harsh umbra
#

so this is what i get if i print balances

and this error am i getting

Cannot access offset of type string on string

on this

            $balances = $this->BtcCalculation();
            $BTCBalance = $balances['BTC'];
            $ETHBalance = $balances['ETH'];
            $USDTBalance = $balances['USDT'];

                    $ETHToUSDT = $ETHBalance * $ETHPrice;
                    if ($ETHToUSDT > 0.0) {
                        $USDTToBTC = $ETHToUSDT / $USDTBalance;
                        $TotalBalance = $USDTToBTC / $BTCPrice;
                    } else {
                        $TotalBalance = $USDTBalance / $BTCPrice + $BTCBalance;
                    }
                    return $TotalBalance;
                } else {
                    return "BTC key not found in the balances array";
                }
            }
#

and if i print just btcbalance i get this 1.0

#

so hmm

high flower
#

U sure the errors is in this code?

harsh umbra
#

maybe here

high flower
#

Wdym maybe

#

You’ve got the whole stack trace

#

See what it points out to

harsh umbra
#
        $response = curl_exec($curl);
        $responseData = json_decode($response, true);

        if ($responseData !== null) {
            if (isset($responseData['result']['list'])) {
                $list = $responseData['result']['list'];
                $count = count($list);
                $balances = array();

                for ($x = 0; $x < $count; $x++) {
                    $currentItem = $list[$x];
                    $coin = $currentItem['coin'];
                    $walletBalance = floatval($currentItem['walletBalance']);

                    $balances[$coin] = $walletBalance;
                }


                return $balances;
            } else {
                return 'Balance data not available: missing "result" or "list" key in the response';
            }
        } else {
            return 'Failed to decode response JSON';
        }
#

i was getting the code 😄

#

the error should be in this code

#

because he is doing the calculation in there etc

harsh umbra
#

so it's getting weird right now because he is saying


                if (isset($balances['BTC'])) {
                    $BTCBalance = $balances['BTC'];
                    $ETHBalance = $balances['ETH'];
                    $USDTBalance = $balances['USDT'];

                    // Create an array to store the balances
                    $result = array(
                        'BTCBalance' => $BTCBalance,
                        'ETHBalance' => $ETHBalance,
                        'USDTBalance' => $USDTBalance
                    );

                    return $result;
                } else {
                    return "BTC key not found in the balances array";
                }

btc key not found in the balances array

#

but if i print $balances['BTC'] i get the walletbalance (1.0)

olive kayak
harsh umbra
#

so i found the real error few mins ago i could ask here but i think the api owners could answer it if you could will also be nice

#

"retCode" => 10001
"retMsg" => "empty value: apiTimestamp[] apiKey[] apiSignature[]"
"result" => []
"retExtInfo" => []
"time" => 1685789721282
]

#

so thats my error

#
  public function BtcCalculation()
    {
        $timestamp = time() * 1000; // Get the current timestamp in milliseconds

        $params_for_signature = $timestamp . $this->apiKey . "5000";

        // Create signature and add it to array
        $signature = hash_hmac('sha256', $params_for_signature, $this->apiSecret);

        $url = 'https://api-testnet.bybit.com/contract/v3/private/account/wallet/balance';

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
            CURLOPT_HTTPHEADER => array(
                "X-BAPI-API-KEY: {$this->apiKey}",
                "X-BAPI-SIGN: {$signature}",
                "X-BAPI-SIGN-TYPE: 2",
                "X-BAPI-TIMESTAMP: {$timestamp}",
                "X-BAPI-RECV-WINDOW: 5000",
                "Content-Type: application/json"
            ),
        ));

        $response = curl_exec($curl);

        $responseData = json_decode($response, true);

        return $responseData;
    }
#

so i am requesting this api

#
    public function fetchAccountBalance()
    {
        $timestamp = time() * 1000; // Get the current timestamp in milliseconds

        $params_for_signature = $timestamp . $this->apiKey . "5000" . "category=inverse";

        // Create signature and add it to the array
        $signature = hash_hmac('sha256', $params_for_signature, $this->apiSecret);

        $url = 'https://api-testnet.bybit.com/v5/market/tickers?category=inverse';

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
            CURLOPT_HTTPHEADER => array(
                "X-BAPI-API-KEY: {$this->apiKey}",
                "X-BAPI-SIGN: {$signature}",
                "X-BAPI-SIGN-TYPE: 2",
                "X-BAPI-TIMESTAMP: {$timestamp}",
                "X-BAPI-RECV-WINDOW: 5000",
                "Content-Type: application/json"
            ),
        ));

        $response = curl_exec($curl);


        $responseData = json_decode($response, true);
        // Parse the API response and extract the ETH and BTC ticker information
        if (isset($responseData['result']['list'])) {
            foreach ($responseData['result']['list'] as $item) {
                if ($item['symbol'] === 'ETHUSD') {
                    $ETHPrice = $item['lastPrice'];
                } elseif ($item['symbol'] === 'BTCUSD') {
                    $BTCPrice = $item['lastPrice'];
                }
            }




            dump($this->BtcCalculation());

and i am polling it with livewire

#

but after the second request he can't get the signature api key and timestamp anymore

faint stirrup
# harsh umbra so thats my error

You say so that’s my error, but you haven’t actually posted an error that’s given to you in the debug with the reference to the where the error is & then just post the relevant code. You have just posted all these code blocks and no real reference to what you want us to do with them… help us to help you!

#

And Martin already asked you to post it, then you just posted more code blocks 🤦‍♂️