From b24ee6840a8f3624bcf40139d9aa1b2f4237bd4a Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 15 Aug 2024 18:38:11 -0400 Subject: [PATCH] chore: Remove crescent provider (#402) (#404) (cherry picked from commit 2216d80966eea3b743dd8fcf469e6b73d2d37cfd) # Conflicts: # oracle/provider/crescent_test.go Co-authored-by: ryanbajollari <54822716+rbajollari@users.noreply.github.com> --- config/currency_provider_tracker.go | 60 +------ config/supported_assets.go | 1 - oracle/oracle.go | 3 - oracle/provider/crescent.go | 269 ---------------------------- oracle/provider/provider.go | 1 - 5 files changed, 9 insertions(+), 325 deletions(-) delete mode 100644 oracle/provider/crescent.go diff --git a/config/currency_provider_tracker.go b/config/currency_provider_tracker.go index 221f4ba7..1a797162 100644 --- a/config/currency_provider_tracker.go +++ b/config/currency_provider_tracker.go @@ -14,15 +14,13 @@ import ( ) const ( - coinGeckoRestURL = "https://api.coingecko.com/api/v3/coins" - coinGeckoListEndpoint = "list" - coinGeckoTickersEndpoint = "tickers" - osmosisRestURL = "https://api.osmo-api.prod.ojo.network" - osmosisAssetPairsEndpoint = "assetpairs" - crescentRestURL = "https://api.cresc-api.prod.ojo.network" - crescentAssetPairsEndpoint = "assetpairs" - requestTimeout = time.Second * 2 - trackingPeriod = time.Hour * 24 + coinGeckoRestURL = "https://api.coingecko.com/api/v3/coins" + coinGeckoListEndpoint = "list" + coinGeckoTickersEndpoint = "tickers" + osmosisRestURL = "https://api.osmo-api.prod.ojo.network" + osmosisAssetPairsEndpoint = "assetpairs" + requestTimeout = time.Second * 2 + trackingPeriod = time.Hour * 24 ) type ( @@ -90,12 +88,7 @@ func NewCurrencyProviderTracker( return nil, err } - crescentAPIPairs, err := currencyProviderTracker.getCrescentAPIPairs() - if err != nil { - return nil, err - } - - if err := currencyProviderTracker.setCurrencyProviders(osmosisAPIPairs, crescentAPIPairs); err != nil { + if err := currencyProviderTracker.setCurrencyProviders(osmosisAPIPairs); err != nil { return nil, err } @@ -159,36 +152,10 @@ func (t *CurrencyProviderTracker) getOsmosisAPIPairs() (map[string]string, error return osmosisAPIPairs, nil } -// getCrescentAPIPairs queries the crescent-api assetpairs endpoint to get the asset pairs -// supported by it. -func (t *CurrencyProviderTracker) getCrescentAPIPairs() (map[string]string, error) { - client := &http.Client{ - Timeout: requestTimeout, - } - crescentAPIPairs := make(map[string]string) - - crescentResp, err := client.Get(fmt.Sprintf("%s/%s", crescentRestURL, crescentAssetPairsEndpoint)) - if err != nil { - return nil, err - } - defer crescentResp.Body.Close() - var assetPairsResponse []assetPair - if err = json.NewDecoder(crescentResp.Body).Decode(&assetPairsResponse); err != nil { - return nil, err - } - - for _, assetPair := range assetPairsResponse { - crescentAPIPairs[assetPair.Base] = assetPair.Quote - } - - return crescentAPIPairs, nil -} - // setCurrencyProviders queries CoinGecko's tickers endpoint to get all the exchanges // that support each price feeder currency pair and store it in the CurrencyProviders map. func (t *CurrencyProviderTracker) setCurrencyProviders( osmosisAPIPairs map[string]string, - crescentAPIPairs map[string]string, ) error { client := &http.Client{ Timeout: requestTimeout, @@ -199,11 +166,6 @@ func (t *CurrencyProviderTracker) setCurrencyProviders( t.CurrencyProviders[pair.Base] = append(t.CurrencyProviders[pair.Base], "osmosis") } - // check if its a pair supported by the crescent api - if crescentAPIPairs[strings.ToUpper(pair.Base)] == strings.ToUpper(pair.Quote) { - t.CurrencyProviders[pair.Base] = append(t.CurrencyProviders[pair.Base], "crescent") - } - // check if CoinGecko API supports pair pairBaseID := t.coinIDSymbolMap[strings.ToLower(pair.Base)] coinGeckoResp, err := client.Get(fmt.Sprintf("%s/%s/%s", coinGeckoRestURL, pairBaseID, coinGeckoTickersEndpoint)) @@ -259,11 +221,7 @@ func (t *CurrencyProviderTracker) trackCurrencyProviders(ctx context.Context) { if err != nil { t.logger.Error().Err(err).Msg("failed to query osmosis-api for available asset pairs") } - crescentAPIPairs, err := t.getOsmosisAPIPairs() - if err != nil { - t.logger.Error().Err(err).Msg("failed to query crescent-api for available asset pairs") - } - if err := t.setCurrencyProviders(osmosisAPIPairs, crescentAPIPairs); err != nil { + if err := t.setCurrencyProviders(osmosisAPIPairs); err != nil { t.logger.Error().Err(err).Msg("failed to set available providers for currencies") } diff --git a/config/supported_assets.go b/config/supported_assets.go index c44c9d9b..42532ef6 100644 --- a/config/supported_assets.go +++ b/config/supported_assets.go @@ -14,7 +14,6 @@ var ( provider.ProviderKraken: false, provider.ProviderBinance: false, provider.ProviderBinanceUS: false, - provider.ProviderCrescent: false, provider.ProviderOsmosis: false, provider.ProviderOkx: false, provider.ProviderHuobi: false, diff --git a/oracle/oracle.go b/oracle/oracle.go index 062c15e8..f41e65b6 100644 --- a/oracle/oracle.go +++ b/oracle/oracle.go @@ -462,9 +462,6 @@ func NewProvider( case provider.ProviderPolygon: return provider.NewPolygonProvider(ctx, logger, endpoint, providerPairs...) - case provider.ProviderCrescent: - return provider.NewCrescentProvider(ctx, logger, endpoint, providerPairs...) - case provider.ProviderKujira: return provider.NewKujiraProvider(ctx, logger, endpoint, providerPairs...) diff --git a/oracle/provider/crescent.go b/oracle/provider/crescent.go deleted file mode 100644 index ed70efab..00000000 --- a/oracle/provider/crescent.go +++ /dev/null @@ -1,269 +0,0 @@ -package provider - -import ( - "context" - "encoding/json" - "net/http" - "net/url" - "strings" - "sync" - - "github.com/gorilla/websocket" - "github.com/ojo-network/price-feeder/oracle/types" - "github.com/rs/zerolog" -) - -const ( - crescentV2WSHost = "api.cresc-api.prod.ojo.network" - crescentV2WSPath = "ws" - crescentV2RestHost = "https://api.cresc-api.prod.ojo.network" - crescentV2RestPath = "/assetpairs" - crescentAckMsg = "ack" -) - -var _ Provider = (*CrescentProvider)(nil) - -type ( - // CrescentProvider defines an Oracle provider implemented by OJO's - // Crescent API. - CrescentProvider struct { - wsc *WebsocketController - wsURL url.URL - logger zerolog.Logger - mtx sync.RWMutex - endpoints Endpoint - - priceStore - } - - CrescentTicker struct { - Price string `json:"Price"` - Volume string `json:"Volume"` - } - - CrescentCandle struct { - Close string `json:"Close"` - Volume string `json:"Volume"` - EndTime int64 `json:"EndTime"` - } - - // CrescentPairsSummary defines the response structure for an Crescent pairs - // summary. - CrescentPairsSummary struct { - Data []CrescentPairData `json:"data"` - } - - // CrescentPairData defines the data response structure for an Crescent pair. - CrescentPairData struct { - Base string `json:"base"` - Quote string `json:"quote"` - } -) - -func NewCrescentProvider( - ctx context.Context, - logger zerolog.Logger, - endpoints Endpoint, - pairs ...types.CurrencyPair, -) (*CrescentProvider, error) { - if endpoints.Name != ProviderCrescent { - endpoints = Endpoint{ - Name: ProviderCrescent, - Rest: crescentV2RestHost, - Websocket: crescentV2WSHost, - } - } - - wsURL := url.URL{ - Scheme: "wss", - Host: endpoints.Websocket, - Path: crescentV2WSPath, - } - - crescentV2Logger := logger.With().Str("provider", "crescent").Logger() - - provider := &CrescentProvider{ - wsURL: wsURL, - logger: crescentV2Logger, - endpoints: endpoints, - priceStore: newPriceStore(crescentV2Logger), - } - provider.setCurrencyPairToTickerAndCandlePair(currencyPairToCrescentPair) - - confirmedPairs, err := ConfirmPairAvailability( - provider, - provider.endpoints.Name, - provider.logger, - pairs..., - ) - if err != nil { - return nil, err - } - - provider.setSubscribedPairs(confirmedPairs...) - - provider.wsc = NewWebsocketController( - ctx, - endpoints.Name, - wsURL, - []interface{}{""}, - provider.messageReceived, - defaultPingDuration, - websocket.PingMessage, - crescentV2Logger, - ) - - return provider, nil -} - -func (p *CrescentProvider) StartConnections() { - p.wsc.StartConnections() -} - -// SubscribeCurrencyPairs sends the new subscription messages to the websocket -// and adds them to the providers subscribedPairs array -func (p *CrescentProvider) SubscribeCurrencyPairs(cps ...types.CurrencyPair) { - p.mtx.Lock() - defer p.mtx.Unlock() - - confirmedPairs, err := ConfirmPairAvailability( - p, - p.endpoints.Name, - p.logger, - cps..., - ) - if err != nil { - return - } - - p.setSubscribedPairs(confirmedPairs...) -} - -func (p *CrescentProvider) messageReceived(_ int, _ *WebsocketConnection, bz []byte) { - // check if message is an ack - if string(bz) == crescentAckMsg { - return - } - - var ( - messageResp map[string]interface{} - messageErr error - tickerResp CrescentTicker - tickerErr error - candleResp []CrescentCandle - candleErr error - ) - - messageErr = json.Unmarshal(bz, &messageResp) - if messageErr != nil { - p.logger.Error(). - Int("length", len(bz)). - AnErr("message", messageErr). - Msg("Error on receive message") - } - - // Check the response for currency pairs that the provider is subscribed - // to and determine whether it is a ticker or candle. - for _, pair := range p.subscribedPairs { - crescentPair := currencyPairToCrescentPair(pair) - if msg, ok := messageResp[crescentPair]; ok { - switch v := msg.(type) { - // ticker response - case map[string]interface{}: - tickerString, _ := json.Marshal(v) - tickerErr = json.Unmarshal(tickerString, &tickerResp) - if tickerErr != nil { - p.logger.Error(). - Int("length", len(bz)). - AnErr("ticker", tickerErr). - Msg("Error on receive message") - continue - } - p.setTickerPair( - tickerResp, - crescentPair, - ) - telemetryWebsocketMessage(ProviderCrescent, MessageTypeTicker) - continue - - // candle response - case []interface{}: - // use latest candlestick in list if there is one - if len(v) == 0 { - continue - } - candleString, _ := json.Marshal(v) - candleErr = json.Unmarshal(candleString, &candleResp) - if candleErr != nil { - p.logger.Error(). - Int("length", len(bz)). - AnErr("candle", candleErr). - Msg("Error on receive message") - continue - } - for _, singleCandle := range candleResp { - p.setCandlePair( - singleCandle, - crescentPair, - ) - } - telemetryWebsocketMessage(ProviderCrescent, MessageTypeCandle) - continue - } - } - } -} - -func (ct CrescentTicker) toTickerPrice() (types.TickerPrice, error) { - return types.NewTickerPrice( - ct.Price, - ct.Volume, - ) -} - -func (cc CrescentCandle) toCandlePrice() (types.CandlePrice, error) { - return types.NewCandlePrice( - cc.Close, - cc.Volume, - cc.EndTime, - ) -} - -// setSubscribedPairs sets N currency pairs to the map of subscribed pairs. -func (p *CrescentProvider) setSubscribedPairs(cps ...types.CurrencyPair) { - for _, cp := range cps { - p.subscribedPairs[cp.String()] = cp - } -} - -// GetAvailablePairs returns all pairs to which the provider can subscribe. -// ex.: map["ATOMUSDT" => {}, "OJOUSDC" => {}]. -func (p *CrescentProvider) GetAvailablePairs() (map[string]struct{}, error) { - resp, err := http.Get(p.endpoints.Rest + crescentV2RestPath) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - var pairsSummary []CrescentPairData - if err := json.NewDecoder(resp.Body).Decode(&pairsSummary); err != nil { - return nil, err - } - - availablePairs := make(map[string]struct{}, len(pairsSummary)) - for _, pair := range pairsSummary { - cp := types.CurrencyPair{ - Base: pair.Base, - Quote: pair.Quote, - } - availablePairs[strings.ToUpper(cp.String())] = struct{}{} - } - - return availablePairs, nil -} - -// currencyPairToCrescentPair receives a currency pair and return crescent -// ticker symbol atomusdt@ticker. -func currencyPairToCrescentPair(cp types.CurrencyPair) string { - return cp.Base + "/" + cp.Quote -} diff --git a/oracle/provider/provider.go b/oracle/provider/provider.go index f7c8b22d..5e954c3c 100644 --- a/oracle/provider/provider.go +++ b/oracle/provider/provider.go @@ -21,7 +21,6 @@ const ( ProviderMexc types.ProviderName = "mexc" ProviderCrypto types.ProviderName = "crypto" ProviderPolygon types.ProviderName = "polygon" - ProviderCrescent types.ProviderName = "crescent" ProviderEthUniswap types.ProviderName = "eth-uniswap" ProviderKujira types.ProviderName = "kujira" ProviderMock types.ProviderName = "mock"