From aa052044b36a626e71b5fc5226e76aac36d54f30 Mon Sep 17 00:00:00 2001 From: Adam Wozniak <29418299+adamewozniak@users.noreply.github.com> Date: Mon, 27 Nov 2023 14:07:53 -0800 Subject: [PATCH] cleanup + add a basic test --- config/supported_assets.go | 3 ++- oracle/provider/astroport.go | 28 +++++++++++---------- oracle/provider/astroport_test.go | 41 +++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 oracle/provider/astroport_test.go diff --git a/config/supported_assets.go b/config/supported_assets.go index 2b15db4..160f83c 100644 --- a/config/supported_assets.go +++ b/config/supported_assets.go @@ -41,12 +41,13 @@ var ( {Base: "ETH", Quote: "USD"}: {}, {Base: "ATOM", Quote: "USD"}: {}, {Base: "OSMO", Quote: "USD"}: {}, + {Base: "INJ", Quote: "USD"}: {}, {Base: "OSMO", Quote: "USDT"}: {}, {Base: "JUNO", Quote: "USDT"}: {}, {Base: "WETH", Quote: "USDC"}: {}, {Base: "WBTC", Quote: "WETH"}: {}, - {Base: "INJ", Quote: "USD"}: {}, + {Base: "INJ", Quote: "USDT"}: {}, } SupportedUniswapCurrencies = map[string]struct{}{ diff --git a/oracle/provider/astroport.go b/oracle/provider/astroport.go index 8b2e391..c70138e 100644 --- a/oracle/provider/astroport.go +++ b/oracle/provider/astroport.go @@ -33,6 +33,7 @@ type ( client *http.Client priceStore + ctx context.Context } // AstroportAssetResponse is the response from the Astroport assets endpoint. @@ -83,6 +84,7 @@ func NewAstroportProvider( endpoints: endpoints, priceStore: newPriceStore(astroLogger), client: &http.Client{}, + ctx: ctx, } confirmedPairs, err := ConfirmPairAvailability( @@ -95,14 +97,6 @@ func NewAstroportProvider( return nil, err } - go func() { - logger.Debug().Msg("starting astroport polling...") - err := provider.poll(ctx) - if err != nil { - logger.Err(err).Msg("astroport provider unable to poll new data") - } - }() - provider.setSubscribedPairs(confirmedPairs...) return provider, nil @@ -149,9 +143,17 @@ func (p *AstroportProvider) SubscribeCurrencyPairs(cps ...types.CurrencyPair) { p.setSubscribedPairs(confirmedPairs...) } -// StartConnections starts the websocket connections. -// This function is a no-op for the astroport provider. -func (p *AstroportProvider) StartConnections() {} +// StartConnections begins the polling process for +// the astroport provider. +func (p *AstroportProvider) StartConnections() { + go func() { + p.logger.Debug().Msg("starting astroport polling...") + err := p.poll() + if err != nil { + p.logger.Err(err).Msg("astroport provider unable to poll new data") + } + }() +} // AstroportTickerPairs is a struct to hold the AstroportTickersResponse and the // corresponding pair. It satisfies the TickerPrice interface. @@ -261,10 +263,10 @@ func (p *AstroportProvider) queryTickers() ([]AstroportTickerPairs, error) { } // This function periodically calls setTickers to update the priceStore. -func (p *AstroportProvider) poll(ctx context.Context) error { +func (p *AstroportProvider) poll() error { for { select { - case <-ctx.Done(): + case <-p.ctx.Done(): return nil default: diff --git a/oracle/provider/astroport_test.go b/oracle/provider/astroport_test.go new file mode 100644 index 0000000..2701006 --- /dev/null +++ b/oracle/provider/astroport_test.go @@ -0,0 +1,41 @@ +package provider + +import ( + "context" + "os" + "testing" + "time" + + "github.com/ojo-network/price-feeder/oracle/types" + "github.com/rs/zerolog" + "github.com/stretchr/testify/require" +) + +// TestAstroportProvider_GetTickers tests the polling process. +// TODO: Make this more comprehensive. +// +// Ref: https://github.com/ojo-network/price-feeder/issues/317 +func TestAstroportProvider_GetTickers(t *testing.T) { + ctx := context.Background() + pairs := []types.CurrencyPair{{ + Base: "STINJ", + Quote: "INJ", + }} + p, err := NewAstroportProvider( + ctx, + zerolog.New(os.Stdout).With().Timestamp().Logger(), + Endpoint{}, + pairs..., + ) + require.NoError(t, err) + availPairs, err := p.GetAvailablePairs() + require.NoError(t, err) + require.NotEmpty(t, availPairs) + + p.StartConnections() + time.Sleep(2 * time.Second) + + res, err := p.GetTickerPrices(pairs...) + require.NoError(t, err) + require.NotEmpty(t, res) +}