Skip to content

ManudattaG/Bitcoin-forex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bitcoin-forex

Problem Statement:

To develop a service that constantly checks the currency exchange rate from Bitcoin to US Dollars (1 Bitcoin => X USD)

Approach:

  • Research and Analysis of Bitcoin APIs and services
  • Create a flask app service around the Bitcoin wrapper function
  • Create 2 REST API endpoints - getLatestRate and getHistoricalRates
  • Transformation of data and mapping the API response to corresponding endpoints
  • Return the JSON API results
  • Testing the service and writing test scenarios covering all edge cases

Architecture Diagram

Alt text

Overview of "Bitcoin-forex" Workflow:

  1. Create Flask API (GET - /api/getLatestRate)

    • An API/wrapper around the Bitcoin service
    • Gets Bitcoin's latest conversion rate(USD) by calling "get_latest_price()" method
    • Returns the json data with the latest USD price
  2. Create Flask API (GET - /api/getHistoricalRates)

    • An API/wrapper around the Bitcoin service
    • Converts startDate and endDate to corresponding datetime format to query Bitcoin service
    • Check period is configurable
    • Gets Bitcoin's historical rates(USD) by calling "get_previous_price_list()" method between startDate and endDate
    • Returns the json data with the collection of historical rates
  3. Create tests for Bitcoin service

    • Unit test cases written by using forex bitcoin service and pytest framework
    • Covered all edge cases and failure cases for both get_latest_price() and get_previous_price_list() methods

API usage:

  1. Get Latest Rate (As of 5th Nov, 14:00 IST) -- https://bitcoin-forex.herokuapp.com/api/getLatestRate

    response:
    	{
    	    "latest_rate": 14460.51
    	}
    
  2. Get Historical Rates (As of 5th Nov, 14:00 IST) -- https://bitcoin-forex.herokuapp.com/api/getHistoricalRates?startDate=2020-10-15&endDate=2020-10-25

    response:
    	{
    	    "historical_rates": {
    		"2020-10-15": 11510.5367,
    		"2020-10-16": 11325.5217,
    		"2020-10-17": 11369.9133,
    		"2020-10-18": 11516.9667,
    		"2020-10-19": 11758.5467,
    		"2020-10-20": 11922.975,
    		"2020-10-21": 12811.4867,
    		"2020-10-22": 12987.9017,
    		"2020-10-23": 12940.1067,
    		"2020-10-24": 13127.055,
    		"2020-10-25": 13039.0133
    	    }
    	}
    

Project Structure:

  1. bitcoin.py -- An API/wrapper function for the Bitcoin service
  2. tests/test_bitcoin.py -- Provides unit testing for Bitcoin service
  3. architecture.png -- Architecture diagram of the project
  4. Procfile -- A file which is used to run a web app deployed on Heroku
  5. requirements.txt -- Dependency libraries for the project
  6. runtime.txt -- A file which contains runtime version on which the app is running

Pre requisites:

  • Python 3.7 or Python 3.8
  • Flask

Libraries Used:

  1. flask_restful -- Extension for Flask that adds support for quickly building REST APIs.
  2. forex-python -- Service that provides Foreign exchange rates and currency conversion.
  3. pytest -- Testing framework based on Python.
  4. flask -- Web application framework written in Python which is based on Werkzeug WSGI toolkit and Jinja2 template engine.

Library Installation:

  • Install virtualenv package to create a virtual env --> pip3 install virtualenv
  • Create virtual env to install the dependencies --> virtualenv .env
  • Activate the virtual env --> source .env/Scripts/activate
  • Install the dependency libraries (Provided in requirements.txt file) --> pip3 install -r requirements.txt

Deployment options:

Hooray! Now our Bitcoin service is ready to use. To make this application production ready, we have several options:

  1. Deploying on Heroku

    • Create a Procfile which is used to run a web app deployed on Heroku
    • Create requirements.txt file which is used as dependency libraries for the project
    • Create an app on Heroku and connect to the GitHub where the project is pushed
    • Create a deployment pipeline in Heroku
  2. Deploying on AWS

    • Create API gateway REST APIs for both the endpoints
    • Additionally, API keys can be created for the REST APIs to be secured
    • Create a lambda function which corresponds to API gateway (trigger to lambda function)
    • Create a CI/CD pipeline using code build, code deploy to automatically trigger the build and deploy the service

AWS resources that can be used to deploy the service:

  • API gateway -- To create REST APIs
  • AWS Lambda function -- To write code without having to worry about infrastructure and scale automatically
  • SSM parameter store -- To secretly store the credentials, URLs and API keys
  • AWS Code Pipeline -- To automate release pipelines
  • AWS CodeBuild -- To compile source code, runs tests, and produces software packages that are ready to deploy
  • AWS CodeDeploy -- To deploy service that automates application deployments to Amazon EC2 instances, serverless Lambda functions, or Amazon ECS services.
  • AWS KMS -- Optionally we can use AWS Key Management Service to encrypt data and to automatically rotate customer master keys(CMK)
  • AWS S3 -- Optionally we can use Simple Storage Service to store metadata information and to host any static web pages, sites.
  • AWS DynamoDB -- Optionally we can use DynamoDB database to store items for high availability and durability and to offload the administrative burden

Solution:

This service is deployed on Heroku and always gets the latest Bitcoin conversion rates (USD) on the date the API is triggered. Find the below API endpoints of the service.

PS: Bitcoin service demo screenshots available here

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages