1

I am encountering this error while deploying my smart contract on the Ganache blockchain, can anyone help me fix it?

Error:  *** Deployment Failed ***

"Coffee" hit an invalid opcode while deploying. Try:
 * Verifying that your constructor params satisfy all assert conditions.
 * Verifying your constructor code doesn't access an array 
 * List item

I deployed it on the test net and it showed no error, but when I deployed it on Ganache, it returned this error.

this is my deployment script

var MyContract = artifacts.require("coffee");

module.exports = function (deployer) {
  deployer.deploy(MyContract);
};

this is my contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract CoffeePurchase {
    address public owner;
    mapping(address => string) private messages;
    struct purchase {
        string from;
        string to;
        address addFrom;
        address addTo;
    }
    purchase[] public purchases;
    event PurchaseMade(address indexed buyer, uint256 amount, string message);

    constructor() {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "Only the owner can access this function");
        _;
    }

    function purchaseCoffee(string calldata _from,string calldata _to,address _addTo,string memory message) public payable {
        require(msg.value > 0, "Coffee price must be greater than 0 wei");
        messages[_addTo] = message;
        purchases.push(purchase(_from,_to,msg.sender,_addTo));
        emit PurchaseMade(msg.sender, msg.value, message);
    }
    function showDetails() external view returns(purchase[] memory){
        return purchases;
    }
    function getMessage() public view returns (string memory) {
        return messages[msg.sender];
    }

    function withdrawFunds() public onlyOwner {
        payable(owner).transfer(address(this).balance);
    }
}
4
  • Which version of Solidity? Which network fork in Ganache?
    – ceseshi
    Aug 16, 2023 at 20:09
  • Please provide your contract code inside your question body, as well as your deploy script.
    – Rohan Nero
    Aug 16, 2023 at 21:38
  • 1
    @ceseshi i am using solc version0.8.13 and network fork london Aug 17, 2023 at 12:44
  • Note to anyone trying to answer: The contract compiles and deploys on Remix while using 0.8.13 solidity and on London fork.
    – Rohan Nero
    Aug 17, 2023 at 21:37

1 Answer 1

0

I had the same problem and it's solved by doing the following..!

Go to truffle-config.js and change the solc version to "0.8.0" or "0.8.2" (both worked fine for me)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.