Skip to content

Commit

Permalink
Merge branch 'master' into coinbase_api_revamp
Browse files Browse the repository at this point in the history
  • Loading branch information
cranktakular committed Jan 14, 2025
2 parents 0b3de87 + 4c7f48a commit 2350629
Show file tree
Hide file tree
Showing 39 changed files with 993 additions and 1,012 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/proto-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
- uses: bufbuild/buf-setup-action@v1.48.0
- uses: bufbuild/buf-setup-action@v1.49.0

- name: buf generate
working-directory: ./gctrpc
Expand Down
42 changes: 42 additions & 0 deletions .github/workflows/stale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: stale-checks
on:
schedule:
- cron: '0 0 * * 1-5'
workflow_dispatch:

permissions:
contents: read

env:
DAYS_BEFORE_STALE: ${{ vars.DAYS_BEFORE_STALE }}
DAYS_BEFORE_CLOSE: ${{ vars.DAYS_BEFORE_CLOSE }}
EXEMPT_ISSUE_LABELS: ${{ vars.EXEMPT_ISSUE_LABELS }}
EXEMPT_PR_LABELS: ${{ vars.EXEMPT_PR_LABELS }}

jobs:
stale:
name: Stale issues and PRs check
runs-on: ubuntu-latest
environment: ci
permissions:
issues: write
pull-requests: write
steps:
- uses: actions/stale@v9
with:
# General settings
days-before-stale: ${{ env.DAYS_BEFORE_STALE }}
days-before-close: ${{ env.DAYS_BEFORE_CLOSE }}
exempt-issue-labels: ${{ env.EXEMPT_ISSUE_LABELS }}
exempt-pr-labels: ${{ env.EXEMPT_PR_LABELS }}
enable-statistics: true

# Issue settings
stale-issue-message: 'This issue is stale because it has been open ${{ env.DAYS_BEFORE_STALE }} days with no activity. Please provide an update or this issue will be automatically closed in ${{ env.DAYS_BEFORE_CLOSE }} days.'
close-issue-message: 'This issue was closed because it has been stalled for ${{ env.DAYS_BEFORE_CLOSE }} days with no activity.'
stale-issue-label: 'stale'

# PR settings
stale-pr-message: 'This PR is stale because it has been open ${{ env.DAYS_BEFORE_STALE }} days with no activity. Please provide an update on the progress of this PR.'
days-before-pr-close: -1
stale-pr-label: 'stale'
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ issues:
- text: "Expect WriteFile permissions to be 0600 or less"
linters:
- gosec
- text: 'shadow: declaration of "err" shadows declaration at'
linters: [ govet ]


exclude-dirs:
- vendor
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ Binaries will be published once the codebase reaches a stable condition.

