Skip to content

Commit

Permalink
feat: v5 get internal transfer records (#117)
Browse files Browse the repository at this point in the history
* feat: implement

* test: integration

* test: unit

* docs: update
  • Loading branch information
hirokisan authored Apr 9, 2023
1 parent a4cf33f commit 1ee5255
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ The following API endpoints have been implemented

- [`/v5/account/wallet-balance` Get Wallet Balance](https://bybit-exchange.github.io/docs/v5/account/wallet-balance)

#### Asset

- [`/v5/asset/transfer/query-inter-transfer-list` Get Internal Transfer Records](https://bybit-exchange.github.io/docs/v5/asset/inter-transfer-list)

#### User

- [`/v5/user/query-api` Get API Key Information](https://bybit-exchange.github.io/docs/v5/user/apikey-info)
Expand Down
8 changes: 8 additions & 0 deletions integrationtest/v5/asset/Makefile
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/asset -v -tags=integrationtestv5asset

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/asset -v -tags=integrationtestv5asset -run ${BYBIT_TEST_METHOD}

25 changes: 25 additions & 0 deletions integrationtest/v5/asset/asset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//go:build integrationtestv5asset

package integrationtestv5asset

import (
"testing"

"github.com/hirokisan/bybit/v2"
"github.com/hirokisan/bybit/v2/integrationtest/testhelper"
"github.com/stretchr/testify/require"
)

func TestGetInternalTransferRecords(t *testing.T) {
client := bybit.NewTestClient().WithAuthFromEnv()
limit := 1
res, err := client.V5().Asset().GetInternalTransferRecords(bybit.V5GetInternalTransferRecordsParam{
Limit: &limit,
})
require.NoError(t, err)
{
goldenFilename := "./testdata/v5-asset-get-internal-transfer-records.json"
testhelper.Compare(t, goldenFilename, testhelper.ConvertToJSON(res.Result))
testhelper.UpdateFile(t, goldenFilename, testhelper.ConvertToJSON(res.Result))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"list": [
{
"transferId": "selfTransfer_5ce5b8d9-8477-4bc6-91a4-9a98dad6dc65",
"coin": "BTC",
"amount": "0.1",
"fromAccountType": "SPOT",
"toAccountType": "CONTRACT",
"timestamp": "1637939106000",
"status": "SUCCESS"
}
],
"nextPageCursor": "eyJtaW5JRCI6MTYyMjgwLCJtYXhJRCI6MTYyMjgwfQ=="
}
56 changes: 56 additions & 0 deletions v5_asset_service.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,66 @@
package bybit

import "github.com/google/go-querystring/query"

// V5AssetServiceI :
type V5AssetServiceI interface {
GetInternalTransferRecords(V5GetInternalTransferRecordsParam) (*V5GetInternalTransferRecordsResponse, error)
}

// V5AssetService :
type V5AssetService struct {
client *Client
}

// V5GetInternalTransferRecordsParam :
type V5GetInternalTransferRecordsParam struct {
TransferID *string `url:"transferId,omitempty"`
Coin *Coin `url:"coin,omitempty"`
Status *TransferStatusV5 `url:"status,omitempty"`
StartTime *int64 `url:"startTime,omitempty"` // The start timestamp (ms)
EndTime *int64 `url:"endTime,omitempty"` // The start timestamp (ms)
Limit *int `url:"limit,omitempty"` // Limit for data size per page. [1, 50]. Default: 20
Cursor *string `url:"cursor,omitempty"`
}

// V5GetInternalTransferRecordsResponse :
type V5GetInternalTransferRecordsResponse struct {
CommonV5Response `json:",inline"`
Result V5GetInternalTransferRecordsResult `json:"result"`
}

// V5GetInternalTransferRecordsResult :
type V5GetInternalTransferRecordsResult struct {
List V5GetInternalTransferRecordsList `json:"list"`
NextPageCursor string `json:"nextPageCursor"`
}

// V5GetInternalTransferRecordsList :
type V5GetInternalTransferRecordsList []V5GetInternalTransferRecordsItem

// V5GetInternalTransferRecordsItem :
type V5GetInternalTransferRecordsItem struct {
TransferID string `json:"transferId"`
Coin Coin `json:"coin"`
Amount string `json:"amount"`
FromAccountType AccountTypeV5 `json:"fromAccountType"`
ToAccountType AccountTypeV5 `json:"toAccountType"`
Timestamp string `json:"timestamp"`
Status TransferStatusV5 `json:"status"`
}

// GetInternalTransferRecords :
func (s *V5AssetService) GetInternalTransferRecords(param V5GetInternalTransferRecordsParam) (*V5GetInternalTransferRecordsResponse, error) {
var res V5GetInternalTransferRecordsResponse

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

if err := s.client.getV5Privately("/v5/asset/transfer/query-inter-transfer-list", queryString, &res); err != nil {
return nil, err
}

return &res, nil
}
90 changes: 90 additions & 0 deletions v5_asset_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package bybit

import (
"encoding/json"
"net/http"
"testing"

"github.com/hirokisan/bybit/v2/testhelper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestV5Asset_GetInternalTransferRecords(t *testing.T) {
t.Run("success", func(t *testing.T) {
param := V5GetInternalTransferRecordsParam{}

path := "/v5/asset/transfer/query-inter-transfer-list"
method := http.MethodGet
status := http.StatusOK
respBody := map[string]interface{}{
"result": map[string]interface{}{
"list": []map[string]interface{}{
{
"transferId": "selfTransfer_5ce5b8d9-8477-4bc6-91a4-9a98dad6dc65",
"coin": "BTC",
"amount": "0.1",
"fromAccountType": "SPOT",
"toAccountType": "CONTRACT",
"timestamp": "1637939106000",
"status": "SUCCESS",
},
},
"nextPageCursor": "eyJtaW5JRCI6MTYyMjgwLCJtYXhJRCI6MTYyMjgwfQ==",
},
}
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().Asset().GetInternalTransferRecords(param)
require.NoError(t, err)

require.NotNil(t, resp)
testhelper.Compare(t, respBody["result"], resp.Result)
})
t.Run("authentication required", func(t *testing.T) {
param := V5GetInternalTransferRecordsParam{}

path := "/v5/asset/transfer/query-inter-transfer-list"
method := http.MethodGet
status := http.StatusOK
respBody := map[string]interface{}{
"result": map[string]interface{}{
"list": []map[string]interface{}{
{
"transferId": "selfTransfer_5ce5b8d9-8477-4bc6-91a4-9a98dad6dc65",
"coin": "BTC",
"amount": "0.1",
"fromAccountType": "SPOT",
"toAccountType": "CONTRACT",
"timestamp": "1637939106000",
"status": "SUCCESS",
},
},
"nextPageCursor": "eyJtaW5JRCI6MTYyMjgwLCJtYXhJRCI6MTYyMjgwfQ==",
},
}
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)

_, err = client.V5().Asset().GetInternalTransferRecords(param)
assert.Error(t, err)
})
}
30 changes: 30 additions & 0 deletions v5_enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,33 @@ const (
// ExecTypeV5Settle :
ExecTypeV5Settle = ExecTypeV5("Settle")
)

// TransferStatusV5 :
type TransferStatusV5 string

const (
// TransferStatusV5SUCCESS :
TransferStatusV5SUCCESS = TransferStatusV5("SUCCESS")
// TransferStatusV5PENDING :
TransferStatusV5PENDING = TransferStatusV5("PENDING")
// TransferStatusV5FAILED :
TransferStatusV5FAILED = TransferStatusV5("FAILED")
)

// AccountTypeV5 :
type AccountTypeV5 string

const (
// AccountTypeV5CONTRACT :
AccountTypeV5CONTRACT = AccountTypeV5("CONTRACT")
// AccountTypeV5SPOT :
AccountTypeV5SPOT = AccountTypeV5("SPOT")
// AccountTypeV5INVESTMENT :
AccountTypeV5INVESTMENT = AccountTypeV5("INVESTMENT")
// AccountTypeV5OPTION :
AccountTypeV5OPTION = AccountTypeV5("OPTION")
// AccountTypeV5UNIFIED :
AccountTypeV5UNIFIED = AccountTypeV5("UNIFIED")
// AccountTypeV5FUND :
AccountTypeV5FUND = AccountTypeV5("FUND")
)

0 comments on commit 1ee5255

Please sign in to comment.