Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

293
Views
How do I grab Contract Balance with useEffect?

Situation

I have created a smart contract and imported the abi and contract address into my react application.

I have successfully accessed the methods and options within my react app. I used useEffect with no issues.

Problem

However, I am struggling to get the balance of the contract. I have tried everything I understand with no result.

This code works fine

import web3 from "./web";
import { useState, useEffect } from "react";
import Lottery from "./Lottery";

const [isManager, setManager] = useState(
        "<em>Loading manager address...</em>"
    );

    useEffect(() => {
    const getManager = async () => {
        const mangWallet = await Lottery.methods.manager().call();
        console.log(mangWallet);
        setManager(mangWallet);
    };

    getManager();
}, []);

This does NOT work - the balance remains 0. More importantly, the console log doesn't even show 0. It's as if the entire getBalance function is not running?

const [balance, setBalance] = useState(0);

useEffect(() => {
        const getBalance = async () => {
            const contBal = await web3.eth.getBalance(Lottery.contract_address);
            setBalance(contBal);
            console.log(balance);
        };
        getBalance();
    }, []);

A few things to be aware of:

  • contract_address is holding the smart contract address on-chain, stored in the Lottery.js file
  • these two code snippets are in one file - app.js. Meaning there are two useEffect functions.
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'm not sure what your problem. But I think this async const contBal = await web3.eth.getBalance(Lottery.contract_address); can throw exception. So I think you need to add try catch like this:

const [balance, setBalance] = useState(0);

useEffect(() => {
    const getBalance = async () => {
        try {
          const contBal = await web3.eth.getBalance(Lottery.contract_address);
          setBalance(contBal);
          console.log(balance);
        } catch(error) {
          console.log(error);
        }
    };
    getBalance();
}, []);

About the console.log(balance), it alway log 0. Because it's behavior of useEffect. So you need to create another useEffect if you want to see the balance value change like this:

useEffect(() => { console.log(balance) }, [balance])
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!