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

websocket/gateio: Add request functions for websocket multi-connection [SPOT] #1598

Merged
merged 173 commits into from
Dec 20, 2024
Merged
Changes from 6 commits
Commits
Show all changes
173 commits
Select commit Hold shift + click to select a range
64c24ab
gateio: Add multi asset websocket support WIP.
Jul 14, 2024
f509399
meow
Jul 14, 2024
feed04e
Add tests and shenanigans
Jul 15, 2024
31a26c0
integrate flushing and for enabling/disabling pairs from rpc shenanigans
Jul 15, 2024
e1f2f7a
some changes
Jul 15, 2024
76524cc
linter: fixes strikes again.
Jul 15, 2024
640e82e
Change name ConnectionAssociation -> ConnectionCandidate for better c…
Jul 15, 2024
3a0440d
Add subscription tests (state functional)
Jul 16, 2024
c7d2b62
glorious:nits + proxy handling
Jul 16, 2024
fc281ee
Spelling
Jul 16, 2024
eaa44ba
linter: fixerino
Jul 16, 2024
a8debf9
instead of nil, dont do nil.
Jul 16, 2024
16b0e22
clean up nils
Jul 16, 2024
32252b2
cya nils
Jul 16, 2024
7c5d9c3
don't need to set URL or check if its running
Jul 17, 2024
2f93b64
stream match update
Jul 17, 2024
dd94e4e
update tests
Jul 17, 2024
ca597f3
linter: fix
Jul 17, 2024
2e0f0ae
glorious: nits + handle context cancellations
Jul 18, 2024
2d2f872
stop ping handler routine leak
Jul 19, 2024
39191c8
* Fix bug where reader routine on error that is not a disconnection e…
Jul 19, 2024
f1c3895
Allow rollback on connect on any error across all connections
Jul 19, 2024
09bff6c
fix shadow jutsu
Jul 19, 2024
e66c9be
glorious/gk: nitters - adds in ws mock server
Jul 22, 2024
03de669
linter: fix
Jul 22, 2024
eddb58b
Merge branch 'master' into gateio_ws
Jul 23, 2024
8159b05
fix deadlock on connection as the previous channel had no reader and …
Jul 24, 2024
4f0b42f
Merge branch 'master' into gateio_ws
Jul 24, 2024
2af934e
Merge branch 'master' into stream_match
Jul 24, 2024
0e3bb31
glorious: whooops
Jul 24, 2024
f98c3aa
gk: nits
Jul 24, 2024
d89a46a
Leak issue and edge case
Jul 25, 2024
8281b88
Websocket: Add SendMessageReturnResponses
gbjk Apr 8, 2024
a38a1d1
whooooooopsie
Jul 25, 2024
77ef366
gk: nitssssss
Jul 26, 2024
6341ccf
Update exchanges/stream/stream_match.go
shazbert Jul 26, 2024
c0d1d43
Update exchanges/stream/stream_match_test.go
shazbert Jul 26, 2024
9e57e65
linter: appease the linter gods
Jul 26, 2024
51b9f17
gk: nits
Jul 26, 2024
6376a12
gk: drain brain
Jul 26, 2024
f7adad2
started
Jul 28, 2024
9905686
more changes before merge match pr
Jul 31, 2024
d275e39
Merge branch 'master' into stream_match
Jul 31, 2024
8a64da1
Merge branch 'stream_match' into ws_outbound_req
Jul 31, 2024
9bb6ef4
gateio: still building out
Jul 31, 2024
3f3da46
gateio: finish spot
Jul 31, 2024
6b9c4ad
fix up tests in gateio
Jul 31, 2024
f7af44c
Add tests for stream package
Jul 31, 2024
3ed912b
rm unused field
Jul 31, 2024
ee8a35c
glorious: nits
Aug 1, 2024
ec33bd3
Merge branch 'stream_match' into ws_outbound_req
Aug 1, 2024
cc97528
rn files, specifically set function names to asset and offload routin…
Aug 3, 2024
66a8778
linter: fix
Aug 6, 2024
431c047
glorious: nits
Aug 15, 2024
7110e88
add counter and update gateio
Aug 16, 2024
67bb87d
Merge branch 'common_counter' into ws_outbound_req
Aug 16, 2024
b70296c
fix collision issue
Aug 16, 2024
d5bbd10
Merge branch 'master' into gateio_ws
Aug 19, 2024
d2f7c2e
Merge branch 'master' into gateio_ws
Aug 23, 2024
16d88cf
Merge branch 'master' into gateio_ws
Aug 24, 2024
9026bc8
Merge branch 'gateio_ws' into ws_outbound_req
Aug 24, 2024
0402bc7
Update exchanges/stream/websocket.go
shazbert Aug 30, 2024
0dfda95
glorious: nits
Aug 30, 2024
c58834e
add tests
Aug 30, 2024
d29893b
linter: fix
Aug 30, 2024
79e0eeb
Merge branch 'master' into gateio_ws
Aug 30, 2024
4f7bcd9
Merge branch 'master' into gateio_ws
Sep 2, 2024
45ff199
After merge
Sep 2, 2024
06acaac
Add error connection info
Sep 2, 2024
aa87e83
Merge branch 'gateio_ws' into ws_outbound_req
Sep 4, 2024
cf2e8d8
upgrade to upstream merge
Sep 5, 2024
af986da
Fix edge case where it does not reconnect made by an already closed c…
Sep 9, 2024
1171278
stream coverage
Sep 12, 2024
4be6214
glorious: nits
Sep 12, 2024
a068ddb
Merge branch 'master' into gateio_ws
Sep 12, 2024
81cba36
glorious: nits removed asset error handling in stream package
Sep 12, 2024
bdc6954
linter: fix
Sep 13, 2024
3fe44ca
rm block
Sep 13, 2024
818584f
Add basic readme
Sep 13, 2024
289ac71
Merge branch 'master' into gateio_ws
Sep 13, 2024
dee32a4
Merge branch 'master' into gateio_ws
Sep 13, 2024
7063311
Merge branch 'master' into gateio_ws
Sep 16, 2024
0709ba3
fix asset enabled flush cycle for multi connection
Sep 16, 2024
b877b38
spella: fix
Sep 16, 2024
f7d1ec8
linter: fix
Sep 18, 2024
818432d
Merge branch 'master' into gateio_ws
Sep 18, 2024
bdc7afb
Add glorious suggestions, fix some race thing
Sep 19, 2024
3d6541e
reinstate name before any routine gets spawned
Sep 19, 2024
33c4128
stop on error in mock tests
Sep 19, 2024
ea25763
glorious: nits
Sep 19, 2024
f15374a
Merge branch 'gateio_ws' into ws_outbound_req
Sep 22, 2024
ae92cd2
glorious: nits found in CI build
Sep 24, 2024
c49a75c
Add test for drain, bumped wait times as there seems to be something …
Sep 24, 2024
5551d54
mutex across shutdown and connect for protection
Sep 24, 2024
99c3c7b
lint: fix
Sep 24, 2024
f56cb78
test time withoffset, reinstate stop
Sep 24, 2024
b4bc7e2
fix whoops
Sep 24, 2024
ce4a5ce
const trafficCheckInterval; rm testmain
Sep 24, 2024
a745992
y
Sep 24, 2024
086fcc3
fix lint
Sep 24, 2024
a64e842
bump time check window
Sep 24, 2024
9e53313
stream: fix intermittant test failures while testing routines and rem…
Sep 25, 2024
10230b0
spells
Sep 25, 2024
4e02c4a
cant do what I did
Sep 25, 2024
7be80a0
protect race due to routine.
Sep 25, 2024
b51cf2f
update testURL
Sep 25, 2024
ff7ae03
use mock websocket connection instead of test URL's
Sep 25, 2024
1040153
linter: fix
Sep 25, 2024
f529bd2
remove url because its throwing errors on CI builds
Sep 25, 2024
5d0a7f7
connections drop all the time, don't need to worry about not being ab…
Sep 25, 2024
56cb431
remove another superfluous url thats not really set up for this
Sep 25, 2024
ca4999e
spawn overwatch routine when there is no errors, inline checker inste…
Sep 25, 2024
4240a0a
linter: fixerino uperino
Sep 26, 2024
8d6febc
glorious: panix
Sep 30, 2024
3df82ef
Merge branch 'master' into gateio_ws
Oct 1, 2024
7ff07d1
Merge branch 'master' into gateio_ws
Oct 1, 2024
3a24640
linter: things
Oct 1, 2024
1eef208
whoops
Oct 1, 2024
261b577
Merge branch 'master' into gateio_ws
Oct 1, 2024
dbd8acd
Merge branch 'gateio_ws' into ws_outbound_req
Oct 2, 2024
b528407
dont need to make consecutive Unix() calls
Oct 2, 2024
ee1cb94
websocket: fix potential panic on error and no responses and adding w…
Oct 3, 2024
9cdac76
Merge branch 'master' into ws_outbound_req
Oct 10, 2024
e5accaf
rm json parser and handle in json package instead
Oct 10, 2024
f124390
Merge branch 'master' into ws_outbound_req
Oct 23, 2024
d950b24
linter: fix
Oct 23, 2024
31339ed
linter: fix again
Oct 23, 2024
622c748
* change field name OutboundRequestSignature to WrapperDefinedConnect…
Oct 23, 2024
b71d3c0
spells and magic and wands
Oct 23, 2024
2bd207f
glorious: nits
Oct 25, 2024
511c78b
comparable check for signature
Oct 25, 2024
0a196b8
mv err var
Oct 25, 2024
f5ffe9f
Merge branch 'master' into ws_outbound_req
Nov 5, 2024
d3343a7
glorious: nits and stuff
Nov 5, 2024
56f483f
Merge branch 'master' into ws_outbound_req
Nov 7, 2024
2872e05
attempt to fix race
Nov 7, 2024
50e7253
Merge branch 'master' into ws_outbound_req
Nov 11, 2024
2b79a9c
glorious: nits
Nov 15, 2024
1696d08
gk: nits; engine log cleanup
Nov 20, 2024
cb2a235
gk: nits; OCD
Nov 20, 2024
78e974a
gk: nits; move function change file names
Nov 20, 2024
00a267d
gk: nits; :rocket:
Nov 20, 2024
72f97dd
gk: nits; convert variadic function and message inspection to interfa…
Nov 20, 2024
274ae8f
gk: nits; continued
Nov 20, 2024
25f00ce
gk: engine nits; rm loaded exchange
Nov 22, 2024
058b184
gk: nits; drop WebsocketLoginResponse
Nov 22, 2024
a87c438
stream: Add match method EnsureMatchWithData
Nov 22, 2024
6a2fbde
gk: nits; rn Inspect to IsFinal
Nov 22, 2024
cb96521
gk: nits; rn to MessageFilter
Nov 22, 2024
3417a55
linter: fix
Nov 23, 2024
03d9045
Merge branch 'master' into ws_outbound_req
Nov 26, 2024
70781cb
gateio: update rate limit definitions (cherry-pick)
Nov 27, 2024
6803a2a
Add test and missing
Nov 27, 2024
f8729d2
Merge branch 'master' into gateio_rate_limits
Dec 5, 2024
4baa7a2
Shared REST rate limit definitions with Websocket service, set lookup…
Dec 5, 2024
c0f0cd4
Merge branch 'master' into gateio_rate_limits
Dec 9, 2024
ede517b
Merge branch 'gateio_rate_limits' into ws_outbound_req
Dec 9, 2024
9c56cbb
integrate rate limits for websocket trading spot
Dec 9, 2024
b015319
Merge branch 'master' into ws_outbound_req
Dec 12, 2024
999a108
bitstamp: fix issue
Dec 12, 2024
c0acea2
glorious: nits
Dec 12, 2024
254ad2b
ch name and commentary
Dec 12, 2024
e803f77
fix bug add test
Dec 12, 2024
a8d30db
rm a thing
Dec 12, 2024
975a54c
fix test
Dec 12, 2024
1bdcd16
Update engine/engine.go
shazbert Dec 19, 2024
8f9e953
thrasher: nits
Dec 19, 2024
0d7b7db
Update exchanges/stream/stream_match_test.go
shazbert Dec 20, 2024
d164878
Update exchanges/stream/stream_match_test.go
shazbert Dec 20, 2024
544fbcc
GK: nits rn websocket functions
Dec 20, 2024
d07de5a
Merge branch 'master' into ws_outbound_req
Dec 20, 2024
df476e2
explicit function names for single to multi outbound orders
Dec 20, 2024
2182ddb
linter: fix
Dec 20, 2024
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
506 changes: 189 additions & 317 deletions exchanges/gateio/gateio.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion exchanges/gateio/gateio_test.go
Original file line number Diff line number Diff line change
@@ -333,7 +333,7 @@ func TestCreateBatchOrders(t *testing.T) {
func TestGetSpotOpenOrders(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, g)
if _, err := g.GateioSpotOpenOrders(context.Background(), 0, 0, false); err != nil {
if _, err := g.GetSpotOpenOrders(context.Background(), 0, 0, false); err != nil {
t.Errorf("%s GetSpotOpenOrders() error %v", g.Name, err)
}
}
10 changes: 4 additions & 6 deletions exchanges/gateio/gateio_websocket.go
Original file line number Diff line number Diff line change
@@ -24,16 +24,14 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
)

