// 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
    }
};

