ftx: implement ftx balances querying

This commit is contained in:
ycdesu 2021-02-08 18:59:36 +08:00
parent eb00720043
commit 46b0315871
4 changed files with 158 additions and 17 deletions

View File

@ -0,0 +1,29 @@
package ftx
import (
"context"
"encoding/json"
"fmt"
)
type balanceRequest struct {
*restRequest
}
func (r *balanceRequest) Balances(ctx context.Context) (balances, error) {
resp, err := r.
Method("GET").
ReferenceURL("api/wallet/balances").
DoAuthenticatedRequest(ctx)
if err != nil {
return balances{}, err
}
var b balances
if err := json.Unmarshal(resp.Body, &b); err != nil {
return balances{}, fmt.Errorf("failed to unmarshal balance response body to json: %w", err)
}
return b, nil
}

View File

@ -2,66 +2,106 @@ package ftx
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
const (
restEndpoint = "https://ftx.com"
defaultHTTPTimeout = 15 * time.Second
)
type Exchange struct {
rest *restRequest
}
func (e Exchange) Name() types.ExchangeName {
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{
rest: rest,
}
}
func (e *Exchange) Name() types.ExchangeName {
panic("implement me")
}
func (e Exchange) PlatformFeeCurrency() string {
func (e *Exchange) PlatformFeeCurrency() string {
panic("implement me")
}
func (e Exchange) NewStream() types.Stream {
func (e *Exchange) NewStream() types.Stream {
panic("implement me")
}
func (e Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
panic("implement me")
}
func (e Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
panic("implement me")
}
func (e Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error) {
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) {
panic("implement me")
}
func (e Exchange) QueryKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error) {
func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) ([]types.Trade, error) {
panic("implement me")
}
func (e Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) ([]types.Trade, error) {
func (e *Exchange) QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []types.Deposit, err error) {
panic("implement me")
}
func (e Exchange) QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []types.Deposit, err error) {
func (e *Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since, until time.Time) (allWithdraws []types.Withdraw, err error) {
panic("implement me")
}
func (e Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since, until time.Time) (allWithdraws []types.Withdraw, err error) {
func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders types.OrderSlice, err error) {
panic("implement me")
}
func (e Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders types.OrderSlice, err error) {
func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error) {
panic("implement me")
}
func (e Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error) {
func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) (orders []types.Order, err error) {
panic("implement me")
}
func (e Exchange) QueryClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) (orders []types.Order, err error) {
panic("implement me")
}
func (e Exchange) CancelOrders(ctx context.Context, orders ...types.Order) error {
func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) error {
panic("implement me")
}

View File

@ -0,0 +1,61 @@
package ftx
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
func TestExchange_QueryAccountBalances(t *testing.T) {
successResp := `
{
"result": [
{
"availableWithoutBorrow": 19.47458865,
"coin": "USD",
"free": 19.48085209,
"spotBorrow": 0.0,
"total": 1094.66405065,
"usdValue": 1094.664050651561
}
],
"success": true
}
`
failureResp := `{"result":[],"success":false}`
i := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if i == 0 {
fmt.Fprintln(w, successResp)
i++
return
}
fmt.Fprintln(w, failureResp)
}))
defer ts.Close()
ex := NewExchange("", "", "")
serverURL, err := url.Parse(ts.URL)
assert.NoError(t, err)
ex.rest = newRestRequest(&http.Client{Timeout: defaultHTTPTimeout}, serverURL)
resp, err := ex.QueryAccountBalances(context.Background())
assert.NoError(t, err)
assert.Len(t, resp, 1)
b, ok := resp["USD"]
assert.True(t, ok)
expectedAvailable := fixedpoint.Must(fixedpoint.NewFromString("19.48085209"))
assert.Equal(t, expectedAvailable, b.Available)
assert.Equal(t, fixedpoint.Must(fixedpoint.NewFromString("1094.66405065")).Sub(expectedAvailable), b.Locked)
resp, err = ex.QueryAccountBalances(context.Background())
assert.EqualError(t, err, "ftx returns querying balances failure")
assert.Nil(t, resp)
}

View File

@ -0,0 +1,11 @@
package ftx
type balances struct {
Success bool `json:"Success"`
Result []struct {
Coin string `json:"coin"`
Free float64 `json:"free"`
Total float64 `json:"total"`
} `json:"result"`
}