2021-02-05 09:29:38 +00:00
|
|
|
package ftx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-08 10:59:36 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-02-05 09:29:38 +00:00
|
|
|
"time"
|
|
|
|
|
2021-02-27 08:47:21 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2021-02-05 09:29:38 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
const (
|
|
|
|
restEndpoint = "https://ftx.com"
|
|
|
|
defaultHTTPTimeout = 15 * time.Second
|
|
|
|
)
|
|
|
|
|
2021-02-27 09:01:20 +00:00
|
|
|
var logger = logrus.WithField("exchange", "ftx")
|
2021-02-27 08:47:21 +00:00
|
|
|
|
2021-02-05 09:29:38 +00:00
|
|
|
type Exchange struct {
|
2021-02-27 08:47:21 +00:00
|
|
|
rest *restRequest
|
|
|
|
key, secret string
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func NewExchange(key, secret string, subAccount string) *Exchange {
|
|
|
|
u, err := url.Parse(restEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
rest := newRestRequest(&http.Client{Timeout: defaultHTTPTimeout}, u).Auth(key, secret)
|
|
|
|
if subAccount != "" {
|
|
|
|
rest.SubAccount(subAccount)
|
|
|
|
}
|
|
|
|
return &Exchange{
|
2021-02-27 08:47:21 +00:00
|
|
|
rest: rest,
|
|
|
|
key: key,
|
|
|
|
secret: secret,
|
2021-02-08 10:59:36 +00:00
|
|
|
}
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) Name() types.ExchangeName {
|
2021-02-08 14:33:12 +00:00
|
|
|
return types.ExchangeFTX
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) PlatformFeeCurrency() string {
|
2021-02-05 09:29:38 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) NewStream() types.Stream {
|
2021-02-27 08:47:21 +00:00
|
|
|
return NewStream(e.key, e.secret)
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
|
2021-02-05 09:29:38 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
|
2021-02-05 09:29:38 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error) {
|
|
|
|
resp, err := e.rest.Balances(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !resp.Success {
|
|
|
|
return nil, fmt.Errorf("ftx returns querying balances failure")
|
|
|
|
}
|
|
|
|
var balances = make(types.BalanceMap)
|
|
|
|
for _, r := range resp.Result {
|
|
|
|
balances[toGlobalCurrency(r.Coin)] = types.Balance{
|
|
|
|
Currency: toGlobalCurrency(r.Coin),
|
|
|
|
Available: fixedpoint.NewFromFloat(r.Free),
|
|
|
|
Locked: fixedpoint.NewFromFloat(r.Total).Sub(fixedpoint.NewFromFloat(r.Free)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return balances, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error) {
|
2021-02-05 09:29:38 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) ([]types.Trade, error) {
|
2021-02-05 09:29:38 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []types.Deposit, err error) {
|
2021-02-05 09:29:38 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since, until time.Time) (allWithdraws []types.Withdraw, err error) {
|
2021-02-05 09:29:38 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders types.OrderSlice, err error) {
|
2021-02-05 09:29:38 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error) {
|
2021-03-07 04:47:11 +00:00
|
|
|
resp, err := e.rest.OpenOrders(ctx, symbol)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !resp.Success {
|
|
|
|
return nil, fmt.Errorf("ftx returns querying open orders failure")
|
|
|
|
}
|
|
|
|
for _, r := range resp.Result {
|
|
|
|
o, err := toGlobalOrderFromOpenOrder(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
orders = append(orders, o)
|
|
|
|
}
|
|
|
|
return orders, nil
|
2021-02-05 09:29:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) (orders []types.Order, err error) {
|
2021-02-05 09:29:38 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:59:36 +00:00
|
|
|
func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) error {
|
2021-02-05 09:29:38 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
2021-02-27 09:24:08 +00:00
|
|
|
|
|
|
|
func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticker, error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) QueryTickers(ctx context.Context, symbol ...string) (map[string]types.Ticker, error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|