0

I am receiving the error "Variable declarations can only be used inside blocks." on line 45 ( (bool sent, ) = guest.call{value: 1 ether}(""); ). when attempting to use the call method to send ether and I don't know why. I don't understand what the error is referencing here by "blocks"

//SPDX-License-Identifier: MIT

pragma solidity 0.8.14;

contract RSVP {

    event RSVPd (address guestWhoRSVPd, string message);
    event checkedIn (string message);
    event fundsHaveBeenDispersed (string message);

    address public Host;
    address[] public rsvpAddresses;
    address[] public checkedInAddresses;

    enum Status {RSVPInProgress, RSVPCheckInOver}
    Status public status;

    constructor() {
        Host = msg.sender;  
    }

    /// Only the Host can call this function
    error OnlyHost();

    modifier onlyHost() {
        if (msg.sender != Host) {
            revert OnlyHost();
        }
        _;
    }

    function rsvp() public payable {
        require(msg.value == 1 ether, "You must pay 1 ether to RSVP");
        rsvpAddresses.push(payable(msg.sender));
        emit RSVPd(msg.sender, "You have RSVP'd");
    }

    function getBalance() public view onlyHost returns (uint) {
        return address(this).balance;
    }

    function eventStartCheckIn(address payable guest) public onlyHost {
        for (uint i = 0; i < rsvpAddresses.length; i++) {
            if (guest == rsvpAddresses[i])
            (bool sent, ) = guest.call{value: 1 ether}("");
            require(sent, "Failed to send Ether");
            checkedInAddresses.push(guest);
        }
    }

    function checkInOver() public onlyHost {
        status = Status.RSVPCheckInOver;
        emit checkedIn ("The check-in period is over!");
    }

    function disperseRemainingFunds() public onlyHost {
        require(status == Status.RSVPCheckInOver);
        uint arraylength = checkedInAddresses.length;
        uint amountToPay = (address(this).balance / checkedInAddresses.length);
        uint i;
        for(i = 0; i < arraylength -1; i++) {
            payable(checkedInAddresses[i]).transfer(amountToPay); 
        }
        payable(checkedInAddresses[arraylength - 1]).transfer(address(this).balance);
        emit fundsHaveBeenDispersed ("The funds have been dispersed");
    }
    
}

1 Answer 1

0

The relevant part comes down to this at the moment:

if (guest == rsvpAddresses[i])
(bool sent, ) = guest.call{value: 1 ether}("");
require(sent, "Failed to send Ether");

Which is the same as:

if (guest == rsvpAddresses[i]) {
  (bool sent, ) = guest.call{value: 1 ether}("");
}
require(sent, "Failed to send Ether");

Written that way, it becomes more clear that sent is declared inside the if-block, and the require call afterwards is outside of the if-block.

Please try it like this instead:

if (guest == rsvpAddresses[i]) {
  (bool sent, ) = guest.call{value: 1 ether}("");
  require(sent, "Failed to send Ether");
}

Your Answer

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

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