Skip to content

Commit

Permalink
Add bingx provider
Browse files Browse the repository at this point in the history
  • Loading branch information
starsquidnodes committed Mar 19, 2024
1 parent 10d2cc0 commit 449aa23
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
provider.ProviderAstroportTerra2: {},
provider.ProviderBinance: {},
provider.ProviderBinanceUS: {},
provider.ProviderBingx: {},
provider.ProviderBitfinex: {},
provider.ProviderBitforex: {},
provider.ProviderBitget: {},
Expand Down
2 changes: 2 additions & 0 deletions oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ func NewProvider(
provider.ProviderBinance,
provider.ProviderBinanceUS:
return provider.NewBinanceProvider(ctx, providerLogger, endpoint, providerPairs...)
case provider.ProviderBingx:
return provider.NewBingxProvider(ctx, providerLogger, endpoint, providerPairs...)
case provider.ProviderBitfinex:
return provider.NewBitfinexProvider(ctx, providerLogger, endpoint, providerPairs...)
case provider.ProviderBitget:
Expand Down
124 changes: 124 additions & 0 deletions oracle/provider/bingx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package provider

import (
"context"
"encoding/json"
"fmt"
"time"

"price-feeder/oracle/types"

"github.com/rs/zerolog"
)

var (
_ Provider = (*BingxProvider)(nil)
bingxDefaultEndpoints = Endpoint{
Name: ProviderBingx,
Urls: []string{"https://open-api.bingx.com"},
PollInterval: 5 * time.Second,
}
)

type (
// BingxProvider defines an oracle provider implemented by the Bingx
// public API.
//
// REF: https://bingx-api.github.io/docs/#/en-us/spot/changelog
BingxProvider struct {
provider
}

BingxTicker struct {
Symbol string `json:"symbol"` // Symbol ex.: BTC-USDT
Price float64 `json:"lastPrice"` // Last price ex.: 0.0025
Volume float64 `json:"volume"` // Total traded base asset volume ex.: 1000
}
)

func NewBingxProvider(
ctx context.Context,
logger zerolog.Logger,
endpoints Endpoint,
pairs ...types.CurrencyPair,
) (*BingxProvider, error) {
provider := &BingxProvider{}
provider.Init(
ctx,
endpoints,
logger,
pairs,
nil,
nil,
)

availablePairs, _ := provider.GetAvailablePairs()
provider.setPairs(pairs, availablePairs, currencyPairToBingxSymbol)

go startPolling(provider, provider.endpoints.PollInterval, logger)
return provider, nil
}

func (p *BingxProvider) getTickers() ([]BingxTicker, error) {
timestamp := time.Now().Unix()
path := fmt.Sprintf("/openApi/spot/v1/ticker/24hr?timestamp=%d", timestamp)

content, err := p.httpGet(path)
if err != nil {
return nil, err
}

var response struct {
Data []BingxTicker `json:"data"`
}
err = json.Unmarshal(content, &response)
if err != nil {
return nil, err
}

return response.Data, nil
}

func (p *BingxProvider) Poll() error {
tickers, err := p.getTickers()
if err != nil {
return err
}

now := time.Now()

p.mtx.Lock()
defer p.mtx.Unlock()
for _, ticker := range tickers {
if !p.isPair(ticker.Symbol) {
continue
}

p.setTickerPrice(
ticker.Symbol,
floatToDec(ticker.Price),
floatToDec(ticker.Volume),
now,
)
}
p.logger.Debug().Msg("updated tickers")
return nil
}

func (p *BingxProvider) GetAvailablePairs() (map[string]struct{}, error) {
tickers, err := p.getTickers()
if err != nil {
return nil, err
}

symbols := map[string]struct{}{}
for _, ticker := range tickers {
symbols[ticker.Symbol] = struct{}{}
}

return symbols, nil
}

func currencyPairToBingxSymbol(pair types.CurrencyPair) string {
return pair.Join("-")
}
3 changes: 3 additions & 0 deletions oracle/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
ProviderAstroportTerra2 Name = "astroport_terra2"
ProviderBinance Name = "binance"
ProviderBinanceUS Name = "binanceus"
ProviderBingx Name = "bingx"
ProviderBitfinex Name = "bitfinex"
ProviderBitforex Name = "bitforex"
ProviderBitget Name = "bitget"
Expand Down Expand Up @@ -396,6 +397,8 @@ func (e *Endpoint) SetDefaults() {
defaults = bitfinexDefaultEndpoints
case ProviderBinanceUS:
defaults = binanceUSDefaultEndpoints
case ProviderBingx:
defaults = bingxDefaultEndpoints
case ProviderBitget:
defaults = bitgetDefaultEndpoints
case ProviderBitmart:
Expand Down

0 comments on commit 449aa23

Please sign in to comment.