HW22: Ticket sale

Problem statement

Implement a smart contract for selling tickets to events.

Example scenario

Sziget Festival decides to use this contract to sell day tickets. They create an instance for 100,000 tickets, setting the price at the wei equivalent of 10,000HUF.

In this case, the ticket identifiers go from 1 to 100,000. People can check the type of any ticket based on this id on the festival's website, e.g. tickets 1 to 10,000 are for Monday, 10,001 to 20,000 are for Tuesday, etc.

Alice and Bob both buy tickets, #784 and #10,322 respectively, paying the wei equivalent of 10,000HUF each. However, Alice wants to go on Tuesday instead, and Bob would rather go on Monday. Thus, they decide to swap. For this, Alice submits a swap offer between their Ethereum addresses. Bob accepts the offer. In the end, Alice owns ticket #10,322, while Bob owns ticket #784.

On the day of the festival, the festival staff scans Alice's QR code at the entrance. The system checks whether Alice has a ticket and whether that ticket is valid at that time. After this, Alice can go in and enjoy the festival.

Contract interface

Contract skeleton

pragma solidity ^0.4.21;

contract TicketSale {

    // <contract_variables>

    // </contract_variables>

    function TicketSale(uint16 numTickets, uint256 price) public {
        // TODO
    }

    function buyTicket(uint16 ticketId) public payable {
        // TODO
    }

    function getTicketOf(address person) public view returns (uint16) {
        // TODO
    }

    function offerSwap(address partner) public {
        // TODO
    }

    function acceptSwap(address partner) public {
        // TODO
    }

    function withdraw() public {
        // TODO
    }

}