https://github.com/hs094/koinx-backend-assignment
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.
https://github.com/hs094/koinx-backend-assignment
api axios crypto-tools mongodb-atlas nestjs nestjs-backend railway-app typescript
Last synced: 7 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/hs094/koinx-backend-assignment
- Owner: hs094
- Created: 2025-01-11T06:28:45.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-01-14T18:50:43.000Z (9 months ago)
- Last Synced: 2025-01-21T03:02:56.577Z (9 months ago)
- Topics: api, axios, crypto-tools, mongodb-atlas, nestjs, nestjs-backend, railway-app, typescript
- Language: TypeScript
- Homepage: https://hs094-crypto-fetch-service.up.railway.app/
- Size: 457 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
#
KoinX Backend Internship Assignment> The `koinx-crypto-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
## Project Description
### Deployment
The Application is Deployed on [**Railway**](https://railway.com/project/9f4cb147-fa48-4cd4-8942-eb47adc0cf1f?environmentId=95c9c3a6-75a7-4e27-8b2c-b054c2f39218).### Example
![]()
A Sample Request made to Deviation Endpoint for matic-network
![]()
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:**
```json
{
"coin": "bitcoin"
}
```**Response:**
```json
{
"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:**
```json
{
"coin": "bitcoin"
}
```**Response:**
```json
{
"deviation": 1500
}
```
### Projct 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.
* koinx-crypto-service/src/app.controller.ts: The main controller of the application.
* koinx-crypto-service/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.
* koinx-crypto-service/src/main.ts: The entry point of the application.## Getting Started
### Steps to Run the Code
1. Clone the repository:
```bash
git clone
```
2. Install the dependencies:
```bash
npm install
```
3. Create a `.env` file in the root directory and fill it with the following content:
```plain-text
MONGO_USERNAME=
MONGO_PASSWORD=
MONGO_DATABASE=
COINGECKO_API_URL=https://api.coingecko.com/api/v3/simple/price?x_cg_demo_api_key=
COINGECKO_API_KEY=
PORT=3000
```
4. Start the application:
```bash
npm run start:dev
```
5. The application will be running at http://localhost:3000.### Run Tests
```bash
# unit tests
npm run test# e2e tests
npm run test:e2e# test coverage
npm run test:cov
```