Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added us domain #13

Merged
merged 1 commit into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ linters:
- bodyclose
- depguard
- goimports
- dupl
enable-all: false
disable-all: true
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ go get github.com/grishinsana/goftx
> See examples directory and test cases for more examples

### TODO
- Futures
- Wallet
- Converts
- Funding Payments
- Leveraged Tokens
- Options
Expand Down Expand Up @@ -96,6 +94,14 @@ func main() {
}
```

### FTX US Mode
If you need to use FTX US than you could set goftx.WithFTXUS option
```go
client := goftx.New(
goftx.WithFTXUS(),
)
```

### Websocket Debug Mode
If need, it is possible to set debug mode to look error and system messages in stream methods
```go
Expand Down
6 changes: 3 additions & 3 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (a *Account) GetAccountInformation() (*models.AccountInformation, error) {
request, err := a.client.prepareRequest(Request{
Auth: true,
Method: http.MethodGet,
URL: fmt.Sprintf("%s%s", apiUrl, apiGetAccountInformation),
URL: fmt.Sprintf("%s%s", a.client.apiURL, apiGetAccountInformation),
})
if err != nil {
return nil, errors.WithStack(err)
Expand All @@ -49,7 +49,7 @@ func (a *Account) GetPositions() ([]*models.Position, error) {
request, err := a.client.prepareRequest(Request{
Auth: true,
Method: http.MethodGet,
URL: fmt.Sprintf("%s%s", apiUrl, apiGetPositions),
URL: fmt.Sprintf("%s%s", a.client.apiURL, apiGetPositions),
})
if err != nil {
return nil, errors.WithStack(err)
Expand Down Expand Up @@ -80,7 +80,7 @@ func (a *Account) ChangeAccountLeverage(leverage decimal.Decimal) error {
request, err := a.client.prepareRequest(Request{
Auth: true,
Method: http.MethodPost,
URL: fmt.Sprintf("%s%s", apiUrl, apiPostLeverage),
URL: fmt.Sprintf("%s%s", a.client.apiURL, apiPostLeverage),
Body: body,
})
if err != nil {
Expand Down
43 changes: 31 additions & 12 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import (
)

const (
apiUrl = "https://ftx.com/api"
apiOtcUrl = "https://otc.ftx.com/api"
apiUrlFormat = "https://ftx.%s/api"
apiOtcUrl = "https://otc.ftx.com/api"

keyHeader = "FTX-KEY"
signHeader = "FTX-SIGN"
tsHeader = "FTX-TS"
subAccountHeader = "FTX-SUBACCOUNT"
keyHeaderFormat = "FTX%s-KEY"
signHeaderFormat = "FTX%s-SIGN"
tsHeaderFormat = "FTX%s-TS"
subAccountHeaderFormat = "FTX%s-SUBACCOUNT"
)

type Option func(c *Client)
Expand All @@ -35,6 +35,12 @@ func WithHTTPClient(client *http.Client) Option {
}
}

func WithFTXUS() Option {
return func(c *Client) {
c.isFtxUS = true
}
}

func WithAuth(key, secret string, subAccount ...string) Option {
return func(c *Client) {
c.apiKey = key
Expand All @@ -54,6 +60,8 @@ type Client struct {
secret string
subAccount string
serverTimeDiff time.Duration
isFtxUS bool
apiURL string
SubAccounts
Markets
Account
Expand All @@ -74,6 +82,12 @@ func New(opts ...Option) *Client {
opt(client)
}

domain := "com"
if client.isFtxUS {
domain = "us"
}
client.apiURL = fmt.Sprintf(apiUrlFormat, domain)

client.SubAccounts = SubAccounts{client: client}
client.Markets = Markets{client: client}
client.Account = Account{client: client}
Expand All @@ -87,7 +101,7 @@ func New(opts ...Option) *Client {
secret: client.secret,
subAccount: client.subAccount,
mu: &sync.Mutex{},
url: wsUrl,
url: fmt.Sprintf(wsUrlFormat, domain),
dialer: websocket.DefaultDialer,
wsReconnectionCount: reconnectCount,
wsReconnectionInterval: reconnectInterval,
Expand Down Expand Up @@ -144,13 +158,18 @@ func (c *Client) prepareRequest(request Request) (*http.Request, error) {
payload += string(request.Body)
}

usPrefix := ""
if c.isFtxUS {
usPrefix = "US"
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set(keyHeader, c.apiKey)
req.Header.Set(signHeader, c.signture(payload))
req.Header.Set(tsHeader, nonce)
req.Header.Set(fmt.Sprintf(keyHeaderFormat, usPrefix), c.apiKey)
req.Header.Set(fmt.Sprintf(signHeaderFormat, usPrefix), c.signture(payload))
req.Header.Set(fmt.Sprintf(tsHeaderFormat, usPrefix), nonce)

if c.subAccount != "" {
req.Header.Set(subAccountHeader, c.subAccount)
req.Header.Set(fmt.Sprintf(subAccountHeaderFormat, usPrefix), c.subAccount)
}
}

Expand Down Expand Up @@ -221,7 +240,7 @@ func (c *Client) GetServerTime() (time.Time, error) {
func (c Client) Ping() error {
request, err := c.prepareRequest(Request{
Method: http.MethodGet,
URL: apiUrl,
URL: c.apiURL,
})
if err != nil {
return errors.WithStack(err)
Expand Down
13 changes: 10 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ func TestClient_GetServerTime(t *testing.T) {
}

func TestClient_Ping(t *testing.T) {
ftx := New()
t.Run("com", func(t *testing.T) {
ftx := New(WithFTXUS())
err := ftx.Ping()
require.NoError(t, err)
})

err := ftx.Ping()
require.NoError(t, err)
t.Run("us", func(t *testing.T) {
ftx := New()
err := ftx.Ping()
require.NoError(t, err)
})
}
6 changes: 3 additions & 3 deletions converts.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (c *Converts) CreateQuote(payload *models.CreateQuotePayload) (int64, error
request, err := c.client.prepareRequest(Request{
Auth: true,
Method: http.MethodPost,
URL: fmt.Sprintf("%s%s", apiUrl, apiQuotes),
URL: fmt.Sprintf("%s%s", c.client.apiURL, apiQuotes),
Body: body,
})
if err != nil {
Expand Down Expand Up @@ -57,7 +57,7 @@ func (c *Converts) GetQuotes(quoteID int64, market *string) ([]*models.QuoteStat
request, err := c.client.prepareRequest(Request{
Auth: true,
Method: http.MethodGet,
URL: fmt.Sprintf("%s%s/%d", apiUrl, apiQuotes, quoteID),
URL: fmt.Sprintf("%s%s/%d", c.client.apiURL, apiQuotes, quoteID),
Params: queryParams,
})
if err != nil {
Expand All @@ -82,7 +82,7 @@ func (c *Converts) AcceptQuote(quoteID int64) error {
request, err := c.client.prepareRequest(Request{
Auth: true,
Method: http.MethodPost,
URL: fmt.Sprintf("%s%s/%d/accept", apiUrl, apiQuotes, quoteID),
URL: fmt.Sprintf("%s%s/%d/accept", c.client.apiURL, apiQuotes, quoteID),
})
if err != nil {
return errors.WithStack(err)
Expand Down
1 change: 1 addition & 0 deletions examples/websocket/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func main() {

client := goftx.New(
goftx.WithAuth(os.Getenv("FTX_KEY"), os.Getenv("FTX_SECRET")),
goftx.WithFTXUS(),
)

client.Stream.SetStreamTimeout(60 * time.Second)
Expand Down
2 changes: 1 addition & 1 deletion fills.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (f *Fills) GetFills(params *models.GetFillsParams) ([]*models.Fill, error)
request, err := f.client.prepareRequest(Request{
Auth: true,
Method: http.MethodGet,
URL: fmt.Sprintf("%s%s", apiUrl, apiFills),
URL: fmt.Sprintf("%s%s", f.client.apiURL, apiFills),
Params: queryParams,
})
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions futures.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Futures struct {
func (f *Futures) GetFutures() ([]*models.Future, error) {
request, err := f.client.prepareRequest(Request{
Method: http.MethodGet,
URL: fmt.Sprintf("%s%s", apiUrl, apiFutures),
URL: fmt.Sprintf("%s%s", f.client.apiURL, apiFutures),
})
if err != nil {
return nil, errors.WithStack(err)
Expand All @@ -49,7 +49,7 @@ func (f *Futures) GetFutures() ([]*models.Future, error) {
func (f *Futures) GetFuture(name string) (*models.Future, error) {
request, err := f.client.prepareRequest(Request{
Method: http.MethodGet,
URL: fmt.Sprintf("%s%s/%s", apiUrl, apiFutures, name),
URL: fmt.Sprintf("%s%s/%s", f.client.apiURL, apiFutures, name),
})
if err != nil {
return nil, errors.WithStack(err)
Expand All @@ -72,7 +72,7 @@ func (f *Futures) GetFuture(name string) (*models.Future, error) {
func (f *Futures) GetFutureStats(name string) (*models.FutureStats, error) {
request, err := f.client.prepareRequest(Request{
Method: http.MethodGet,
URL: fmt.Sprintf("%s%s/%s/stats", apiUrl, apiFutures, name),
URL: fmt.Sprintf("%s%s/%s/stats", f.client.apiURL, apiFutures, name),
})
if err != nil {
return nil, errors.WithStack(err)
Expand Down Expand Up @@ -100,7 +100,7 @@ func (f *Futures) GetFundingRates(params *models.GetFundingRatesParams) ([]*mode

request, err := f.client.prepareRequest(Request{
Method: http.MethodGet,
URL: fmt.Sprintf("%s%s", apiUrl, apiFundingRates),
URL: fmt.Sprintf("%s%s", f.client.apiURL, apiFundingRates),
Params: queryParams,
})
if err != nil {
Expand All @@ -124,7 +124,7 @@ func (f *Futures) GetFundingRates(params *models.GetFundingRatesParams) ([]*mode
func (f *Futures) GetIndexWeights(indexName string) (map[string]decimal.Decimal, error) {
request, err := f.client.prepareRequest(Request{
Method: http.MethodGet,
URL: fmt.Sprintf("%s%s", apiUrl, fmt.Sprintf(apiIndexWeights, indexName)),
URL: fmt.Sprintf("%s%s", f.client.apiURL, fmt.Sprintf(apiIndexWeights, indexName)),
})
if err != nil {
return nil, errors.WithStack(err)
Expand All @@ -147,7 +147,7 @@ func (f *Futures) GetIndexWeights(indexName string) (map[string]decimal.Decimal,
func (f *Futures) GetExpiredFutures() ([]*models.FutureExpired, error) {
request, err := f.client.prepareRequest(Request{
Method: http.MethodGet,
URL: fmt.Sprintf("%s%s", apiUrl, apiExpiredFutures),
URL: fmt.Sprintf("%s%s", f.client.apiURL, apiExpiredFutures),
})
if err != nil {
return nil, errors.WithStack(err)
Expand Down Expand Up @@ -175,7 +175,7 @@ func (f *Futures) GetHistoricalIndex(market string, params *models.GetHistorical

request, err := f.client.prepareRequest(Request{
Method: http.MethodGet,
URL: fmt.Sprintf("%s%s", apiUrl, fmt.Sprintf(apiIndexCandles, market)),
URL: fmt.Sprintf("%s%s", f.client.apiURL, fmt.Sprintf(apiIndexCandles, market)),
Params: queryParams,
})
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions markets.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Markets struct {
func (m *Markets) GetMarkets() ([]*models.Market, error) {
request, err := m.client.prepareRequest(Request{
Method: http.MethodGet,
URL: fmt.Sprintf("%s%s", apiUrl, apiGetMarkets),
URL: fmt.Sprintf("%s%s", m.client.apiURL, apiGetMarkets),
})
if err != nil {
return nil, errors.WithStack(err)
Expand All @@ -47,7 +47,7 @@ func (m *Markets) GetMarkets() ([]*models.Market, error) {
func (m *Markets) GetMarketByName(name string) (*models.Market, error) {
request, err := m.client.prepareRequest(Request{
Method: http.MethodGet,
URL: fmt.Sprintf("%s%s/%s", apiUrl, apiGetMarkets, name),
URL: fmt.Sprintf("%s%s/%s", m.client.apiURL, apiGetMarkets, name),
})
if err != nil {
return nil, errors.WithStack(err)
Expand Down Expand Up @@ -77,7 +77,7 @@ func (m *Markets) GetOrderBook(marketName string, depth *int) (*models.OrderBook

request, err := m.client.prepareRequest(Request{
Method: http.MethodGet,
URL: fmt.Sprintf("%s%s", apiUrl, path),
URL: fmt.Sprintf("%s%s", m.client.apiURL, path),
Params: params,
})
if err != nil {
Expand Down Expand Up @@ -107,7 +107,7 @@ func (m *Markets) GetTrades(marketName string, params *models.GetTradesParams) (
path := fmt.Sprintf(apiGetTrades, marketName)
request, err := m.client.prepareRequest(Request{
Method: http.MethodGet,
URL: fmt.Sprintf("%s%s", apiUrl, path),
URL: fmt.Sprintf("%s%s", m.client.apiURL, path),
Params: queryParams,
})
if err != nil {
Expand Down Expand Up @@ -137,7 +137,7 @@ func (m *Markets) GetHistoricalPrices(marketName string, params *models.GetHisto
path := fmt.Sprintf(apiGetHistoricalPrices, marketName)
request, err := m.client.prepareRequest(Request{
Method: http.MethodGet,
URL: fmt.Sprintf("%s%s", apiUrl, path),
URL: fmt.Sprintf("%s%s", m.client.apiURL, path),
Params: queryParams,
})
if err != nil {
Expand Down
Loading