Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

287
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda