Skip to content

Commit

Permalink
feat: v5 get insurance (#125)
Browse files Browse the repository at this point in the history
* feat: implement

* test: integration

* test: unit

* docs: update
  • Loading branch information
hirokisan committed Apr 26, 2023
1 parent 3afc0c3 commit 5391e16
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ The following API endpoints have been implemented
- [`/v5/market/recent-trade` Get Public Trading History](https://bybit-exchange.github.io/docs/v5/market/recent-trade)
- [`/v5/market/open-interest` Get Open Interest](https://bybit-exchange.github.io/docs/v5/market/open-interest)
- [`/v5/market/historical-volatility` Get Historical Volatility](https://bybit-exchange.github.io/docs/v5/market/iv)
- [`/v5/market/insurance` Get Insurance](https://bybit-exchange.github.io/docs/v5/market/insurance)

#### Position

Expand Down
11 changes: 11 additions & 0 deletions integrationtest/v5/market/market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,14 @@ func TestGetHistoricalVolatility(t *testing.T) {
testhelper.UpdateFile(t, goldenFilename, testhelper.ConvertToJSON(res.Result))
}
}

func TestGetInsurance(t *testing.T) {
client := bybit.NewTestClient()
res, err := client.V5().Market().GetInsurance(bybit.V5GetInsuranceParam{})
require.NoError(t, err)
{
goldenFilename := "./testdata/v5-market-get-insurance.json"
testhelper.Compare(t, goldenFilename, testhelper.ConvertToJSON(res.Result))
testhelper.UpdateFile(t, goldenFilename, testhelper.ConvertToJSON(res.Result))
}
}
65 changes: 65 additions & 0 deletions integrationtest/v5/market/testdata/v5-market-get-insurance.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"updatedTime": "1682467200000",
"list": [
{
"coin": "USDT",
"balance": "472565175.36148953",
"value": "472629325.5808998"
},
{
"coin": "USDC",
"balance": "996049.97239122",
"value": "996044.9942383142"
},
{
"coin": "EOS",
"balance": "767102.58626542",
"value": "833072.7720000001"
},
{
"coin": "BTC",
"balance": "47533.89021999",
"value": "1418307241.21"
},
{
"coin": "ADA",
"balance": "895412.56208743",
"value": "372849.5568"
},
{
"coin": "BIT",
"balance": "101256.65607912",
"value": "51903.8256"
},
{
"coin": "SOL",
"balance": "7760.59832427",
"value": "101888.8"
},
{
"coin": "LTC",
"balance": "909951.79684841",
"value": "85062219.48"
},
{
"coin": "DOT",
"balance": "1103850.50091492",
"value": "6832831.5"
},
{
"coin": "ETH",
"balance": "981392.36669191",
"value": "1922772648.16"
},
{
"coin": "XRP",
"balance": "212730.43859841",
"value": "102642.22499999999"
},
{
"coin": "MANA",
"balance": "4996881.117703",
"value": "2875705.0155"
}
]
}
38 changes: 38 additions & 0 deletions v5_market_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type V5MarketServiceI interface {
GetPublicTradingHistory(V5GetPublicTradingHistoryParam) (*V5GetPublicTradingHistoryResponse, error)
GetOpenInterest(V5GetOpenInterestParam) (*V5GetOpenInterestResponse, error)
GetHistoricalVolatility(V5GetHistoricalVolatilityParam) (*V5GetHistoricalVolatilityResponse, error)
GetInsurance(V5GetInsuranceParam) (*V5GetInsuranceResponse, error)
}

// V5MarketService :
Expand Down Expand Up @@ -973,3 +974,40 @@ func (s *V5MarketService) GetHistoricalVolatility(param V5GetHistoricalVolatilit

return &res, nil
}

// V5GetInsuranceParam :
type V5GetInsuranceParam struct {
Coin *Coin `url:"coin"`
}

// V5GetInsuranceResponse :
type V5GetInsuranceResponse struct {
CommonV5Response `json:",inline"`
Result V5GetInsuranceResult `json:"result"`
}

// V5GetInsuranceResult :
type V5GetInsuranceResult struct {
UpdatedTime string `json:"updatedTime"`
List []struct {
Coin Coin `json:"coin"`
Balance string `json:"balance"`
Value string `json:"value"`
} `json:"list"`
}

// GetInsurance :
func (s *V5MarketService) GetInsurance(param V5GetInsuranceParam) (*V5GetInsuranceResponse, error) {
var res V5GetInsuranceResponse

queryString, err := query.Values(param)
if err != nil {
return nil, err
}

if err := s.client.getPublicly("/v5/market/insurance", queryString, &res); err != nil {
return nil, err
}

return &res, nil
}
91 changes: 91 additions & 0 deletions v5_market_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,3 +745,94 @@ func TestV5Market_GetHistoricalVolatility(t *testing.T) {
assert.Equal(t, respBody["category"], string(resp.Result.Category))
assert.Equal(t, respBody["result"].([]map[string]interface{})[0]["period"], resp.Result.List[0].Period)
}

func TestV5Market_GetInsurance(t *testing.T) {
param := V5GetInsuranceParam{}

path := "/v5/market/insurance"
method := http.MethodGet
status := http.StatusOK
respBody := map[string]interface{}{
"result": map[string]interface{}{
"updatedTime": "1682467200000",
"list": []map[string]interface{}{
{
"coin": "USDT",
"balance": "472565175.36148953",
"value": "472629325.5808998",
},
{
"coin": "USDC",
"balance": "996049.97239122",
"value": "996044.9942383142",
},
{
"coin": "EOS",
"balance": "767102.58626542",
"value": "833072.7720000001",
},
{
"coin": "BTC",
"balance": "47533.89021999",
"value": "1418307241.21",
},
{
"coin": "ADA",
"balance": "895412.56208743",
"value": "372849.5568",
},
{
"coin": "BIT",
"balance": "101256.65607912",
"value": "51903.8256",
},
{
"coin": "SOL",
"balance": "7760.59832427",
"value": "101888.8",
},
{
"coin": "LTC",
"balance": "909951.79684841",
"value": "85062219.48",
},
{
"coin": "DOT",
"balance": "1103850.50091492",
"value": "6832831.5",
},
{
"coin": "ETH",
"balance": "981392.36669191",
"value": "1922772648.16",
},
{
"coin": "XRP",
"balance": "212730.43859841",
"value": "102642.22499999999",
},
{
"coin": "MANA",
"balance": "4996881.117703",
"value": "2875705.0155",
},
},
},
}
bytesBody, err := json.Marshal(respBody)
require.NoError(t, err)

server, teardown := testhelper.NewServer(
testhelper.WithHandlerOption(path, method, status, bytesBody),
)
defer teardown()

client := NewTestClient().
WithBaseURL(server.URL)

resp, err := client.V5().Market().GetInsurance(param)
require.NoError(t, err)

require.NotNil(t, resp)
testhelper.Compare(t, respBody["result"], resp.Result)
}

0 comments on commit 5391e16

Please sign in to comment.