0

How would I set a limit to the amount of people who could send ether to a contract? I can't find anything on google or youtube. Ideally the contract would stop taking inputs after 2(or however many) different people have sent an amount.

Below is simple code from https://solidity-by-example.org/sending-ether/ for sending ether to an address, but i'm not sure what I would do. I'm using remix and running it with ganache on my machine. Is it possible to make it read-only after X transactions?

https://github.com/CrazybutSolid/ethereum-lottery/blob/master/contracts/lottery.sol has a kind of similar example but it seems too complicated and I'm still a noob.

pragma solidity ^0.5.16;

contract Receive {

    function () external payable {}

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

contract Send {

    function sendViaCall(address payable _to) public payable {

        (bool sent, bytes memory data) = _to.call.value(msg.value)("");
        require(sent, "Failed to send Ether");
    }
}

Thank you

1

If you want to restrict it to at most 2 transfers, then you can do:

uint256 public count;

function () external payable {
    require(count < 2);
    count++;
}

If you want to restrict it to transfers from at most 2 different senders, then you can do:

uint256 public count;
mapping (address => bool) public senders;

function () external payable {
    if (senders[msg.sender] == false) {
        require(count < 2);
        senders[msg.sender] = true;
        count++;
    }
}
| improve this answer | |
  • that's perfect, thank you. – rubecuber May 7 at 18:03
  • @rubecuber: You're welcome. – goodvibration May 7 at 18:06

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.