Skip to content

Commit 49ce682

Browse files
authored
loan api for portfolio margin (#30)
1 parent 02a5513 commit 49ce682

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

v2/portfolio/account_service.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,132 @@ type LeverageBrackets struct {
165165
MaintenanceMarginRatio float64 `json:"maintMarginRatio"`
166166
} `json:"brackets"`
167167
}
168+
169+
type GetMarginMaxBorrowableService struct {
170+
c *Client
171+
asset string
172+
}
173+
174+
func (s *GetMarginMaxBorrowableService) Asset(asset string) *GetMarginMaxBorrowableService {
175+
s.asset = asset
176+
return s
177+
}
178+
179+
type GetMarginMaxBorrowableResponse struct {
180+
Amount float64 `json:"amount"`
181+
BorrowLimit float64 `json:"borrowLimit"`
182+
}
183+
184+
// Weight: 100
185+
func (s *GetMarginMaxBorrowableService) Do(
186+
ctx context.Context,
187+
opts ...RequestOption,
188+
) (*GetMarginMaxBorrowableResponse, error) {
189+
r := &request{
190+
method: http.MethodGet,
191+
endpoint: "/papi/v1/margin/maxBorrowable",
192+
secType: secTypeSigned,
193+
}
194+
r.setParam("asset", s.asset)
195+
data, err := s.c.callAPI(ctx, r, opts...)
196+
if err != nil {
197+
return nil, err
198+
}
199+
200+
var res GetMarginMaxBorrowableResponse
201+
if err := json.Unmarshal(data, &res); err != nil {
202+
return nil, err
203+
}
204+
205+
return &res, nil
206+
}
207+
208+
type MarginBorrowService struct {
209+
c *Client
210+
asset string
211+
amount float64
212+
}
213+
214+
func (s *MarginBorrowService) Asset(asset string) *MarginBorrowService {
215+
s.asset = asset
216+
return s
217+
}
218+
219+
func (s *MarginBorrowService) Amount(amount float64) *MarginBorrowService {
220+
s.amount = amount
221+
return s
222+
}
223+
224+
type MarginBorrowResponse struct {
225+
ID int64 `json:"tranId"`
226+
}
227+
228+
// Weight: 100
229+
func (s *MarginBorrowService) Do(
230+
ctx context.Context,
231+
opts ...RequestOption,
232+
) (*MarginBorrowResponse, error) {
233+
r := &request{
234+
method: http.MethodPost,
235+
endpoint: "/papi/v1/marginLoan",
236+
secType: secTypeSigned,
237+
}
238+
r.setParam("asset", s.asset)
239+
r.setParam("amount", s.amount)
240+
data, err := s.c.callAPI(ctx, r, opts...)
241+
if err != nil {
242+
return nil, err
243+
}
244+
245+
var res MarginBorrowResponse
246+
if err := json.Unmarshal(data, &res); err != nil {
247+
return nil, err
248+
}
249+
250+
return &res, nil
251+
}
252+
253+
type MarginRepayService struct {
254+
c *Client
255+
asset string
256+
amount float64
257+
}
258+
259+
func (s *MarginRepayService) Asset(asset string) *MarginRepayService {
260+
s.asset = asset
261+
return s
262+
}
263+
264+
func (s *MarginRepayService) Amount(amount float64) *MarginRepayService {
265+
s.amount = amount
266+
return s
267+
}
268+
269+
type MarginRepayResponse struct {
270+
ID int64 `json:"tranId"`
271+
}
272+
273+
// Weight: 100
274+
func (s *MarginRepayService) Do(
275+
ctx context.Context,
276+
opts ...RequestOption,
277+
) (*MarginRepayResponse, error) {
278+
r := &request{
279+
method: http.MethodPost,
280+
endpoint: "/papi/v1/repayLoan",
281+
secType: secTypeSigned,
282+
}
283+
r.setParam("asset", s.asset)
284+
r.setParam("amount", s.amount)
285+
data, err := s.c.callAPI(ctx, r, opts...)
286+
if err != nil {
287+
return nil, err
288+
}
289+
290+
var res MarginRepayResponse
291+
if err := json.Unmarshal(data, &res); err != nil {
292+
return nil, err
293+
}
294+
295+
return &res, nil
296+
}

v2/portfolio/client.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,21 @@ func (c *Client) NewGetLeverageBracketServiceUM() *GetLeverageBracketServiceUM {
402402
c: c,
403403
}
404404
}
405+
406+
func (c *Client) NewGetMarginMaxBorrowableService() *GetMarginMaxBorrowableService {
407+
return &GetMarginMaxBorrowableService{
408+
c: c,
409+
}
410+
}
411+
412+
func (c *Client) NewMarginBorrowService() *MarginBorrowService {
413+
return &MarginBorrowService{
414+
c: c,
415+
}
416+
}
417+
418+
func (c *Client) NewMarginRepayService() *MarginRepayService {
419+
return &MarginRepayService{
420+
c: c,
421+
}
422+
}

0 commit comments

Comments
 (0)