Skip to content

Commit

Permalink
Fix bybit.Init for switching testnet
Browse files Browse the repository at this point in the history
  • Loading branch information
rluisr committed Sep 4, 2022
1 parent afbccc2 commit 12163b2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 33 deletions.
5 changes: 3 additions & 2 deletions pkg/adapter/controllers/tv_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ type TVController struct {
Interactor usecase.TVInteractor
}

func NewTVController(rwDB, roDB *gorm.DB) *TVController {
func NewTVController(rwDB, roDB *gorm.DB, httpClient *http.Client) *TVController {
return &TVController{
Interactor: usecase.TVInteractor{
TVRepository: &gateway.TVRepository{
RWDB: rwDB,
RODB: roDB,
},
BybitRepository: &gateway.BybitRepository{
Client: nil,
Client: nil,
HTTPClient: httpClient,
},
},
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/adapter/gateway/bybit_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ type (
BaseURL string
APIKey string
APISecretKey string
HTTPClient *http.Client
Client *rest.ByBit
}
)

func (r *BybitRepository) Set(req domain.TV) {
r.Client, r.BaseURL = bybit.Init(req)
r.Client, r.BaseURL = bybit.Init(req, r.HTTPClient)
r.APIKey = req.APIKey
r.APISecretKey = req.APISecretKey
}
Expand Down Expand Up @@ -273,15 +274,14 @@ func (r *BybitRepository) signedRequestWithHeader(method, path string, body []by

url := fmt.Sprintf("%s%s", r.BaseURL, path)

client := &http.Client{}
req, _ := http.NewRequest(method, url, payload)
req.Header.Add("X-BAPI-API-KEY", r.APIKey)
req.Header.Add("X-BAPI-SIGN", crypto.HexEncodeToString(hmacSigned))
req.Header.Add("X-BAPI-SIGN-TYPE", "2")
req.Header.Add("X-BAPI-TIMESTAMP", nowTimeInMilli)
req.Header.Add("X-BAPI-RECV-WINDOW", "5000")

resp, err := client.Do(req)
resp, err := r.HTTPClient.Do(req)
if err != nil {
return "", err
}
Expand Down
24 changes: 4 additions & 20 deletions pkg/external/bybit/bybit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package bybit

import (
"net"
"net/http"
"time"

"github.com/frankrap/bybit-api/rest"
"github.com/rluisr/tvbit-bot/pkg/domain"
)

var BaseURL = "https://api.bybit.com/"
var BaseURL string

func Init(req domain.TV) (*rest.ByBit, string) {
func Init(req domain.TV, httpClient *http.Client) (*rest.ByBit, string) {
if req.IsTestNet {
BaseURL = "https://api-testnet.bybit.com/"
}

httpClient := &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 128,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
Timeout: 60 * time.Second,
} else {
BaseURL = "https://api.bybit.com/"
}

return rest.New(httpClient, BaseURL, req.APIKey, req.APISecretKey, false), BaseURL
Expand Down
10 changes: 3 additions & 7 deletions pkg/external/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func cron() {
Verbose: true,
})

task.Task("* * * * *", func(ctx context.Context) (int, error) {
task.Task("0 * * * *", func(ctx context.Context) (int, error) {
settings, err := tvController.Interactor.TVRepository.GetSettings()
if err != nil {
return 0, err
Expand Down Expand Up @@ -79,15 +79,11 @@ func cron() {
if err != nil {
return 1, err
}

balance = decimal.NewFromFloat(bybitDerivWallet.Equity)
totalRPL = decimal.NewFromFloat(bybitDerivWallet.CumRealisedPnl)

walletHistories = append(walletHistories, domain.WalletHistory{
SettingID: setting.ID,
Type: "usdt",
Balance: balance,
TotalRPL: totalRPL,
Balance: decimal.NewFromFloat(bybitDerivWallet.Equity),
TotalRPL: decimal.NewFromFloat(bybitDerivWallet.CumRealisedPnl),
})
}
}
Expand Down
45 changes: 45 additions & 0 deletions pkg/external/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
*
* tvbit-bot
* Copyright (C) 2022 rluisr(Takuya Hasegawa)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* /
*/

package external

import (
"net"
"net/http"
"time"
)

func NewHTTPClient() *http.Client {
return &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 128,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
Timeout: 60 * time.Second,
}
}
3 changes: 2 additions & 1 deletion pkg/external/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func Init() (err error) {
return err
}

tvController = controllers.NewTVController(rwDB, roDB)
httpClient := NewHTTPClient()
tvController = controllers.NewTVController(rwDB, roDB, httpClient)
settingController = controllers.NewSettingController(rwDB, roDB)

return nil
Expand Down

0 comments on commit 12163b2

Please sign in to comment.