margin

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BaseURL = "https://api.binance.com"
)

Variables

This section is empty.

Functions

func GenerateSignature

func GenerateSignature(secretKey, data string) string

GenerateSignature generates an HMAC SHA256 signature

func Timestamp

func Timestamp() int64

Timestamp returns the current timestamp in milliseconds

Types

type APIError

type APIError struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

func (*APIError) Error

func (e *APIError) Error() string

type Client

type Client struct {
	BaseURL    string
	APIKey     string
	SecretKey  string
	HTTPClient *http.Client
	Logger     *zap.SugaredLogger
}

func NewClient

func NewClient() *Client

func (*Client) Borrow

func (c *Client) Borrow(ctx context.Context, asset string, amount float64, isIsolated bool, symbol string) (int64, error)

Borrow borrows an asset from the margin account Endpoint: POST /sapi/v1/margin/loan isIsolated: "TRUE" or "FALSE" (default "FALSE" if empty) symbol: mandatory if isIsolated is "TRUE"

func (*Client) CancelOrder

func (c *Client) CancelOrder(ctx context.Context, symbol string, orderID int64, origClientOrderID string, isIsolated bool) (*MarginOrder, error)

CancelOrder excels a margin order Endpoint: DELETE /sapi/v1/margin/order

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, endpoint string, params map[string]interface{}, signed bool, result interface{}) error

func (*Client) Get

func (c *Client) Get(ctx context.Context, endpoint string, params map[string]interface{}, signed bool, result interface{}) error

func (*Client) GetAccount

func (c *Client) GetAccount(ctx context.Context) (*MarginAccount, error)

GetAccount queries the cross margin account details Endpoint: GET /sapi/v1/margin/account

func (*Client) GetIsolatedAccount

func (c *Client) GetIsolatedAccount(ctx context.Context, symbols string) (*IsolatedMarginAccount, error)

GetIsolatedAccount queries the isolated margin account details Endpoint: GET /sapi/v1/margin/isolated/account symbols: Optional, max 5 symbols, comma separated (e.g. "BTCUSDT,ETHUSDT")

func (*Client) GetOrder

func (c *Client) GetOrder(ctx context.Context, symbol string, orderID int64, origClientOrderID string, isIsolated bool) (*MarginOrder, error)

GetOrder queries a margin order Endpoint: GET /sapi/v1/margin/order

func (*Client) PlaceOrder

func (c *Client) PlaceOrder(ctx context.Context, params *PlaceOrderParams) (*OrderResponseFull, error)

PlaceOrder places a margin order Endpoint: POST /sapi/v1/margin/order

func (*Client) Post

func (c *Client) Post(ctx context.Context, endpoint string, params map[string]interface{}, signed bool, result interface{}) error

func (*Client) Repay

func (c *Client) Repay(ctx context.Context, asset string, amount float64, isIsolated bool, symbol string) (int64, error)

Repay repays a loan for the margin account Endpoint: POST /sapi/v1/margin/repay isIsolated: "TRUE" or "FALSE" (default "FALSE" if empty) symbol: mandatory if isIsolated is "TRUE"

func (*Client) WithBaseURL

func (c *Client) WithBaseURL(url string) *Client

func (*Client) WithCredentials

func (c *Client) WithCredentials(apiKey, secretKey string) *Client

type IsolatedMarginAccount

type IsolatedMarginAccount struct {
	Assets              []IsolatedMarginSymbol `json:"assets"`
	TotalAssetOfBtc     string                 `json:"totalAssetOfBtc"`
	TotalLiabilityOfBtc string                 `json:"totalLiabilityOfBtc"`
	TotalNetAssetOfBtc  string                 `json:"totalNetAssetOfBtc"`
}

type IsolatedMarginAsset

type IsolatedMarginAsset struct {
	Asset         string `json:"asset"`
	BorrowEnabled bool   `json:"borrowEnabled"`
	Borrowed      string `json:"borrowed"`
	Free          string `json:"free"`
	Interest      string `json:"interest"`
	Locked        string `json:"locked"`
	NetAsset      string `json:"netAsset"`
	NetAssetOfBtc string `json:"netAssetOfBtc"`
	RepayEnabled  bool   `json:"repayEnabled"`
	TotalAsset    string `json:"totalAsset"`
}

type IsolatedMarginSymbol

type IsolatedMarginSymbol struct {
	BaseAsset         IsolatedMarginAsset `json:"baseAsset"`
	QuoteAsset        IsolatedMarginAsset `json:"quoteAsset"`
	Symbol            string              `json:"symbol"`
	IsolatedCreated   bool                `json:"isolatedCreated"`
	Enabled           bool                `json:"enabled"`
	MarginLevel       string              `json:"marginLevel"`
	MarginLevelStatus string              `json:"marginLevelStatus"`
	MarginRatio       string              `json:"marginRatio"`
	IndexPrice        string              `json:"indexPrice"`
	LiquidatePrice    string              `json:"liquidatePrice"`
	LiquidateRate     string              `json:"liquidateRate"`
	TradeEnabled      bool                `json:"tradeEnabled"`
}

type MarginAccount

type MarginAccount struct {
	BorrowEnabled       bool        `json:"borrowEnabled"`
	MarginLevel         string      `json:"marginLevel"`
	TotalAssetOfBtc     string      `json:"totalAssetOfBtc"`
	TotalLiabilityOfBtc string      `json:"totalLiabilityOfBtc"`
	TotalNetAssetOfBtc  string      `json:"totalNetAssetOfBtc"`
	TradeEnabled        bool        `json:"tradeEnabled"`
	TransferEnabled     bool        `json:"transferEnabled"`
	UserAssets          []UserAsset `json:"userAssets"`
}

MarginAccount represents the response from /sapi/v1/margin/account

type MarginOrder

type MarginOrder struct {
	Symbol              string `json:"symbol"`
	OrderID             int64  `json:"orderId"`
	ClientOrderID       string `json:"clientOrderId"`
	TransactTime        int64  `json:"transactTime"`
	Price               string `json:"price"`
	OrigQty             string `json:"origQty"`
	ExecutedQty         string `json:"executedQty"`
	CummulativeQuoteQty string `json:"cummulativeQuoteQty"`
	Status              string `json:"status"`
	TimeInForce         string `json:"timeInForce"`
	Type                string `json:"type"`
	Side                string `json:"side"`
	IsIsolated          bool   `json:"isIsolated"`
}

MarginOrder represents the response from order placement

type OrderResponseFull

type OrderResponseFull struct {
	MarginOrder
	Fills []struct {
		Price           string `json:"price"`
		Qty             string `json:"qty"`
		Commission      string `json:"commission"`
		CommissionAsset string `json:"commissionAsset"`
	} `json:"fills"`
}

type PlaceOrderParams

type PlaceOrderParams struct {
	Symbol           string
	Side             string
	Type             string
	TimeInForce      string // Optional
	Quantity         float64
	QuoteOrderQty    float64 // Optional
	Price            float64 // Optional
	NewClientOrderID string  // Optional
	SideEffectType   string  // NO_SIDE_EFFECT, MARGIN_BUY, AUTO_REPAY
	IsIsolated       bool
}

type TransactionResult

type TransactionResult struct {
	TranId int64 `json:"tranId"`
}

TransactionResult represents the response for Borrow/Repay

type UserAsset

type UserAsset struct {
	Asset    string `json:"asset"`
	Borrowed string `json:"borrowed"`
	Free     string `json:"free"`
	Interest string `json:"interest"`
	Locked   string `json:"locked"`
	NetAsset string `json:"netAsset"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL