UNPKG

leumas-private-shared

Version:

Private React JSX Package For Leumas Shared Components, Headers, Footers, Asides, Login Pages, API Key Manager and much more. Styles and everything reusable to avoid DRY code across all of our subdomains

23 lines (16 loc) 807 B
// balanceUtils.js import Web3 from 'web3'; export const updateBalance = async (address, contractABI, contractAddress) => { try { const web3 = new Web3(window.ethereum); const contract = new web3.eth.Contract(contractABI, contractAddress); const balance = await contract.methods.balanceOf(address).call(); const balanceNumber = Number(balance.toString()) / 1e18; // Convert to a regular number and divide by 10^18 const roundedBalance = balanceNumber.toFixed(2); // Round to 2 decimal places console.log("Balance:", roundedBalance); return roundedBalance; } catch (err) { console.error("Error fetching balance", err); return null; // Returning null to signify an error in fetching balance } };