# Install & run data providers locally

## **System requirements**

* Mac/Linux machine, with at least 4GB of RAM and 10GB of storage.
* [Go](https://go.dev/doc/install) version 18 and above.
* [Cockroach DB](https://www.cockroachlabs.com/docs/stable/install-cockroachdb-mac.html), an SQL database system highly compatible with Go.

## **Installation Steps**

After cloning the [klayoracle-monorepo](https://github.com/KlayOracle/klayoracle-monorepo) as described in the [fundamentals](https://klayoracle.gitbook.io/v1.0.0/fundamentals), follow these steps to install & run your data provider:

### 1. Define data feeds

From the monorepo, navigate to the `feeds` folder within the `data-provider` package.

```bash
# from monorepo folder
cd data-provider/feeds
```

<figure><img src="https://1353558919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyOr8lsxlSYYa6WW10BrH%2Fuploads%2FD9farmt1ptehiTbIFD92%2Fimage.png?alt=media&#x26;token=4f4b589b-d7c6-4ad0-840c-9e9f7b7de338" alt="" width="253"><figcaption><p><code>feeds</code> folder in monorepo</p></figcaption></figure>

Configure the data feeds (*aka* adapters) which your data provider will use to fetch and serve data to subscribed consumer contracts. Each data feed includes options for connecting to your data source via HTTP(S), filtering your data and returning a single value using reducers, and sending the value to your oracle contract.

Visit the [Configuring Data Feeds](https://klayoracle.gitbook.io/v1.0.0/data-providers/broken-reference) section of this documentation for step-by-step instructions on defining each of your data feeds.

{% hint style="info" %}
We've also provided sample data feeds for [KLAY\_USD](https://github.com/KlayOracle/klayoracle-monorepo/blob/development/data-provider/feeds/KLAY_USD.json) and [WEMIX\_USD](https://github.com/KlayOracle/klayoracle-monorepo/blob/development/data-provider/feeds/WEMIX_USD.json), which you can edit in order to get started quickly.
{% endhint %}

### 2. Generate a unique adapter ID for each feed

Using the `make` command, run the `adapter-id-gen` utility, which generates a unique 32-byte string identifier for each feed defined as a variable within the `ADAPTERS` argument.

```bash
make adapter-id-gen ADAPTERS="ETH_USD.json WEMIX_USD.json"
```

For example, the above command generates separate string identifiers for the `ETH_USD.json` and `WEMIX_USD.json` feeds. Each string is saved within its respective feed as an `adapterId` property.

<figure><img src="https://1353558919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyOr8lsxlSYYa6WW10BrH%2Fuploads%2FFC8UHM1WRggZO7WwZ1CV%2Fimage.png?alt=media&#x26;token=0ac896a8-e517-40ee-9e80-a4c0d0c09b02" alt=""><figcaption><p>run <code>adapter-id-gen</code></p></figcaption></figure>

<figure><img src="https://1353558919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyOr8lsxlSYYa6WW10BrH%2Fuploads%2FkkkiHZFx7kTM4XFNtt7z%2Fimage.png?alt=media&#x26;token=e5d31ba8-911d-4a9f-b631-2bf6abe5ff30" alt=""><figcaption><p><code>adapterId</code> in feed</p></figcaption></figure>

{% hint style="info" %}
Running the `adapter-id-gen` utility compresses and uglifies the feed's JSON. Use a [JSON Formatter](https://jsonformatter.curiousconcept.com/) to revert the file's contents to a more readable format.&#x20;
{% endhint %}

### 3. Deploy OracleProvider contract

Once the feeds' adapter IDs have been created, follow the instructions in the video below to deploy the data provider's oracle contract. This contract sends data on-chain, which is then [consumed by valid data subscribers](https://klayoracle.gitbook.io/v1.0.0/fundamentals/architecture).

{% embed url="<https://www.youtube.com/watch?v=7cW9Ryro6pI>" %}

An separate `OracleProvider` contract should be deployed for each feed. Once deployed, replace the `oracleAddress` key in each feed with the key of its deployed contract.

<figure><img src="https://1353558919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyOr8lsxlSYYa6WW10BrH%2Fuploads%2F35OBgllisXQWpVQtsSvu%2Fimage.png?alt=media&#x26;token=c70a5f0e-a369-48a9-a1dc-18af0e446576" alt="" width="563"><figcaption><p><code>oracleAddress</code> in <code>LTC_USD</code> feed</p></figcaption></figure>

### 4. Do a dry run

After defining your data feeds and generating an adapter ID, the next step is to do a dry run.

A dry run verifies that the configured data feeds are valid and will function correctly when the data provider is [connected to a KlayOracle node](https://klayoracle.gitbook.io/v1.0.0/data-providers/broken-reference) in a production environment.

It does this by simulating the execution of a node to fetch, aggregate and return data using the properties defined within the data feed.

Run the `adapter-dry-run` command, passing the list of data feeds you want to verify to the `ADAPTERS` argument. For example:

```bash
make adapter-dry-run ADAPTERS="ETH_USD.json WEMIX_USD.json"
```

A successful dry run fetches data from defined data sources, filters the data using the reducers defined in the adapter, and returns the single value which the oracle contract consumes in a production environment.

<figure><img src="https://1353558919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyOr8lsxlSYYa6WW10BrH%2Fuploads%2Ff8qmD88erbZRLfvs1Ded%2Fimage.png?alt=media&#x26;token=7db26388-06d6-472e-a247-7e9b9342c7eb" alt=""><figcaption><p>output of <code>adapter-dry-run</code> command</p></figcaption></figure>

{% hint style="info" %}
`adapter-id-gen` and `adapter-dry-run` are two of several data provider utilities defined within KlayOracle. Visit the [Utilities](https://klayoracle.gitbook.io/v1.0.0/data-providers/broken-reference) page to learn more.
{% endhint %}

### 5. Define parameters to connect with node

In order to connect successfully, node runners are required to provide connecting data providers with specific config values, to be used within the data provider's `config.yml` & `data-providers/.env` files.

These parameters include:

* the node's IP address.
* the node's public certificate
* the node's OAuth token

<details>

<summary>Node IP address</summary>

The connected node's IP address is defined in the data provider's `data-provider/config.yml` file as a `service_node` parameter.

<img src="https://1353558919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyOr8lsxlSYYa6WW10BrH%2Fuploads%2FSwg3SKMMmBO2eZu76ig7%2Fimage.png?alt=media&#x26;token=38769450-7bde-4e8c-b460-0e4d56d549a3" alt="" data-size="original">

</details>

<details>

<summary>Node SSL public certificate</summary>

Data providers require the node's self-signed public certificate to communicate securely with the node via SSL (Secure Socket Layer).

Once provided by the node, the certificate file should be saved by the data provider within the `data-provider/certs/node` folder. For example, in the image below, the public certificate file `535797165684.pem` is saved in the `x509` folder (`x509` being an arbitrary name given to the node by the data provider).

![](https://1353558919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyOr8lsxlSYYa6WW10BrH%2Fuploads%2F91pjrHA2FHAGcodjYmOZ%2Fimage.png?alt=media\&token=e91a62ae-0a4b-4c39-8c7e-95e095864b4d)

Once saved, the file path should also be defined within the `data-provider/config.yml` file as a `certificate` value.

![](https://1353558919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyOr8lsxlSYYa6WW10BrH%2Fuploads%2Fh6YKtRQvQMFaIcmZJBzo%2Fimage.png?alt=media\&token=313ee0e4-7a81-4ab3-9b91-50195ee7add7)

</details>

<details>

<summary>Node OAuth Token</summary>

This credential authenticates the data provider to connect to the node and communicate securely via gRPC.&#x20;

This token should be kept secret and stored as an environmental variable in the `data-providers/.env` file.

<img src="https://1353558919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyOr8lsxlSYYa6WW10BrH%2Fuploads%2Fu9jDtFcHtDmHg171rRf8%2Fimage.png?alt=media&#x26;token=52086d44-b9fb-44e5-90a4-66621c3522f6" alt="" data-size="original">

</details>

{% hint style="info" %}
To learn more about nodes and data providers communicating securely via SSL & OAuth, see [Node & data provider communication](https://klayoracle.gitbook.io/v1.0.0/fundamentals/node-and-data-provider-communication).
{% endhint %}

### 6. Define other config & environmental variables

Apart from the connected node's parameters, the `data-providers/config.yml` file and `data-providers/.env` file need to be configured with other details of the data provider.

The mandatory parameters include:

* the data provider's organization details (name, website, and organization ID).
* the list of feeds you want the node to aggregate. Any feed name not defined in this list will not be sent to the node.
* the DNS details of the data provider, particularly its IP address.

<figure><img src="https://1353558919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyOr8lsxlSYYa6WW10BrH%2Fuploads%2FQkgaAocbCYzxfyecrH3n%2Fimage.png?alt=media&#x26;token=bbca8314-535c-46f2-ab71-8c1627318161" alt="" width="375"><figcaption><p>data-providers/config.yml</p></figcaption></figure>

<figure><img src="https://1353558919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyOr8lsxlSYYa6WW10BrH%2Fuploads%2FLTDpU18GWgQsBqfB6pws%2Fimage.png?alt=media&#x26;token=b0993391-9980-4f42-812c-4ed9b0755784" alt="" width="375"><figcaption><p>data-providers/.env</p></figcaption></figure>

See [Configuration Files & Environmental Variables](https://klayoracle.gitbook.io/v1.0.0/data-providers/how-it-works/configuration-files-and-environmental-variables) for a comprehensive guide on setting up these parameters.

### 7. Start data provider

Once the above have been completed, run the following commands to start the data provider on your local machine.

The `HOST_IP` parameter must match the one set in your `.env` file, as described in the [previous step](#5.-define-other-config-and-environmental-variables).

```bash
make gomodtidy
# start data provider without logging to console
make dp-client-nolog HOST_IP=0.0.0.0:50002
# to start with comprehensive logging
make dp-client HOST_IP=0.0.0.0:50002
```
