[SOLVED]Error doing execute function "Cannot estimate Gas"

Hi, I’m trying to execute a write function on my contract here’s my code and the ABI but I’m getting an error:

cannot estimate gas; transaction may fail or may require manual gas limit 
const handleOk = async () => {
        //setConfirmLoading(true);
        let options = {
            chain: "rinkeby",
            contractAddress: props.contractAddress,
            functionName: "setPriceForAll",
            abi: props.abi,
            params : {
                _priceList : [0],
                _id : [0]
            }
        }
        try {
            let tx = await Moralis.executeFunction(options)
            .then(() => {
                console.log(tx);
                setShowModal(false);
            })
        } catch (error) {
            console.log(error.message);
        }
    }

my abi function


"inputs": [
        {
          "internalType": "uint256[]",
          "name": "_priceList",
          "type": "uint256[]"
        },
        {
          "internalType": "uint256[]",
          "name": "_id",
          "type": "uint256[]"
        }
      ],
      "name": "setPriceForAll",
      "outputs": [],
      "stateMutability": "nonpayable",
      "type": "function"
    },

my props.address is not null, my function works correctly in remix (in other words my smart contract is working) and the error occur when calling the function with Moralis.

check if youre passing the right params values/types

Well, my function requires 2 params which are uint256[], array of numbers.
You can see in my options object the params field contains the 2 arrays with 1 number in it.

Im dont think this error is related to moralis. Most of the times it comes from wrong params or one of the require() checks is not fulfilled

Well here is my solidity code for the function:


function setPriceForAll(uint256[] memory _priceList, uint256[] memory _id) public onlyOwner {
        require(!saleStarted, "You cannot change price of your items once the sale started");
        require(_priceList.length <= tierQuantity.length, "Your pricelist is bigger than the amount of tier you have");
        for(uint256 i = 0; i<_id.length; i++) {
                require(_id[i]<tierId.current(), "tier specified does not exist");
                tierPrice[_id[i]] = _priceList[i];
        }
    }

The first require is good, it is also the case for the second one with both array of length 1.

I can maybe fetch the tierId.current() to be sure it’s correct but it has to be. If you see any error in my code above let me know

EDIT: I removed all the required from my function but I am still having the error so I am not sure what’s going on.

@louisleroi the function has a modifier onlyOwner so are you calling the function with the correct address that is the onlyOwner?

Hi, yes I am
and on my front end only the owner can see the button anyway.
Also I have other functions that are onlyOwner but they work fine.

EDIT: I’ve noticed something weird. I can call the function from any network but Rinkeby, which is weird given the fact that my contract is deployed on the rinkeby network.
EDIT2: This morning after restarting I can execute the function properly not sure what was going on yesterday. Thanks anyway

1 Like