fcoin

package module
v0.0.0-...-2cf4c76 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2019 License: MIT Imports: 15 Imported by: 0

README

fcoin-go-sdk

FCoin Go SDK

Dependencies

Name Url HEAD tested
websocket https://github.com/gorilla/websocket 5ed622c449da6d44c3c8329331ff47a9e5844f71

Usage

// Auth
t := time.Now().Unix() * 1000  // Unix time(milliseconds)
timeStr := strconv.FormatInt(t, 10)
api := fcoin.Authorize("key", "secret", timeStr)

// Usage
rsp, err := api.AccountsBalance()
if err != nil || rsp.Status != 0 {
    // non-zero indicates API call failed
    fmt.Printf(rsp.Msg)
}
fmt.Printf(rsp.Data)

// API with args
args := &CreateOrderArgs{
	Symbol: symbol,
	Side:   side,
	Type:   t,
	Price:  price,
	Amount: amount,
}
rsp, err := api.GetOrders(args)

Websocket Usage

Example:

// Initialize websocket client(market data only)
api := fcoin.Client{}
if err := api.InitWS(); err != nil {
    //
}
rsp, err := api.WSPing()

// Subscribe to topic and receive(see ws_test.go)
api.WSSubscribe("message-id", "topic1", "topic2")  // or more topics

// message-id can be a empty string
// api.WSSubscribe("", "topic3")

// subscription result will contain same "message-id" if the field is not empty
var rsp2 WSSubRsp
if err := api.ws.ReadJSON(&rsp2) {
    // Suppose subscription result returns faster than data
    rsp2.ID == "message-id"
}

Caution: websocket provides bi-directional communication, Ping() in example does not work when you are subscribing to topics because next message would not necessarily be Ping-Response from server.

Recommended: subscribe to expected topics, pass client to your own message dispatcher and enjoy data.

Documentation

Index

Constants

View Source
const (
	// API base
	BaseUrl   = "https://api.fcoin.com/v2"
	WSBaseUrl = "wss://api.fcoin.com/v2/ws"

	// Public endpoints
	GetServerTime = "/public/server-time"
	GetCurrencies = "/public/currencies"
	GetSymbols    = "/public/symbols"

	// Account endpoints
	GetBalance = "/accounts/balance"

	// Order endpoints
	OrdersBase = "/orders"

	// API Spec
	MaxTimeDiffMs = 30 * 1000
	NeedSignature = true
	NoSignature   = false
)

Variables

This section is empty.

Functions

func Sign

func Sign(method, uri, ts, args, key string) string

Sign request with client secret using HMAC-SHA1 args should be ordered URI format

Types

type APIResponse

type APIResponse struct {
	// Status code, (error) Message
	Status int    `json:"status"`
	Msg    string `json:"msg"`
}

type AccountsBalanceRsp

type AccountsBalanceRsp struct {
	APIResponse
	Data []CurrencyBalance `json:"data"`
}

type Client

type Client struct {

	// websocket
	WS *websocket.Conn
	// contains filtered or unexported fields
}

func Authorize

func Authorize(httpClient *http.Client, key, secret string, localtime int64) (*Client, error)

func (*Client) AccountsBalance

func (c *Client) AccountsBalance() (AccountsBalanceRsp, error)

Account API call

func (*Client) CreateOrder

func (c *Client) CreateOrder(args *CreateOrderArgs) (CreateOrderRsp, error)

Order API call

func (*Client) Currencies

func (c *Client) Currencies() (CurrenciesRsp, error)

func (*Client) GetOrder

func (c *Client) GetOrder(orderId string) (GetOrderRsp, error)

func (*Client) GetOrders

func (c *Client) GetOrders(args *GetOrdersArgs) (GetOrdersRsp, error)

func (*Client) InitWS

func (c *Client) InitWS() error

func (*Client) ServerTime

func (c *Client) ServerTime() (ServerTimeRsp, error)

Public API call

func (*Client) SubmitCancelOrder

func (c *Client) SubmitCancelOrder(orderId string) (SubmitCancelOrderRsp, error)

func (*Client) Symbols

func (c *Client) Symbols() (SymbolsRsp, error)

func (*Client) WSPing

func (c *Client) WSPing() (WSPingRsp, error)

func (*Client) WSReq

func (c *Client) WSReq(id string, args ...string) error

Request once

func (*Client) WSSubscribe

func (c *Client) WSSubscribe(id string, topics ...string) error

Add new subscription

func (*Client) WSUnsubscribe

func (c *Client) WSUnsubscribe(id string, topics ...string) error

Un-subscription

type CreateOrderArgs

type CreateOrderArgs struct {
	Amount string `json:"amount"`
	Price  string `json:"price"`
	Type   string `json:"type"`
	Side   string `json:"side"`
	Symbol string `json:"symbol"`
}

