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

283
Views
Web3.js-- getBalance to web3.utils.fromWei, store the value in a variable to be used in a table

So ive tried figuring this out for a few hours and I can console.log the balance, but I cannot seems to store the balance in a var to be used later. The goal is the get the users account balance (eth) and display it in the table. Anyway heres what I got.

 
 function App(){
 
 ///.....////
 
 
 const Web3 = require("web3");
  const web3 = new Web3(
    Web3.givenProvider ||
      "https://ropsten.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161"
  );

  async function checkBalance() {
    try {
      await web3.eth.getBalance(account).then(web3.utils.fromWei());
    } catch (error) {
      console.log(error);
    }
  }
  
  
  ////.....//// {checkBalance} to be displayed in a table
  
  <Table variant="striped" width="100%">
            <TableCaption>Token balances of {account}</TableCaption>
            <Thead>
              <Tr>
                <Th>Token</Th>
                <Th>Contract Address</Th>
                <Th>Balance</Th>
              </Tr>
            </Thead>
            <Tbody>
              <Tr>
                <Td>ETH</Td>
                <Td>0x0000000000000000000000000000000000000000</Td>
                <Td>{checkBalance}</Td>
              </Tr>
            </Tbody>
            <Tfoot>
              <Tr>
                <Th>Token</Th>
                <Th>Contract Address</Th>
                <Th>Balance</Th>
              </Tr>
            </Tfoot>
          </Table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

The error I get in the console is "Warning: Functions are not valid as a React child. This may happen if you return a Component instead of from render. Or maybe you meant to call this function rather than return it."

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The function web3.eth.getBalance(address).then() requires a function to be passed into the then() clause.

In the snippet below I'm passing a function that accepts balanceInWei as a parameter. Only then you can pass balanceInWei variable to the web3.utils.fromWei(balanceInWei) function for it to return the ETH balance of the address

const Web3 = require("web3");
const web3 = new Web3("https://ropsten.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161");
let address = "0x0000000000000000000000000000000000000000";
let balance;

async function checkBalance() {
    try {
        web3.eth.getBalance(address).then((balanceInWei) => {
            balance = web3.utils.fromWei(balanceInWei);
            console.log("Balance in wei:", balanceInWei);
            console.log("Balance in ETH:", balance);
        });
    } catch (error) {
        console.log(error);
    }
}

checkBalance();

Additionally you shouldn't post your infura links. You made the credentials of your infura project public by posting it here. Next time replace it with eq. https://ropsten.infura.io/v3/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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!