PeakeCoin Games - JavaScript Balance Showing Error

I've been working on a PeakeCoin Poker game to gain some traction with grasping how to utilize Hive Tokens. So I'm working on the JavaScript files to find out how these transactions work out exactly in JavaScript after being schooled by @thecrazygm on the proper order of things in Python. Finding out you've been asking the blockchain to do something while asking with the wrong permissions is like beating your head into the wall.

So here's my fetch balance function is below here... and the game is on https://geocities.ws/peakecoin/games/Peaker/poker.html

I still can't figure out why I can't get the balance to show. I have to be sooooo dag'um close. I need one of you young whipper snappers to put your eyes upon my writings and find the error in my ways.

As I'm writing this, I'm also working on a keychain backup. That's why the message states 'Trying fallback...' so it could work while allowing me to fix the problems, while the game itself functions. Opposed to this, if I can't figure this out or I get no help, I'll muddle through it for sure. If you can help me out, I'd appreciate it.


async function fetchBalance() {
    const url = 'https://api.hive-engine.com/rpc/contracts';
    const symbol = 'PEK';
    const account = user.address ? user.address.toLowerCase() : '';
    const payload = {
        jsonrpc: '2.0',
        method: 'find',
        params: {
            contract: 'tokens',
            table: 'balances',
            query: { account, symbol }
        },
        id: 1
    };
    showMessage('Loading PEK balance...');
    let gotBalance = false;
    try {
        const r = await fetch(url, {
            method: 'POST',
            body: JSON.stringify(payload)
        });
        const data = await r.json();
        console.log('[DEBUG] Hive Engine balance response:', data);
        if (data.result && Array.isArray(data.result) && data.result.length > 0 && data.result[0].balance) {
            user.balance = parseFloat(data.result[0].balance);
            gotBalance = true;
            showMessage('PEK balance loaded.');
        } else {
            user.balance = 0;
            showMessage('No PEK balance found for this account. Trying fallback...');
        }
        user.token = symbol;
        updateWalletUI();
    } catch (e) {
        user.balance = 0;
        user.token = symbol;
        showMessage('Error fetching PEK balance. Trying fallback...');
        updateWalletUI();
        console.error('[DEBUG] fetchBalance error:', e);
    }



0
0
0.000
5 comments
avatar

without testing it and just at a first glance before coffee: my guess is you are missing the headers for the POST e.g.

        const r = await fetch(url, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' }, // <- this part
            body: JSON.stringify(payload)
        });
0
0
0.000
avatar

@thecrazygm I tried playing with it some and I get a 415 error. I'm sure it's a key thing as per usual, JavaScript isn't as easy to read as python is... The struggles.

I'm currently studying for my master electrical license also, but I will be back to hash this out

0
0
0.000
avatar
(Edited)

Unusual error for what I think you are attempting to do. If you haven't looked into it yet:

415 Unsupported Media Type

The request entity has a media type which the server or resource does not support. For example, the client uploads an image as image/svg+xml, but the server requires that images use a different format.

EDIT: So, depending on where it's giving the error, it could still be the type application/json line.

0
0
0.000
avatar

I fixed this. Now working on the render function.

0
0
0.000