首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Oraclize指定时间的可靠调用

使用Oraclize指定时间的可靠调用
EN

Ethereum用户
提问于 2018-03-08 23:28:14
回答 1查看 692关注 0票数 0

我已经开始扎实地学习编码,并使用官方的Oraclize指南编写了下面的合同。

我的目标是:

  • 使update()函数在指定的时间约束下自行调用,以便更新温度测量。
  • 每次调用更新时(可能是电子邮件或文本文件上的任何其他方式)都会收到更新的通知。

我应该如何配置我的代码以实现这个结果?

代码语言:javascript
复制
    pragma solidity ^0.4.18;
    import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";

    contract WolframAlpha is usingOraclize { 



    string public temperature;
    mapping(bytes32=>bool) validIds;
    event LogNewOraclizeQuery(string description);
    event newTemperatureMeasure(string temperature);

    function WolframAlpha() {
        update();
    }

    function __callback(bytes32 myid, string result) {
        require(validIds[myid] ==true);
        require(msg.sender == oraclize_cbAddress());       
        temperature = result;
        newTemperatureMeasure(temperature);
        delete validIds[myid];
        // do something with the temperature measure..
    }

    function update() payable {
        if(oraclize_getPrice("WolframAlpha") > 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..");
            bytes32 queryId = oraclize_query(60,"WolframAlpha","Temperature in London");
            validIds[queryId] =true;
        }
    }
} 

仅供参考

  • 我使用的是在线可靠编译器"Remix“。
  • 将合同部署到ropsten testnet
EN

回答 1

Ethereum用户

发布于 2018-03-09 05:33:37

您可以按以下方式安排查询的执行。

来自Oraclize的文档:-

查询的执行可以在以后的日期安排。函数oraclize_query以秒为单位接受当前时间的延迟或将来的时间戳作为第一个参数。

代码语言:javascript
复制
 // Relative time: get the result from the given URL 60 seconds from now
 oraclize_query(60, "URL","json(https://api.kraken.com/0/public/Ticker?pair=ETHXBT).result.XETHXXBT.c.0")


 // Absolute time: get the result from the given datasource at the specified UTC timestamp in the future
 oraclize_query(scheduled_arrivaltime+3*3600,"WolframAlpha", strConcat("flight ", flight_number, " landed"));

从您的问题中,您必须为特定的timeperiods.For调用update函数--您必须使用JS来调度调用。在每次调用时,它都会发出LogNewOraclizeQuery.You可以对事件进行监视的事件,以通知最新的更新。

代码语言:javascript
复制
 var num=contractInstance.Result({},{fromBlock: 0, toBlock: 'latest' });
          num.watch(function(error,result){
 });
票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/42174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档