0

I am following a tutorial to write a registrant contract and when I try to submit transactions to the contract I always get error :it seems this transaction will fail. If you submit it, it may consume all the gas upi provide.
I create run a private block chain on my laptop and I deployed the contract through Mist. The source code are shown below:

pragma solidity ^0.4.18;

contract EventRegistrant {

    //basic class of registrant
    struct Registrant {
        uint amount;
        uint numTickets;
        string email;
    }

    //define variables
    address public owner;
    uint public numTicketsSold;
    uint public quota;
    uint public price;
    mapping (address => Registrant) registrantsPaid;

    event Deposit (address _from, uint _amount);
    event Refund (address _to, uint _amount);

    //Modifiers to define following functions
    //character "_" means this is a modifier
    modifier onlyOwner(){
        require(msg.sender != owner);
        _;
    }

    modifier soldOut(){
        require(numTicketsSold >= quota);
        _;
    }

    function EventRegistrant(uint _quota, uint _price) public {
        owner = msg.sender;
        numTicketsSold = 0;
        quota = _quota;
        price = _price;
    }

    function buyTicket(string email, uint numTickets) soldOut public payable {
        uint totalAmount = price * numTickets;
        require(msg.value > totalAmount);
        if (registrantsPaid[msg.sender].amount > 0) {
            registrantsPaid[msg.sender].amount += msg.value;
            registrantsPaid[msg.sender].numTickets += numTickets;
            registrantsPaid[msg.sender].email = email;
        } else {
            registrantsPaid[msg.sender].amount = totalAmount;
            registrantsPaid[msg.sender].numTickets = numTickets;
            registrantsPaid[msg.sender].email = email;
        }

        numTicketsSold += numTickets;

        if (msg.value > totalAmount) {
            uint refundAmount = msg.value - totalAmount;
            require(msg.sender.send(refundAmount));
        }

        Deposit(msg.sender, msg.value);
    }

    function refundTickets(address buyer) onlyOwner public {
        if (registrantsPaid[buyer].amount > 0) {
            if (this.balance >= registrantsPaid[buyer].amount) {
                registrantsPaid[buyer].amount = 0;
                numTicketsSold = numTicketsSold - registrantsPaid[buyer].numTickets;
                require(buyer.send(registrantsPaid[buyer].amount));
                Refund(buyer, registrantsPaid[buyer].amount);
            }
        }
    }

    function withdrawFunds() onlyOwner public {
        require(owner.send(this.balance));
    }

    function getRegistrantsAmountPaid(address buyer) public view returns (uint) {
        return registrantsPaid[buyer].amount;
    }

    //destructor, to delete the contract when there is no need
    function kill() onlyOwner public {
        selfdestruct(owner);
    }
}

This is how my contract looks like in the Mist. enter image description here This is error when I submit the transaction. enter image description here

Anyone can help me? thank you in advance.

1

buyTickets seems to only allow someone to buy a ticket if all the tickets are already sold (soldOut). Even if this isn't the source of the problem, it's almost certainly a mistake.

  • yes, you are right. I changed modifier soldOut and it worked. Thank you – Frankie Jan 21 '18 at 22:52
  • but there is an another question: when i called the buyTickets, the balance of buyer and contract do not change. The transaction is already confirmed by miner(I run on my own laptop). Can you check if I wrote event Deposit correctly? – Frankie Jan 21 '18 at 22:55
  • Are you sure that the balances don't change? Note that you seem to have set the price to 1 wei. (0.000000000000000001 ether), so the change may have been miniscule. – user19510 Jan 21 '18 at 22:57
  • Yes, I set it as 1 wei when I deploy contract. The balance actually changed but only 0.0000000000000001 ETHER. Thank you – Frankie Jan 21 '18 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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