I want to create a contract, which has a deposit function.

But I also want, in the constructor, to start a "thread" that polls an external data source (via oraclize/chainlink or similar) at certain frequency.

Or instead of setting up a loop, should I just call each new block ?

contract SimpleOraclizeContract is usingOraclize {

    function SimpleOraclizeContract() {
      // something like this in java
       new thread().run(new Runnable(){
            if(oraclize_getPrice() > 100) {

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

Given the above code how could I call oraclize_getPrice("URL") (which I assume is inherited) every for 10 seconds, or every new block ? Completely outside of a payable function.

  • the simple answer: you can't. – Majd TL Sep 11 '20 at 6:58
  • @MajdTL nice. Any further info ? – NimChimpsky Sep 11 '20 at 7:00

Smart contracts does not allow for security reason to call outside services. You will not be able to start new threads, this feature does not exist. Can you give more details about your needs to see how we can help you ?

  • I thought you could oraclize a call ? – NimChimpsky Sep 11 '20 at 6:04
  • Oracles "inject" your data into the smart contract from outside. Smart contracts are not able to call outside services. – Emmanuel Collin Sep 11 '20 at 6:13
  • OK, I will expand question – NimChimpsky Sep 11 '20 at 6:15

What you are trying to do is not possible with smart contracts in this form.

  • A contract cannot automatically run periodically. It has to be invoked by a transaction
  • There are no threads in smart contracts, only single-thread synchronous execution
  • A contract cannot interact with off-chain services (e.g. an API)

If you want to have some kind of off-chain values in your contract you have to provide them with a transaction.

contract USDPrice {
    address admin;
    uint price;

    constructor() public {
        admin = msg.sender;    

    function setPrice(uint newPrice) external {
        require(admin == msg.sender, "only admin is allowed to set price");
        price = newPrice;

Then you can make a piece of software (e.g. in Java) which queries the API you want to use for changes. If it detects a change it initiates a call to setPrice with the value.

  • Hi thanks. I get the single threaded idea. But I thought oraclize was for external api calls, and chainlink ? – NimChimpsky Sep 11 '20 at 10:19
  • In essence Chainlink works like this. Of course in reality there is a lot more involved - especially making sure that the correct values are supplied to the contract. – Bobface Sep 11 '20 at 10:21
  • ok. But even without chainlink, oraclize can call an external api right ? – NimChimpsky Sep 11 '20 at 10:22
  • No. A smart contract can never directly call an API or anything outside the chain. It can only interact with other contracts during execution. – Bobface Sep 11 '20 at 10:25

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.