Skip to main content

ETH Price Tracker

Introduction

This guide provides a detailed walkthrough for setting up a price tracker for the Ethereum token, from hereon referred to as ETH.

tip

Prerequisites: Before we dive into the implementation of ETH price tracker, you must follow all the steps to setup your Devnet Node

Scenario: ETH Price Tracking

We aim to capture real-time changes in ETH's price conversion ratio. Watch the tutorial below for a detailed walkthrough.

Steps to Implement this Use Case

Cloning the Deploy Repository

Clone repository against the respective branch (main by default). Open the terminal and run the below command to clone the deploy repo in a directory named powerloom_deploy.

git clone https://github.com/PowerLoom/deploy.git --single-branch powerloom_deploy --branch devnet && cd powerloom_deploy

Configuring The Node - Copy env.example to .env

Required variables

  • SOURCE_RPC_URL : The URL for the Ethereum RPC (Local node/Infura/Alchemy) service. Set the RPC URL to Ethereum mainnet to snapshot the data source contracts on it.
  • SIGNER_ACCOUNT_ADDRESS : The address of the signer account. This should be an unused "burner" address that does not need to have any token balance. You can create a new burner wallet from our tool.
  • SIGNER_ACCOUNT_PRIVATE_KEY : The private key corresponding to the signer account address.
  • SLOT_ID : Enter your Devnet Slot ID.
info

Unless it is a customized need or instructed by us, the below need not be changed or modified from the values already supplied in env.example

  • SNAPSHOT_CONFIG_REPO=https://github.com/powerloom/snapshotter-configs
  • SNAPSHOT_CONFIG_REPO_BRANCH=eth_price_monitor
  • SNAPSHOTTER_COMPUTE_REPO=https://github.com/powerloom/snapshotter-computes
  • SNAPSHOTTER_COMPUTE_REPO_BRANCH=eth_price_monitor
  • PROTOCOL_STATE_CONTRACT : The contract address for the protocol state.
  • RELAYER_HOST: The host address for the relayer.
  • NAMESPACE : The unique key used to identify your project namespace.
  • PROST_CHAIN_ID : The Devnet chain ID.
  • PROST_RPC_URL : The URL for the PROST RPC service.
  • POWERLOOM_REPORTING_URL : The URL for reporting to Powerloom.

Optional

  • IPFS URL : The URL for the IPFS (InterPlanetary File System) service in HTTP/HTTPS (e.g. https://ipfs.infura.io:5001) multiaddr format (e.g. /dns/ipfs.infura.io/tcp/5001/https)
  • IPFS_API_KEY: The API key for the IPFS service (if required).
  • IPFS_API_SECRET: The API secret for the IPFS service (if required).
  • SLACK_REPORTING_URL: The URL for reporting to Slack.
  • WEB3_STORAGE_TOKEN: The token for Web3 Storage. You can generate or retrieve this token from your API token page after signing up for a free plan at web3.storage.

Snapshot Building

info

It is recommended to look at the docs on snapshot generation.

Snapshotter node has a simple interface as part of GenericProcessorSnapshot class that business logic specific computes need to implement. The rest of the heavy lifting around reliable submissions and finalization is taken care of by the rest of the features of the node.

In this case, EthPriceProcessor located in snapshotter/modules/computes is the sole compute class for ETH price tracking. This class implements the GenericProcessorSnapshot interface, most notably the compute() callback. This callback is where we build the snapshots.

eth_price_tracking.py
loading...

get_eth_price_usd() is located in snapshotter/modules/computes and is a utility function that calculates the price of ETH as a weighted average of its price conversion ratios against DAI, USDC and USDT in the corresponding Uniswap V2 pair contracts. The weights assigned are relative to the total liquidity locked against the W(rapped)ETH token across the three pair contracts.

snapshotter/utils/snapshot_utils.py
loading...

Set Up the Codebase

Setup the codebase by running the bootstrap.sh command in the terminal. Developers may use different shells, such as bash or zsh. Adjust the command accordingly.

For bash:

bash bootstrap.sh

or

zsh bootstrap.sh

Run the Snapshotter Node

bash build.sh

or

zsh build.sh

Once your node is running, the core API server can be found on http://localhost:8002. It comes with an easy to use SwaggerUI at http://localhost:8002/docs to test out the endpoints to retrieve snapshotted data, or monitor the node status among other things.

endpoints

The snapshotted data as generated by the compute module referred above is retrieved from /data/{epoch_id}/{project_id} endpoint. It requires two parameters

projects.example.json
loading...
info

To learn more about the details of the first element in the tuple being returned from compute() being considered as the data source in project ID generation, refer to the docs on Data Source Specification: Bulk Mode

Verifying snapshotted data

As the node continues snapshotting data, the quickest way to check the snapshotted data is to check against epoch_id and project_id returned in the logs. Copy these values and paste them in the endpoint to retrieve project data in the SwaggerUI exposed against the core API.

endpoints

For example, in the screenshot above, the epoch_id is 100792. Hit execute. The corresponding snapshotted data to this specific epoch_id displays the price of ETH as 3067.935921.

As the epoch_id moves ahead to 100793, we find the above endpoint reflects the latest price captured of ethereum as 3068.034792

endpoints endpoints

How to program this

  1. Define the correct project ID against which data has to be fetched
project_type = "eth:price_tracking"  # from https://github.com/PowerLoom/snapshotter-configs/blob/eth_price_monitor/projects.example.json#L4
project_namespace = "DEVNET" # from https://github.com/PowerLoom/deploy/blob/95ceb83a97a16279816c406eef484245df483fb1/env.example#L25
data_source = "example" # from https://github.com/PowerLoom/snapshotter-computes/blob/15059013c6c17327d1c0d413d3885c23a6383305/eth_price_tracking.py#L45
project_id = f'{project_type}:{data_source}:{project_namespace}'
  1. Fetch the last finalized epoch from the API endpoint http://localhost:8002/last_finalized_epoch/{project_id}
  2. Fetch the data against this epoch ID from the API endpoint http://localhost:8002/data/{epoch_id}/{project_id}

Reach out to us

For any sort of support or clarification, reach out to us on our discord developer channel.