I have a two smart contracts. One receives ether(ReceiveEther) and the other sends ether(SendEther).
I have it set so whoever made SendEther can only send to ReceiveEther if both contract creators are the same.
However, the ReceiveEther contract can receive from anyone.
How do I make it so ReceiveEther can receive from anyone only after SendEther has successfully executed once?
pragma solidity ^0.5.11;
contract ReceiveEther {
uint256 public count;
address public creator= msg.sender;
function () external payable {
require(count < 2);
count++;
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
contract SendEther {
address public creator= msg.sender;
uint256 public sendcount;
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");
require(sendcount < 1);
sendcount++;
}
}
I'm sure i could create another Sending contract that looks at the counter of ReceiveEther and works only if it is >1, but I don't want to deploy an extra contract.
Thank you for any help.
EDIT
My most recent attempt is where i added another counter and a while loop.
The idea is that now there is a button the contract creator must press. While it is pressed once, only he can send to the contract. If he presses it twice, he no longer is the only one who can send to it.
It compiles without error, but I cannot send ether at any point.
pragma solidity ^0.5.11;
contract ReceiveEther {
uint256 public clicksfromcreatorcount;
address public creator= msg.sender;
function increaseifcreatorclicks() external {
require(msg.sender == creator);
clicksfromcreatorcount++;
}
function () external payable {
require (clicksfromcreatorcount >=1);
while (clicksfromcreatorcount==1 ) {
require(msg.sender == creator);
}
}
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");
}
}
i get this message from the compiler
transact to SendEtherFirst.sendViaCall errored: VM error: revert.
revert The transaction has been reverted to the initial state.
Reason provided by the contract: "failed to send ether".
Maybe the issue is with the while loop or the extra "require". I'll keep looking into it. I'm using solidity.readthedocs to understand how i should be doing things.
require(count < 2)
logic looks like it was taken from a solution to a different question of yours (and I'm pretty sure I'm the one who gave that solution). What exactly is it doing here? Seems completely inappropriate to your description and goal. PLEASE - try to understand why specific pieces of code are being used before you just "throw them into your project"!I only want this contract to be able to receive something twice
- is that still part of the requirements? Your description says something completely different (and even contradicting to a certain extent)!