Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ratios endpoint to proxy calls to Coingecko coin markets endpoint #1234

Merged
merged 2 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/ratios/rest_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func RestRun(command *cobra.Command, args []string) {
r.Get("/v2/relative/provider/coingecko/{coinIDs}/{vsCurrencies}/{duration}", middleware.InstrumentHandler("GetRelativeHandler", ratios.GetRelativeHandler(s)).ServeHTTP)
r.Get("/v2/history/coingecko/{coinID}/{vsCurrency}/{duration}", middleware.InstrumentHandler("GetHistoryHandler", ratios.GetHistoryHandler(s)).ServeHTTP)
r.Get("/v2/coinmap/provider/coingecko", middleware.InstrumentHandler("GetMappingHandler", ratios.GetMappingHandler(s)).ServeHTTP)
r.Get("/v2/market/provider/coingecko", middleware.InstrumentHandler("GetCoinMarketsHandler", ratios.GetCoinMarketsHandler(s)).ServeHTTP)

err = cmd.SetupJobWorkers(command.Context(), s.Jobs())
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ services:
- DEBUG=1
- BAT_SETTLEMENT_ADDRESS
- BRAVE_TRANSFER_PROMOTION_ID
- COINGECKO_SERVICE
- COINGECKO_APIKEY
- CHALLENGE_BYPASS_SERVER=http://challenge-bypass:2416
- CHALLENGE_BYPASS_TOKEN
- "DATABASE_MIGRATIONS_URL=file:///src/migrations"
Expand Down
60 changes: 59 additions & 1 deletion ratios/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func GetRelativeHandler(service *Service) handlers.AppHandler {
if err != nil {
ctx, logger = logging.SetupLogger(ctx)
}

var coinIDs = new(CoingeckoCoinList)
if err = inputs.DecodeAndValidate(ctx, coinIDs, []byte(coinIDsInput)); err != nil {
if errors.Is(err, ErrCoingeckoCoinInvalid) {
Expand Down Expand Up @@ -189,3 +188,62 @@ func GetMappingHandler(service *Service) handlers.AppHandler {
return handlers.RenderContent(ctx, resp, w, http.StatusOK)
})
}

// GetCoinMarketsHandler - handler to get top currency data
func GetCoinMarketsHandler(service *Service) handlers.AppHandler {
return handlers.AppHandler(func(w http.ResponseWriter, r *http.Request) *handlers.AppError {
// get context from request
ctx := r.Context()

var (
vsCurrencyInput = r.URL.Query().Get("vsCurrency")
limitInput = r.URL.Query().Get("limit")
)

// get logger from context
logger, err := appctx.GetLogger(ctx)
if err != nil {
ctx, logger = logging.SetupLogger(ctx)
}

var vsCurrency = new(CoingeckoVsCurrency)
if err = inputs.DecodeAndValidate(ctx, vsCurrency, []byte(vsCurrencyInput)); err != nil {
if errors.Is(err, ErrCoingeckoVsCurrencyInvalid) {
logger.Error().Err(err).Msg("invalid vs currency input from caller")
return handlers.ValidationError(
"Error validating vs currency url parameter",
map[string]interface{}{
"err": err.Error(),
"vsCurrency": "invalid vs currency",
},
)
}
// degraded, unknown error when validating/decoding
logger.Error().Err(err).Msg("unforseen error in decode and validation")
return handlers.WrapError(err, "degraded: ", http.StatusInternalServerError)
}

var limit = new(CoingeckoLimit)
if err = inputs.DecodeAndValidate(ctx, limit, []byte(limitInput)); err != nil {
if errors.Is(err, ErrCoingeckoLimitInvalid) {
logger.Error().Err(err).Msg("invalid limit input from caller")
return handlers.ValidationError(
"Error validating vs currency url parameter",
map[string]interface{}{
"err": err.Error(),
"limit": "invalid limit",
},
)
}
logger.Error().Err(err).Msg("unforseen error in decode and validation")
return handlers.WrapError(err, "degraded: ", http.StatusInternalServerError)
}

data, err := service.GetCoinMarkets(ctx, *vsCurrency, *limit)
if err != nil {
logger.Error().Err(err).Msg("failed to get top currencies")
return handlers.WrapError(err, "failed to get top currencies", http.StatusInternalServerError)
}
return handlers.RenderContent(ctx, data, w, http.StatusOK)
})
}
47 changes: 45 additions & 2 deletions ratios/controllers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ func (suite *ControllersTestSuite) TestGetHistoryHandler() {
}

func (suite *ControllersTestSuite) TestGetRelativeHandler() {

handler := ratios.GetRelativeHandler(suite.service)

respy := coingecko.SimplePriceResponse(map[string]map[string]decimal.Decimal{
"basic-attention-token": map[string]decimal.Decimal{"usd": decimal.Zero},
})
Expand Down Expand Up @@ -186,3 +184,48 @@ func (suite *ControllersTestSuite) TestGetRelativeHandler() {
_, okUsd := v["usd"]
suite.Require().True(okUsd)
}

func (suite *ControllersTestSuite) TestGetCoinMarketsHandler() {
handler := ratios.GetCoinMarketsHandler(suite.service)
coingeckoResp := coingecko.CoinMarketResponse(
[]coingecko.CoinMarket{
{
ID: "bitcoin",
Symbol: "btc",
Name: "Bitcoin",
Image: "https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579",
MarketCap: 728577821016,
MarketCapRank: 1,
CurrentPrice: 38400,
PriceChange24h: 558.39,
PriceChangePercentage24h: 1.4756,
TotalVolume: 41369367560,
},
},
)
suite.mockClient.EXPECT().
FetchCoinMarkets(gomock.Any(), gomock.Any(), gomock.Any()).
Return(&coingeckoResp, time.Now(), nil)

// new request for coin markets handler
req, err := http.NewRequest("GET", "/v2/market/provider/coingecko?vsCurrency=usd&limit=1", nil)
suite.Require().NoError(err)

rctx := chi.NewRouteContext()

// add in our suite ctx
req = req.WithContext(suite.ctx)
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
// validate response code matches
suite.Require().Equal(http.StatusOK, rr.Code)

var resp = new(ratios.GetCoinMarketsResponse)
err = json.Unmarshal(rr.Body.Bytes(), resp)
suite.Require().NoError(err)

suite.Require().Equal(len(resp.Payload), 1)
suite.Require().Equal(resp.Payload[0].Symbol, "btc")
}
37 changes: 37 additions & 0 deletions ratios/inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strconv"
"strings"

appctx "github.com/brave-intl/bat-go/utils/context"
Expand Down Expand Up @@ -221,6 +222,9 @@ var (

// ErrCoingeckoDurationInvalid - indicates there is a validation issue with the duration
ErrCoingeckoDurationInvalid = errors.New("invalid duration")

// ErrCoingeckoLimitInvalid - indicates there is a validation issue with the Limit
ErrCoingeckoLimitInvalid = errors.New("invalid limit")
)

// Decode - implement decodable
Expand All @@ -237,3 +241,36 @@ func (cd *CoingeckoDuration) Validate(ctx context.Context) error {
}
return fmt.Errorf("%w: %s is not valid", ErrCoingeckoDurationInvalid, cd.String())
}

// CoingeckoLimit - type for number of results per page
// Note: we only will request the first page
type CoingeckoLimit int

// String - stringer implmentation
func (cl *CoingeckoLimit) String() string {
return strconv.Itoa(int(*cl))
}

// Int - int conversion implmentation
func (cl *CoingeckoLimit) Int() int {
return int(*cl)
}

// Decode - implement decodable
func (cl *CoingeckoLimit) Decode(ctx context.Context, v []byte) error {
l, err := strconv.Atoi(string(v))
if err != nil {
return err
}

*cl = CoingeckoLimit(l)
return nil
}

// Validate - implement validatable
func (cl *CoingeckoLimit) Validate(ctx context.Context) error {
if !(0 < cl.Int() && cl.Int() <= 250) {
return fmt.Errorf("%w: %s is not valid", ErrCoingeckoLimitInvalid, cl.String())
}
return nil
}
32 changes: 31 additions & 1 deletion ratios/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func InitService(ctx context.Context) (context.Context, *Service, error) {
logger.Error().Err(err).Msg("failed to initialize the coingecko client")
return ctx, nil, fmt.Errorf("failed to initialize coingecko client: %w", err)
}

service := NewService(ctx, client, redis)

ctx, err = service.initializeCoingeckoCurrencies(ctx)
Expand Down Expand Up @@ -230,3 +229,34 @@ func (s *Service) GetHistory(ctx context.Context, coinID CoingeckoCoin, vsCurren
LastUpdated: updated,
}, nil
}

// GetCoinMarketsResponse - the response structure for top currency calls
type GetCoinMarketsResponse struct {
Payload coingecko.CoinMarketResponse `json:"payload"`
LastUpdated time.Time `json:"lastUpdated"`
}

// GetCoinMarkets - respond to caller with top currencies
func (s *Service) GetCoinMarkets(
ctx context.Context,
vsCurrency CoingeckoVsCurrency,
limit CoingeckoLimit,
) (*GetCoinMarketsResponse, error) {

// get logger from context
logger, err := appctx.GetLogger(ctx)
if err != nil {
ctx, logger = logging.SetupLogger(ctx)
}

payload, updated, err := s.coingecko.FetchCoinMarkets(ctx, vsCurrency.String(), limit.Int())
if err != nil {
logger.Error().Err(err).Msg("failed to fetch coin markets data from coingecko")
return nil, fmt.Errorf("failed to fetch coin markets data from coingecko: %w", err)
}

return &GetCoinMarketsResponse{
Payload: *payload,
LastUpdated: updated,
}, nil
}
1 change: 1 addition & 0 deletions utils/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ func (c *SimpleHTTPClient) do(ctx context.Context, req *http.Request, v interfac
return resp, errors.Wrap(err, ErrUnableToDecode)
}
}

