-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v5): Add
/v5/account/wallet-balance
and /v5/user/query-api
e…
…ndpoints (#80) * feat(v5): add `/v5/account/wallet-balance` endpoint * chore: comment * chore: move enums to enum file * chore: fix comment * feat(v5): add intf for User category * docs(v5): Add `/v5/user/query-api` * feat(v5): add support for `/v5/user/query-api` * fix: wrong struct * test: add user servce tests * test: add integration
- Loading branch information
Showing
11 changed files
with
365 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,8 @@ | ||
BYBIT_TEST_UPDATED=false | ||
|
||
test: | ||
BYBIT_TEST_UPDATED=${BYBIT_TEST_UPDATED} BYBIT_TEST_KEY=${BYBIT_TEST_KEY} BYBIT_TEST_SECRET=${BYBIT_TEST_SECRET} go test github.com/hirokisan/bybit/v2/integrationtest/v5/account -v -tags=integrationtestv5account | ||
|
||
test-spec: | ||
BYBIT_TEST_UPDATED=${BYBIT_TEST_UPDATED} BYBIT_TEST_KEY=${BYBIT_TEST_KEY} BYBIT_TEST_SECRET=${BYBIT_TEST_SECRET} go test github.com/hirokisan/bybit/v2/integrationtest/v5/account -v -tags=integrationtestv5account -run ${BYBIT_TEST_METHOD} | ||
|
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,23 @@ | ||
//go:build integrationtestv5account | ||
|
||
package integrationtestv5account | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hirokisan/bybit/v2" | ||
"github.com/hirokisan/bybit/v2/integrationtest/testhelper" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetWalletBalance(t *testing.T) { | ||
client := bybit.NewTestClient().WithAuthFromEnv() | ||
symbol := bybit.SymbolV5BTCUSDT | ||
res, err := client.V5().Account().GetWalletBalance(bybit.UnifiedAccount, "") | ||
require.NoError(t, err) | ||
{ | ||
goldenFilename := "./testdata/v5-account-get-wallet-balance.json" // TODO | ||
testhelper.Compare(t, goldenFilename, testhelper.ConvertToJSON(res.Result)) | ||
testhelper.UpdateFile(t, goldenFilename, testhelper.ConvertToJSON(res.Result)) | ||
} | ||
} |
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,8 @@ | ||
BYBIT_TEST_UPDATED=false | ||
|
||
test: | ||
BYBIT_TEST_UPDATED=${BYBIT_TEST_UPDATED} BYBIT_TEST_KEY=${BYBIT_TEST_KEY} BYBIT_TEST_SECRET=${BYBIT_TEST_SECRET} go test github.com/hirokisan/bybit/v2/integrationtest/v5/user -v -tags=integrationtestv5user | ||
|
||
test-spec: | ||
BYBIT_TEST_UPDATED=${BYBIT_TEST_UPDATED} BYBIT_TEST_KEY=${BYBIT_TEST_KEY} BYBIT_TEST_SECRET=${BYBIT_TEST_SECRET} go test github.com/hirokisan/bybit/v2/integrationtest/v5/user -v -tags=integrationtestv5user -run ${BYBIT_TEST_METHOD} | ||
|
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,22 @@ | ||
//go:build integrationtestv5user | ||
|
||
package integrationtestv5user | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hirokisan/bybit/v2" | ||
"github.com/hirokisan/bybit/v2/integrationtest/testhelper" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetPositionInfo(t *testing.T) { | ||
client := bybit.NewTestClient().WithAuthFromEnv() | ||
res, err := client.V5().User().GetAPIKeyInfo() | ||
require.NoError(t, err) | ||
{ | ||
goldenFilename := "./testdata/v5-user-get-api-key-info.json" // TODO | ||
testhelper.Compare(t, goldenFilename, testhelper.ConvertToJSON(res.Result)) | ||
testhelper.UpdateFile(t, goldenFilename, testhelper.ConvertToJSON(res.Result)) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,10 +1,83 @@ | ||
package bybit | ||
|
||
import ( | ||
"net/url" | ||
) | ||
|
||
// V5AccountServiceI : | ||
type V5AccountServiceI interface { | ||
GetWalletBalance(AccountType, string) (*V5WalletBalanceResponse, error) | ||
} | ||
|
||
// V5AccountService : | ||
type V5AccountService struct { | ||
client *Client | ||
} | ||
|
||
// V5WalletBalanceResponse : | ||
type V5WalletBalanceResponse struct { | ||
CommonV5Response `json:",inline"` | ||
Result V5WalletBalanceResult `json:"result"` | ||
} | ||
|
||
// V5WalletBalanceResult : | ||
type V5WalletBalanceResult struct { | ||
List []V5WalletBalanceList `json:"list"` | ||
} | ||
|
||
// VV5WalletBalanceCoin : | ||
type V5WalletBalanceCoin struct { | ||
AvailableToBorrow string `json:"availableToBorrow"` | ||
AccruedInterest string `json:"accruedInterest"` | ||
AvailableToWithdraw string `json:"availableToWithdraw"` | ||
TotalOrderIM string `json:"totalOrderIM"` | ||
Equity string `json:"equity"` | ||
TotalPositionMM string `json:"totalPositionMM"` | ||
UsdValue string `json:"usdValue"` | ||
UnrealisedPnl string `json:"unrealisedPnl"` | ||
BorrowAmount string `json:"borrowAmount"` | ||
TotalPositionIM string `json:"totalPositionIM"` | ||
WalletBalance string `json:"walletBalance"` | ||
CumRealisedPnl string `json:"cumRealisedPnl"` | ||
Coin string `json:"coin"` | ||
} | ||
|
||
// V5WalletBalanceList : | ||
type V5WalletBalanceList struct { | ||
TotalEquity string `json:"totalEquity"` | ||
AccountIMRate string `json:"accountIMRate"` | ||
TotalMarginBalance string `json:"totalMarginBalance"` | ||
TotalInitialMargin string `json:"totalInitialMargin"` | ||
AccountType string `json:"accountType"` | ||
TotalAvailableBalance string `json:"totalAvailableBalance"` | ||
AccountMMRate string `json:"accountMMRate"` | ||
TotalPerpUPL string `json:"totalPerpUPL"` | ||
TotalWalletBalance string `json:"totalWalletBalance"` | ||
TotalMaintenanceMargin string `json:"totalMaintenanceMargin"` | ||
Coin []V5WalletBalanceCoin `json:"coin"` | ||
} | ||
|
||
// GetWalletBalance : | ||
// | ||
// at: UNIFIED or CONTRACT | ||
// | ||
// coin: | ||
// If not passed, it returns non-zero asset info | ||
// You can pass multiple coins to query, separated by comma. "USDT,USDC". | ||
func (s *V5AccountService) GetWalletBalance(at AccountType, coin string) (*V5WalletBalanceResponse, error) { | ||
var ( | ||
res V5WalletBalanceResponse | ||
query = make(url.Values) | ||
) | ||
|
||
query.Add("accountType", string(at)) | ||
if coin != "" { | ||
query.Add("coin", coin) | ||
} | ||
|
||
if err := s.client.getV5Privately("/v5/account/wallet-balance", query, &res); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &res, 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,71 @@ | ||
package bybit | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/hirokisan/bybit/v2/testhelper" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestV5Account_GetWalletBalance(t *testing.T) { | ||
t.Run("success", func(t *testing.T) { | ||
path := "/v5/account/wallet-balance" | ||
method := http.MethodGet | ||
status := http.StatusOK | ||
respBody := map[string]interface{}{ | ||
"result": map[string]interface{}{ | ||
"list": []map[string]interface{}{ | ||
{ | ||
"totalEquity": "18070.32797922", | ||
"accountIMRate": "0.0101", | ||
"totalMarginBalance": "18070.32797922", | ||
"totalInitialMargin": "182.60183684", | ||
"accountType": "UNIFIED", | ||
"totalAvailableBalance": "17887.72614237", | ||
"accountMMRate": "0", | ||
"totalPerpUPL": "-0.11001349", | ||
"totalWalletBalance": "18070.43799271", | ||
"totalMaintenanceMargin": "0.38106773", | ||
"coin": []map[string]interface{}{ | ||
{ | ||
"availableToBorrow": "2.5", | ||
"accruedInterest": "0", | ||
"availableToWithdraw": "0.805994", | ||
"totalOrderIM": "0", | ||
"equity": "0.805994", | ||
"totalPositionMM": "0", | ||
"usdValue": "12920.95352538", | ||
"unrealisedPnl": "0", | ||
"borrowAmount": "0", | ||
"totalPositionIM": "0", | ||
"walletBalance": "0.805994", | ||
"cumRealisedPnl": "0", | ||
"coin": "BTC", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
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). | ||
WithAuth("test", "test") | ||
|
||
resp, err := client.V5().Account().GetWalletBalance(UnifiedAccount, "") | ||
require.NoError(t, err) | ||
|
||
require.NotNil(t, resp) | ||
testhelper.Compare(t, respBody["result"], resp.Result) | ||
}) | ||
} |
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
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,67 @@ | ||
package bybit | ||
|
||
import ( | ||
"net/url" | ||
"time" | ||
) | ||
|
||
// V5UserServiceI : | ||
type V5UserServiceI interface { | ||
GetAPIKey() (*V5APIKeyResponse, error) | ||
} | ||
|
||
// V5UserService : | ||
type V5UserService struct { | ||
client *Client | ||
} | ||
|
||
// V5APIKeyResponse : | ||
type V5APIKeyResponse struct { | ||
CommonV5Response `json:",inline"` | ||
Result V5ApiKeyResult `json:"result"` | ||
} | ||
|
||
// V5ApiKeyResult : | ||
type V5ApiKeyResult struct { | ||
ID string `json:"id"` | ||
Note string `json:"note"` | ||
APIKey string `json:"apiKey"` | ||
ReadOnly int `json:"readOnly"` | ||
Secret string `json:"secret"` | ||
Permissions struct { | ||
ContractTrade []interface{} `json:"ContractTrade"` | ||
Spot []interface{} `json:"Spot"` | ||
Wallet []string `json:"Wallet"` | ||
Options []interface{} `json:"Options"` | ||
Derivatives []interface{} `json:"Derivatives"` | ||
CopyTrading []interface{} `json:"CopyTrading"` | ||
BlockTrade []interface{} `json:"BlockTrade"` | ||
Exchange []interface{} `json:"Exchange"` | ||
Nft []interface{} `json:"NFT"` | ||
} `json:"permissions"` | ||
Ips []string `json:"ips"` | ||
Type int `json:"type"` | ||
DeadlineDay int `json:"deadlineDay"` | ||
ExpiredAt time.Time `json:"expiredAt"` | ||
CreatedAt time.Time `json:"createdAt"` | ||
Unified int `json:"unified"` | ||
Uta int `json:"uta"` | ||
UserID int `json:"userID"` | ||
InviterID int `json:"inviterID"` | ||
VipLevel string `json:"vipLevel"` | ||
MktMakerLevel string `json:"mktMakerLevel"` | ||
AffiliateID int `json:"affiliateID"` | ||
} | ||
|
||
// GetAPIKey : | ||
func (s *V5UserService) GetAPIKey() (*V5APIKeyResponse, error) { | ||
var ( | ||
res V5APIKeyResponse | ||
) | ||
|
||
if err := s.client.getV5Privately("/v5/user/query-api", url.Values{}, &res); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &res, nil | ||
} |
Oops, something went wrong.