0

I want to write a function let user deposit 1 eth to a specific address for registering purpose.

pragma solidity ^0.5.0;

contract Election {
    mapping(uint => UserRequest) public userRequest;
    // chairman account, let airline deposit to become a member
    address payable chairman;

    // Constructor
    constructor () public payable {
        chairman = msg.sender;
    }

    function register() public payable{
        chairman.transfer(1 ether);
    }
}

While I test the register() on the remix, the following error threw out.

transact to Election.register errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The constructor should be payable if you send value. Debug the transaction to get more information.

Some solution I found said I need to deploy it with a certain value, but what does that mean?

  • How exactly are you testing the register function (i.e., how do you call it and what msg.value do you pass to it)? – goodvibration Apr 28 at 16:39
  • I'm asking, because it looks like you want to get the funds (ether) to be transferred from the user's account (i.e., the caller of function register) to your own account (i.e., the account which you have deployed your contract with). But you do this fund-transferring via the contract, i.e., the user sends to the contract and the contract sends to your account. No problem with this method so far, only, you are assuming that there is always at least 1 ether left in the contract, while this fact depends on how much the users have sent each time one of them has called the register function. – goodvibration Apr 28 at 16:41
  • I am quite confused right now. What I want is there is an address called chairman who can gather all member register fee(which is 1 Ether). How I test: I deploy it with account A, switch the account to account B, and then click register on the remix IDE – Coda Chang Apr 28 at 16:56
1

As pointed by @goodvibration, it's probably trying to transfer 1 ether to the chairman, regardless of the amount the user sent to the contract when calling register.

If an user needs to send at least 1 ether everytime he calls this function, and you want to be sure of it, you can add a require statement and check the msg.value, like that :

    function register() public payable{
        require(msg.value >= 1 ether);
        chairman.transfer(1 ether);
    }
  • Thanks, now I understand the contract can have ETH as well. – Coda Chang May 3 at 22:32

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.