2

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.

3
  • 2
    You need to be more detailed with your question, include code, and explain why Ethereum Alarm Clock and Oraclize didn't work. Please refer to How To Ask A Question
    – ReyHaynes
    Mar 24, 2018 at 17:25
  • Sorry, its my first time.
    – nestor-gm
    Mar 24, 2018 at 18:20
  • to get started, try commenting out the callThisToStart(); call from within the __callback function and check if that works: in order for this to work, you need to have funds available in the contract as the first oraclize call is the only free one - alternatively just comment the last line of your callback function! Mar 24, 2018 at 20:55

1 Answer 1

2

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();
    }

}
6
  • Really love the clever coding here and works for this particular situation...however, if he actually needed the functionality to actually execute a function at a scheduled time, this wouldn't work.
    – ReyHaynes
    Mar 27, 2018 at 15:01
  • Yes. The core idea of my answer is to make the contract such that it can run the scheduled txn itself.
    – Ayushya
    Mar 27, 2018 at 15:03
  • 2
    Sorry for being dumb here, but I can't see how is it that doSomething() has been executed by contract itself? Without actual call of doSomething() Jul 13, 2018 at 0:44
  • 1
    same here @KwahuNashoba, what if I want my function to just run in exactly one hour without manually going and doing it myself. I checked Ethereum Alarm Clock which does this service but I am unsure whether its active and running or not
    – Mr.O
    Aug 13, 2018 at 10:00
  • 3
    How is this scheduling anything? doSomething has to be called by something else, and then it just lazily calculates and returns the number of minutes since start time, and it's not even a transaction...
    – Saeb Amini
    Jan 24, 2019 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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