Skip to content

Commit

Permalink
maxapi: simplify ticker response parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Apr 12, 2023
1 parent 012ef4a commit f7d3fca
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 60 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,7 @@ golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
32 changes: 16 additions & 16 deletions pkg/exchange/max/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticke

return &types.Ticker{
Time: ticker.Time,
Volume: fixedpoint.MustNewFromString(ticker.Volume),
Last: fixedpoint.MustNewFromString(ticker.Last),
Open: fixedpoint.MustNewFromString(ticker.Open),
High: fixedpoint.MustNewFromString(ticker.High),
Low: fixedpoint.MustNewFromString(ticker.Low),
Buy: fixedpoint.MustNewFromString(ticker.Buy),
Sell: fixedpoint.MustNewFromString(ticker.Sell),
Volume: ticker.Volume,
Last: ticker.Last,
Open: ticker.Open,
High: ticker.High,
Low: ticker.Low,
Buy: ticker.Buy,
Sell: ticker.Sell,
}, nil
}

Expand Down Expand Up @@ -112,15 +112,16 @@ func (e *Exchange) QueryTickers(ctx context.Context, symbol ...string) (map[stri
if _, ok := m[toGlobalSymbol(k)]; len(symbol) != 0 && !ok {
continue
}

tickers[toGlobalSymbol(k)] = types.Ticker{
Time: v.Time,
Volume: fixedpoint.MustNewFromString(v.Volume),
Last: fixedpoint.MustNewFromString(v.Last),
Open: fixedpoint.MustNewFromString(v.Open),
High: fixedpoint.MustNewFromString(v.High),
Low: fixedpoint.MustNewFromString(v.Low),
Buy: fixedpoint.MustNewFromString(v.Buy),
Sell: fixedpoint.MustNewFromString(v.Sell),
Volume: v.Volume,
Last: v.Last,
Open: v.Open,
High: v.High,
Low: v.Low,
Buy: v.Buy,
Sell: v.Sell,
}
}
}
Expand Down Expand Up @@ -959,8 +960,7 @@ func (e *Exchange) QueryAveragePrice(ctx context.Context, symbol string) (fixedp
return fixedpoint.Zero, err
}

return fixedpoint.MustNewFromString(ticker.Sell).
Add(fixedpoint.MustNewFromString(ticker.Buy)).Div(Two), nil
return ticker.Sell.Add(ticker.Buy).Div(Two), nil
}

func (e *Exchange) RepayMarginAsset(ctx context.Context, asset string, amount fixedpoint.Value) error {
Expand Down
20 changes: 20 additions & 0 deletions pkg/exchange/max/maxapi/get_ticker_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package max

import (
"github.com/c9s/requestgen"
)

//go:generate -command GetRequest requestgen -method GET
//go:generate -command PostRequest requestgen -method POST
//go:generate -command DeleteRequest requestgen -method DELETE

//go:generate GetRequest -url "/api/v2/tickers/:market" -type GetTickerRequest -responseType .Ticker
type GetTickerRequest struct {
client requestgen.APIClient

market *string `param:"market,slug"`
}

func (c *RestClient) NewGetTickerRequest() *GetTickerRequest {
return &GetTickerRequest{client: c}
}
154 changes: 154 additions & 0 deletions pkg/exchange/max/maxapi/get_ticker_request_requestgen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 12 additions & 44 deletions pkg/exchange/max/maxapi/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package max
import (
"context"
"fmt"
"net/url"
"strings"
"time"

Expand Down Expand Up @@ -34,15 +33,15 @@ type Market struct {
type Ticker struct {
Time time.Time

At int64 `json:"at"`
Buy string `json:"buy"`
Sell string `json:"sell"`
Open string `json:"open"`
High string `json:"high"`
Low string `json:"low"`
Last string `json:"last"`
Volume string `json:"vol"`
VolumeInBTC string `json:"vol_in_btc"`
At int64 `json:"at"`
Buy fixedpoint.Value `json:"buy"`
Sell fixedpoint.Value `json:"sell"`
Open fixedpoint.Value `json:"open"`
High fixedpoint.Value `json:"high"`
Low fixedpoint.Value `json:"low"`
Last fixedpoint.Value `json:"last"`
Volume fixedpoint.Value `json:"vol"`
VolumeInBTC fixedpoint.Value `json:"vol_in_btc"`
}

func (s *PublicService) Timestamp() (int64, error) {
Expand Down Expand Up @@ -71,40 +70,9 @@ func (s *PublicService) Tickers() (TickerMap, error) {
}

func (s *PublicService) Ticker(market string) (*Ticker, error) {
var endPoint = "v2/tickers/" + market
req, err := s.client.NewRequest(context.Background(), "GET", endPoint, url.Values{}, nil)
if err != nil {
return nil, err
}

response, err := s.client.SendRequest(req)
if err != nil {
return nil, err
}

v, err := fastjson.ParseBytes(response.Body)
if err != nil {
return nil, err
}

var ticker = mustParseTicker(v)
return &ticker, nil
}

func mustParseTicker(v *fastjson.Value) Ticker {
var at = v.GetInt64("at")
return Ticker{
Time: time.Unix(at, 0),
At: at,
Buy: string(v.GetStringBytes("buy")),
Sell: string(v.GetStringBytes("sell")),
Volume: string(v.GetStringBytes("vol")),
VolumeInBTC: string(v.GetStringBytes("vol_in_btc")),
Last: string(v.GetStringBytes("last")),
Open: string(v.GetStringBytes("open")),
High: string(v.GetStringBytes("high")),
Low: string(v.GetStringBytes("low")),
}
req := s.client.NewGetTickerRequest()
req.Market(market)
return req.Do(context.Background())
}

type Interval int64
Expand Down
9 changes: 9 additions & 0 deletions pkg/exchange/max/maxapi/reward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ func TestPublicService(t *testing.T) {
assert.NotEmpty(t, tickers)
assert.NotEmpty(t, tickers["btcusdt"])
})

t.Run("v2/ticker/:market", func(t *testing.T) {
req := client.NewGetTickerRequest()
req.Market("btcusdt")
ticker, err := req.Do(ctx)
assert.NoError(t, err)
assert.NotNil(t, ticker)
assert.NotEmpty(t, ticker.Sell)
})
}

func TestRewardService_GetRewardsRequest(t *testing.T) {
Expand Down

0 comments on commit f7d3fca

Please sign in to comment.