[Ethereum] How to automatically schedule the execution of a function in the future

contract-developmentsolidity

I need a function in my contract to be executed daily by itself.
I know that Ethereum Alarm Clock and Oraclize do it but none of them worked for me.

For example this code using Oraclize

pragma solidity ^0.4.2;    

import "http://github.com/oraclize/ethereum-api/oraclizeAPI_0.4.sol";

contract MyContract is usingOraclize {

    uint public tmp = 0;
    function callThisToStart() {
        oraclize_query(60, "URL", "");
    }

    function __callback(bytes32 myid, string result) {
        if (msg.sender != oraclize_cbAddress()) throw;
        doSomething();
        callThisToStart();
    }

    function doSomething() {
        tmp++;
    }

}

I do not really know why it does not work (tmp is not increased)
I do not care about using EAC or Oraclize, but I really want to get that for example every minute the value of a variable increases

Thank you.

Best Answer

Its not necessary to use Ethereum Alarm Clock and Oraclize if you just want to execute a function.

Lets get down to basic. What is a function?

A function is a type of procedure or routine. In solidity, you call a function to modify some state variables. And you can know that a state has been modified when you get the value of that variable.

Whichever variable you want to modify, you need not use it as a variable. Instead, you can declare that variable as a function, which returns the value on time variables of the blockchain, example, blockNumber, block.timestamp.

For answering your question, you can do the above described process by the contract given below.

This contract increases the value returned by tmp every minute. And there are separate functions to enable and disable this feature.

pragma solidity ^0.4.2;    

contract MyContract {

    uint startTime;
    function tmp() public view returns(uint){
        require(startTime != 0);
        return (now - startTime)/(1 minutes);
    }
    function callThisToStart() {
        startTime = now;
    }
    function callThisToStop() {
        startTime = 0;
    }
    function doSomething() returns (uint){
        return tmp();
    }

}
Related Topic