Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/v5-asset-withdraw #165

Merged
merged 2 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions v5_asset_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down Expand Up @@ -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
}
78 changes: 78 additions & 0 deletions v5_asset_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Comment on lines +603 to +613
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes must be applied here as well. I will handle this one.


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)
})
}