Skip to content

Commit

Permalink
cleanup + add a basic test
Browse files Browse the repository at this point in the history
  • Loading branch information
adamewozniak committed Nov 27, 2023
1 parent 75454f3 commit aa05204
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
3 changes: 2 additions & 1 deletion config/supported_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down
28 changes: 15 additions & 13 deletions oracle/provider/astroport.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type (

client *http.Client
priceStore
ctx context.Context
}

// AstroportAssetResponse is the response from the Astroport assets endpoint.
Expand Down Expand Up @@ -83,6 +84,7 @@ func NewAstroportProvider(
endpoints: endpoints,
priceStore: newPriceStore(astroLogger),
client: &http.Client{},
ctx: ctx,
}

confirmedPairs, err := ConfirmPairAvailability(
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
41 changes: 41 additions & 0 deletions oracle/provider/astroport_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit aa05204

Please sign in to comment.