Skip to content

Latest commit

 

History

History
140 lines (95 loc) · 10 KB

4.4.3-teeracle-oracle-framework.md

File metadata and controls

140 lines (95 loc) · 10 KB

4.4.3 TEEracle - Oracle Framework

The TEEracle SDK facilitates submission of trustworthy oracle data into the Integritee parachain. It retrieves website or API data and serves source-authenticated data to the parachain.

Generic TEEracle

Coming soon!

Exchange rate oracle

We already have a running TEEracle connected to the Integritee parachain that supplies the TEER-USD exchange rate. This exchange rate will be used to offer stable USD fees.
The exchange rate values are from two different sources:

https://www.coingecko.com/
https://coinmarketcap.com/api/

These sources and are updated once per day.

How to use the SDK

  1. Experiment with the template running the teeracle demo​.
  2. Fork the worker repository https://github.com/integritee-network/worker.git from the SDK release branch (release will be done in the next two weeks).
  3. Build the worker in teeracle mode.
  4. Customize your own exchange rate oracle.
  5. Deploy on the Integritee parachain or your own testnet.
  6. Add the trusted sources to the Teeracle whitelist.
  7. Try the tutorial How to build a USD-DOT Oracle from the Template on your own testnet.

Building the worker in teeracle mode

In order to build the worker in teeracle mode, the corresponding cargo feature teeracle needs to be set. In the Makefiles, the environment variable WORKER_MODE is used to set the cargo features for the worker mode.

In case you build with make directly, do so with:

WORKER_MODE=teeracle make

Alternatively you can use docker with our build.Dockerfile. Then use the --build-arg WORKER_MODE_ARG=teeracle. An example of a docker build command (as currently used for GitHub CI):

docker build -t integritee-worker --target deployed-worker --build-arg WORKER_MODE_ARG=teeracle -f build.Dockerfile 

Customizing an Exchange Rate Oracle

The following example customization describes how to add another currency pair because it is the easiest example. However, having an entirely customized teeracle with arbitrary is also possible with reasonable effort! A streamlined SDK flavor is coming soon! See #generic-teeracle.

Relevant code of the Teeracle:

  • The core part of the exchange oracle-specific code is in the ita-exchange-oracle crate.
  • The Teeracle is launched at the start of the service.
  • The exchange rate value is fetched and updated within the enclave.

Scheduling

To change the update rate interval, you can change the default setting value or use a CLI argument**:**

./integritee-service run --teeracle-interval 5s15m2h1d

Trading pair

The trading pair consists of a crypto- and a fiat currency symbol. Ex: {"TEER", "USD"}. It is set at the start of the oracle service : teeracle-service.

To convert the currency symbols into the unique identifiers according to the source API standards, we use a map: Coinmarket Cap and CoinGecko. If your currency is not already listed in our mapping already, you will need to add it. It can be looked up in:

See How to build a USD-DOT Oracle from the Template on your own testnet as an example.

API Keys

Our Teeracle reads the API-Key from an environment variable.

  • CoinGecko: Does not need an API-Key for the free plan.
  • Coinmarket Cap needs an API-Key, which has to be set in the environment variable COINMARKETCAP_KEY.

Exchange Rate Oracle Source

To add a new exchange rate oracle, you need to :

  1. Create an oracle with a new source in the ita-exchange-oracle crate.
  • New source
    We query the exchange rate value via the REST API of the desired source. We use a TLS RestAPI client and explicitly check that the source's TLS-Root-Certificates are correct. This gives us non-repudiation, i.e, it is guaranteed that the result is indeed from the advertised source.
    The code for our two sources can be found in Coinmarket Cap and CoinGecko.
    The new ExchangeRateSource must:
    • Implement the OraclSource.
    • Provide a valid root certificate and the HTTP request to the right endpoint.
    • Define the mapping between the currency symbols and the ID used to identify the exchange rate endpoint.
    • Implement the parsing of the JSON response data to get the exchange rate value.
  • New oracle
    Define a new type for your oracle in lib.rs and add the create function.

2. Get the exchange rate and create an extrinsic from within the enclave
In update_market_data_internal add your new oracle.

To remove an existing exchange rate oracle source, you need to: remove the oracle from update_market_data_internal

Add trusted source to whitelist

The teeracle pallet maintains a whitelist of trusted exchange rate oracles. A trusted source can be added to the whitelist with a privileged call via Sudo or Democracy.

Call : teeracle, addToWhitelist ( dataSource , 0x mrenclave_hex)

The dataSource is the base URL of the source: https://pro-api.coinmarketcap.com/ or https://api.coingecko.com/. The mrenclave_hex is written to the logs when the teeracle service starts.

{% hint style="info" %} Warning: The dataSouce must have

  • no whitespaces at the end
  • must have a "/" at the end. {% endhint %}

How to build a USD-DOT Oracle using the Template on your own testnet

  1. Fork the worker repository https://github.com/integritee-network/worker.git from the SDK release branch (TODO release)

  2. Customize the trading pair:

  3. Build the worker in teeracle mode

  4. Run an Integritee solo node in dev mode. The node runtime has the teeracle pallet

  5. Coinmarket Cap needs an API-Key. Get it from https://coinmarketcap.com/api/documentation/v1/#section/Introduction and set it as an environment variable:

    COINMARKETCAP_KEY="xxxxxxxxxxxx"
    export COINMARKETCAP_KEY` 
    
  6. Run the teeracle service in dev mode with a smaller update rate interval 24s

    cd bin
    ./integritee-service --clean-reset run --node-url <yournodeip> --node-port <port> --dev  --teeracle-interval 24s
    
  7. Add Coingecko and Coinmarket_CAP to the whitelist with a SUDO call.

  8. Check the events in https://polkadot.js.org : teeracle.ExchangeRateUpdated.
    You should get every 24s an event for https://api.coingecko.com/ and an event for https://pro-api.coinmarketcap.com/ with the USD/DOT exchange rate value