-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba25316
commit 86b254a
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package sources | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/NibiruChain/nibiru/x/common/set" | ||
"github.com/NibiruChain/pricefeeder/types" | ||
"io" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
const ( | ||
Bybit = "bybit" | ||
) | ||
|
||
var _ types.FetchPricesFunc = BybitPriceUpdate | ||
|
||
type BybitTicker struct { | ||
Symbol string `json:"symbol"` | ||
Price string `json:"lastPrice"` | ||
} | ||
|
||
type Response struct { | ||
Data struct { | ||
List []BybitTicker `json:"list"` | ||
} `json:"result"` | ||
} | ||
|
||
// BybitPriceUpdate returns the prices for given symbols or an error. | ||
// Uses BYBIT API at https://bybit-exchange.github.io/docs/v5/market/tickers. | ||
func BybitPriceUpdate(symbols set.Set[types.Symbol]) (rawPrices map[types.Symbol]float64, err error) { | ||
|
||
url := "https://api.bybit.com/v5/market/tickers?category=spot" | ||
|
||
resp, err := http.Get(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
b, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var response Response | ||
err = json.Unmarshal(b, &response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rawPrices = make(map[types.Symbol]float64) | ||
|
||
for _, ticker := range response.Data.List { | ||
|
||
symbol := types.Symbol(ticker.Symbol) | ||
price, err := strconv.ParseFloat(ticker.Price, 64) | ||
if err != nil { | ||
return rawPrices, err | ||
} | ||
|
||
if _, ok := symbols[symbol]; ok { | ||
rawPrices[symbol] = price | ||
} | ||
|
||
} | ||
return rawPrices, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package sources | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/NibiruChain/nibiru/x/common/set" | ||
"github.com/NibiruChain/pricefeeder/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBybitPriceUpdate(t *testing.T) { | ||
|
||
t.Run("success", func(t *testing.T) { | ||
rawPrices, err := BybitPriceUpdate(set.New[types.Symbol]("BTCUSDT", "ETHUSDT")) | ||
require.NoError(t, err) | ||
require.Equal(t, 2, len(rawPrices)) | ||
require.NotZero(t, rawPrices["BTCUSDT"]) | ||
require.NotZero(t, rawPrices["ETHUSDT"]) | ||
fmt.Println(rawPrices) | ||
fmt.Printf("BTC Price: %f\n", rawPrices["BTCUSDT"]) | ||
fmt.Printf("ETH Price: %f\n", rawPrices["ETHUSDT"]) | ||
}) | ||
} |