diff --git a/models/spotMarginResponse.go b/models/spotMarginResponse.go index fef246a..4474e72 100644 --- a/models/spotMarginResponse.go +++ b/models/spotMarginResponse.go @@ -59,3 +59,40 @@ type ClassicalSpotMarginLoanResult struct { Status int `json:"status"` // Use int for integer type SwitchStatus int `json:"switchStatus"` // Use int for integer type } + +type SpotMarginBorrowOrders struct { + List []struct { + AccountId string `json:"accountId"` // Account ID + Coin string `json:"coin"` // Coin name + CreatedTime int64 `json:"createdTime"` // Borrow order created timestamp (ms) + Id string `json:"id"` // Borrow order ID + InterestAmount string `json:"interestAmount"` // Total interest + InterestBalance string `json:"interestBalance"` // Outstanding interest + LoanAmount string `json:"loanAmount"` // Principal amount + LoanBalance string `json:"loanBalance"` // Outstanding principal + RemainAmount string `json:"remainAmount"` // Remaining debt = interestBalance + loanBalance + Status int `json:"status"` // Status 1: uncleared, 2: cleared + Type int `json:"type"` // Order Type 1: manual loan, 2: auto loan + } `json:"list"` +} + +type SpotBorrowOrderResult struct { + TransactId string `json:"transactId"` +} + +type SpotRepayOrderResult struct { + RepayId string `json:"repayId"` +} + +type SpotToggleMarginResult struct { + SwitchStatus string `json:"switchStatus"` +} + +type SpotMarginLeverageResult struct { + Leverage string `json:"leverage"` +} + +type SpotMarginStateResult struct { + SpotLeverage string `json:"spotLeverage"` + SpotMarginMode string `json:"spotMarginMode"` +} diff --git a/spot_margin.go b/spot_margin.go index 17a6ff1..300770a 100644 --- a/spot_margin.go +++ b/spot_margin.go @@ -141,3 +141,181 @@ func (s *SpotMarginClient) GetSpotMarginLoanAccountInfo(ctx context.Context, opt } return res, nil } + +func (s *SpotMarginClient) GetSpotMarginBorrowOrders(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + if s.isUta { + return nil, errors.New("this function only works for classical accounts") + } + if err = handlers.ValidateParams(s.params); err != nil { + return nil, err + } + r := &request{ + method: http.MethodGet, + endpoint: "/v5/spot-cross-margin-trade/orders", + secType: secTypeSigned, + } + r.setParams(s.params) + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *SpotMarginClient) GetSpotMarginRepaymentOrders(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + if s.isUta { + return nil, errors.New("this function only works for classical accounts") + } + if err = handlers.ValidateParams(s.params); err != nil { + return nil, err + } + r := &request{ + method: http.MethodGet, + endpoint: "/v5/spot-cross-margin-trade/repay-history", + secType: secTypeSigned, + } + r.setParams(s.params) + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *SpotMarginClient) BorrowSpotMarginLoan(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + if s.isUta { + return nil, errors.New("this function only works for classical accounts") + } + if err = handlers.ValidateParams(s.params); err != nil { + return nil, err + } + r := &request{ + method: http.MethodPost, + endpoint: "/v5/spot-cross-margin-trade/loan", + secType: secTypeSigned, + } + r.setParams(s.params) + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *SpotMarginClient) RepaySpotMarginLoan(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + if s.isUta { + return nil, errors.New("this function only works for classical accounts") + } + if err = handlers.ValidateParams(s.params); err != nil { + return nil, err + } + r := &request{ + method: http.MethodPost, + endpoint: "/v5/spot-cross-margin-trade/repay", + secType: secTypeSigned, + } + r.setParams(s.params) + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *SpotMarginClient) SetSpotMarginLeverage(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + if !s.isUta { + return nil, errors.New("this function only works for UTA accounts") + } + if err = handlers.ValidateParams(s.params); err != nil { + return nil, err + } + r := &request{ + method: http.MethodPost, + endpoint: "/v5/spot-margin-trade/set-leverage", + secType: secTypeSigned, + } + r.setParams(s.params) + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *SpotMarginClient) GetSpotMarginState(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + if !s.isUta { + return nil, errors.New("this function only works for UTA accounts") + } + if err = handlers.ValidateParams(s.params); err != nil { + return nil, err + } + r := &request{ + method: http.MethodGet, + endpoint: "/v5/spot-margin-trade/state", + secType: secTypeSigned, + } + r.setParams(s.params) + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *SpotMarginClient) ToggleSpotMarginTrade(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + if err = handlers.ValidateParams(s.params); err != nil { + return nil, err + } + var endpoint string + if !s.isUta { + endpoint = "/v5/spot-margin-trade/switch-mode" + } else { + endpoint = "/v5/spot-cross-margin-trade/data" + } + r := &request{ + method: http.MethodPost, + endpoint: endpoint, + secType: secTypeSigned, + } + r.setParams(s.params) + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +}