From 1042ce83aa3b32c0e927350d31e724c88ddb645a Mon Sep 17 00:00:00 2001 From: Dean Eigenmann <7621705+decanus@users.noreply.github.com> Date: Sat, 24 Feb 2024 05:32:50 +0100 Subject: [PATCH] feature/v5-asset-withdraw (#165) * added asset withdraw endpoint * fixes --- v5_asset_service.go | 38 ++++++++++++++++++++ v5_asset_service_test.go | 78 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/v5_asset_service.go b/v5_asset_service.go index 522e844..0578a9d 100644 --- a/v5_asset_service.go +++ b/v5_asset_service.go @@ -20,6 +20,7 @@ type V5AssetServiceI interface { GetWithdrawalRecords(V5GetWithdrawalRecordsParam) (*V5GetWithdrawalRecordsResponse, error) GetCoinInfo(V5GetCoinInfoParam) (*V5GetCoinInfoResponse, error) GetAllCoinsBalance(V5GetAllCoinsBalanceParam) (*V5GetAllCoinsBalanceResponse, error) + Withdraw(param V5WithdrawParam) (*V5WithdrawResponse, error) } // V5AssetService : @@ -476,3 +477,40 @@ func (s *V5AssetService) GetAllCoinsBalance(param V5GetAllCoinsBalanceParam) (*V return &res, nil } + +type V5WithdrawResponse struct { + CommonV5Response `json:",inline"` + Result V5WithdrawResult `json:"result"` +} + +type V5WithdrawResult struct { + ID string `json:"id"` +} + +type V5WithdrawParam struct { + Coin Coin `json:"coin"` + Chain *string `json:"chain,omitempty"` + Address string `json:"address"` + Tag *string `json:"tag,omitempty"` + Amount string `json:"amount"` + Timestamp int64 `json:"timestamp"` + ForceChain *bool `json:"forceChain,omitempty"` + AccountType *AccountType `json:"accountType,omitempty"` + FeeType *int `json:"feeType,omitempty"` + RequestID *string `json:"requestId,omitempty"` +} + +func (s *V5AssetService) Withdraw(param V5WithdrawParam) (*V5WithdrawResponse, error) { + var res V5WithdrawResponse + + body, err := json.Marshal(param) + if err != nil { + return &res, fmt.Errorf("json marshal: %w", err) + } + + if err := s.client.postV5JSON("/v5/asset/withdraw/create", body, &res); err != nil { + return &res, err + } + + return &res, nil +} diff --git a/v5_asset_service_test.go b/v5_asset_service_test.go index 101b45c..e7c0da4 100644 --- a/v5_asset_service_test.go +++ b/v5_asset_service_test.go @@ -597,3 +597,81 @@ func TestGetAllCoinsBalance(t *testing.T) { assert.Error(t, err) }) } + +func TestV5Asset_Withdraw(t *testing.T) { + t.Run("success", func(t *testing.T) { + param := V5WithdrawParam{ + Coin: CoinETH, + Chain: "ETH", + Address: "0x99ced129603abc771c0dabe935c326ff6c86645d", + Tag: nil, + Amount: "24", + Timestamp: 1672196561407, + ForceChain: true, + AccountType: AccountTypeFunding, + FeeType: 0, + } + + path := "/v5/asset/withdraw/create" + method := http.MethodPost + status := http.StatusOK + respBody := map[string]interface{}{ + "result": map[string]interface{}{ + "id": "42c0cfb0-6bca-c242-bc76-4e6df6cbcb16", + }, + } + 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().Withdraw(param) + require.NoError(t, err) + + require.NotNil(t, resp) + fmt.Println(resp.Result, respBody["result"]) + testhelper.Compare(t, respBody["result"], resp.Result) + }) + t.Run("authentication required", func(t *testing.T) { + param := V5WithdrawParam{ + Coin: CoinETH, + Chain: "ETH", + Address: "0x99ced129603abc771c0dabe935c326ff6c86645d", + Tag: nil, + Amount: "24", + Timestamp: 1672196561407, + ForceChain: true, + AccountType: AccountTypeFunding, + FeeType: 0, + } + + path := "/v5/asset/withdraw/create" + method := http.MethodPost + status := http.StatusOK + respBody := map[string]interface{}{ + "result": map[string]interface{}{ + "transferId": "42c0cfb0-6bca-c242-bc76-4e6df6cbcb16", + }, + } + 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().Withdraw(param) + assert.Error(t, err) + }) +}