|User|Contribution Amount|
|--|--|
| [thrasher-](https://github.com/thrasher-) | 700 |
| [shazbert](https://github.com/shazbert) | 345 |
| [dependabot[bot]](https://github.com/apps/dependabot) | 317 |
| [gloriousCode](https://github.com/gloriousCode) | 234 |
| [gbjk](https://github.com/gbjk) | 93 |
| [thrasher-](https://github.com/thrasher-) | 703 |
| [shazbert](https://github.com/shazbert) | 355 |
| [dependabot[bot]](https://github.com/apps/dependabot) | 331 |
| [gloriousCode](https://github.com/gloriousCode) | 236 |
| [gbjk](https://github.com/gbjk) | 107 |
| [dependabot-preview[bot]](https://github.com/apps/dependabot-preview) | 88 |
| [xtda](https://github.com/xtda) | 47 |
| [lrascao](https://github.com/lrascao) | 27 |
Expand Down
21 changes: 16 additions & 5 deletions cmd/documentation/exchanges_templates/deribit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,23 @@ if err != nil {
}
```

### How to do Websocket public/private calls
### Subscriptions

```go
// Exchanges will be abstracted out in further updates and examples will be
// supplied then
```
All default subscriptions are for all enabled assets.

Default Public Subscriptions:
- Candles ( Timeframe: 1 day )
- Orderbook ( Full depth @ Interval: 100ms )
- Ticker ( Interval: 100ms )
- All Trades ( Interval: 100ms )

Default Authenticated Subscriptions:
- My Account Orders
- My Account Trades

kline.Raw Interval configurable for a raw orderbook subscription when authenticated

Subscriptions are subject to enabled assets and pairs.

### Please click GoDocs chevron above to view current GoDoc information for this package
{{template "contributions"}}
Expand Down
3 changes: 3 additions & 0 deletions cmd/documentation/exchanges_templates/kucoin.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Default Authenticated Subscriptions:

Subscriptions are subject to enabled assets and pairs.

Margin subscriptions for ticker, orderbook and All trades are merged into Spot subscriptions because duplicates are not allowed,
unless Spot subscription does not exist, i.e. Spot asset not enabled, or subscription configured only for Margin

Limitations:
- 100 symbols per subscription
- 300 symbols per connection
Expand Down
9 changes: 5 additions & 4 deletions currency/pairs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
"strings"
)

// Public Errors
var (
ErrPairDuplication = errors.New("currency pair duplication")
)

var (
errSymbolEmpty = errors.New("symbol is empty")
errNoDelimiter = errors.New("no delimiter was supplied")
errPairFormattingInconsistent = errors.New("pair formatting is inconsistent")

// ErrPairDuplication defines an error when there is multiple of the same
// currency pairs found.
ErrPairDuplication = errors.New("currency pair duplication")
)

// NewPairsFromStrings takes in currency pair strings and returns a currency
Expand Down
74 changes: 37 additions & 37 deletions engine/currency_state_manager.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# GoCryptoTrader package Currency state manager

<img src="/common/gctlogo.png?raw=true" width="350px" height="350px" hspace="70">


[![Build Status](https://github.com/thrasher-corp/gocryptotrader/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/thrasher-corp/gocryptotrader/actions/workflows/tests.yml)
[![Software License](https://img.shields.io/badge/License-MIT-orange.svg?style=flat-square)](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
[![GoDoc](https://godoc.org/github.com/thrasher-corp/gocryptotrader?status.svg)](https://godoc.org/github.com/thrasher-corp/gocryptotrader/engine/currency_state_manager)
[![Coverage Status](https://codecov.io/gh/thrasher-corp/gocryptotrader/graph/badge.svg?token=41784B23TS)](https://codecov.io/gh/thrasher-corp/gocryptotrader)
[![Go Report Card](https://goreportcard.com/badge/github.com/thrasher-corp/gocryptotrader)](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)


This currency_state_manager package is part of the GoCryptoTrader codebase.

## This is still in active development

You can track ideas, planned features and what's in progress on our [GoCryptoTrader Kanban board](https://github.com/orgs/thrasher-corp/projects/3).

Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
# GoCryptoTrader package Currency state manager

<img src="/common/gctlogo.png?raw=true" width="350px" height="350px" hspace="70">


[![Build Status](https://github.com/thrasher-corp/gocryptotrader/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/thrasher-corp/gocryptotrader/actions/workflows/tests.yml)
[![Software License](https://img.shields.io/badge/License-MIT-orange.svg?style=flat-square)](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
[![GoDoc](https://godoc.org/github.com/thrasher-corp/gocryptotrader?status.svg)](https://godoc.org/github.com/thrasher-corp/gocryptotrader/engine/currency_state_manager)
[![Coverage Status](https://codecov.io/gh/thrasher-corp/gocryptotrader/graph/badge.svg?token=41784B23TS)](https://codecov.io/gh/thrasher-corp/gocryptotrader)
[![Go Report Card](https://goreportcard.com/badge/github.com/thrasher-corp/gocryptotrader)](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)


This currency_state_manager package is part of the GoCryptoTrader codebase.

## This is still in active development

You can track ideas, planned features and what's in progress on our [GoCryptoTrader Kanban board](https://github.com/orgs/thrasher-corp/projects/3).

Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)

## Current Features for Currency state manager
+ The state manager keeps currency states up to date, which include:
Expand All @@ -27,22 +27,22 @@ Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader
+ This allows for an internal state check to compliment internal and external
strategies.


## Contribution

Please feel free to submit any pull requests or suggest any desired features to be added.

When submitting a PR, please abide by our coding guidelines:

+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
+ Pull requests need to be based on and opened against the `master` branch.

## Donations

<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">

If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:


## Contribution

Please feel free to submit any pull requests or suggest any desired features to be added.

When submitting a PR, please abide by our coding guidelines:

+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
+ Pull requests need to be based on and opened against the `master` branch.

## Donations

<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">

If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:

***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
5 changes: 2 additions & 3 deletions engine/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ var (
errGRPCShutdownSignalIsNil = errors.New("cannot shutdown, gRPC shutdown channel is nil")
errInvalidStrategy = errors.New("invalid strategy")
errSpecificPairNotEnabled = errors.New("specified pair is not enabled")
errPairNotEnabled = errors.New("pair is not enabled")
)

// RPCServer struct
Expand Down Expand Up @@ -4722,7 +4721,7 @@ func (s *RPCServer) GetFundingRates(ctx context.Context, r *gctrpc.GetFundingRat
}

if !pairs.Contains(cp, true) {
return nil, fmt.Errorf("%w %v", errPairNotEnabled, cp)
return nil, fmt.Errorf("%w %v", currency.ErrPairNotEnabled, cp)
}

funding, err := exch.GetHistoricalFundingRates(ctx, &fundingrate.HistoricalRatesRequest{
Expand Down Expand Up @@ -4820,7 +4819,7 @@ func (s *RPCServer) GetLatestFundingRate(ctx context.Context, r *gctrpc.GetLates
}

if !pairs.Contains(cp, true) {
return nil, fmt.Errorf("%w %v", errPairNotEnabled, cp)
return nil, fmt.Errorf("%w %v", currency.ErrPairNotEnabled, cp)
}

fundingRates, err := exch.GetLatestFundingRates(ctx, &fundingrate.LatestRateRequest{
Expand Down
2 changes: 1 addition & 1 deletion exchanges/btse/btse_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ func (b *BTSE) UpdateOrderExecutionLimits(ctx context.Context, a asset.Item) err
var errs error
limits := make([]order.MinMaxLevel, 0, len(summary))
for _, marketInfo := range summary {
p, err := marketInfo.Pair() //nolint:govet // Deliberately shadow err
p, err := marketInfo.Pair()
if err != nil {
errs = common.AppendError(err, fmt.Errorf("%s: %w", p, err))
continue
Expand Down
67 changes: 44 additions & 23 deletions exchanges/bybit/bybit.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ import (
// Bybit is the overarching type across this package
type Bybit struct {
exchange.Base

// AccountType holds information about whether the account to which the api key belongs is a unified margin account or not.
// 0: unified, and 1: for normal account
AccountType int64
account accountTypeHolder
}

const (
Expand All @@ -45,8 +42,8 @@ const (

cSpot, cLinear, cOption, cInverse = "spot", "linear", "option", "inverse"

accountTypeNormal = 0 // 0: regular account
accountTypeUnified = 1 // 1: unified trade account
accountTypeNormal AccountType = 1
accountTypeUnified AccountType = 2

longDatedFormat = "02Jan06"
)
Expand Down Expand Up @@ -1176,8 +1173,9 @@ func (by *Bybit) GetPreUpgradeOrderHistory(ctx context.Context, category, symbol
if err != nil {
return nil, err
}
if by.AccountType == accountTypeNormal {
return nil, errAPIKeyIsNotUnified
err = by.RequiresUnifiedAccount(ctx)
if err != nil {
return nil, err
}
var resp *TradeOrders
return resp, by.SendAuthHTTPRequestV5(ctx, exchange.RestSpot, http.MethodGet, "/v5/pre-upgrade/order/history", params, nil, &resp, defaultEPL)
Expand All @@ -1189,8 +1187,9 @@ func (by *Bybit) GetPreUpgradeTradeHistory(ctx context.Context, category, symbol
if err != nil {
return nil, err
}
if by.AccountType == accountTypeNormal {
return nil, errAPIKeyIsNotUnified
err = by.RequiresUnifiedAccount(ctx)
if err != nil {
return nil, err
}
if executionType != "" {
params.Set("executionType", executionType)
Expand All @@ -1205,8 +1204,9 @@ func (by *Bybit) GetPreUpgradeClosedPnL(ctx context.Context, category, symbol, c
if err != nil {
return nil, err
}
if by.AccountType == accountTypeNormal {
return nil, errAPIKeyIsNotUnified
err = by.RequiresUnifiedAccount(ctx)
if err != nil {
return nil, err
}
var resp *ClosedProfitAndLossResponse
return resp, by.SendAuthHTTPRequestV5(ctx, exchange.RestSpot, http.MethodGet, "/v5/pre-upgrade/position/closed-pnl", params, nil, &resp, defaultEPL)
Expand All @@ -1218,8 +1218,9 @@ func (by *Bybit) GetPreUpgradeTransactionLog(ctx context.Context, category, base
if err != nil {
return nil, err
}
if by.AccountType == accountTypeNormal {
return nil, errAPIKeyIsNotUnified
err = by.RequiresUnifiedAccount(ctx)
if err != nil {
return nil, err
}
if transactionType != "" {
params.Set("type", transactionType)
Expand All @@ -1234,8 +1235,9 @@ func (by *Bybit) GetPreUpgradeOptionDeliveryRecord(ctx context.Context, category
if err != nil {
return nil, err
}
if by.AccountType == accountTypeNormal {
return nil, errAPIKeyIsNotUnified
err = by.RequiresUnifiedAccount(ctx)
if err != nil {
return nil, err
}
if !expiryDate.IsZero() {
params.Set("expData", expiryDate.Format(longDatedFormat))
Expand All @@ -1250,8 +1252,9 @@ func (by *Bybit) GetPreUpgradeUSDCSessionSettlement(ctx context.Context, categor
if err != nil {
return nil, err
}
if by.AccountType == accountTypeNormal {
return nil, errAPIKeyIsNotUnified
err = by.RequiresUnifiedAccount(ctx)
if err != nil {
return nil, err
}
var resp *SettlementSession
return resp, by.SendAuthHTTPRequestV5(ctx, exchange.RestSpot, http.MethodGet, "/v5/pre-upgrade/asset/settlement-record", params, nil, &resp, defaultEPL)
Expand Down Expand Up @@ -2699,13 +2702,31 @@ func getSign(sign, secret string) (string, error) {
return crypto.HexEncodeToString(hmacSigned), nil
}

// RetrieveAndSetAccountType retrieve to set account type information
func (by *Bybit) RetrieveAndSetAccountType(ctx context.Context) error {
accInfo, err := by.GetAPIKeyInformation(ctx)
// FetchAccountType if not set fetches the account type from the API, stores it and returns it. Else returns the stored account type.
func (by *Bybit) FetchAccountType(ctx context.Context) (AccountType, error) {
by.account.m.Lock()
defer by.account.m.Unlock()
if by.account.accountType == 0 {
accInfo, err := by.GetAPIKeyInformation(ctx)
if err != nil {
return 0, err
}
// From endpoint 0:regular account; 1:unified trade account
// + 1 to make it 1 and 2 so that a zero value can be used to check if the account type has been set or not.
by.account.accountType = AccountType(accInfo.IsUnifiedTradeAccount + 1)
}
return by.account.accountType, nil
}

// RequiresUnifiedAccount checks account type and returns error if not unified
func (by *Bybit) RequiresUnifiedAccount(ctx context.Context) error {
at, err := by.FetchAccountType(ctx)
if err != nil {
return err
return nil //nolint:nilerr // if we can't get the account type, we can't check if it's unified or not, fail on call
}
if at != accountTypeUnified {
return fmt.Errorf("%w, account type: %s", errAPIKeyIsNotUnified, at)
}
by.AccountType = accInfo.IsUnifiedTradeAccount // 0:regular account; 1:unified trade account
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions exchanges/bybit/bybit_live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestMain(m *testing.M) {
}

if b.API.AuthenticatedSupport {
if err := b.RetrieveAndSetAccountType(context.Background()); err != nil {
log.Printf("%s unable to RetrieveAndSetAccountType: %v", b.Name, err)
if _, err := b.FetchAccountType(context.Background()); err != nil {
log.Printf("%s unable to FetchAccountType: %v", b.Name, err)
}
}

Expand Down
Loading

0 comments on commit 2350629

Please sign in to comment.