You are viewing a single comment's thread from:

RE: SOLVED: SUDDENLY ERROR: How to RECEIVE TOKEN STAKE? My old method stopped working (see code) | ADVICE very WELCOME! SOLVED!

in Learn to Code23 days ago

Hi Paul bro,

I am looked the code in my vscode and it showed some errors. I have corrected it with best of my knowledge to remove errors, I have no idea of backend development so you do it as you feel fit.

let toastManager
async function findUserCCDStake(username) {
    try {
        const response = await fetch('https://api.hive-engine.com/rpc/contracts', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                jsonrpc: '2.0',
                method: 'find',
                params: {
                    contract: 'tokens',
                    table: 'balances',
                    query: {
                        symbol: 'CCD',
                        account: username, // Search for user
                    },
                },
                id: 1,
            }),
        });
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
            // toastManager.createToast('Error on api.hive-engine.com/rpc/contracts: ' + err, 'danger');
        }

        const userData = await response.json();

        if (userData.result && userData.result.length > 0) {
            let userCCDStake = parseFloat(userData.result[0].stake || 0);
            
            return userCCDStake;
        } else {
            return 0; // If no CCD stake found
        }

    } catch (error) {
        toastManager.createToast('Error on api.hive-engine.com/rpc/contracts: ' + error, 'danger');
        console.error('Error:', error.message);
        return 0;
    }
}

I have created a variable toastManager as it was not declared and was giving error.
commented toastManager line in if block as createToast is not working and err is not defined.
In Catch block, I have added error instead of err as it was not declared anywhere in the code.

You need to use the function findUserCCDStake(username), make sure it is fetching data from node servers and whether you have created another script file to import this code to be implemented.

I hope you'll try and give me your feedback.

Sort:  

Hey. Thank you.

the toastStuff & err is defined further at the top of the file, don't worry about the toastStuff. This snippet is just a part of the whole file - but everything needed for fetching the stake. Yes, would be better to delete it from the example to don't confuse... We need the focus on fetching the data - you was able to do so?

You need to use the function findUserCCDStake(username), make sure it is fetching data from node servers

Exactly, you was able to do so? This is the problem. All other stuff is okay in my file.

Let me clean the code to leave only the minimum without any toastStuff or so.

Thank you for your look!

SOLVED. Or at least seems to work / works for me again. Node seems to deliver the data as needed again. So when the node is working the CCCEO users should be able to use the voter as normal. In the long term I have to find a more solid solution.

THANK YOU once again!

It's good to know that your CCD fetch mechanism is now intact and working to deliver the desired results. 😀 Happy times again and ever.