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

(edited)

token-stake-error-01.png

In the past I checked the users CCD token stake via the node https://api.hive-engine.com/rpc/contracts - but this stopped working. I tried a few other nodes, but can't get it working (because the other nodes don't deliver the full spectrum of functions?). However, I decided to ask for help here. If you have an idea or solution or can forward this question/post to a Hivian who could help, this would be awesome!

Is there another way to check the stake (in best case with JavaScript I think)? Which node do you use? What is wrong suddenly? It worked until 2, 3 days ago. Then api.hive-engine.com was down completely, now the node seems to be back, but my stake reading doesn't start to work again. Your support would be very welcome.

(Yes, I asked at the HiveDevs Discord and two buddies, too. As soon as I have a solution from anywhere I post the solution as comment under this article.)

The way I did it all the time - and worked as far as the api.hive-engine.com node was running well:

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 as now and in the past, the CCCEO users should be able to use the voter as normal.

async function findUserCCDStake() {
    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: 'powerpaul', // powerpaul has 300,000 CCD in stake
                    },
                },
                id: 1,
            }),
        });
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }

        const userData = await response.json();

        if (userData.result && userData.result.length > 0) {
            userCCDStake = parseFloat(userData.result[0].stake || 0);

            console.log(userCCDStake);
            return userCCDStake;
        } else {
            return 0; // If no CCD stake found
        }

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

Once again thank you for your support!


Greetings from Paraguay!

Follow for the hottest & most buggy stuff!
No... Better follow @CryptoCompany, my game & blockchain project! You don't will regret it!

Your vote keeps things, stupidities & fridges rollin'!


Rock 'n' Roll & Hive a great day!

1.16242015 BEE
6 comments

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.

0.00035043 BEE

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!

0.00022442 BEE

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!

0.00022218 BEE

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.

0.00036003 BEE

Hi my big brother with the best of vibes. Ouch to be honest it's frustrating when a reliable method suddenly stops working. Have you tried reaching out to the Hive community directly? Sometimes a fresh pair of eyes can spot the issue😁. Good luck with finding a solution my good brother. As always more pow POW POW powwwwwwwweerrrrr to Great Paul

0.00034735 BEE

Hey. Yes, kind of. Asked in Discords, a few friends, mentioned this under the voter comments - let's see where help comes up first...

Actually I am on finishing the @BroBang stuff for the passed season which ended a few hours ago. As soon as this is done, I am back in the code for this task here...

When it comes, it comes all together... lol

Hive a great day my friend! I will have, too - unrelated to every "aaaaargh".

Stay groovy &

untitled.gif

0.00001206 BEE

Hope everything goes really well for you my good friend.... Have a great week Paul🥰🥰🥰🥰🥰

0.00034139 BEE

Glad you find a solution for that issue!

Curated by @arc7icwolf.byte for the #LearnToCode community.

0.00000837 BEE

Me too. Always a little tricky when the own service depends on other services. I have to solve this step by step, if this would improve the strength of my doings and/or our network and not exceed my manpower. But in every case I am glad that we are such a powerful community... At the end we all try to give our best to empower our blockchain and it is pretty amazing what the people do for it - also if something runs not as planned, most people are very engaged to do their part wherever they see their role/possibilities; and that id pretty awesome! We are really kind of unique community here... That's foo awesome!

Wolf, buddy, thank you for your curation! Awesome to be with you! Hive on!

0E-8 BEE

Hive teamwork at its best! I love to read that there are so many people ready to help solve an issue :) this is the true potential of Hive.

Wolf, buddy, thank you for your curation!

Just trying to give back something, mate! :)

Let's keep grinding!

!PIZZA

0E-8 BEE

Interesting to see how things happen in the backround, curated by byte tribe

0E-8 BEE

PIZZA!

$PIZZA slices delivered:
@arc7icwolf(7/10) tipped @powerpaul

0E-8 BEE

Congratulations @powerpaul! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You distributed more than 24000 upvotes.
Your next target is to reach 25000 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out our last posts:

Hive Power Up Month Challenge - June 2024 Winners List
Be ready for the July edition of the Hive Power Up Month!
Hive Power Up Day - July 1st 2024
0E-8 BEE