return resp, nil
}

Expand Down
123 changes: 123 additions & 0 deletions utils/clients/coingecko/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ import (
"github.com/shopspring/decimal"
)

const (
coinMarketsPageSize = 250
coinMarketsCacheTTLHours = 1 // How long we consider Redis cached FetchCoinMarkets responses to be valid
)

// Client abstracts over the underlying client
type Client interface {
FetchSimplePrice(ctx context.Context, ids string, vsCurrencies string, include24hrChange bool) (*SimplePriceResponse, error)
FetchCoinList(ctx context.Context, includePlatform bool) (*CoinListResponse, error)
FetchSupportedVsCurrencies(ctx context.Context) (*SupportedVsCurrenciesResponse, error)
FetchMarketChart(ctx context.Context, id string, vsCurrency string, days float32) (*MarketChartResponse, time.Time, error)
FetchCoinMarkets(ctx context.Context, vsCurrency string, limit int) (*CoinMarketResponse, time.Time, error)
}

// HTTPClient wraps http.Client for interacting with the coingecko server
Expand Down Expand Up @@ -289,3 +295,120 @@ func (c *HTTPClient) FetchSupportedVsCurrencies(ctx context.Context) (*Supported

return &body, nil
}

type coinMarketParams struct {
baseParams
VsCurrency string `url:"vs_currency"`
Page int `url:"page"`
PerPage int `url:"per_page"`
Limit int `url:"-"`
}

