'Web3 contract call is always returning null

I am trying to connect to a smart contract deployed using Truffle. I am using Web3 from a React app but when I make a call to any of the contract functions I always get a null object as a result.

Here is my code:

import  CopcoinABI  from  "../../../src/contracts/Copcoin.json";

const [web3, setWeb3] = useState({});

  const loadBlockchain = async function() {
    const web3Instance = new Web3(Web3.givenProvider || 'http://127.0.0.1:7545');
    setWeb3(web3Instance);
 }

function getContract(web3){
    return new Promise(async (resolve, reject) => {
        try{
            const contract = new web3.eth.Contract(CopcoinABI.abi, process.env.CONTRACT_ADDRESS);
            resolve({result:true, contract});
        }
        catch (e){
            console.log(e);
            resolve({result:false});
        }
    })
}

export const totalSupply = async (web3) => {
    getContract(web3)
    .then(async (response) => {
        if(response.result){
            const contract = response.contract;
            contract.methods.totalSupply().call()
            .then((result) => {
                console.log(result);
                return result;
            });
            return totalSupply;
        }
        else{
            throw new Error(`Error conectando con el contrato.`);
        }
    });
}

The functions are separated in different classes but I decided to put them all here.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source