Skip to content

Commit 618d071

Browse files
committed
feature: Add max price
1 parent 2ddc359 commit 618d071

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

internal/route/ticker/handler.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ func (a *API) GetPrice(c *gin.Context) {
7474
price, err = a.medianPrice(symbol, exchanges, trading)
7575
case "min":
7676
price, err = a.minPrice(symbol, exchanges, trading)
77+
case "max":
78+
price, err = a.maxPrice(symbol, exchanges, trading)
7779
default:
7880
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid method"})
7981
return
@@ -185,3 +187,27 @@ func (a *API) minPrice(symbol string, exchanges []string, trading string) (*big.
185187

186188
return min, nil
187189
}
190+
191+
func (a *API) maxPrice(symbol string, exchanges []string, trading string) (*big.Float, error) {
192+
max := big.NewFloat(math.SmallestNonzeroFloat64)
193+
194+
isSpot := trading == "spot"
195+
for _, exchange := range exchanges {
196+
ticker, err := a.store.Get(fmt.Sprintf("%s:%s:%s", exchange, symbol, trading))
197+
if err != nil {
198+
return nil, fmt.Errorf("ticker not found: %w", err)
199+
}
200+
201+
if isSpot {
202+
if ticker.Spot.Price.Cmp(max) > 0 {
203+
max = ticker.Spot.Price
204+
}
205+
} else {
206+
if ticker.Futures.IndexPrice.Cmp(max) > 0 {
207+
max = ticker.Futures.IndexPrice
208+
}
209+
}
210+
}
211+
212+
return max, nil
213+
}

internal/route/ticker/handler_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,62 @@ func TestMinPrice(t *testing.T) {
214214
assert.Nil(t, price)
215215
})
216216
}
217+
218+
func TestMaxPrice(t *testing.T) {
219+
t.Run("BTCUSDT max price", func(t *testing.T) {
220+
memory := storage.NewMemory[connector.TickerUpdate]()
221+
api := &API{
222+
store: memory,
223+
}
224+
225+
api.store.Store("binance:BTCUSDT:spot", connector.TickerUpdate{
226+
Spot: &connector.SpotPriceUpdate{
227+
Price: big.NewFloat(10000),
228+
},
229+
})
230+
231+
api.store.Store("okx:BTCUSDT:spot", connector.TickerUpdate{
232+
Spot: &connector.SpotPriceUpdate{
233+
Price: big.NewFloat(10010),
234+
},
235+
})
236+
237+
price, err := api.maxPrice("BTCUSDT", []string{"binance", "okx"}, "spot")
238+
assert.NoError(t, err)
239+
assert.Equal(t, big.NewFloat(10010), price)
240+
})
241+
242+
t.Run("Exchange not found", func(t *testing.T) {
243+
memory := storage.NewMemory[connector.TickerUpdate]()
244+
api := &API{
245+
store: memory,
246+
}
247+
248+
api.store.Store("binance:BTCUSDT:spot", connector.TickerUpdate{
249+
Spot: &connector.SpotPriceUpdate{
250+
Price: big.NewFloat(10000),
251+
},
252+
})
253+
254+
price, err := api.maxPrice("BTCUSDT", []string{"binance", "okx"}, "spot")
255+
assert.Error(t, err)
256+
assert.Nil(t, price)
257+
})
258+
259+
t.Run("Symbol not found", func(t *testing.T) {
260+
memory := storage.NewMemory[connector.TickerUpdate]()
261+
api := &API{
262+
store: memory,
263+
}
264+
265+
api.store.Store("binance:BTCUSDT:spot", connector.TickerUpdate{
266+
Spot: &connector.SpotPriceUpdate{
267+
Price: big.NewFloat(10000),
268+
},
269+
})
270+
271+
price, err := api.maxPrice("ETHUSDT", []string{"binance"}, "spot")
272+
assert.Error(t, err)
273+
assert.Nil(t, price)
274+
})
275+
}

0 commit comments

Comments
 (0)