This project fetches real-time and historical cryptocurrency prices using the CoinGecko API, stores the data in a Hyperbee database, and exposes the data through Hyperswarm RPC.
git clone git@github.com:PrashantHalaki/pears_assignment.gitcd pears_assignmentnpm installFollow the official Pear installation guide:
π https://docs.pears.com/guides/getting-started
npm install -g hyperdhtThis command sets up the bootstrap node. Run it in a separate terminal and keep it active:
hyperdht --bootstrap --host 127.0.0.1 --port 30001This starts the data collection pipeline, loads prices into Hyperbee, and starts the RPC server. The public key will be displayed in the console.
npm run devExample output:
β° Scheduled run: fetching crypto prices...
Starting RPC server...
RPC Server started on public key: <RPC Server Key>
In a new terminal, use the public key generated from the previous step:
npm run client <RPC_PUBLIC_KEY>Once inside the RPC client terminal:
latest btc,ethFetches the latest average prices for the listed coins.
historical btc,eth 2023-01-01 2023-01-31Returns historical prices between the provided date range.
closeTerminates the client gracefully.
- Data is fetched from CoinGecko's public API every 30 seconds.
- Top 5 cryptocurrencies (by market cap) are monitored.
- For each currency, prices from the top 3 exchanges are averaged.
- Data is stored in Hypercore using Hyperbee.
- Communication is enabled through Hyperswarm RPC and HyperDHT.
pears_assignment/
β
βββ db/ # DB related reusable files
β βββ db.js # Hypercore and Hyperbee instance
β βββ dht.js # Hyperdht instance
β βββ getPrices.js # getLatestPrices and getHistoricalPrices functions to read data from db
β βββ index.js # file to export reusable functions from db
β βββ storePrices.js # storePrices function to write data from db
β
βββ config/ # Configurations
β βββ index.js # All the envs and constants are available here
β
βββ data/ # Persistent local storage for Hypercore/Hyperbee
β
βββ test/ # Test cases folder
β βββ index.test.js # Empty test file TODO: Write unit test cases
β
β
βββ src/
β βββ server/ # RPC server logic
β β βββ index.js # Entry point for RPC server
β β
β βββ client/ # RPC client to query server
β β βββ index.js # Sample client requesting prices
β β
β βββ collector/ # Data pipeline logic
β β βββ priceFetcher.js # Fetches top 3 exchanges from CoinGecko API for each token
β β βββ topCoins.js # Fetches top 5 usdt coins
β β βββ index.js # Fetches all prices
β β
β βββ job/ # Data pipeline calling jobs
β β βββ index.js # exports jobs and runs pipeline on demand when --once argument is passed
β β βββ pipeline.js # Logic to call the store data
β β βββ schedule.js # Logic to schedule the data loader for defined time frame
β β
β βββ utils/ # Helper functions
β βββ axiosInterface.js # Axios interface to call coingeko APIs
β βββ sleep.js # Sleep function to make the functions sleep for defined time frame
β
βββ .env.example # Example of all environmental variables in the app
βββ index.js # Start of the app - Initializing scheduler and RPC server
βββ .gitignore
βββ package.json
βββ package.lock.json
βββ README.md
# Terminal 1
hyperdht --bootstrap --host 127.0.0.1 --port 30001
# Terminal 2
npm run dev
# Terminal 3
npm run client <RPC_PUBLIC_KEY>This document outlines the features and improvements that could not be completed within the given timeline. The reasons for each pending task are described below.
- Problem: The Axios instance used to fetch data from the CoinGecko public API throws a
MODULE_NOT_FOUND: Cannot find module 'crypto'error when executed usingpear run. - Reason: Pear runtime has limited documentation and compatibility for third-party Node.js modules like Axios that rely on Node.js core modules (
cryptoin this case). - Status: Raised the concern with provided contacts but havenβt received a response yet.
- Resolution: If any guidelines or examples are shared on making external HTTP requests via Pear, this can be fixed in a few minutes.
- Plan: The intent was to first complete the entire pipeline (data collection, storage, scheduling, RPC setup) before adding unit tests.
- Status: Couldnβt add test cases due to tight time constraints.
- Note: The code is modular and can easily be covered with tests using frameworks like Jest or Mocha.
- Current Approach: The data loading job is scheduled using a
setTimeout-based recursive approach to simulate a 30-second interval. - Reason:
node-crondid not work in the Pear runtime environment. - Recommendation: If Pear supports any built-in scheduling utilities or has a compatible cron-like tool, this logic can be replaced easily.
- Missing Element: Due to time constraints, detailed inline comments and a structured code walkthrough are not included.
- Compensation: The variable names and function definitions follow clear and intuitive naming conventions, making the logic easy to follow for developers.
These pending tasks do not affect the core functionality. With slight documentation and runtime support, the issues mentioned above can be addressed quickly.