Skip to content

Commit

Permalink
feature/v5-asset-withdraw (#165)
Browse files Browse the repository at this point in the history
* added asset withdraw endpoint

* fixes
  • Loading branch information
decanus authored Feb 24, 2024
1 parent c07a9e5 commit 1042ce8
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
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",

Check failure on line 605 in v5_asset_service_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use "ETH" (untyped string constant) as *string value in struct literal

Check failure on line 605 in v5_asset_service_test.go

View workflow job for this annotation

GitHub Actions / test

cannot use "ETH" (untyped string constant) as *string value in struct literal
Address: "0x99ced129603abc771c0dabe935c326ff6c86645d",
Tag: nil,
Amount: "24",
Timestamp: 1672196561407,
ForceChain: true,

Check failure on line 610 in v5_asset_service_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use true (untyped bool constant) as *bool value in struct literal

Check failure on line 610 in v5_asset_service_test.go

View workflow job for this annotation

GitHub Actions / test

cannot use true (untyped bool constant) as *bool value in struct literal
AccountType: AccountTypeFunding,

Check failure on line 611 in v5_asset_service_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use AccountTypeFunding (constant "FUND" of type AccountType) as *AccountType value in struct literal

Check failure on line 611 in v5_asset_service_test.go

View workflow job for this annotation

GitHub Actions / test

cannot use AccountTypeFunding (constant "FUND" of type AccountType) as *AccountType value in struct literal
FeeType: 0,

Check failure on line 612 in v5_asset_service_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use 0 (untyped int constant) as *int value in struct literal

Check failure on line 612 in v5_asset_service_test.go

View workflow job for this annotation

GitHub Actions / test

cannot use 0 (untyped int constant) as *int value in struct literal
}

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",

Check failure on line 645 in v5_asset_service_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use "ETH" (untyped string constant) as *string value in struct literal

Check failure on line 645 in v5_asset_service_test.go

View workflow job for this annotation

GitHub Actions / test

cannot use "ETH" (untyped string constant) as *string value in struct literal
Address: "0x99ced129603abc771c0dabe935c326ff6c86645d",
Tag: nil,
Amount: "24",
Timestamp: 1672196561407,
ForceChain: true,

Check failure on line 650 in v5_asset_service_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use true (untyped bool constant) as *bool value in struct literal

Check failure on line 650 in v5_asset_service_test.go

View workflow job for this annotation

GitHub Actions / test

cannot use true (untyped bool constant) as *bool value in struct literal
AccountType: AccountTypeFunding,

Check failure on line 651 in v5_asset_service_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use AccountTypeFunding (constant "FUND" of type AccountType) as *AccountType value in struct literal

Check failure on line 651 in v5_asset_service_test.go

View workflow job for this annotation

GitHub Actions / test

cannot use AccountTypeFunding (constant "FUND" of type AccountType) as *AccountType value in struct literal
FeeType: 0,

Check failure on line 652 in v5_asset_service_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use 0 (untyped int constant) as *int value in struct literal (typecheck)

Check failure on line 652 in v5_asset_service_test.go

View workflow job for this annotation

GitHub Actions / test

cannot use 0 (untyped int constant) as *int value in struct literal
}

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

0 comments on commit 1042ce8

Please sign in to comment.