Skip to main content
Bounty Ended with 25 reputation awarded by CommunityBot
added 676 characters in body
Source Link
Mark Toda
  • 341
  • 1
  • 3

The issue with getBalance2 has to do with the asynchronous web3.eth.getBalance call. You need to make the entire function async and make that call await in order to correctly return a value to your callsite. The return wei line inside the callback is only returning from the callback, it does not propagate out as the return value to getBalance2. I think a fix would look something like this:

export async function getBalance2(address) {
   console.log("debug: helpers/getBalance2-A"); 
    try {
        return await objWeb3.eth.getBalance(address)
    } catch (err) {
        console.log("debug: helpers/getBalance2-E error=" + err);
    }
}

Edit: I was able to get a working version of your code here (the issue was with the rpcURL address -- it had to be http rather than https):

const Web3 = require('web3') 
const rpcURL = 'http://127.0.0.1:7545' 
const objWeb3 = new Web3(rpcURL);

async function getBalance(address) {
   console.log("debug: helpers/getBalance2-A"); 
    try {
        return await objWeb3.eth.getBalance(address)
    } catch (err) {
        console.log("debug: helpers/getBalance2-E error=" + err);
    }
}

async function demo() {
    const address = '0x1D41CB7d963a5B96687e80Be1b23320e40176F02';
    const balance = await getBalance(address);
    console.log(balance);
}

demo();

The issue with getBalance2 has to do with the asynchronous web3.eth.getBalance call. You need to make the entire function async and make that call await in order to correctly return a value to your callsite. The return wei line inside the callback is only returning from the callback, it does not propagate out as the return value to getBalance2. I think a fix would look something like this:

export async function getBalance2(address) {
   console.log("debug: helpers/getBalance2-A"); 
    try {
        return await objWeb3.eth.getBalance(address)
    } catch (err) {
        console.log("debug: helpers/getBalance2-E error=" + err);
    }
}

The issue with getBalance2 has to do with the asynchronous web3.eth.getBalance call. You need to make the entire function async and make that call await in order to correctly return a value to your callsite. The return wei line inside the callback is only returning from the callback, it does not propagate out as the return value to getBalance2. I think a fix would look something like this:

export async function getBalance2(address) {
   console.log("debug: helpers/getBalance2-A"); 
    try {
        return await objWeb3.eth.getBalance(address)
    } catch (err) {
        console.log("debug: helpers/getBalance2-E error=" + err);
    }
}

Edit: I was able to get a working version of your code here (the issue was with the rpcURL address -- it had to be http rather than https):

const Web3 = require('web3') 
const rpcURL = 'http://127.0.0.1:7545' 
const objWeb3 = new Web3(rpcURL);

async function getBalance(address) {
   console.log("debug: helpers/getBalance2-A"); 
    try {
        return await objWeb3.eth.getBalance(address)
    } catch (err) {
        console.log("debug: helpers/getBalance2-E error=" + err);
    }
}

async function demo() {
    const address = '0x1D41CB7d963a5B96687e80Be1b23320e40176F02';
    const balance = await getBalance(address);
    console.log(balance);
}

demo();

Source Link
Mark Toda
  • 341
  • 1
  • 3

The issue with getBalance2 has to do with the asynchronous web3.eth.getBalance call. You need to make the entire function async and make that call await in order to correctly return a value to your callsite. The return wei line inside the callback is only returning from the callback, it does not propagate out as the return value to getBalance2. I think a fix would look something like this:

export async function getBalance2(address) {
   console.log("debug: helpers/getBalance2-A"); 
    try {
        return await objWeb3.eth.getBalance(address)
    } catch (err) {
        console.log("debug: helpers/getBalance2-E error=" + err);
    }
}