Orders API

type CreateOrderRsp

type CreateOrderRsp struct {
	APIResponse
	Data string `json:"data"`
}

type CurrenciesRsp

type CurrenciesRsp struct {
	APIResponse
	Data []string `json:"data"`
}

type CurrencyBalance

type CurrencyBalance struct {
	Currency  string `json:"currency"`
	Category  string `json:"category"`
	Available string `json:"available"`
	Frozen    string `json:"frozen"`
	Balance   string `json:"balance"`
}

Account API

type GetOrderRsp

type GetOrderRsp struct {
	APIResponse
	Data OrderDetail `json:"data"`
}

type GetOrdersArgs

type GetOrdersArgs struct {
	// after/before certain page
	After  string `json:"After,omitempty"`
	Before string `json:"before,omitempty"`
	Limit  string `json:"limit,omitempty"`
	States string `json:"states,omitempty"`
	Symbol string `json:"symbol,omitempty"`
}

type GetOrdersRsp

type GetOrdersRsp struct {
	APIResponse
	Data []OrderDetail `json:"data"`
}

type OrderDetail

type OrderDetail struct {
	ID            string `json:"id"`
	Symbol        string `json:"symbol"`
	Type          string `json:"type"`
	Side          string `json:"side"`
	Price         string `json:"price"`
	Amount        string `json:"amount"`
	State         string `json:"state"`
	ExecutedValue string `json:"executed_value"`
	FillFees      string `json:"fill_fees"`
	FilledAmount  string `json:"filled_amount"`
	CreatedAt     int    `json:"created_at"`
	Source        string `json:"source"`
}

type ServerTimeRsp

type ServerTimeRsp struct {
	APIResponse
	Data int64 `json:"data"`
}

Public API

type SubmitCancelOrderRsp

type SubmitCancelOrderRsp struct {
	APIResponse
	Data bool `json:"data"`
}

type SymbolPair

type SymbolPair struct {
	Name          string `json:"name"`
	BaseCurrenty  string `json:"base_currency"`
	QuoteCurrenty string `json:"quote_currency"`
	PriceDecimal  int    `json:"price_decimal"`
	AmountDecimal int    `json:"amount_decimal"`
}

type SymbolsRsp

type SymbolsRsp struct {
	APIResponse
	Data []SymbolPair `json:"data"`
}

type WSArgs

type WSArgs struct {
	// Message id, server returns this for accounting
	ID   string        `json:"id,omitempty"`
	Args []interface{} `json:"args,omitempty"`
	Cmd  string        `json:"cmd,omitempty"`
}

type WSCandle

type WSCandle struct {
	// candle.$resolution.$symbol
	Type     string  `json:"type"`
	ID       int     `json:"id"`
	Seq      int     `json:"seq"`
	Open     float64 `json:"open"`
	Close    float64 `json:"close"`
	High     float64 `json:"high"`
	Low      float64 `json:"low"`
	Count    int     `json:"count"`
	BaseVol  float64 `json:"base_vol"`
	QuoteVol float64 `json:"quote_vol"`
}

type WSHello

type WSHello struct {
	Type string `json:"type"`
	TS   int64  `json:"ts"`
}

Websocket

type WSPingRsp

type WSPingRsp struct {
	Type     string `json:"type"`
	RemoteTs int64  `json:"ts"`
	Gap      int64  `json:"gap"`
}

type WSSubRsp

type WSSubRsp struct {
	ID     string   `json:"id"`
	Type   string   `json:"type"`
	Topics []string `json:"topics"`
}

type WSSymbolList

type WSSymbolList struct {
	// helper for sub "all-tickers", topic is always "all-tickers"
	Topic   string `json:"topic"`
	Tickers []struct {
		Symbol string    `json:"symbol"`
		Ticker []float64 `json:"ticker"`
	} `json:"tickers"`
}

Websocket Message structs for ReadJSON

func (*WSSymbolList) Symbols

func (l *WSSymbolList) Symbols() []string

type WSTick

type WSTick struct {
	// ticker.$symbol
	Type   string      `json:"type"`
	Seq    int         `json:"seq"`
	Ticker [11]float64 `json:"ticker"`
}

type WSTickDepth

type WSTickDepth struct {
	// depth.$level.$symbol
	Bids []float64 `json:"bids"`
	Asks []float64 `json:"asks"`
	Ts   int64     `json:"ts"`
	Seq  int       `json:"seq"`
	Type string    `json:"type"`
}

type WSTrade

type WSTrade struct {
	// trade.$symbol
	Type   string  `json:"type"`
	ID     int     `json:"id"`
	Amount float64 `json:"amount"`
	Ts     int64   `json:"ts"`
	Side   string  `json:"side"`
	Price  float64 `json:"price"`
}

Jump to

Keyboard shortcuts

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