Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Crypto Price Fetcher

A Go application that fetches real-time cryptocurrency exchange rates using the CEX.io API. The application supports concurrent requests for multiple cryptocurrencies.

Project Structure

.
├── main.go                 # Entry point with concurrent goroutines
├── go.mod                  # Go module definition
├── Api/
│   ├── cex.go             # CEX.io API client
│   ├── responses.go       # API response structures
│   └── Cex_test.go        # Unit tests
├── datatypes/
│   └── data.go            # Data type definitions
└── hello/

Features

  • Concurrent API Calls: Uses Go goroutines to fetch multiple cryptocurrency prices simultaneously
  • CEX.io Integration: Fetches real-time exchange rates from the CEX.io API
  • Error Handling: Validates input and handles API errors gracefully
  • Clean Data Types: Structured types for API responses and rate data

Supported Cryptocurrencies

  • Bitcoin (BTC)
  • Ethereum (ETH)
  • Bitcoin Cash (BCH)

Getting Started

Prerequisites

  • Go 1.22.2 or higher

Installation

git clone https://github.com/ayushman1210/Crypto_tracker.git
cd crypto
go mod tidy

Running the Application

go run .

This will fetch the current USD exchange rates for BTC, ETH, and BCH concurrently.

Running Tests

go test ./...

API Details

CEX.io Ticker Endpoint

  • URL: https://cex.io/api/ticker/{CURRENCY}/USD
  • Method: GET
  • Response: JSON with bid/ask prices and volume data

Currency Validation

  • Currency code must be exactly 3 characters long
  • Automatically converts input to uppercase

Code Overview

Main Function (main.go)

Uses sync.WaitGroup to manage concurrent API requests:

var wg sync.WaitGroup
for _, currency := range currencies {
    wg.Add(1)
    go func(currencycode string) {
        getcurrency(currencycode)
        wg.Done()
    }(currency)
}
wg.Wait()

API Client (Api/cex.go)

  • Getcurr(currency string): Fetches the current price for a given cryptocurrency
  • Handles HTTP requests, JSON parsing, and error validation
  • Returns *datatypes.Rate containing currency and price information

Data Types (datatypes/data.go)

type Rate struct {
    Currency string
    Price    float64
}

Error Handling

The application handles various error scenarios:

  • Invalid currency codes (not 3 characters)
  • Network errors
  • HTTP status code errors
  • JSON parsing errors

Output Format

the rate for BTC is 45000.00
the rate for ETH is 2500.50
the rate for BCH is 350.75

Dependencies

  • Standard Go libraries only:
    • net/http
    • encoding/json
    • sync
    • io
    • fmt
    • strings

Author

Ayushman1210

About

A Go application that fetches real-time cryptocurrency exchange rates using the CEX.io API.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages