0

I want to transfer per second of given time but I got Error: Note: The called function should be payable if you send value and the value you send should be less than your current balance.

pragma solidity ^0.8.3;

contract ReceiveEther{

    fallback() external payable {}

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

contract SendEther {

    uint balance;
    
    function invest() external payable{
         balance += msg.value;
    }

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


    function transferBalance(address payable _to, uint endTime) public payable{
        uint startTime = block.timestamp;
        uint totalSecond = endTime + startTime;

        require(_to != msg.sender, "Can't transfer to ownself");
        require(msg.value <= balance, "Insufficent balance.");
        require(msg.value > 0 , "Amount is zero.");
        require(totalSecond > startTime, "stop time before the start time.");

        uint amount = msg.value;
        uint transferPerSecond = amount/endTime;

        while(startTime <= totalSecond){
            uint timeLeft = totalSecond - block.timestamp;
            if(timeLeft > 0){
                _to.transfer(transferPerSecond);
            }
        }
    }
}

1 Answer 1

0

literally everything is wrong with your transferBalance function. Marking a function payable means that it's designed to receive ether, not to send it. Your second require statement doesnt make any sense (it basically prevents a user to send more ether to the contract than what's already in there (if there's 1eth in the contract and the user tries to send 2 eth it would revert). Your while loop is completely broken (After the first iteration, timeleft will be equal to endTime, after the 2nd iteration trying to compute timeleft will result in an underflow. Btw startTime will always be smaller than totalSecond, which makes it an infinite loop). Also you can't perform any action automatically like this with solidity (sending 1 eth every second simply won't happen, sending 1 eth every block maybe, but you'd need an off chain script for that, that would call on every block a function that sends 1ETH)

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.