// GenerateQueryString - implement the QueryStringBody interface
func (p *coinMarketParams) GenerateQueryString() (url.Values, error) {
p.Page = 1
p.PerPage = coinMarketsPageSize
return query.Values(p)
}

// CoinMarket represents the market data for a single coin returned in
// FetchCoinMarkets call to coingecko
type CoinMarket struct {
ID string `json:"id"`
Symbol string `json:"symbol"`
Name string `json:"name"`
Image string `json:"image"`
MarketCap int `json:"market_cap"`
MarketCapRank int `json:"market_cap_rank"`
CurrentPrice float64 `json:"current_price"`
PriceChange24h float64 `json:"price_change_24h"`
PriceChangePercentage24h float64 `json:"price_change_percentage_24h"`
TotalVolume float64 `json:"total_volume"`
}

// CoinMarketResponse is the coingecko response for FetchCoinMarkets
type CoinMarketResponse []CoinMarket

func (cmr *CoinMarketResponse) applyLimit(limit int) CoinMarketResponse {
return (*cmr)[:limit]
}

// FetchCoinMarkets fetches the market data for the top coins
func (c *HTTPClient) FetchCoinMarkets(
ctx context.Context,
vsCurrency string,
limit int,
) (*CoinMarketResponse, time.Time, error) {
updated := time.Now()
url := "/api/v3/coins/markets"
params := &coinMarketParams{
baseParams: c.baseParams,
VsCurrency: vsCurrency,
Limit: limit,
}

cacheKey, err := c.cacheKey(ctx, url, params)
husobee marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, updated, err
}

conn := c.redis.Get()
defer closers.Log(ctx, conn)

var body CoinMarketResponse
var entry cacheEntry
entryBytes, err := redis.Bytes(conn.Do("GET", cacheKey))

// Check cache first before making request to Coingecko
if err == nil {
err = json.Unmarshal(entryBytes, &entry)
if err != nil {
return nil, updated, err
}

err = json.Unmarshal([]byte(entry.Payload), &body)
if err != nil {
return nil, updated, err
}

// Check if cache is still fresh
if time.Since(entry.LastUpdated).Hours() < float64(coinMarketsCacheTTLHours) {
body = (&body).applyLimit(params.Limit)
return &body, entry.LastUpdated, err
}
}
req, err := c.client.NewRequest(ctx, "GET", url, nil, params)
if err != nil {
return nil, updated, err
}

_, err = c.client.Do(ctx, req, &body)
if err != nil {
// attempt to use cache response on error if exists
if len(entry.Payload) > 0 {
body = (&body).applyLimit(params.Limit)
return &body, entry.LastUpdated, nil
}

return nil, updated, err
}

body = (&body).applyLimit(params.Limit)
bodyBytes, err := json.Marshal(&body)
if err != nil {
return nil, updated, err
}

// Update the cache
entryBytes, err = json.Marshal(&cacheEntry{Payload: string(bodyBytes), LastUpdated: updated})
if err != nil {
return nil, updated, err
}

_, err = conn.Do("SET", cacheKey, entryBytes)
if err != nil {
return nil, updated, err
}

return &body, updated, nil
}
Loading