-1

Hello everyone can you please help me to solve this error : The conditions I want to run: I use NodeJs with Web3.js to find the balance of the account address in Blockchain using 'getBalance()' function. My code is run perfectly on 'localhost' and I get the correct account balance .For checking the result I return the balance using express route to the webpage. But the problem is.. when we upload the program on the server it can't get balance It gets error(i.e catch block is executed ) and return '{}' empty parenthesis.

For solving this error I try many approaches like:

  • Changing web3 provider.
  • Changing different address.
  • and I also use web3 'privateKeyToAccount(privateKey)' function for getting account address using private key and I am geting excited that this function is executed successfully and give a correct address. But 'getBalance' function is not excuted correctly.
  • And I also use transaction function but it is also not run correctly.
  • I search many documentations and tutorials but I not get solution.
  • versions I use for nodejs 'v18.14.1' and for web3 '1.8.2'.

Here is my code:

const Web3 = require('web3');
const express = require('express');

const web3 = new Web3('https://mainnet.infura.io/v3/<MYINFURAKEY>');

const app = express();
app.use(express.json());

app.get('/', (req, res) => {

web3.eth.getBalance("0x7830c87C02e56AFf27FA8Ab1241711331FA86F43").then(balance => {
    res.send(balance);
  })
  .catch(error => {
      res.send(error);
  });
});

app.listen(3000, () => {
console.log('Server listening on port 3000');
});

I try to get balance of account address using nodejs and web3. For ofline it executed correctly but when we upload on server it not execute successfully it gets error.

After spending more time to solve this error I found small code related to this.

const Web3 = require('web3');
const express = require('express');

web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/v3/e365c07adf544980a71258eca29fceed'));

const app = express();
app.use(express.json());

app.get('/',(req, res) => {

    web3.eth.net.isListening()
    .then(() => res.send('is connected'))
    .catch(e => res.send('Wow. Something went wrong: '+ e));
});

app.listen(3000, () => {
console.log('Server listening on port 3000');
});

This code gives the same error

{Wow. Something went wrong: Error: CONNECTION ERROR: Couldn't connect to node https://mainnet.infura.io/v3/e365c07adf544980a71258eca29fceed.}

for online but offline it works fine.

TylerH
  • 20,799
  • 66
  • 75
  • 101
AnWho
  • 1
  • 1
  • Please add the details about this error ("it not execute successfully it gets error.....") – Luuk Mar 25 '23 at 12:10
  • how are u setting `MYINFURAKEY` in production. in env or hardcoding? – Yilmaz Mar 25 '23 at 13:39
  • It returns empty parenthesis '{}' – AnWho Mar 27 '23 at 06:36
  • But when store error in database it store this error: Error: CONNECTION ERROR: Couldn't connect to node https://mainnet.infura.io/v3/ But ofline it runs correctly and I also check our infura account the number of request also show on my account. When we login then it get an API key this API key I used as an Infura key. – AnWho Mar 27 '23 at 06:59
  • Welcome! StackOverflow is not a free coding service. You're expected to [try to solve the problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-uhttps://stackoverflow.com/q/5741187sers). Please update your question to show what you have already tried in a [mre]. For further information, please see [How to Ask](https://stackoverflow.com/questions/how-to-ask), and take the [tour](https://stackoverflow.com/tour). (Please put ALL info in the question, and do add info which should have been mentioned in the question) – Luuk Mar 27 '23 at 17:52
  • Thankyou! I know very well that StackOverflow is not a free coding service that's why I tried first then send to StackOverflow. And I send a updated code and error if you able to solve the error plz focus to solve error. – AnWho Mar 28 '23 at 08:21
  • When you get "CONNECTION ERROR" you should check the connection parameters, possibly add some debugging, to find out WHY you are getting this error, and then solve it. I cannot solve this from here, because I do not know the details about your connection like username/password, or even the exact error besides "CONNECTION ERROR" . – Luuk Mar 28 '23 at 08:29
  • CAN ANYBODY IS HERE TO SOLVE THIS ? – AnWho Mar 29 '23 at 05:59

1 Answers1

0

try using any other rpc instead of infura like chainlist Rpc