Skip to content

Commit f58c27b

Browse files
authored
Add MarginBorrowRepayService (#662)
1 parent def0432 commit f58c27b

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

v2/client.go

+12
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ var (
131131
// SelfTradePreventionMode define self trade prevention strategy
132132
type SelfTradePreventionMode string
133133

134+
type MarginAccountBorrowRepayType string
135+
134136
// UseTestnet switch all the API endpoints from production to the testnet
135137
var UseTestnet = false
136138

@@ -321,6 +323,9 @@ const (
321323
SelfTradePreventionModeExpireTaker SelfTradePreventionMode = "EXPIRE_TAKER"
322324
SelfTradePreventionModeExpireBoth SelfTradePreventionMode = "EXPIRE_BOTH"
323325
SelfTradePreventionModeExpireMaker SelfTradePreventionMode = "EXPIRE_MAKER"
326+
327+
MarginAccountBorrow MarginAccountBorrowRepayType = "BORROW"
328+
MarginAccountRepay MarginAccountBorrowRepayType = "REPAY"
324329
)
325330

326331
func currentTimestamp() int64 {
@@ -771,15 +776,22 @@ func (c *Client) NewMarginTransferService() *MarginTransferService {
771776
}
772777

773778
// NewMarginLoanService init margin account loan service
779+
// Deprecated: use NewMarginBorrowRepayService instead
774780
func (c *Client) NewMarginLoanService() *MarginLoanService {
775781
return &MarginLoanService{c: c}
776782
}
777783

778784
// NewMarginRepayService init margin account repay service
785+
// Deprecated: use NewMarginBorrowRepayService instead
779786
func (c *Client) NewMarginRepayService() *MarginRepayService {
780787
return &MarginRepayService{c: c}
781788
}
782789

790+
// NewMarginBorrowRepayService init margin account borrow/repay service
791+
func (c *Client) NewMarginBorrowRepayService() *MarginBorrowRepayService {
792+
return &MarginBorrowRepayService{c: c}
793+
}
794+
783795
// NewCreateMarginOrderService init creating margin order service
784796
func (c *Client) NewCreateMarginOrderService() *CreateMarginOrderService {
785797
return &CreateMarginOrderService{c: c}

v2/margin_service.go

+73
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type TransactionResponse struct {
6464
}
6565

6666
// MarginLoanService apply for a loan
67+
// Deprecated: use MarginBorrowRepayService instead
6768
type MarginLoanService struct {
6869
c *Client
6970
asset string
@@ -128,6 +129,7 @@ func (s *MarginLoanService) Do(ctx context.Context, opts ...RequestOption) (res
128129
}
129130

130131
// MarginRepayService repay loan for margin account
132+
// Deprecated: use MarginBorrowRepayService instead
131133
type MarginRepayService struct {
132134
c *Client
133135
asset string
@@ -191,6 +193,77 @@ func (s *MarginRepayService) Do(ctx context.Context, opts ...RequestOption) (res
191193
return res, nil
192194
}
193195

196+
// MarginBorrowRepayService borrow/repay for margin account
197+
type MarginBorrowRepayService struct {
198+
c *Client
199+
asset string // Mandatory:YES
200+
amount string // Mandatory:YES
201+
isIsolated bool // Mandatory:YES, TRUE for Isolated Margin, FALSE for Cross Margin, Default FALSE
202+
symbol string // Mandatory:YES, Only for Isolated margin
203+
_type MarginAccountBorrowRepayType // Mandatory:YES, BORROW or REPAY
204+
}
205+
206+
// Asset set asset being transferred, e.g., BTC
207+
func (s *MarginBorrowRepayService) Asset(asset string) *MarginBorrowRepayService {
208+
s.asset = asset
209+
return s
210+
}
211+
212+
// Amount the amount to be transferred
213+
func (s *MarginBorrowRepayService) Amount(amount string) *MarginBorrowRepayService {
214+
s.amount = amount
215+
return s
216+
}
217+
218+
// IsIsolated is for isolated margin or not, "TRUE", "FALSE",default "FALSE"
219+
func (s *MarginBorrowRepayService) IsIsolated(isIsolated bool) *MarginBorrowRepayService {
220+
s.isIsolated = isIsolated
221+
return s
222+
}
223+
224+
// Symbol set isolated symbol
225+
func (s *MarginBorrowRepayService) Symbol(symbol string) *MarginBorrowRepayService {
226+
s.symbol = symbol
227+
return s
228+
}
229+
230+
func (s *MarginBorrowRepayService) Type(marginBorrowRepayType MarginAccountBorrowRepayType) *MarginBorrowRepayService {
231+
s._type = marginBorrowRepayType
232+
return s
233+
}
234+
235+
// Do send request
236+
func (s *MarginBorrowRepayService) Do(ctx context.Context, opts ...RequestOption) (res *TransactionResponse, err error) {
237+
r := &request{
238+
method: http.MethodPost,
239+
endpoint: "/sapi/v1/margin/borrow-repay",
240+
secType: secTypeSigned,
241+
}
242+
m := params{
243+
"asset": s.asset,
244+
"amount": s.amount,
245+
"type": string(s._type),
246+
}
247+
r.setFormParams(m)
248+
if s.isIsolated {
249+
r.setParam("isIsolated", "TRUE")
250+
}
251+
if s.symbol != "" {
252+
r.setParam("symbol", s.symbol)
253+
}
254+
255+
res = new(TransactionResponse)
256+
data, err := s.c.callAPI(ctx, r, opts...)
257+
if err != nil {
258+
return nil, err
259+
}
260+
err = json.Unmarshal(data, res)
261+
if err != nil {
262+
return nil, err
263+
}
264+
return res, nil
265+
}
266+
194267
// ListMarginLoansService list loan record
195268
type ListMarginLoansService struct {
196269
c *Client

v2/margin_service_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,64 @@ func (s *marginTestSuite) TestRepay() {
9292
s.assertTransactionResponseEqual(e, res)
9393
}
9494

95+
func (s *marginTestSuite) TestBorrowRepayBorrow() {
96+
data := []byte(`{
97+
"tranId": 100000001
98+
}`)
99+
s.mockDo(data, nil)
100+
defer s.assertDo()
101+
asset := "BTC"
102+
amount := "1.000"
103+
_type := MarginAccountBorrow
104+
s.assertReq(func(r *request) {
105+
e := newSignedRequest().setFormParams(params{
106+
"asset": asset,
107+
"amount": amount,
108+
"type": string(_type),
109+
})
110+
s.assertRequestEqual(e, r)
111+
})
112+
res, err := s.client.NewMarginBorrowRepayService().
113+
Asset(asset).
114+
Amount(amount).
115+
Type(_type).
116+
Do(newContext())
117+
s.r().NoError(err)
118+
e := &TransactionResponse{
119+
TranID: 100000001,
120+
}
121+
s.assertTransactionResponseEqual(e, res)
122+
}
123+
124+
func (s *marginTestSuite) TestBorrowRepayRepay() {
125+
data := []byte(`{
126+
"tranId": 100000001
127+
}`)
128+
s.mockDo(data, nil)
129+
defer s.assertDo()
130+
asset := "BTC"
131+
amount := "1.000"
132+
_type := MarginAccountRepay
133+
s.assertReq(func(r *request) {
134+
e := newSignedRequest().setFormParams(params{
135+
"asset": asset,
136+
"amount": amount,
137+
"type": string(_type),
138+
})
139+
s.assertRequestEqual(e, r)
140+
})
141+
res, err := s.client.NewMarginBorrowRepayService().
142+
Asset(asset).
143+
Amount(amount).
144+
Type(_type).
145+
Do(newContext())
146+
s.r().NoError(err)
147+
e := &TransactionResponse{
148+
TranID: 100000001,
149+
}
150+
s.assertTransactionResponseEqual(e, res)
151+
}
152+
95153
func (s *marginTestSuite) TestListMarginLoans() {
96154
data := []byte(`{
97155
"rows": [

0 commit comments

Comments
 (0)