Skip to content

Commit

Permalink
Add exchange-oracle crate and implement get_exchange_rate for coinGec…
Browse files Browse the repository at this point in the history
…ko (#442)

author echevrier <[email protected]> 1633529369 +0200
committer echevrier <[email protected]> 1636648987 +0100

Exchange Rate Oracle Showcase

Add exchange-oracle crate and implement get_exchange_rate for coinGecko

Clean according review

Call update market data (Exchange rate) from enclave

Rename pallet market to teeracle

cargo fmt

Set MARKET_DATA_UPDATE_INTERVAL to 24h

[GA] bump node artifact

Changes from review

Set substrate-fixed version to 0.5.6 (before update to new substrate)

Changes from review

Rebase

Changes due to review

Rebase to get PR #498 to rework code according review

* Rebase

* cargo clippy and catch error in execute_update_market to prevent that the thread is stopped

* settings for demo

* update CI because this branch will not be merged in master, but in exchange_rate_oracle branch

* changes from review

* changes from review

* Remove reference to light client for oracle

* Set update interval for exchange range to 24h

* Changes review

* Changes review

* Cargo clippy

* Changes from review: improve variable names

Co-authored-by: echevrier <[email protected]>
  • Loading branch information
echevrier and echevrier authored Nov 17, 2021
1 parent 8fa333c commit 7d29a4c
Show file tree
Hide file tree
Showing 19 changed files with 590 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name: Build, Test, Clippy
on:
workflow_dispatch:
push:
branches: [ master ]
branches: [ master, exchange_rate_oracle ]
pull_request:
branches: [ master ]
branches: [ master, exchange_rate_oracle ]

env:
CARGO_TERM_COLOR: always
Expand Down
27 changes: 26 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2050,6 +2050,7 @@ dependencies = [
"sp-keyring",
"sp-runtime",
"substrate-api-client",
"substrate-fixed 0.5.6",
"thiserror 1.0.29",
"tokio",
"ws",
Expand Down Expand Up @@ -2097,6 +2098,21 @@ dependencies = [
"walkdir",
]

[[package]]
name = "ita-exchange-oracle"
version = "0.8.0"
dependencies = [
"itc-rest-client",
"log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.130",
"serde_json 1.0.67",
"sgx_tstd",
"thiserror 1.0.29",
"thiserror 1.0.9",
"url 2.1.1",
"url 2.2.2",
]

[[package]]
name = "ita-stf"
version = "0.8.0"
Expand Down Expand Up @@ -3910,7 +3926,7 @@ dependencies = [
"sp-io 4.0.0-dev (git+https://github.com/paritytech/substrate.git?branch=master)",
"sp-runtime",
"sp-std",
"substrate-fixed",
"substrate-fixed 0.5.7",
"test-utils",
]

Expand Down Expand Up @@ -6380,6 +6396,15 @@ dependencies = [
"sp-keystore",
]

[[package]]
name = "substrate-fixed"
version = "0.5.6"
source = "git+https://github.com/encointer/substrate-fixed?tag=v0.5.6#b33d186888c60f38adafcfc0ec3a21aab263aef1"
dependencies = [
"parity-scale-codec",
"typenum 1.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "substrate-fixed"
version = "0.5.7"
Expand Down
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
resolver = "2"
members = [
"app-libs/exchange-oracle",
"app-libs/stf",
"cli",
"core/direct-rpc-server",
Expand Down Expand Up @@ -38,9 +39,11 @@ members = [
"sidechain/validateer-fetch",
]

#[patch."https://github.com/integritee-network/pallet-teerex.git"]
#pallet-teerex = { path = "../pallet-teerex" }
#[patch."https://github.com/integritee-network/integritee-node"]
#my-node-runtime = { path = "../integritee-node/runtime", package = "integritee-node-runtime"}

#[patch."https://github.com/integritee-network/pallets.git"]
#pallet-teerex = { path = "../pallets/teerex" }

#[patch."https://github.com/scs/substrate-api-client"]
#substrate-api-client = { path = "../substrate-api-client" }
Expand Down
42 changes: 42 additions & 0 deletions app-libs/exchange-oracle/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
name = "ita-exchange-oracle"
version = "0.8.0"
authors = ["Integritee AG <[email protected]>"]
edition = "2018"

[features]
default = ["std"]
std = [
"itc-rest-client/std",
"log/std",
"serde/std",
"serde_json/std",
"thiserror",
"url",
]
sgx = [
"itc-rest-client/sgx",
"sgx_tstd",
"thiserror_sgx",
"url_sgx",
]

[dependencies]

# std dependencies
thiserror = { version = "1.0.26", optional = true }
url = { version = "2.0.0", optional = true }

# sgx dependencies
sgx_tstd = { rev = "v1.1.3", git = "https://github.com/apache/teaclave-sgx-sdk.git", optional = true}
thiserror_sgx = { package = "thiserror", git = "https://github.com/mesalock-linux/thiserror-sgx", tag = "sgx_1.1.3", optional = true }
url_sgx = { package = "url", git = "https://github.com/mesalock-linux/rust-url-sgx", tag = "sgx_1.1.3", optional = true }

# no_std dependencies
log = { version = "0.4", default-features = false }
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }

# internal dependencies
itc-rest-client = { path = "../../core/rest-client", default-features = false }

131 changes: 131 additions & 0 deletions app-libs/exchange-oracle/src/coingecko.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
Copyright 2021 Integritee AG and Supercomputing Systems AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#[cfg(all(not(feature = "std"), feature = "sgx"))]
use crate::sgx_reexport_prelude::*;

use crate::{error::Error, GetExchangeRate};
use itc_rest_client::{http_client::HttpClient, rest_client::RestClient, RestGet, RestPath};
use log::*;
use serde::{Deserialize, Serialize};
use std::{
string::{String, ToString},
time::Duration,
vec::Vec,
};
use url::Url;

const COINGECKO_URL: &str = "https://api.coingecko.com";
const COINGECKO_PARAM_CURRENCY: &str = "vs_currency";
const COINGECKO_PARAM_COIN: &str = "ids";
const COINGECKO_PATH: &str = "api/v3/coins/markets";
const COINGECKO_TIMEOUT: Duration = Duration::from_secs(3u64);

/// REST client to make requests to CoinGecko.
pub struct CoinGeckoClient {
client: RestClient<HttpClient>,
}
impl CoinGeckoClient {
pub fn new(baseurl: Url) -> Self {
let http_client = HttpClient::new(true, Some(COINGECKO_TIMEOUT), None, None);
let rest_client = RestClient::new(http_client, baseurl);
CoinGeckoClient { client: rest_client }
}
pub fn base_url() -> Result<Url, Error> {
Url::parse(COINGECKO_URL).map_err(|e| Error::Other(format!("{:?}", e).into()))
}
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CoinGeckoMarketStruct {
id: String,
symbol: String,
name: String,
current_price: Option<f32>,
last_updated: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CoinGeckoMarket(pub Vec<CoinGeckoMarketStruct>);

impl RestPath<String> for CoinGeckoMarket {
fn get_path(path: String) -> Result<String, itc_rest_client::error::Error> {
Ok(path)
}
}

impl GetExchangeRate for CoinGeckoClient {
fn get_exchange_rate(&mut self, coin: &str, currency: &str) -> Result<f32, Error> {
let response = self
.client
.get_with::<String, CoinGeckoMarket>(
COINGECKO_PATH.to_string(),
&[(COINGECKO_PARAM_CURRENCY, currency), (COINGECKO_PARAM_COIN, coin)],
)
.map_err(Error::RestClient)?;
let list = response.0;
if list.is_empty() {
error!("Got no market data from coinGecko. Check params {},{}", currency, coin);
return Err(Error::NoValidData)
}
match list[0].current_price {
Some(r) => Ok(r),
None => {
error!("Failed to get the exchange rate of {} to {}", currency, coin);
Err(Error::EmptyExchangeRate)
},
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn get_exchange_rate_for_undefined_coins_fails() {
let url = CoinGeckoClient::base_url().unwrap();
let mut coingecko_client = CoinGeckoClient::new(url);
let result = coingecko_client.get_exchange_rate("invalid_coin", "usd");
assert_matches!(result, Err(Error::NoValidData));
}

#[test]
fn get_exchange_rate_for_undefined_currency_fails() {
let url = CoinGeckoClient::base_url().unwrap();
let mut coingecko_client = CoinGeckoClient::new(url);
let result = coingecko_client.get_exchange_rate("polkadot", "ch");
assert_matches!(result, Err(Error::RestClient(_)));
}

#[test]
fn get_exchange_rate_from_coingecko_works() {
let url = CoinGeckoClient::base_url().unwrap();
let mut coingecko_client = CoinGeckoClient::new(url);
let dot_usd = coingecko_client.get_exchange_rate("polkadot", "usd").unwrap();
assert!(dot_usd > 0f32);
let bit_usd = coingecko_client.get_exchange_rate("bitcoin", "usd").unwrap();
assert!(bit_usd > 0f32);
let dot_chf = coingecko_client.get_exchange_rate("polkadot", "chf").unwrap();
assert!(dot_chf > 0f32);
let bit_chf = coingecko_client.get_exchange_rate("bitcoin", "chf").unwrap();
assert!(bit_chf > 0f32);
assert_eq!(
(dot_usd * 100000. / bit_usd).round() / 100000.,
(dot_chf * 100000. / bit_chf).round() / 100000.
);
}
}
32 changes: 32 additions & 0 deletions app-libs/exchange-oracle/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2021 Integritee AG and Supercomputing Systems AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#[cfg(all(not(feature = "std"), feature = "sgx"))]
use crate::sgx_reexport_prelude::*;
use std::boxed::Box;

/// Exchange rate error
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Rest client error")]
RestClient(itc_rest_client::error::Error),
#[error("Other error")]
Other(Box<dyn std::error::Error>),
#[error("Could not retrieve any data")]
NoValidData,
#[error("Value for exchange rate is null")]
EmptyExchangeRate,
}
47 changes: 47 additions & 0 deletions app-libs/exchange-oracle/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2021 Integritee AG and Supercomputing Systems AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(test, feature(assert_matches))]

#[cfg(all(feature = "std", feature = "sgx"))]
compile_error!("feature \"std\" and feature \"sgx\" cannot be enabled at the same time");

#[cfg(all(not(feature = "std"), feature = "sgx"))]
#[macro_use]
extern crate sgx_tstd as std;

// re-export module to properly feature gate sgx and regular std environment
#[cfg(all(not(feature = "std"), feature = "sgx"))]
pub mod sgx_reexport_prelude {
pub use thiserror_sgx as thiserror;
pub use url_sgx as url;
}

use crate::error::Error;

pub mod coingecko;
pub mod error;

pub trait GetExchangeRate {
/// Get the cryptocurrency/fiat_currency exchange rate
fn get_exchange_rate(
&mut self,
cryptocurrency: &str,
fiat_currency: &str,
) -> Result<f32, Error>;
}
2 changes: 2 additions & 0 deletions core-primitives/api-client-extensions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use substrate_api_client::ApiClientError;

pub mod account;
pub mod chain;
pub mod pallet_teeracle;
pub mod pallet_teerex;

pub use account::*;
pub use chain::*;
pub use pallet_teeracle::*;
pub use pallet_teerex::*;

pub type ApiResult<T> = Result<T, ApiClientError>;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const TEERACLE: &str = "Teeracle";
11 changes: 11 additions & 0 deletions core-primitives/enclave-api/ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ extern "C" {
unchecked_extrinsic_size: u32,
) -> sgx_status_t;

pub fn update_market_data_xt(
eid: sgx_enclave_id_t,
retval: *mut sgx_status_t,
genesis_hash: *const u8,
genesis_hash_size: u32,
currency: *const u8,
currency_size: u32,
unchecked_extrinsic: *mut u8,
unchecked_extrinsic_size: u32,
) -> sgx_status_t;

pub fn run_key_provisioning_server(
eid: sgx_enclave_id_t,
retval: *mut sgx_status_t,
Expand Down
1 change: 1 addition & 0 deletions core-primitives/enclave-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub mod enclave_test;
pub mod error;
pub mod remote_attestation;
pub mod sidechain;
pub mod teeracle_api;
pub mod teerex_api;
pub mod utils;

Expand Down
Loading

0 comments on commit 7d29a4c

Please sign in to comment.