From 3afc0c30b9958190f06b36ce980e0cceb108d4f0 Mon Sep 17 00:00:00 2001 From: hirokisan Date: Sun, 23 Apr 2023 11:41:15 +0900 Subject: [PATCH] feat: v5 get withdrawal records (#124) * feat: implement * test: integration * test: unit * docs: update --- README.md | 1 + integrationtest/v5/asset/asset_test.go | 16 +++++ .../v5-asset-get-withdrawal-records.json | 4 ++ v5_asset_service.go | 59 +++++++++++++++++++ v5_asset_service_test.go | 59 +++++++++++++++++++ v5_enum.go | 20 +++++++ 6 files changed, 159 insertions(+) create mode 100644 integrationtest/v5/asset/testdata/v5-asset-get-withdrawal-records.json diff --git a/README.md b/README.md index 111f33a..059e0f6 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,7 @@ The following API endpoints have been implemented - [`/v5/asset/deposit/query-record` Get Deposit Records](https://bybit-exchange.github.io/docs/v5/asset/deposit-record) - [`/v5/asset/deposit/query-sub-member-record` Get Sub Deposit Records](https://bybit-exchange.github.io/docs/v5/asset/sub-deposit-record) - [`/v5/asset/deposit/query-internal-record` Get Internal Deposit Records](https://bybit-exchange.github.io/docs/v5/asset/internal-deposit-record) +- [`/v5/asset/withdraw/query-record` Get Withdrawal Records](https://bybit-exchange.github.io/docs/v5/asset/withdraw-record) #### User diff --git a/integrationtest/v5/asset/asset_test.go b/integrationtest/v5/asset/asset_test.go index 03501e2..492c279 100644 --- a/integrationtest/v5/asset/asset_test.go +++ b/integrationtest/v5/asset/asset_test.go @@ -66,3 +66,19 @@ func TestGetInternalDepositRecords(t *testing.T) { testhelper.UpdateFile(t, goldenFilename, testhelper.ConvertToJSON(res.Result)) } } + +func TestGetWithdrawalRecords(t *testing.T) { + client := bybit.NewTestClient().WithAuthFromEnv() + limit := 1 + typ := bybit.WithdrawTypeAll + res, err := client.V5().Asset().GetWithdrawalRecords(bybit.V5GetWithdrawalRecordsParam{ + Limit: &limit, + WithdrawType: &typ, + }) + require.NoError(t, err) + { + goldenFilename := "./testdata/v5-asset-get-withdrawal-records.json" + testhelper.Compare(t, goldenFilename, testhelper.ConvertToJSON(res.Result)) + testhelper.UpdateFile(t, goldenFilename, testhelper.ConvertToJSON(res.Result)) + } +} diff --git a/integrationtest/v5/asset/testdata/v5-asset-get-withdrawal-records.json b/integrationtest/v5/asset/testdata/v5-asset-get-withdrawal-records.json new file mode 100644 index 0000000..3d993bd --- /dev/null +++ b/integrationtest/v5/asset/testdata/v5-asset-get-withdrawal-records.json @@ -0,0 +1,4 @@ +{ + "rows": [], + "nextPageCursor": "" +} \ No newline at end of file diff --git a/v5_asset_service.go b/v5_asset_service.go index 0f09626..d998dcf 100644 --- a/v5_asset_service.go +++ b/v5_asset_service.go @@ -8,6 +8,7 @@ type V5AssetServiceI interface { GetDepositRecords(V5GetDepositRecordsParam) (*V5GetDepositRecordsResponse, error) GetSubDepositRecords(V5GetSubDepositRecordsParam) (*V5GetSubDepositRecordsResponse, error) GetInternalDepositRecords(V5GetInternalDepositRecordsParam) (*V5GetInternalDepositRecordsResponse, error) + GetWithdrawalRecords(V5GetWithdrawalRecordsParam) (*V5GetWithdrawalRecordsResponse, error) } // V5AssetService : @@ -232,3 +233,61 @@ func (s *V5AssetService) GetInternalDepositRecords(param V5GetInternalDepositRec return &res, nil } + +// V5GetWithdrawalRecordsParam : +type V5GetWithdrawalRecordsParam struct { + WithdrawID *string `url:"withdrawId,omitempty"` + Coin *Coin `url:"coin,omitempty"` + WithdrawType *WithdrawTypeV5 `url:"withdrawType,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: 50 + Cursor *string `url:"cursor,omitempty"` +} + +// V5GetWithdrawalRecordsResponse : +type V5GetWithdrawalRecordsResponse struct { + CommonV5Response `json:",inline"` + Result V5GetWithdrawalRecordsResult `json:"result"` +} + +// V5GetWithdrawalRecordsResult : +type V5GetWithdrawalRecordsResult struct { + Rows V5GetWithdrawalRecordsRows `json:"rows"` + NextPageCursor string `json:"nextPageCursor"` +} + +// V5GetWithdrawalRecordsRows : +type V5GetWithdrawalRecordsRows []V5GetWithdrawalRecordsRow + +// V5GetWithdrawalRecordsRow : +type V5GetWithdrawalRecordsRow struct { + WithdrawId string `json:"withdrawId"` + TxID string `json:"txId"` + WithdrawType WithdrawTypeV5 `json:"withdrawType"` + Coin Coin `json:"coin"` + Chain string `json:"chain"` + Amount string `json:"amount"` + WithdrawFee string `json:"withdrawFee"` + Status WithdrawStatusV5 `json:"status"` + ToAddress string `json:"toAddress"` + Tag string `json:"tag"` + CreatedTime string `json:"createTime"` + UpdatedTime string `json:"updateTime"` +} + +// GetWithdrawalRecords : +func (s *V5AssetService) GetWithdrawalRecords(param V5GetWithdrawalRecordsParam) (*V5GetWithdrawalRecordsResponse, error) { + var res V5GetWithdrawalRecordsResponse + + queryString, err := query.Values(param) + if err != nil { + return nil, err + } + + if err := s.client.getV5Privately("/v5/asset/withdraw/query-record", queryString, &res); err != nil { + return nil, err + } + + return &res, nil +} diff --git a/v5_asset_service_test.go b/v5_asset_service_test.go index 1384cfe..0f7963e 100644 --- a/v5_asset_service_test.go +++ b/v5_asset_service_test.go @@ -295,3 +295,62 @@ func GetInternalDepositRecords(t *testing.T) { assert.Error(t, err) }) } + +func TestGetWithdrawalRecords(t *testing.T) { + t.Run("success", func(t *testing.T) { + param := V5GetWithdrawalRecordsParam{} + + path := "/v5/asset/withdraw/query-record" + method := http.MethodGet + status := http.StatusOK + respBody := map[string]interface{}{ + "result": map[string]interface{}{ + "rows": []map[string]interface{}{}, + "nextPageCursor": "", + }, + } + 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().GetWithdrawalRecords(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 := V5GetWithdrawalRecordsParam{} + + path := "/v5/asset/withdraw/query-record" + method := http.MethodGet + status := http.StatusOK + respBody := map[string]interface{}{ + "result": map[string]interface{}{ + "rows": []map[string]interface{}{}, + "nextPageCursor": "", + }, + } + 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().GetWithdrawalRecords(param) + assert.Error(t, err) + }) +} diff --git a/v5_enum.go b/v5_enum.go index ac3abd2..abfbff9 100644 --- a/v5_enum.go +++ b/v5_enum.go @@ -264,3 +264,23 @@ const ( DepositStatusV5Success = DepositStatusV5(3) DepositStatusV5Failed = DepositStatusV5(4) ) + +type WithdrawTypeV5 int + +const ( + WithdrawTypeOnChain = WithdrawTypeV5(0) + WithdrawTypeOffChain = WithdrawTypeV5(1) + WithdrawTypeAll = WithdrawTypeV5(2) +) + +type WithdrawStatusV5 string + +const ( + WithdrawStatusV5SecurityCheck = WithdrawStatusV5("SecurityCheck") + WithdrawStatusV5Pending = WithdrawStatusV5("Pending") + WithdrawStatusV5Success = WithdrawStatusV5("success") + WithdrawStatusV5CancelByUser = WithdrawStatusV5("CancelByUser") + WithdrawStatusV5Reject = WithdrawStatusV5("Reject") + WithdrawStatusV5Fail = WithdrawStatusV5("Fail") + WithdrawStatusV5BlockchainConfirmed = WithdrawStatusV5("BlockchainConfirmed") +)