Compare commits

..

No commits in common. "main" and "VictorWuBranche" have entirely different histories.

26 changed files with 106 additions and 80 deletions

28
.github/workflows/go.yml vendored Normal file
View File

@ -0,0 +1,28 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
name: Go
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: Build
run: go build -v ./...
- name: Test
run: go test -v ./...

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

9
.idea/bybit.go.api.iml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/bybit.go.api.iml" filepath="$PROJECT_DIR$/.idea/bybit.go.api.iml" />
</modules>
</component>
</project>

7
.idea/vcs.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -2,9 +2,8 @@ package bybit_connector
import ( import (
"context" "context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http" "net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
) )
type AccountClient struct { type AccountClient struct {

View File

@ -2,9 +2,8 @@ package bybit_connector
import ( import (
"context" "context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http" "net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
) )
type AssetClient struct { type AssetClient struct {
@ -472,7 +471,6 @@ func (s *AssetClient) GetWithdrawalAmount(ctx context.Context, opts ...RequestOp
} }
return res, nil return res, nil
} }
func (s *AssetClient) GetWithdrawalRecords(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { func (s *AssetClient) GetWithdrawalRecords(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
r := &request{ r := &request{
method: http.MethodGet, method: http.MethodGet,

View File

@ -2,9 +2,8 @@ package bybit_connector
import ( import (
"context" "context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http" "net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
) )
type BrokerServiceClient struct { type BrokerServiceClient struct {

View File

@ -14,9 +14,9 @@ import (
"strconv" "strconv"
"time" "time"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
"github.com/bitly/go-simplejson" "github.com/bitly/go-simplejson"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"github.com/wuhewuhe/bybit.go.api/handlers"
) )
var json = jsoniter.ConfigCompatibleWithStandardLibrary var json = jsoniter.ConfigCompatibleWithStandardLibrary
@ -199,7 +199,9 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
c.debug("response status code: %d", res.StatusCode) c.debug("response status code: %d", res.StatusCode)
if res.StatusCode >= http.StatusBadRequest { if res.StatusCode >= http.StatusBadRequest {
apiErr := new(handlers.APIError) var (
apiErr = new(handlers.APIError)
)
e := json.Unmarshal(data, apiErr) e := json.Unmarshal(data, apiErr)
if e != nil { if e != nil {
c.debug("failed to unmarshal json: %s", e) c.debug("failed to unmarshal json: %s", e)
@ -308,6 +310,7 @@ func (c *Client) NewPositionService(params map[string]interface{}) *PositionClie
c: c, c: c,
params: params, params: params,
} }
} }
func (c *Client) NewPreUpgradeService(params map[string]interface{}) *PreUpgradeClient { func (c *Client) NewPreUpgradeService(params map[string]interface{}) *PreUpgradeClient {

View File

@ -5,30 +5,28 @@ import (
"crypto/hmac" "crypto/hmac"
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"errors"
"fmt" "fmt"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"golang.org/x/sync/errgroup"
) )
type MessageHandler func(message string) error type MessageHandler func(message string) error
func (b *WebSocket) handleIncomingMessages() error { func (b *WebSocket) handleIncomingMessages() {
for { for {
_, message, err := b.conn.ReadMessage() _, message, err := b.conn.ReadMessage()
if err != nil { if err != nil {
fmt.Println("Error reading:", err) fmt.Println("Error reading:", err)
return err return
} }
if b.onMessage != nil { if b.onMessage != nil {
err := b.onMessage(string(message)) err := b.onMessage(string(message))
if err != nil { if err != nil {
fmt.Println("Error handling message:", err) fmt.Println("Error handling message:", err)
return err return
} }
} }
} }
@ -114,49 +112,35 @@ func (b *WebSocket) Connect(args []string) error {
} }
} }
eg := errgroup.Group{} go b.handleIncomingMessages()
b.ctx, b.cancel = context.WithCancel(context.Background()) b.ctx, b.cancel = context.WithCancel(context.Background())
Ping(b)
eg.Go(b.Ping) return b.sendSubscription(args)
eg.Go(b.handleIncomingMessages)
if err := b.sendSubscription(args); err != nil {
return fmt.Errorf("failed to send subscription: %w", err)
}
if err := eg.Wait(); err != nil {
b.Disconnect()
return fmt.Errorf("failed to handle message: %w", err)
}
return nil
} }
var ErrPingFailed = errors.New("failed to send ping") func Ping(b *WebSocket) {
func (b *WebSocket) Ping() error {
ticker := time.NewTicker(time.Duration(b.pingInterval) * time.Second) ticker := time.NewTicker(time.Duration(b.pingInterval) * time.Second)
defer ticker.Stop() // Ensure the ticker is stopped when this goroutine ends go func() {
for { defer ticker.Stop() // Ensure the ticker is stopped when this goroutine ends
select { for {
case <-ticker.C: // Wait until the ticker sends a signal select {
if err := b.conn.WriteMessage(websocket.PingMessage, nil); err != nil { case <-ticker.C: // Wait until the ticker sends a signal
fmt.Println("Failed to send ping:", err) if err := b.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
return ErrPingFailed fmt.Println("Failed to send ping:", err)
}
case <-b.ctx.Done():
fmt.Println("Exit ping")
return
} }
case <-b.ctx.Done():
fmt.Println("Exit ping")
return nil
} }
} }()
} }
func (b *WebSocket) Disconnect() error { func (b *WebSocket) Disconnect() error {
b.cancel() b.cancel()
err := b.conn.Close() return b.conn.Close()
fmt.Println("WebSocket disconnected, err =", err)
return err
} }
func (b *WebSocket) Send(message string) error { func (b *WebSocket) Send(message string) error {

View File

@ -3,8 +3,7 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
bybit "github.com/wuhewuhe/bybit.go.api"
bybit "gitea.computernetthings.ru/yash/bybit.go.api"
) )
func main() { func main() {

View File

@ -3,8 +3,7 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
bybit "github.com/wuhewuhe/bybit.go.api"
bybit "gitea.computernetthings.ru/yash/bybit.go.api"
) )
func main() { func main() {

View File

@ -3,8 +3,7 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
bybit "github.com/wuhewuhe/bybit.go.api"
bybit "gitea.computernetthings.ru/yash/bybit.go.api"
) )
func main() { func main() {

View File

@ -3,8 +3,7 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
bybit "github.com/wuhewuhe/bybit.go.api"
bybit "gitea.computernetthings.ru/yash/bybit.go.api"
) )
func main() { func main() {
@ -13,8 +12,7 @@ func main() {
func PlaceBatchTrade() { func PlaceBatchTrade() {
client := bybit.NewBybitHttpClient("8wYkmpLsMg10eNQyPm", "Ouxc34myDnXvei54XsBZgoQzfGxO4bkr2Zsj", bybit.WithBaseURL(bybit.TESTNET)) client := bybit.NewBybitHttpClient("8wYkmpLsMg10eNQyPm", "Ouxc34myDnXvei54XsBZgoQzfGxO4bkr2Zsj", bybit.WithBaseURL(bybit.TESTNET))
params := map[string]interface{}{ params := map[string]interface{}{"category": "option",
"category": "option",
"request": []map[string]interface{}{ "request": []map[string]interface{}{
{ {
"category": "option", "category": "option",

View File

@ -3,8 +3,7 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
bybit "github.com/wuhewuhe/bybit.go.api"
bybit "gitea.computernetthings.ru/yash/bybit.go.api"
) )
func main() { func main() {

3
go.mod
View File

@ -1,4 +1,4 @@
module gitea.computernetthings.ru/yash/bybit.go.api module github.com/wuhewuhe/bybit.go.api
go 1.21 go 1.21
@ -8,7 +8,6 @@ require (
github.com/gorilla/websocket v1.5.1 github.com/gorilla/websocket v1.5.1
github.com/json-iterator/go v1.1.12 github.com/json-iterator/go v1.1.12
github.com/stretchr/testify v1.8.4 github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.7.0
) )
require ( require (

3
go.sum
View File

@ -10,6 +10,7 @@ github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@ -30,8 +31,6 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -2,9 +2,8 @@ package bybit_connector
import ( import (
"context" "context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http" "net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
) )
type LendingServiceClient struct { type LendingServiceClient struct {

View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/models" "github.com/wuhewuhe/bybit.go.api/models"
) )
// MarketKlinesService Market Kline (GET /v5/market/kline) // MarketKlinesService Market Kline (GET /v5/market/kline)

View File

@ -4,8 +4,8 @@ import (
"net/http" "net/http"
"testing" "testing"
"gitea.computernetthings.ru/yash/bybit.go.api/models"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/wuhewuhe/bybit.go.api/models"
) )
type marketTestSuite struct { type marketTestSuite struct {
@ -128,7 +128,6 @@ func (s *marketTestSuite) assertMarketKlineEqual(expected, actual *models.Market
r.Equal(len(expected.List), len(actual.List), "List") r.Equal(len(expected.List), len(actual.List), "List")
r.Equal(expected.List, actual.List) r.Equal(expected.List, actual.List)
} }
func (s *marketTestSuite) TestMarketMarkPriceKline() { func (s *marketTestSuite) TestMarketMarkPriceKline() {
data := []byte(`{ data := []byte(`{
"retCode": 0, "retCode": 0,
@ -1297,7 +1296,6 @@ func (s *marketTestSuite) assertMarketLongShortRatioInfoEqual(expected, actual *
r := s.r() r := s.r()
r.Equal(expected.List, actual.List, "List") r.Equal(expected.List, actual.List, "List")
} }
func (s *marketTestSuite) TestGetServerTime() { func (s *marketTestSuite) TestGetServerTime() {
data := []byte(`{ data := []byte(`{
"retCode": 0, "retCode": 0,

View File

@ -2,9 +2,8 @@ package bybit_connector
import ( import (
"context" "context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http" "net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
) )
type PositionClient struct { type PositionClient struct {

View File

@ -2,9 +2,8 @@ package bybit_connector
import ( import (
"context" "context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http" "net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
) )
type PreUpgradeClient struct { type PreUpgradeClient struct {

View File

@ -2,9 +2,8 @@ package bybit_connector
import ( import (
"context" "context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http" "net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
) )
type SpotLeverageClient struct { type SpotLeverageClient struct {

View File

@ -3,9 +3,8 @@ package bybit_connector
import ( import (
"context" "context"
"errors" "errors"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http" "net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
) )
type SpotMarginClient struct { type SpotMarginClient struct {

View File

@ -2,10 +2,9 @@ package bybit_connector
import ( import (
"context" "context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"github.com/wuhewuhe/bybit.go.api/models"
"net/http" "net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
"gitea.computernetthings.ru/yash/bybit.go.api/models"
) )
type TradeClient struct { type TradeClient struct {

View File

@ -2,9 +2,8 @@ package bybit_connector
import ( import (
"context" "context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http" "net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
) )
type UserServiceClient struct { type UserServiceClient struct {