Skip to content

Commit

Permalink
fix: osmosis multi client (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Johnson authored Jul 25, 2024
1 parent e478121 commit 51f29a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
6 changes: 3 additions & 3 deletions providers/apis/defi/osmosis/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,12 @@ func (mc *MultiClientImpl) SpotPrice(ctx context.Context, poolID uint64, baseAss
var wg sync.WaitGroup
wg.Add(len(mc.clients))

for i, client := range mc.clients {
for i := range mc.clients {
url := mc.api.Endpoints[i].URL

index := i
go func(index int, client Client) {
defer wg.Done()
resp, err := client.SpotPrice(ctx, poolID, baseAsset, quoteAsset)
if err != nil {
mc.logger.Error("failed to spot price in sub client", zap.String("url", url), zap.Error(err))
Expand All @@ -203,8 +204,7 @@ func (mc *MultiClientImpl) SpotPrice(ctx context.Context, poolID uint64, baseAss
mc.logger.Debug("successfully fetched accounts", zap.String("url", url))

resps[index] = resp
}(index, client)

}(index, mc.clients[i])
}

wg.Wait()
Expand Down
14 changes: 8 additions & 6 deletions providers/apis/defi/osmosis/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/mock"

"github.com/stretchr/testify/require"
"go.uber.org/zap"

Expand Down Expand Up @@ -81,15 +83,15 @@ func TestMultiClient(t *testing.T) {
defer cancel()

// mocks
client1.On("SpotPrice", ctx, poolID, baseAsset, quoteAsset).Return(osmosis.SpotPriceResponse{
client1.On("SpotPrice", mock.Anything, poolID, baseAsset, quoteAsset).Return(osmosis.SpotPriceResponse{
SpotPrice: expectedPrice,
}, nil).Once()

client2.On("SpotPrice", ctx, poolID, baseAsset, quoteAsset).Return(osmosis.SpotPriceResponse{
client2.On("SpotPrice", mock.Anything, poolID, baseAsset, quoteAsset).Return(osmosis.SpotPriceResponse{
SpotPrice: expectedPrice,
}, nil).Once()

client3.On("SpotPrice", ctx, poolID, baseAsset, quoteAsset).Return(osmosis.SpotPriceResponse{},
client3.On("SpotPrice", mock.Anything, poolID, baseAsset, quoteAsset).Return(osmosis.SpotPriceResponse{},
fmt.Errorf("error")).Once()

resp, err := client.SpotPrice(ctx, poolID, baseAsset, quoteAsset)
Expand All @@ -111,13 +113,13 @@ func TestMultiClient(t *testing.T) {
defer cancel()

// mocks
client1.On("SpotPrice", ctx, poolID, baseAsset, quoteAsset).Return(osmosis.SpotPriceResponse{
client1.On("SpotPrice", mock.Anything, poolID, baseAsset, quoteAsset).Return(osmosis.SpotPriceResponse{
SpotPrice: expectedPrice,
}, nil).Once()
client2.On("SpotPrice", ctx, poolID, baseAsset, quoteAsset).Return(osmosis.SpotPriceResponse{
client2.On("SpotPrice", mock.Anything, poolID, baseAsset, quoteAsset).Return(osmosis.SpotPriceResponse{
SpotPrice: expectedPrice,
}, nil).Once()
client3.On("SpotPrice", ctx, poolID, baseAsset, quoteAsset).Return(osmosis.SpotPriceResponse{
client3.On("SpotPrice", mock.Anything, poolID, baseAsset, quoteAsset).Return(osmosis.SpotPriceResponse{
SpotPrice: expectedPrice,
}, nil).Once()

Expand Down

0 comments on commit 51f29a3

Please sign in to comment.