0

I am trying to get an account balance into a variable in web3.js. I don't want it in a function, just stored as a variable. This is the equivalent code I have in web3.py, which is what I want it to do:

balance = web3.eth.balance(address)

such that

print(balance)

prints the balance of my account. I have tried all of the following, and none have worked:

var balance = web3.eth.getBalance(address);
var balance = await web3.eth.getBalance(address);
web3.eth.getbalance(address (err,bal) => {balance = bal})

and others, and nothing worked. To just print to the console, this worked:

web3.eth.getBalance(address).then(console.log);

but I want to save to a variable. How do I do this? It should be pretty simple.

2 Answers 2

0

web3.eth.getBalance() function returns a promise, which means that the balance will not be immediately available when you call the function. Instead, you have to handle the promise by either using then() method (which you already did) or async/await:

async function getBalance(){
    try {
        const balance = await web3.eth.getBalance(account);
        console.log(balance);
    } catch (error) {
        console.log(error);
    }
}
getBalance();
4
  • This isn't working. I don't want to just print the result to the console. I want to store it as a variable. So what I want is something like: var balance = web3.eth.getbalance(address) balance = balance * 2 console.log(balance) with no problem.
    – Ilan Katz
    Jan 25, 2023 at 21:05
  • The code snippet I provided was an example to demonstrate how you can deal with promise. You can start from there and make whatever changes you want.
    – LAYcodes
    Jan 25, 2023 at 21:51
  • It would be helpful if you provide the error your get
    – LAYcodes
    Jan 25, 2023 at 21:52
  • I don't get an error, it just doesn't return what I want. I don't want to just print the value, I want it saved to a variable, so I can do stuff with it later. When I do return (balance) instead of console.log(balance) I still get a pending promise.
    – Ilan Katz
    Jan 25, 2023 at 22:25
0

you can use the await since is inside a async function:

async function getBalance(address) {
    const balance = await web3.eth.getBalance(address);
    return balance;
}
let balance = await getBalance(address);
console.log(balance);

alternatively, using promise should work too:

let balance = 0
web3.eth.getBalance(address).then((bal) => { 
    balance = bal 
    console.log(balance);
});
2
  • This didn't work. I want the following code, basically: var balance = web3.eth.getBalance(address) console.log(balance). I don't want the console.log inside the parenthesis of the .then.
    – Ilan Katz
    Jan 25, 2023 at 21:04
  • Javascript is asynchronous and use Promises (nested "then") is just one possible programing style. It is possible to put the entire code inside the "then", or use the first example wich is more close to "pythonic" way.
    – Rafael
    Jan 25, 2023 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.