Skip to content

This Backend-Project is a NestJS-based backend server that fetches the latest cryptocurrency prices, calculates statistics and deviations, and provides endpoints for retrieving this data.

Notifications You must be signed in to change notification settings

hs094/CryptoFetch-Backend-Service

Repository files navigation

CryptoFetch Backend Service

The crypto-fetch-service is a NestJS-based backend server that fetches the latest cryptocurrency prices, calculates statistics and deviations, and provides endpoints for retrieving this data.

Table of Contents
  1. Project Description
  2. Getting Started

(back to top)

Project Description

Deployment

The Application is Deployed on Railway.

Example

Image 1 A Sample Request made to Deviation Endpoint for matic-network Image 2 A Sample Request made to Stats Endpoint for ethereum

Background Services

Crypto Fetch Service

The Crypto Fetch Service is responsible for fetching the latest cryptocurrency prices from the CoinGecko API. It supports the following coins:

  • bitcoin
  • matic-network
  • ethereum

The service runs every 2 hours and stores the fetched data in a MongoDB database.

MongoDB Store

The MongoDB Store is used to persist the fetched cryptocurrency data. The data includes:

  • Coin ID (coinId: string)
  • Price in USD (priceUsd: number)
  • Market Cap in USD (marketCapUsd: number)
  • 24-hour Change in USD (change24h: number)
  • Timestamp (timestamp: Date)

API Endpoints

1. Get Latest Stats

Endpoint: /stats

Method: POST

Description: Fetches the latest statistics for a specified cryptocurrency.

Request Body:

{
  "coin": "bitcoin"
}

Response:

{
  "price": 50000,
  "marketCap": 1000000000,
  "24hChange": 5
}

2. Get Deviation

Endpoint: /deviation

Method: POST

Description: Calculates the standard deviation of the price for a specified cryptocurrency based on the latest 100 records.

Request Body:

{
  "coin": "bitcoin"
}

Response:

{
  "deviation": 1500
}

Project Structure

  .env
  .gitignore
  .prettierrc
  nest-cli.json
  package.json
  .eslintrc.js
  README.md
  src/
      app.controller.spec.ts
      app.controller.ts
      app.module.ts
      app.service.ts
      controller/
          deviation.controller.ts
          stats.controller.ts
      crypto-data/
          crypto-data.module.ts
          crypto-data.service.spec.ts
          crypto-data.service.ts
          schemas/
              crypto-data.schema.ts
      main.ts
  test/
      app.e2e-spec.ts
      jest-e2e.json
  tsconfig.build.json
  tsconfig.json
  • app.module.ts: The main module of the application.
  • src/app.controller.ts: The main controller of the application.
  • src/app.service.ts: The main service of the application.
  • src/controller/: Contains the controllers for the API endpoints.
  • src/crypto-data/: Contains the module, service, and schema for handling cryptocurrency data.
  • src/main.ts: The entry point of the application.

Getting Started

Steps to Run the Code

  1. Clone the repository:
git clone <repository-url>
  1. Install the dependencies:
npm install
  1. Create a .env file in the root directory and fill it with the following content:
MONGO_USERNAME=<your-mongodb-username>
MONGO_PASSWORD=<your-mongodb-password>
MONGO_DATABASE=<your-mongodb-database>
COINGECKO_API_URL=https://api.coingecko.com/api/v3/simple/price?x_cg_demo_api_key=
COINGECKO_API_KEY=<your-coingecko-api-key>
PORT=3000
  1. Start the application:
npm run start:dev
  1. The application will be running at http://localhost:3000.

Run Tests

# unit tests
npm run test

# e2e tests
npm run test:e2e

# test coverage
npm run test:cov

About

This Backend-Project is a NestJS-based backend server that fetches the latest cryptocurrency prices, calculates statistics and deviations, and provides endpoints for retrieving this data.

Topics

Resources

Stars

Watchers

Forks