oracle-infinity's People
oracle-infinity's Issues
Averaging all the prices to make sure the value is valid
There has three steps for data processing:
-
filter
value == 0
; iffilter_values_length
< 2, give up this round -
calculate variance, variance_threshold = 100 as default; the threshold should be put in the store and can be modified by authoritor.
-
If variance is valid, calculate the average value of the
filter_values
Using Offchain worker to implement oracle
Offchain worker
-> OW
- Config
ow
oracle SRML and testow
workflow - Submit unsigned transaction
- Using
ow
http function to request BTC/USD price - Parse json date from response
- Using Authority key to sign and verify
payload
- Add 2 more price sources
- An on-chain verification mechanism like
averaging
should be implemented to determine what information gets into the chain
Feel free to add more features and discuss the existing ones
Fetch BTC/USD price with http request of offchain worker
- 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.
-
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);
});
- 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
Two changes in the codebase for this update:
- Update package git reference to
bbe90e31ed2fd71dc037fc385dae32584d2b1ce8
- Code change in
service.rs
Parse JSON data from response
When using Offchain httpRequest
to get BTC/USD price, the result is a JSON data with binary format. We need to parse the JSON data to extract the price value.
Reference:
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.