5

I want to have my smart contract call a function every 24 hours automatically. Can this be achieved as follows?

pragma solidity ^0.4.2;    

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

contract MyContract is usingOraclize {

    function callThisToStart() {
        oraclize_query(1*day, "URL", "");
    }

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

    function doSomething() {
        // then this should run every 24 hours
    }

}

I don't understand how Oracle works yet so this may be completely wrong - callThisToStart() would be called, which would then run the __callback function a day later, which will then run some code in doSomething() and call callThisToStart() afterwards so the process would be repeated in a day.

0
2

It should work. Have a look at this example.

The example just adds a clean check:

function update() payable {
        if (oraclize.getPrice("URL") > this.balance) {
            newOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee");
        } else {
            newOraclizeQuery("Oraclize query was sent, standing by for the answer..");
            oraclize_query(60, "URL", "json(https://api.kraken.com/0/public/Ticker?pair=ETHXBT).result.XETHXXBT.c.0");
        }
    }

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