Giter Site home page Giter Site logo

oracle-infinity's People

Contributors

heyacherry avatar mzxyz avatar xlc avatar yi-kuai-lian-xi avatar

Stargazers

 avatar

Watchers

 avatar  avatar

oracle-infinity's Issues

Averaging all the prices to make sure the value is valid

There has three steps for data processing:

  1. filter value == 0; if filter_values_length < 2, give up this round

  2. calculate variance, variance_threshold = 100 as default; the threshold should be put in the store and can be modified by authoritor.

  3. If variance is valid, calculate the average value of the filter_values

Fetch BTC/USD price with http request of offchain worker

  1. Trying to use Http module in substrate/core/offchain
This module is composed of two structs: [`HttpApi`] and [`HttpWorker`]. Calling the [`http`]
function returns a pair of [`HttpApi`] and [`HttpWorker`] that share some state.

The [`HttpApi`] is (indirectly) passed to the runtime when calling an offchain worker, while
the [`HttpWorker`] must be processed in the background. The [`HttpApi`] mimicks the API of the
HTTP-related methods available to offchain workers.

The reason for this design is driven by the fact that HTTP requests should continue running
(i.e.: the socket should continue being processed) in the background even if the runtime isn't
actively calling any function.
  1. Reference for requesting code

  2. Using coinmarketcap api to request BTC/USD price, JS code sample

const rp = require('request-promise');
const requestOptions = {
  method: 'GET',
  uri: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?id=1',
  headers: {
    'X-CMC_PRO_API_KEY': '*****'
  },
  json: true,
  gzip: true
};

rp(requestOptions).then(response => {
  console.log('API call response:', response.data[1].quote.USD.price);
}).catch((err) => {
  console.log('API call error:', err.message);
});
  1. Temporary solution
 let uri = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?id=1";
        let api_key_value = "*****";
        let api_key = "X-CMC_PRO_API_KEY";

        let id: HttpRequestId = runtime_io::http_request_start("GET", uri, &[0]).unwrap();
	    match runtime_io::http_request_add_header(id, api_key, api_key_value) {
		    Err(_) => { debug("Add request header failed !!!") }
		    Ok(_) => {  debug("Add request header succeed") }
	    };

        let deadline = runtime_io::timestamp().add(Duration::from_millis(10_000));
        match runtime_io::http_response_wait(&[id], Some(deadline))[0] {
			HttpRequestStatus::Finished(200) => {debug("request succeed")},
			_ => {debug("request failed")}
		}

        let buffer_len = 2048;
        let mut buf = Vec::with_capacity(buffer_len as usize);
		buf.resize(buffer_len as usize, 0);
		let res = runtime_io::http_response_read_body(id, &mut buf, Some(deadline));
        match res {
            Ok(read) => { runtime_io::print_utf8(&buf[..read])}
            Err(_) => {debug("parse body failed")}
        }

Upgrade the version of packages to support some new features

Refer to the example of offchain worker recommended by @jingleizhang. It uses a new feature which can print any message in the terminal console log.

Code example:

runtime_io::print_utf8(b"Try to execute off-chain worker");

Related PR

The result in the console
Screen Shot 2019-10-24 at 1 50 56 PM

Two changes in the codebase for this update:

  • Update package git reference to bbe90e31ed2fd71dc037fc385dae32584d2b1ce8
  • Code change in service.rs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.