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

fix(get-market-chart): handle app panic when geckoterminal return err or empty #1202

Merged
merged 3 commits into from
Sep 26, 2023
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
13 changes: 11 additions & 2 deletions pkg/entities/defi.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,29 @@ func (e *Entity) GetHistoricalMarketChart(req *request.GetMarketChartRequest) (*

data, err := e.svc.GeckoTerminal.GetHistoricalMarketData(nerwork, poolAddr, now.Unix())
if err != nil {
return nil, err, 500
if errors.Is(err, baseerrs.ErrTokenNotSupportedYet) {
return nil, err, http.StatusUnprocessableEntity
}
e.log.Fields(logger.Fields{"req": req}).Error(err, "[entity.GetHistoricalMarketChart] svc.GeckoTerminal.GetHistoricalMarketData() failed")
return nil, err, http.StatusInternalServerError
}

resp = data
default:
data, err, statusCode := e.svc.CoinGecko.GetHistoricalMarketData(req.CoinID, req.Currency, req.Days)
if err != nil {
e.log.Fields(logger.Fields{"req": req}).Error(err, "[entity.GetHistoricalMarketChart] svc.CoinGecko.GetHistoricalMarketData() failed")
return nil, err, statusCode
}

resp = data
}

data := &response.CoinPriceHistoryResponse{}

if len(resp.Prices) == 0 {
return data, nil, http.StatusOK
}

for _, p := range resp.Prices {
timestamp := time.UnixMilli(int64(p[0])).Format("01-02")
data.Times = append(data.Times, timestamp)
Expand Down
1 change: 1 addition & 0 deletions pkg/model/errors/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ var (
ErrInvalidCoingeckoSvcParam = NewStringError("Invalid parameter", 400)
ErrFriendTechKeyAlreadyTracked = NewStringError("Friend tech key already tracked", 409)
ErrFriendTechKeyNotTrackedYet = NewStringError("Friend tech key not tracked yet", 409)
ErrTokenNotSupportedYet = NewStringError("Token not supported yet", 422)
)
10 changes: 8 additions & 2 deletions pkg/service/geckoterminal/geckoterminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"net/url"
"strconv"

"github.com/defipod/mochi/pkg/config"
"github.com/defipod/mochi/pkg/response"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/stealth"

"github.com/defipod/mochi/pkg/config"
"github.com/defipod/mochi/pkg/model/errors"
"github.com/defipod/mochi/pkg/response"
)

const (
Expand Down Expand Up @@ -236,6 +238,10 @@ func (g *GeckoTerminal) GetHistoricalMarketData(network, poolAddr string, before
return nil, err
}

if len(candlesticks.Data.Attributes.OhlcvList) == 0 {
return nil, errors.ErrTokenNotSupportedYet
}

prices := [][]float64{}
marketCaps := [][]float64{}

Expand Down
9 changes: 9 additions & 0 deletions pkg/service/geckoterminal/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,12 @@ type CandlestickData struct {
type CandlestickAttributes struct {
OhlcvList [][]float64 `json:"ohlcv_list"`
}

type ErrorResp struct {
Errors []Error `json:"errors"`
}

type Error struct {
Status string `json:"status"`
Title string `json:"title"`
}