Giter Club home page Giter Club logo

coingecko-graphql's Introduction

coingeck-graphql

Setup

Prerequisites:

  • docker is installed along with docker compose
  • cargo is installed through rustup

In the root folder of the project execute the following:

  • cargo build
  • docker compose up
  • cargo run
  • open the browser with the following link

Important notes

  • I extended the original concept with the normal currency (currency_ticker) against which I retrieve prices. There is an additional endpoint by which the user can figure out what sort of "eth" ticker types exist. The different variants are called crypto_ids and they form the basis of any subsequent queries.
  • I used a small in memory cache for storing crypto_ids that gets filled up at the beginning of the server lifecycle.
  • Subscription mostly goes from the db if the record is not older than 10 minutes (coingecko states that they refresh current prices every 5 minutes, however that was not what I experienced)
  • Migration running should not be a part of the application initialization cycle, however it is convenient for this demo (should be part of CI).
  • I do not try to handle the data granularity of the historical endpoint, however it would be better to sort and create separate tables according to daily/hourly/5min prices as stated in their documentation:
    • 1 day from current time = 5 minute interval data
      2 - 90 days of date range = hourly data
      above 90 days of date range = daily data (00:00 UTC)
      
  • The historical data is stored in the db however it is not queried from there for now.

How to run migrations:

Run the following commands:

  • install cargo install sqlx-cli
  • sqlx migrate run --database-url "postgres://user:pass@localhost/prices"

Tests

  • Two unit tests were written for the get_current_price_service.

Example queries

To get the cryptoIds pertaining to a crypto ticker such as "eth"


{
cryptoTickerIds(cryptoTicker: "eth")
}

Query Result:


{
"data": {
"cryptoTickerIds": [
"bridged-wrapped-ether-starkgate",
"ethereum",
"ethereum-wormhole"
]
}
}

To get the current price by cryptoId and the currency ticker:


{
getCurrentPrice(cryptoId: "bitcoin", currencyTicker: "usd") {
cryptoId
currencyTicker
price
}
}

Query Result:


{
"data": {
"getCurrentPrice": {
"cryptoId": "bitcoin",
"currencyTicker": "usd",
"price": 26903
}
}
}

Get the historical prices


{
getHistoricalPrice(
cryptoId: "bitcoin"
currencyTicker: "usd"
from: "2023-09-01T00:00:00.12345Z"
to: "2022-10-15T00:00:00.12345Z"
) {
cryptoId
currencyTicker
price
}
}

Query Result:


{
"data": {
"getHistoricalPrice": [
{
"cryptoId": "zombie-inu-2",
"currencyTicker": "usd",
"price": 0.001330072162941906,
"utcTimestamp": "2023-08-12T01:01:26Z"
},
{
"cryptoId": "zombie-inu-2",
"currencyTicker": "usd",
"price": 0.001330303633929849,
"utcTimestamp": "2023-08-12T02:01:00Z"
},
{
"cryptoId": "zombie-inu-2",
"currencyTicker": "usd",
"price": 0.0013305024345516816,
"utcTimestamp": "2023-08-12T03:01:41Z"
},
....
]
}
}

Subscribe to latest price changes:


subscription{
currentPrice(cryptoId:"bitcoin", currencyTicker:"usd"){cryptoId, currencyTicker, price}
}

Query Result:


{
"data": {
"currentPrice": {
"cryptoId": "bitcoin",
"currencyTicker": "usd",
"price": 27068
}
}
}


coingecko-graphql's People

Contributors

balazssevecsek avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.