0

I have three contracts all interacting with each other.
Contract "Receive Ether" can receive ether from one of two "Send Ether" contracts
Contract "Send Ether First" should only be accessible by whoever deployed the contracts.
Contract "Send Ether Second" should be accessible by whoever.
When I run "Send Ether First" in Remix i get

"transact to SendEtherFirst.sendViaCall errored: VM error: revert.
revert  The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.  Debug the transaction to get more information. " 

Is it saying that the address of "Receive Ether" is the only one that can "Send Ether First"?
I can send ether via "Send Ether Second" no problem.
How do I make the sender of "Send Ether First" the same person who deployed the contracts? I can't seem to work it out.

pragma solidity ^0.5.11;

contract ReceiveEther {
uint256 public count;
address public owner= address(this);

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

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

contract SendEtherFirst {
    address owner;
    function sendViaCall (address payable _to) public payable {
        (bool sent, bytes memory data) = _to.call.value(msg.value)("");
       require(msg.sender == owner);
        require (sent, "failed to send ether");
    }
}

  contract SendEtherSecond {
    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 for any help

  • What a mess! What does the address of "Receive Ether" is the only one that can "Send Ether First" even mean??? Contract ReceiveEther is the only one that can call function sendViaCall in contract SendEtherFirst? How exactly are you make a function call (any function call) using this contract??? The only way I can think of, is by placing that function call inside the code of one of the function in that contract, and then call that function. But that's not to be seen anywhere in your code! – goodvibration May 15 at 18:28
  • Why is variable owner in contract SendEtherFirst not initialized anywhere? How do you expect require(msg.sender == owner) to complete without reverting, when owner remains 0? – goodvibration May 15 at 18:29
  • BTW, not related to your problem (but is related to the general mess in your code) - require statements are usually placed at the beginning of the function, otherwise they lose their main advantage of reducing the gas cost for the caller. – goodvibration May 15 at 18:30
  • Thank you, I'll keep working on it – rubecuber May 15 at 18:32
  • I can send ether via "Send Ether Second" no problem - has it occurred to you to compare the two functions? They are identical, with only one exception - the function which fails for you has ONE additional statement - require(msg.sender == owner);. Simple logic - this statement is the reason for the failure (and a very simple reason, as explained in my second comment above). Did you actually write this code yourself, or are you just testing someone else's work without understanding any of it? Just asking, because... well, how exactly are you planning to move forward once you solve this one? – goodvibration May 15 at 18:34
0

Moving the address to the "SendEtherFirst" Contract was essential. So was understanding address and msg.sender and how they can interplay.

pragma solidity ^0.5.11;

contract ReceiveEther {
    uint256 public count;
        function () external payable {
            require(count < 2);
                count++;
}

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

contract SendEtherFirst {
    address public creator= msg.sender;
        function sendViaCall (address payable _to) public payable {
            require(msg.sender == creator);
            (bool sent, bytes memory data) = _to.call.value(msg.value)("");
            require (sent, "failed to send ether");
    }
}

contract SendEtherSecond {
        function sendViaCall (address payable _to) public payable {
            (bool sent, bytes memory data) = _to.call.value(msg.value)("");
            require (sent, "failed to send ether");
    }
}

| improve this answer | |

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.