function cancelEvent(uint256 eventId) external returns (bool){ require(eventExists[eventId], "Event does not exist"); require(events[eventId].eventstatus != Status.CLOSED, "Event is closed"); require(events[eventId].eventHost == msg.sender, "Only event host can cancel the event");

    // check event balance before cancelling and refunding
    require(eventBalance[eventId] > 0, "Event balance is zero or negative");

    // cancel the event
    events[eventId].eventstatus = Status.CANCELED;
    emit eventCancelled(eventId);

    // transfer funds to ticket holders
    for (uint256 i = 0; i < eventToTicketIds[eventId].length; i++) {
        uint256 ticketId = eventToTicketIds[eventId][i];
        uint ticketQty = tickets[ticketId].ticketQty;
        address payable ticketHolder = payable(ticketHolders[ticketId]);
        uint refundAmount = ticketQty * events[eventId].ticketPrice;

        // transfer refund amount and update ticket and event balance
        if (eventBalance[eventId] >= refundAmount) {
            eventBalance[eventId] -= refundAmount;
            payOut(ticketHolder, refundAmount);

            // shift remaining tickets left and delete refunded ticket
            uint256[] storage holderTicketIds = buyerToTicketIds[ticketHolder];
            for (uint256 j = 0; j < holderTicketIds.length; j++) {
                if (holderTicketIds[j] == ticketId) {
                    for (uint256 k = j; k < holderTicketIds.length - 1; k++) {
                        holderTicketIds[k] = holderTicketIds[k+1];

            tickets[ticketId].ticketstatus = TicketStatus.Refunded;
            emit ticketRefunded(ticketHolder, eventId, ticketId, refundAmount);
    delete eventToTicketIds[eventId];
    delete events[eventId];
    eventExists[eventId] = false;

    return true;

1 Answer 1


To help you better, it's necessary more information about the specific error message or behavior that occurs when there are purchased tickets. For example, is there a specific error message that is displayed? How does the purchase ticket function works?

Based on the information you've provided, I suspect that the function is failing when attempting to shift remaining tickets left and delete refunded tickets. If the eventToTicketIds array contains a large number of ticket IDs, shifting them left for every refunded ticket could consume a significant amount of gas and potentially exceed the gas limit for the transaction.

As an advice, it's not recommended to have a shift operation inside a function, especially for big arrays, as it can result in high gas costs.

Without more information about the specific error message or behavior that occurs when there are purchased tickets, it's difficult to say for sure what the issue might be. It would be helpful to see any error messages or logs that are generated when the function fails, as well as information about the input values used when calling the function.

I hope this helps!

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.