Merge pull request #157 from c9s/ftx/new-rest-everytime

fix: new rest client everytime
This commit is contained in:
YC 2021-03-15 20:39:45 +08:00 committed by GitHub
commit edff115c20
2 changed files with 19 additions and 13 deletions

View File

@ -21,8 +21,9 @@ const (
var logger = logrus.WithField("exchange", "ftx") var logger = logrus.WithField("exchange", "ftx")
type Exchange struct { type Exchange struct {
rest *restRequest
key, secret string key, secret string
subAccount string
restEndpoint *url.URL
} }
func NewExchange(key, secret string, subAccount string) *Exchange { func NewExchange(key, secret string, subAccount string) *Exchange {
@ -30,17 +31,22 @@ func NewExchange(key, secret string, subAccount string) *Exchange {
if err != nil { if err != nil {
panic(err) panic(err)
} }
rest := newRestRequest(&http.Client{Timeout: defaultHTTPTimeout}, u).Auth(key, secret)
if subAccount != "" {
rest.SubAccount(subAccount)
}
return &Exchange{ return &Exchange{
rest: rest, restEndpoint: u,
key: key, key: key,
secret: secret, secret: secret,
subAccount: subAccount,
} }
} }
func (e *Exchange) newRest() *restRequest {
r := newRestRequest(&http.Client{Timeout: defaultHTTPTimeout}, e.restEndpoint).Auth(e.key, e.secret)
if len(e.subAccount) > 0 {
r.SubAccount(e.subAccount)
}
return r
}
func (e *Exchange) Name() types.ExchangeName { func (e *Exchange) Name() types.ExchangeName {
return types.ExchangeFTX return types.ExchangeFTX
} }
@ -62,7 +68,7 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
} }
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) resp, err := e.newRest().Balances(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -105,7 +111,7 @@ func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder
if so.TimeInForce != "GTC" { if so.TimeInForce != "GTC" {
return createdOrders, fmt.Errorf("unsupported TimeInForce %s. only support GTC", so.TimeInForce) return createdOrders, fmt.Errorf("unsupported TimeInForce %s. only support GTC", so.TimeInForce)
} }
or, err := e.rest.PlaceOrder(ctx, PlaceOrderPayload{ or, err := e.newRest().PlaceOrder(ctx, PlaceOrderPayload{
Market: TrimUpperString(so.Symbol), Market: TrimUpperString(so.Symbol),
Side: TrimLowerString(string(so.Side)), Side: TrimLowerString(string(so.Side)),
Price: so.Price, Price: so.Price,
@ -133,7 +139,7 @@ func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder
func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error) { func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error) {
// TODO: invoke open trigger orders // TODO: invoke open trigger orders
resp, err := e.rest.OpenOrders(ctx, symbol) resp, err := e.newRest().OpenOrders(ctx, symbol)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -44,7 +44,7 @@ func TestExchange_QueryAccountBalances(t *testing.T) {
ex := NewExchange("", "", "") ex := NewExchange("", "", "")
serverURL, err := url.Parse(ts.URL) serverURL, err := url.Parse(ts.URL)
assert.NoError(t, err) assert.NoError(t, err)
ex.rest = newRestRequest(&http.Client{Timeout: defaultHTTPTimeout}, serverURL) ex.restEndpoint = serverURL
resp, err := ex.QueryAccountBalances(context.Background()) resp, err := ex.QueryAccountBalances(context.Background())
assert.NoError(t, err) assert.NoError(t, err)