I am getting an Invalid Opcode error while calling the refundAmount function which will refund the amount to the customer and cancel his/her ticket bookings. registrations is a structure mapping and the structure includes 3 variables that are in the below manner.

struct Registration {

uint8 noOfTickets;
uint amount;
string email;
address payable public owner;
uint public ticketsSold;
uint8 public totalTickets;
uint public price;

mapping (address => Registration) public registrations;

The below is the refundAmount function. I could have used msg.sender for this but my javascript takes care of it by using the coinbase address.

    function refundAmount(address payable _customer)
                      payable {
    if(registrations[_customer].amount > 0){
        if(address(this).balance >= registrations[_customer].amount) {

            ticketsSold = ticketsSold - registrations[_customer].noOfTickets;
            registrations[_customer].noOfTickets = 0;
            registrations[_customer].email = " ";
            uint refundCustAmount = registrations[_customer].amount;
            registrations[_customer].amount = 0;
            emit Refund(_customer, refundCustAmount);


Please help me. Thank you in advance and Cheers to Ethereum!!

Edit: - This works completely fine on Rinkeby testnet but reverts while using Ganache.

  • Is _customer a contract? Where are you testing the contract?
    – Ismael
    Commented Apr 22, 2021 at 14:08
  • No customer is the address. Also, I am testing it on truffle ganache.
    – Yash Attal
    Commented Apr 23, 2021 at 13:09
  • Which solidity, ganache, truffle version are you using? It is ganache or geth private testnet? Did you try with a more recent versions? Unless you use recent solidity version with an old ganache it is unlikely it will cause an invalid opcode. If you were using a geth private testnet then it is likely you didn't enable newer opcodes.
    – Ismael
    Commented Apr 23, 2021 at 19:58
  • I am using ganache. I have deployed the same using remix and it works for me but after I deployed it using truffle, at the time of calling this function throws me this error. The Solidity version that I am using is 0.5.0. Ganache is upto date. I was able to interact with all the functions when I deployed the same using Remix on Ganache using injected web3.
    – Yash Attal
    Commented Apr 24, 2021 at 5:08
  • You can take a look at my github repo if required. github.com/attalyash/Event-Registration
    – Yash Attal
    Commented Apr 24, 2021 at 6:18

2 Answers 2


Hitting an invalid opcode typically means you're interacting with an old contract incorrectly, probably calling it with the wrong ABI. For cases were you want to debug this, but don't want to submit verified sources to a third party, you can use the local debugger in Contract Library.


would recommend you too to deploy this contract, verify it and add it to tenderly (https://tenderly.co/) and debug the issue. Secondly would recommend you to use call() instead of transfer it is less costly after the Istanbul fork.

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.