0

I want to add a thing to my contract where every 10minutes all the ETH from a contract is sent to my wallet. I don't know how to do it. Any help appreciated

1

There is no way to have timers in smart contracts. You'll have to write a script off-chain that sends a transaction to the contract telling it to withdraw. Alternatively you could just forward all payments to the contract to your address.

  • Well I am just thinking of something like a lottery - contract gets money and on a specific date sends all the money to a random person. The problem is that the lottery would be ran like every 10minutes. Thanks for your answer, but is there not even a way to get time from an external server or something? – Jorikas May 7 '18 at 13:28
  • The contract can get the current time from itself, it doesn't need to get time from an external server. But the contract has to be executed by a transaction. – flygoing May 7 '18 at 13:30
  • Ok thanks, I'll work with it :) would upvote but I have no rep :) – Jorikas May 7 '18 at 13:34
  • No problem (: Feel free to message me on gitter @flygoing if you have any more questions – flygoing May 7 '18 at 13:41
  • 1
    Yeah, I know, but I wanted to warn people that don't know it ;) – Florian Castelain May 7 '18 at 19:55
1

There is a way to do this.

The contract can get the time directly but cannot execute itself.

You can use services like Chronos, which allows you to schedule calls to your contract in a simple way. You will find examples of how to do exactly what you are asking for here look at the examples. This can be used on Rinkeby for testing. You can, for instance, request your contract to be called every N blocks.

Others services are available as well, like Oraclize and ethereum alarm clock

Example of how to do what you asked using in Chronos (contract balance should be enough to pay for gas or you can pre-charge you contract address using the functions of Chronos, you can always claim back what you don't use):

pragma solidity ^0.4.20; 


contract _Chronos {
    function registerCall(address contractAddress, uint256 callOnBlock, uint256 gasAmount) public returns (uint256);
    function clientWithdraw(uint256 value) public;
    function clientFunding(address contractAddress) public payable;

}

contract Client {
    function setCallrequest(uint256 blockNumber, uint256 gasAmount) public;
    function callBack() public;
    function withdrawFromChronos(uint value) public;
    function getDepositsFromChronos() public payable;
}



contract YourContract is Client {
    address public chronosAddress;
    address public admin;

    function YourContract() public {
        chronosAddress = address(0x4896FE22970B06b778592F9d56F7003799E7400f);
        admin = msg.sender;
    }




    function setCallrequest(uint256 blockNumber, uint256 gasAmount) public {
        _Chronos ChronosInstance = _Chronos(chronosAddress);
        uint256 costs = ChronosInstance.registerCall(address(this), blockNumber, gasAmount);
        require(address(this).balance >= costs);
        ChronosInstance.clientFunding.value(costs)(address(this));
    }


    function callBack() public {
        setCallrequest(block.number + 40, 200000); // 40 indicated how many blocks to wait to receive the call, 40 blocks * 15sec/block = 600 sec = 10 min

        admin.transfer(this.balance); // you may want to leave some ether in your contract to pay for the next call. The gas used is small but is not zero, in this example 200000 gas is passed. If this is not consumed the contract will save it and you can withdraw what you do not use. if the gas you pass is not enough the call is not executed. 
    }



    function () public payable {}



}

Disclaimer: I wrote the code for Chronos.

Let me know if this works for you. hope it helps

  • Thanks. I currently don't have the development stuff. I just want to develop for this specific lottery, so it'll take a while to make. Thanks :) – Jorikas May 7 '18 at 15:06
  • you do not need to develop anything else, the code I show do what you asked for, you can add all your other functions to this contract, let me know if I can help. – Jaime May 7 '18 at 15:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.