const (
gateioWebsocketEndpoint = "wss://api.gateio.ws/ws/v4/"
gateioWebsocketRateLimit = 120 * time.Millisecond
gateioWebsocketEndpoint = "wss://api.gateio.ws/ws/v4/"

spotPingChannel = "spot.ping"
spotPongChannel = "spot.pong"
@@ -90,7 +88,7 @@ func (g *Gateio) WsConnectSpot(ctx context.Context, conn stream.Connection) erro
if err != nil {
return err
}
conn.SetupPingHandler(request.Unset, stream.PingHandler{
conn.SetupPingHandler(websocketRateLimitNotNeededEPL, stream.PingHandler{
Websocket: true,
Delay: time.Second * 15,
Message: pingMessage,
@@ -645,7 +643,7 @@ func (g *Gateio) manageSubs(ctx context.Context, event string, conn stream.Conne
if err != nil {
return err
}
result, err := conn.SendMessageReturnResponse(ctx, request.Unset, msg.ID, msg)
result, err := conn.SendMessageReturnResponse(ctx, websocketRateLimitNotNeededEPL, msg.ID, msg)
if err != nil {
return err
}
@@ -756,7 +754,7 @@ func (g *Gateio) handleSubscription(ctx context.Context, conn stream.Connection,
}
var errs error
for k := range payloads {
result, err := conn.SendMessageReturnResponse(ctx, request.Unset, payloads[k].ID, payloads[k])
result, err := conn.SendMessageReturnResponse(ctx, websocketRateLimitNotNeededEPL, payloads[k].ID, payloads[k])
if err != nil {
errs = common.AppendError(errs, err)
continue
10 changes: 3 additions & 7 deletions exchanges/gateio/gateio_wrapper.go
Original file line number Diff line number Diff line change
@@ -151,7 +151,7 @@ func (g *Gateio) SetDefaults() {
}
g.Requester, err = request.New(g.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(GetRateLimit()),
request.WithLimiter(packageRateLimits),
)
if err != nil {
log.Errorln(log.ExchangeSys, err)
@@ -203,14 +203,14 @@ func (g *Gateio) Setup(exch *config.Exchange) error {
FillsFeed: g.Features.Enabled.FillsFeed,
TradeFeed: g.Features.Enabled.TradeFeed,
UseMultiConnectionManagement: true,
RateLimitDefinitions: packageRateLimits,
})
if err != nil {
return err
}
// Spot connection
err = g.Websocket.SetupNewConnection(&stream.ConnectionSetup{
URL: gateioWebsocketEndpoint,
RateLimit: request.NewWeightedRateLimitByDuration(gateioWebsocketRateLimit),
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
Handler: g.WsHandleSpotData,
@@ -228,7 +228,6 @@ func (g *Gateio) Setup(exch *config.Exchange) error {
// Futures connection - USDT margined
err = g.Websocket.SetupNewConnection(&stream.ConnectionSetup{
URL: futuresWebsocketUsdtURL,
RateLimit: request.NewWeightedRateLimitByDuration(gateioWebsocketRateLimit),
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
Handler: func(ctx context.Context, incoming []byte) error {
@@ -248,7 +247,6 @@ func (g *Gateio) Setup(exch *config.Exchange) error {
// Futures connection - BTC margined
err = g.Websocket.SetupNewConnection(&stream.ConnectionSetup{
URL: futuresWebsocketBtcURL,
RateLimit: request.NewWeightedRateLimitByDuration(gateioWebsocketRateLimit),
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
Handler: func(ctx context.Context, incoming []byte) error {
@@ -269,7 +267,6 @@ func (g *Gateio) Setup(exch *config.Exchange) error {
// Futures connection - Delivery - USDT margined
err = g.Websocket.SetupNewConnection(&stream.ConnectionSetup{
URL: deliveryRealUSDTTradingURL,
RateLimit: request.NewWeightedRateLimitByDuration(gateioWebsocketRateLimit),
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
Handler: func(ctx context.Context, incoming []byte) error {
@@ -289,7 +286,6 @@ func (g *Gateio) Setup(exch *config.Exchange) error {
// Futures connection - Options
return g.Websocket.SetupNewConnection(&stream.ConnectionSetup{
URL: optionsWebsocketURL,
RateLimit: request.NewWeightedRateLimitByDuration(gateioWebsocketRateLimit),
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
Handler: g.WsHandleOptionsData,
@@ -1664,7 +1660,7 @@ func (g *Gateio) GetActiveOrders(ctx context.Context, req *order.MultiOrderReque
switch req.AssetType {
case asset.Spot, asset.Margin, asset.CrossMargin:
var spotOrders []SpotOrdersDetail
spotOrders, err = g.GateioSpotOpenOrders(ctx, 0, 0, req.AssetType == asset.CrossMargin)
spotOrders, err = g.GetSpotOpenOrders(ctx, 0, 0, req.AssetType == asset.CrossMargin)
if err != nil {
return nil, err
}
449 changes: 407 additions & 42 deletions exchanges/gateio/ratelimiter.go
Original file line number Diff line number Diff line change
@@ -6,49 +6,414 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
)

// GateIO endpoints limits.
// GateIO endpoints limits. See: https://www.gate.io/docs/developers/apiv4/en/#frequency-limit-rule
const (
spotDefaultEPL request.EndpointLimit = iota
spotPrivateEPL
spotPlaceOrdersEPL
spotCancelOrdersEPL
perpetualSwapDefaultEPL
perpetualSwapPlaceOrdersEPL
perpetualSwapPrivateEPL
perpetualSwapCancelOrdersEPL
walletEPL
withdrawalEPL

// Request rates per interval

spotPublicRate = 900
spotPrivateRate = 900
spotPlaceOrdersRate = 10
spotCancelOrdersRate = 500
perpetualSwapPublicRate = 300
perpetualSwapPlaceOrdersRate = 100
perpetualSwapPrivateRate = 400
perpetualSwapCancelOrdersRate = 400
walletRate = 200
withdrawalRate = 1

// interval
oneSecondInterval = time.Second
threeSecondsInterval = time.Second * 3
publicTickersSpotEPL request.EndpointLimit = iota + 1
publicOrderbookSpotEPL
publicMarketTradesSpotEPL
publicCandleStickSpotEPL
publicCurrencyPairDetailSpotEPL
publicListCurrencyPairsSpotEPL
publicCurrenciesSpotEPL

publicCurrencyPairsMarginEPL
publicOrderbookMarginEPL

publicInsuranceDeliveryEPL
publicDeliveryContractsEPL
publicOrderbookDeliveryEPL
publicTradingHistoryDeliveryEPL
publicCandleSticksDeliveryEPL
publicTickersDeliveryEPL

publicFuturesContractsEPL
publicOrderbookFuturesEPL
publicTradingHistoryFuturesEPL
publicCandleSticksFuturesEPL
publicPremiumIndexEPL
publicTickersFuturesEPL
publicFundingRatesEPL
publicInsuranceFuturesEPL
publicStatsFuturesEPL
publicIndexConstituentsEPL
publicLiquidationHistoryEPL

publicUnderlyingOptionsEPL
publicExpirationOptionsEPL
publicContractsOptionsEPL
publicSettlementOptionsEPL
publicOrderbookOptionsEPL
publicTickerOptionsEPL
publicUnderlyingTickerOptionsEPL
publicCandleSticksOptionsEPL
publicMarkpriceCandleSticksOptionsEPL
publicTradeHistoryOptionsEPL

publicGetServerTimeEPL
publicFlashSwapEPL
publicListCurrencyChainEPL

walletDepositAddressEPL
walletWithdrawalRecordsEPL
walletDepositRecordsEPL
walletTransferCurrencyEPL
walletSubAccountTransferEPL
walletSubAccountTransferHistoryEPL
walletSubAccountToSubAccountTransferEPL
walletWithdrawStatusEPL
walletSubAccountBalancesEPL
walletSubAccountMarginBalancesEPL
walletSubAccountFuturesBalancesEPL
walletSubAccountCrossMarginBalancesEPL
walletSavedAddressesEPL
walletTradingFeeEPL
walletTotalBalanceEPL
walletWithdrawEPL
walletCancelWithdrawEPL

subAccountEPL

spotTradingFeeEPL
spotAccountsEPL
spotGetOpenOrdersEPL
spotClosePositionEPL
spotBatchOrdersEPL
spotPlaceOrderEPL
spotGetOrdersEPL
spotCancelAllOpenOrdersEPL
spotCancelBatchOrdersEPL
spotGetOrderEPL
spotAmendOrderEPL
spotCancelSingleOrderEPL
spotTradingHistoryEPL
spotCountdownCancelEPL
spotCreateTriggerOrderEPL
spotGetTriggerOrderListEPL
spotCancelTriggerOrdersEPL
spotGetTriggerOrderEPL
spotCancelTriggerOrderEPL

marginAccountListEPL
marginAccountBalanceEPL
marginFundingAccountListEPL
marginLendBorrowEPL
marginAllLoansEPL
marginMergeLendingLoansEPL
marginGetLoanEPL
marginModifyLoanEPL
marginCancelLoanEPL
marginRepayLoanEPL
marginListLoansEPL
marginRepaymentRecordEPL
marginSingleRecordEPL
marginModifyLoanRecordEPL
marginAutoRepayEPL
marginGetAutoRepaySettingsEPL
marginGetMaxTransferEPL
marginGetMaxBorrowEPL
marginSupportedCurrencyCrossListEPL
marginSupportedCurrencyCrossEPL
marginAccountsEPL
marginAccountHistoryEPL
marginCreateCrossBorrowLoanEPL
marginExecuteRepaymentsEPL
marginGetCrossMarginRepaymentsEPL
marginGetMaxTransferCrossEPL
marginGetMaxBorrowCrossEPL
marginGetCrossBorrowHistoryEPL
marginGetBorrowEPL

flashSwapOrderEPL
flashGetOrdersEPL
flashGetOrderEPL
flashOrderReviewEPL

privateUnifiedSpotEPL

perpetualAccountEPL
perpetualAccountBooksEPL
perpetualPositionsEPL
perpetualPositionEPL
perpetualUpdateMarginEPL
perpetualUpdateLeverageEPL
perpetualUpdateRiskEPL
perpetualToggleDualModeEPL
perpetualPositionsDualModeEPL
perpetualUpdateMarginDualModeEPL
perpetualUpdateLeverageDualModeEPL
perpetualUpdateRiskDualModeEPL
perpetualSubmitOrderEPL
perpetualGetOrdersEPL
perpetualSubmitBatchOrdersEPL
perpetualFetchOrderEPL
perpetualCancelOrderEPL
perpetualAmendOrderEPL
perpetualTradingHistoryEPL
perpetualClosePositionEPL
perpetualLiquidationHistoryEPL
perpetualCancelTriggerOrdersEPL
perpetualSubmitTriggerOrderEPL
perpetualListOpenOrdersEPL
perpetualCancelOpenOrdersEPL
perpetualGetTriggerOrderEPL
perpetualCancelTriggerOrderEPL

deliveryAccountEPL
deliveryAccountBooksEPL
deliveryPositionsEPL
deliveryUpdateMarginEPL
deliveryUpdateLeverageEPL
deliveryUpdateRiskLimitEPL
deliverySubmitOrderEPL
deliveryGetOrdersEPL
deliveryCancelOrdersEPL
deliveryGetOrderEPL
deliveryCancelOrderEPL
deliveryTradingHistoryEPL
deliveryCloseHistoryEPL
deliveryLiquidationHistoryEPL
deliverySettlementHistoryEPL
deliveryGetTriggerOrdersEPL
deliveryAutoOrdersEPL
deliveryCancelTriggerOrdersEPL
deliveryGetTriggerOrderEPL
deliveryCancelTriggerOrderEPL

optionsSettlementsEPL
optionsAccountsEPL
optionsAccountBooksEPL
optionsPositions
optionsLiquidationHistoryEPL
optionsSubmitOrderEPL
optionsOrdersEPL
optionsCancelOrdersEPL
optionsOrderEPL
optionsCancelOrderEPL
optionsTradingHistoryEPL

websocketRateLimitNotNeededEPL
)

// GetRateLimit returns the rate limiter for the exchange
func GetRateLimit() request.RateLimitDefinitions {
return request.RateLimitDefinitions{
spotDefaultEPL: request.NewRateLimitWithWeight(oneSecondInterval, spotPublicRate, 1),
spotPrivateEPL: request.NewRateLimitWithWeight(oneSecondInterval, spotPrivateRate, 1),
spotPlaceOrdersEPL: request.NewRateLimitWithWeight(oneSecondInterval, spotPlaceOrdersRate, 1),
spotCancelOrdersEPL: request.NewRateLimitWithWeight(oneSecondInterval, spotCancelOrdersRate, 1),
perpetualSwapDefaultEPL: request.NewRateLimitWithWeight(oneSecondInterval, perpetualSwapPublicRate, 1),
perpetualSwapPlaceOrdersEPL: request.NewRateLimitWithWeight(oneSecondInterval, perpetualSwapPlaceOrdersRate, 1),
perpetualSwapPrivateEPL: request.NewRateLimitWithWeight(oneSecondInterval, perpetualSwapPrivateRate, 1),
perpetualSwapCancelOrdersEPL: request.NewRateLimitWithWeight(oneSecondInterval, perpetualSwapCancelOrdersRate, 1),
walletEPL: request.NewRateLimitWithWeight(oneSecondInterval, walletRate, 1),
withdrawalEPL: request.NewRateLimitWithWeight(threeSecondsInterval, withdrawalRate, 1),
}
// package level rate limits for REST API
var packageRateLimits = request.RateLimitDefinitions{
publicOrderbookSpotEPL: standardRateLimit(),
publicMarketTradesSpotEPL: standardRateLimit(),
publicCandleStickSpotEPL: standardRateLimit(),
publicTickersSpotEPL: standardRateLimit(),
publicCurrencyPairDetailSpotEPL: standardRateLimit(),
publicListCurrencyPairsSpotEPL: standardRateLimit(),
publicCurrenciesSpotEPL: standardRateLimit(),

publicCurrencyPairsMarginEPL: standardRateLimit(),
publicOrderbookMarginEPL: standardRateLimit(),

publicInsuranceDeliveryEPL: standardRateLimit(),
publicDeliveryContractsEPL: standardRateLimit(),
publicOrderbookDeliveryEPL: standardRateLimit(),
publicTradingHistoryDeliveryEPL: standardRateLimit(),
publicCandleSticksDeliveryEPL: standardRateLimit(),
publicTickersDeliveryEPL: standardRateLimit(),

publicFuturesContractsEPL: standardRateLimit(),
publicOrderbookFuturesEPL: standardRateLimit(),
publicTradingHistoryFuturesEPL: standardRateLimit(),
publicCandleSticksFuturesEPL: standardRateLimit(),
publicPremiumIndexEPL: standardRateLimit(),
publicTickersFuturesEPL: standardRateLimit(),
publicFundingRatesEPL: standardRateLimit(),
publicInsuranceFuturesEPL: standardRateLimit(),
publicStatsFuturesEPL: standardRateLimit(),
publicIndexConstituentsEPL: standardRateLimit(),
publicLiquidationHistoryEPL: standardRateLimit(),

publicUnderlyingOptionsEPL: standardRateLimit(),
publicExpirationOptionsEPL: standardRateLimit(),
publicContractsOptionsEPL: standardRateLimit(),
publicSettlementOptionsEPL: standardRateLimit(),
publicOrderbookOptionsEPL: standardRateLimit(),
publicTickerOptionsEPL: standardRateLimit(),
publicUnderlyingTickerOptionsEPL: standardRateLimit(),
publicCandleSticksOptionsEPL: standardRateLimit(),
publicMarkpriceCandleSticksOptionsEPL: standardRateLimit(),
publicTradeHistoryOptionsEPL: standardRateLimit(),

publicGetServerTimeEPL: standardRateLimit(),
publicFlashSwapEPL: standardRateLimit(),
publicListCurrencyChainEPL: standardRateLimit(),

walletDepositAddressEPL: standardRateLimit(),
walletWithdrawalRecordsEPL: standardRateLimit(),
walletDepositRecordsEPL: standardRateLimit(),
walletTransferCurrencyEPL: personalAccountRateLimit(),
walletSubAccountTransferEPL: personalAccountRateLimit(),
walletSubAccountTransferHistoryEPL: standardRateLimit(),
walletSubAccountToSubAccountTransferEPL: personalAccountRateLimit(),
walletWithdrawStatusEPL: standardRateLimit(),
walletSubAccountBalancesEPL: personalAccountRateLimit(),
walletSubAccountMarginBalancesEPL: personalAccountRateLimit(),
walletSubAccountFuturesBalancesEPL: personalAccountRateLimit(),
walletSubAccountCrossMarginBalancesEPL: personalAccountRateLimit(),
walletSavedAddressesEPL: standardRateLimit(),
walletTradingFeeEPL: standardRateLimit(),
walletTotalBalanceEPL: personalAccountRateLimit(),
walletWithdrawEPL: withdrawFromWalletRateLimit(),
walletCancelWithdrawEPL: standardRateLimit(),

subAccountEPL: personalAccountRateLimit(),

spotTradingFeeEPL: standardRateLimit(),
spotAccountsEPL: standardRateLimit(),
spotGetOpenOrdersEPL: standardRateLimit(),
spotClosePositionEPL: orderCloseRateLimit(),
spotBatchOrdersEPL: spotOrderPlacementRateLimit(),
spotPlaceOrderEPL: spotOrderPlacementRateLimit(),
spotGetOrdersEPL: standardRateLimit(),
spotCancelAllOpenOrdersEPL: orderCloseRateLimit(),
spotCancelBatchOrdersEPL: orderCloseRateLimit(),
spotGetOrderEPL: standardRateLimit(),
spotAmendOrderEPL: spotOrderPlacementRateLimit(),
spotCancelSingleOrderEPL: orderCloseRateLimit(),
spotTradingHistoryEPL: standardRateLimit(),
spotCountdownCancelEPL: orderCloseRateLimit(),
spotCreateTriggerOrderEPL: spotOrderPlacementRateLimit(),
spotGetTriggerOrderListEPL: standardRateLimit(),
spotCancelTriggerOrdersEPL: orderCloseRateLimit(),
spotGetTriggerOrderEPL: standardRateLimit(),
spotCancelTriggerOrderEPL: orderCloseRateLimit(),

marginAccountListEPL: otherPrivateEndpointRateLimit(),
marginAccountBalanceEPL: otherPrivateEndpointRateLimit(),
marginFundingAccountListEPL: otherPrivateEndpointRateLimit(),
marginLendBorrowEPL: otherPrivateEndpointRateLimit(),
marginAllLoansEPL: otherPrivateEndpointRateLimit(),
marginMergeLendingLoansEPL: otherPrivateEndpointRateLimit(),
marginGetLoanEPL: otherPrivateEndpointRateLimit(),
marginModifyLoanEPL: otherPrivateEndpointRateLimit(),
marginCancelLoanEPL: otherPrivateEndpointRateLimit(),
marginRepayLoanEPL: otherPrivateEndpointRateLimit(),
marginListLoansEPL: otherPrivateEndpointRateLimit(),
marginRepaymentRecordEPL: otherPrivateEndpointRateLimit(),
marginSingleRecordEPL: otherPrivateEndpointRateLimit(),
marginModifyLoanRecordEPL: otherPrivateEndpointRateLimit(),
marginAutoRepayEPL: otherPrivateEndpointRateLimit(),
marginGetAutoRepaySettingsEPL: otherPrivateEndpointRateLimit(),
marginGetMaxTransferEPL: otherPrivateEndpointRateLimit(),
marginGetMaxBorrowEPL: otherPrivateEndpointRateLimit(),
marginSupportedCurrencyCrossListEPL: otherPrivateEndpointRateLimit(),
marginSupportedCurrencyCrossEPL: otherPrivateEndpointRateLimit(),
marginAccountsEPL: otherPrivateEndpointRateLimit(),
marginAccountHistoryEPL: otherPrivateEndpointRateLimit(),
marginCreateCrossBorrowLoanEPL: otherPrivateEndpointRateLimit(),
marginExecuteRepaymentsEPL: otherPrivateEndpointRateLimit(),
marginGetCrossMarginRepaymentsEPL: otherPrivateEndpointRateLimit(),
marginGetMaxTransferCrossEPL: otherPrivateEndpointRateLimit(),
marginGetMaxBorrowCrossEPL: otherPrivateEndpointRateLimit(),
marginGetCrossBorrowHistoryEPL: otherPrivateEndpointRateLimit(),
marginGetBorrowEPL: otherPrivateEndpointRateLimit(),

flashSwapOrderEPL: otherPrivateEndpointRateLimit(),
flashGetOrdersEPL: otherPrivateEndpointRateLimit(),
flashGetOrderEPL: otherPrivateEndpointRateLimit(),
flashOrderReviewEPL: otherPrivateEndpointRateLimit(),

perpetualAccountEPL: standardRateLimit(),
perpetualAccountBooksEPL: standardRateLimit(),
perpetualPositionsEPL: standardRateLimit(),
perpetualPositionEPL: standardRateLimit(),
perpetualUpdateMarginEPL: standardRateLimit(),
perpetualUpdateLeverageEPL: standardRateLimit(),
perpetualUpdateRiskEPL: standardRateLimit(),
perpetualToggleDualModeEPL: standardRateLimit(),
perpetualPositionsDualModeEPL: standardRateLimit(),
perpetualUpdateMarginDualModeEPL: standardRateLimit(),
perpetualUpdateLeverageDualModeEPL: standardRateLimit(),
perpetualUpdateRiskDualModeEPL: standardRateLimit(),
perpetualSubmitOrderEPL: perpetualOrderplacementRateLimit(),
perpetualGetOrdersEPL: standardRateLimit(),
perpetualSubmitBatchOrdersEPL: perpetualOrderplacementRateLimit(),
perpetualFetchOrderEPL: standardRateLimit(),
perpetualCancelOrderEPL: orderCloseRateLimit(),
perpetualAmendOrderEPL: perpetualOrderplacementRateLimit(),
perpetualTradingHistoryEPL: standardRateLimit(),
perpetualClosePositionEPL: orderCloseRateLimit(),
perpetualLiquidationHistoryEPL: standardRateLimit(),
perpetualCancelTriggerOrdersEPL: orderCloseRateLimit(),
perpetualSubmitTriggerOrderEPL: perpetualOrderplacementRateLimit(),
perpetualListOpenOrdersEPL: standardRateLimit(),
perpetualCancelOpenOrdersEPL: orderCloseRateLimit(),
perpetualGetTriggerOrderEPL: standardRateLimit(),
perpetualCancelTriggerOrderEPL: orderCloseRateLimit(),

deliveryAccountEPL: standardRateLimit(),
deliveryAccountBooksEPL: standardRateLimit(),
deliveryPositionsEPL: standardRateLimit(),
deliveryUpdateMarginEPL: standardRateLimit(),
deliveryUpdateLeverageEPL: standardRateLimit(),
deliveryUpdateRiskLimitEPL: standardRateLimit(),
deliverySubmitOrderEPL: deliverySubmitCancelAmendRateLimit(),
deliveryGetOrdersEPL: standardRateLimit(),
deliveryCancelOrdersEPL: deliverySubmitCancelAmendRateLimit(),
deliveryGetOrderEPL: standardRateLimit(),
deliveryCancelOrderEPL: deliverySubmitCancelAmendRateLimit(),
deliveryTradingHistoryEPL: standardRateLimit(),
deliveryCloseHistoryEPL: standardRateLimit(),
deliveryLiquidationHistoryEPL: standardRateLimit(),
deliverySettlementHistoryEPL: standardRateLimit(),
deliveryGetTriggerOrdersEPL: standardRateLimit(),
deliveryAutoOrdersEPL: standardRateLimit(),
deliveryCancelTriggerOrdersEPL: deliverySubmitCancelAmendRateLimit(),
deliveryGetTriggerOrderEPL: standardRateLimit(),
deliveryCancelTriggerOrderEPL: deliverySubmitCancelAmendRateLimit(),

optionsSettlementsEPL: standardRateLimit(),
optionsAccountsEPL: standardRateLimit(),
optionsAccountBooksEPL: standardRateLimit(),
optionsPositions: standardRateLimit(),
optionsLiquidationHistoryEPL: standardRateLimit(),
optionsSubmitOrderEPL: optionsSubmitCancelAmendRateLimit(),
optionsOrdersEPL: standardRateLimit(),
optionsCancelOrdersEPL: optionsSubmitCancelAmendRateLimit(),
optionsOrderEPL: standardRateLimit(),
optionsCancelOrderEPL: optionsSubmitCancelAmendRateLimit(),
optionsTradingHistoryEPL: standardRateLimit(),

privateUnifiedSpotEPL: standardRateLimit(),

websocketRateLimitNotNeededEPL: nil, // no rate limit for certain websocket functions
}

func standardRateLimit() *request.RateLimiterWithWeight {
return request.NewRateLimitWithWeight(time.Second*10, 200, 1)
}

func personalAccountRateLimit() *request.RateLimiterWithWeight {
return request.NewRateLimitWithWeight(time.Second*10, 80, 1)
}

func orderCloseRateLimit() *request.RateLimiterWithWeight {
return request.NewRateLimitWithWeight(time.Second, 200, 1)
}

func spotOrderPlacementRateLimit() *request.RateLimiterWithWeight {
return request.NewRateLimitWithWeight(time.Second, 10, 1)
}

func otherPrivateEndpointRateLimit() *request.RateLimiterWithWeight {
return request.NewRateLimitWithWeight(time.Second*10, 150, 1)
}

func perpetualOrderplacementRateLimit() *request.RateLimiterWithWeight {
return request.NewRateLimitWithWeight(time.Second, 100, 1)
}

func deliverySubmitCancelAmendRateLimit() *request.RateLimiterWithWeight {
return request.NewRateLimitWithWeight(time.Second*10, 500, 1)
}

func optionsSubmitCancelAmendRateLimit() *request.RateLimiterWithWeight {
return request.NewRateLimitWithWeight(time.Second, 200, 1)
}

func withdrawFromWalletRateLimit() *request.RateLimiterWithWeight {
return request.NewRateLimitWithWeight(time.Second*3, 1, 1)
}
16 changes: 16 additions & 0 deletions exchanges/gateio/ratelimiter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package gateio

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRateLimits(t *testing.T) {
for epl := range optionsTradingHistoryEPL {
if epl == 0 {
continue
}
assert.NotEmptyf(t, packageRateLimits[epl], "Empty rate limit not found for const %v", epl)
}
}