Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please tell me how to do it.contract EthUsdPriceTicker is usingOraclize #106

Open
chitokiser opened this issue Jan 28, 2022 · 0 comments
Open

Comments

@chitokiser
Copy link

The examples in the book do not compile because they are not interactive.
Please tell me how to do it.

Using Oraclize to update the ETH/USD exchange rate from an external source

//MIT License

/*
ETH/USD price ticker leveraging CryptoCompare API

This contract keeps in storage an updated ETH/USD price,
which is updated every 10 minutes.
*/

pragma solidity ^0.4.1;

import "https://github.com/provable-things/ethereum-api/blob/master/oraclizeAPI_0.5.sol";

/*
"oraclize_" prepended methods indicate inheritance from "usingOraclize"
*/
contract EthUsdPriceTicker is usingOraclize {

uint public ethUsd;

event newOraclizeQuery(string description);
event newCallbackResult(string result);

function EthUsdPriceTicker() payable {
    // signals TLSN proof generation and storage on IPFS
    oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS);

    // requests query
    queryTicker();
}

function __callback(bytes32 _queryId, string _result, bytes _proof) public {
    if (msg.sender != oraclize_cbAddress()) throw;
    newCallbackResult(_result);

    /*
     * Parse the result string into an unsigned integer for on-chain use.
     * Uses inherited "parseInt" helper from "usingOraclize", allowing for
     * a string result such as "123.45" to be converted to uint 12345.
     */
    ethUsd = parseInt(_result, 2);

    // called from callback since we're polling the price
    queryTicker();
}

function queryTicker() external 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...");

        // query params are (delay in seconds, datasource type,
        // datasource argument)
        // specifies JSONPath, to fetch specific portion of JSON API result
        oraclize_query(60 * 10, "URL","json(https://min-api.cryptocompare.com/data/price?\fsym=ETH&tsyms=USD,EUR,GBP).USD");
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant