RE: RE: SOLVED: SUDDENLY ERROR: How to RECEIVE TOKEN STAKE? My old method stopped working (see code) | ADVICE very WELCOME! SOLVED!
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!

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

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.

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now