@varla/polymarket 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/dist/clob-client.d.ts.map +1 -1
- package/dist/clob-trading-client.d.ts +4 -0
- package/dist/clob-trading-client.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +277 -6
- package/dist/rate-limits.d.ts +140 -0
- package/dist/rate-limits.d.ts.map +1 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @varla/polymarket
|
|
2
2
|
|
|
3
|
+
## 2.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 230109e: Add exported rate limit constants and in-memory limiters with enhanced CLOB trading request throttling and backoff handling.
|
|
8
|
+
|
|
9
|
+
- export rate limit constants and helper limiter utilities from the package,- enforce per-endpoint rate budgets in the trading client with retry/backoff improvements and 429 handling,- use conservative keeper read budgets as default CLOB client limits
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 571e536: Applies a non-functional formatting change to package metadata.
|
|
14
|
+
|
|
15
|
+
- Reformatted the files list in package.json without altering behavior
|
|
16
|
+
|
|
3
17
|
## 2.1.0
|
|
4
18
|
|
|
5
19
|
### Minor Changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clob-client.d.ts","sourceRoot":"","sources":["../src/clob-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAGL,aAAa,EAId,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"clob-client.d.ts","sourceRoot":"","sources":["../src/clob-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAGL,aAAa,EAId,MAAM,kBAAkB,CAAC;AAI1B,OAA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AAe,IAAI,IAAI,CAEtC"}
|
|
@@ -46,6 +46,10 @@ export declare class ClobTradingClient {
|
|
|
46
46
|
private readonly requestTimeoutMs;
|
|
47
47
|
private readonly maxRetries;
|
|
48
48
|
private readonly log?;
|
|
49
|
+
/** Per-endpoint-class rate limiters (in-memory, no persistence). */
|
|
50
|
+
private readonly limiters;
|
|
51
|
+
/** Metrics: rate limit hits observed from upstream 429 responses. */
|
|
52
|
+
rateLimitHits: number;
|
|
49
53
|
private creds?;
|
|
50
54
|
private initialized;
|
|
51
55
|
constructor(walletClient: WalletClient, config?: ClobTradingClientFullConfig);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clob-trading-client.d.ts","sourceRoot":"","sources":["../src/clob-trading-client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AAGzC,OAAO,KAAK,EACV,WAAW,EACX,kBAAkB,EAClB,aAAa,EACb,iBAAiB,EACjB,mBAAmB,EACnB,qBAAqB,EACrB,cAAc,EACd,uBAAuB,EACvB,iBAAiB,EACjB,0BAA0B,EAC1B,eAAe,EACf,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,KAAK,EACL,SAAS,EACT,WAAW,EACX,aAAa,EACb,SAAS,EACT,aAAa,EAEb,WAAW,EACX,WAAW,EACX,KAAK,EACL,kBAAkB,EAClB,YAAY,EACb,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"clob-trading-client.d.ts","sourceRoot":"","sources":["../src/clob-trading-client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AAGzC,OAAO,KAAK,EACV,WAAW,EACX,kBAAkB,EAClB,aAAa,EACb,iBAAiB,EACjB,mBAAmB,EACnB,qBAAqB,EACrB,cAAc,EACd,uBAAuB,EACvB,iBAAiB,EACjB,0BAA0B,EAC1B,eAAe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AAiB,IAAI,OAAO,CAAC,iBAAiB,EAAE,CAAC;IAQvD;;;;OAIG;IACG,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC;IAU1C;;;;;;;OAOG;IACG,gBAAgB,CACpB,MAAM,CAAC,EAAE,mBAAmB,EAC5B,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,qBAAqB,CAAC;IAoBjC,OAAO,CAAC,iBAAiB;YAMX,oBAAoB;CA0FnC;AAMD;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,YAAY,EAAE,YAAY,EAC1B,MAAM,CAAC,EAAE,2BAA2B,GACnC,iBAAiB,CAEnB"}
|
package/dist/index.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export * from "./clob-ws.js";
|
|
|
15
15
|
export * from "./clob-errors.js";
|
|
16
16
|
export * from "./data-api-client.js";
|
|
17
17
|
export * from "./pnl.js";
|
|
18
|
+
export * from "./rate-limits.js";
|
|
18
19
|
export { parseDecimalToBigint } from "./utils/decimal.js";
|
|
19
20
|
export { twapFromHistory } from "./utils/twap.js";
|
|
20
21
|
export { probToE8, collateralPriceFromMarkets } from "./utils/pricing.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,mBAAmB,CAAC;AAMlC,cAAc,gBAAgB,CAAC;AAM/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAM9B,cAAc,yBAAyB,CAAC;AACxC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AAMjC,cAAc,sBAAsB,CAAC;AAMrC,cAAc,UAAU,CAAC;AAMzB,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,0BAA0B,EAAE,MAAM,oBAAoB,CAAC;AAM1E,cAAc,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,mBAAmB,CAAC;AAMlC,cAAc,gBAAgB,CAAC;AAM/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAM9B,cAAc,yBAAyB,CAAC;AACxC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AAMjC,cAAc,sBAAsB,CAAC;AAMrC,cAAc,UAAU,CAAC;AAMzB,cAAc,kBAAkB,CAAC;AAMjC,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,0BAA0B,EAAE,MAAM,oBAAoB,CAAC;AAM1E,cAAc,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -772,6 +772,186 @@ function classifyHttpStatus(status, context, responseBody) {
|
|
|
772
772
|
return new ClobError("UNKNOWN" /* UNKNOWN */, `Unexpected HTTP status ${status}`, ctx, true);
|
|
773
773
|
}
|
|
774
774
|
|
|
775
|
+
// src/rate-limits.ts
|
|
776
|
+
var POLYMARKET_GENERAL_LIMIT_PER_10S = 15000;
|
|
777
|
+
var CLOB_GENERAL_LIMIT_PER_10S = 9000;
|
|
778
|
+
var CLOB_BOOK_LIMIT_PER_10S = 1500;
|
|
779
|
+
var CLOB_BOOKS_LIMIT_PER_10S = 500;
|
|
780
|
+
var CLOB_PRICE_LIMIT_PER_10S = 1500;
|
|
781
|
+
var CLOB_PRICES_LIMIT_PER_10S = 500;
|
|
782
|
+
var CLOB_MIDPRICE_LIMIT_PER_10S = 1500;
|
|
783
|
+
var CLOB_MIDPRICES_LIMIT_PER_10S = 500;
|
|
784
|
+
var CLOB_TICK_SIZE_LIMIT_PER_10S = 200;
|
|
785
|
+
var CLOB_PRICE_HISTORY_LIMIT_PER_10S = 1000;
|
|
786
|
+
var CLOB_BALANCE_ALLOWANCE_GET_LIMIT_PER_10S = 200;
|
|
787
|
+
var CLOB_BALANCE_ALLOWANCE_UPDATE_LIMIT_PER_10S = 50;
|
|
788
|
+
var CLOB_LEDGER_LIMIT_PER_10S = 900;
|
|
789
|
+
var CLOB_DATA_ORDERS_LIMIT_PER_10S = 500;
|
|
790
|
+
var CLOB_DATA_TRADES_LIMIT_PER_10S = 500;
|
|
791
|
+
var CLOB_NOTIFICATIONS_LIMIT_PER_10S = 125;
|
|
792
|
+
var CLOB_API_KEYS_LIMIT_PER_10S = 100;
|
|
793
|
+
var CLOB_POST_ORDER_BURST_PER_10S = 3500;
|
|
794
|
+
var CLOB_POST_ORDER_SUSTAINED_PER_10MIN = 36000;
|
|
795
|
+
var CLOB_DELETE_ORDER_BURST_PER_10S = 3000;
|
|
796
|
+
var CLOB_DELETE_ORDER_SUSTAINED_PER_10MIN = 30000;
|
|
797
|
+
var CLOB_POST_ORDERS_BURST_PER_10S = 1000;
|
|
798
|
+
var CLOB_POST_ORDERS_SUSTAINED_PER_10MIN = 15000;
|
|
799
|
+
var CLOB_DELETE_ORDERS_BURST_PER_10S = 1000;
|
|
800
|
+
var CLOB_DELETE_ORDERS_SUSTAINED_PER_10MIN = 15000;
|
|
801
|
+
var CLOB_CANCEL_ALL_BURST_PER_10S = 250;
|
|
802
|
+
var CLOB_CANCEL_ALL_SUSTAINED_PER_10MIN = 6000;
|
|
803
|
+
var CLOB_CANCEL_MARKET_ORDERS_BURST_PER_10S = 1000;
|
|
804
|
+
var CLOB_CANCEL_MARKET_ORDERS_SUSTAINED_PER_10MIN = 1500;
|
|
805
|
+
var DATA_API_GENERAL_LIMIT_PER_10S = 1000;
|
|
806
|
+
var DATA_API_TRADES_LIMIT_PER_10S = 200;
|
|
807
|
+
var DATA_API_POSITIONS_LIMIT_PER_10S = 150;
|
|
808
|
+
var DATA_API_CLOSED_POSITIONS_LIMIT_PER_10S = 150;
|
|
809
|
+
var GAMMA_GENERAL_LIMIT_PER_10S = 4000;
|
|
810
|
+
var GAMMA_EVENTS_LIMIT_PER_10S = 500;
|
|
811
|
+
var GAMMA_MARKETS_LIMIT_PER_10S = 300;
|
|
812
|
+
var GAMMA_MARKETS_EVENTS_LIMIT_PER_10S = 900;
|
|
813
|
+
var GAMMA_COMMENTS_LIMIT_PER_10S = 200;
|
|
814
|
+
var GAMMA_TAGS_LIMIT_PER_10S = 200;
|
|
815
|
+
var GAMMA_SEARCH_LIMIT_PER_10S = 350;
|
|
816
|
+
var RELAYER_SUBMIT_LIMIT_PER_1MIN = 25;
|
|
817
|
+
var USER_PNL_LIMIT_PER_10S = 200;
|
|
818
|
+
var KEEPER_CLOB_READ_RPS = 5;
|
|
819
|
+
var KEEPER_CLOB_READ_BURST = 15;
|
|
820
|
+
var KEEPER_CLOB_ORDER_READ_RPS = 2;
|
|
821
|
+
var KEEPER_CLOB_ORDER_READ_BURST = 8;
|
|
822
|
+
var KEEPER_CLOB_POST_ORDER_MAX_PER_10S = 20;
|
|
823
|
+
var KEEPER_CLOB_POST_ORDER_MAX_PER_10MIN = 200;
|
|
824
|
+
var KEEPER_CLOB_DELETE_ORDER_MAX_PER_10S = 15;
|
|
825
|
+
var KEEPER_CLOB_DELETE_ORDER_MAX_PER_10MIN = 150;
|
|
826
|
+
var KEEPER_CLOB_CANCEL_ALL_MAX_PER_10S = 3;
|
|
827
|
+
var KEEPER_CLOB_CANCEL_ALL_MAX_PER_10MIN = 30;
|
|
828
|
+
|
|
829
|
+
class SlidingWindowLimiter {
|
|
830
|
+
maxRequests;
|
|
831
|
+
windowMs;
|
|
832
|
+
now;
|
|
833
|
+
slots;
|
|
834
|
+
slotMs;
|
|
835
|
+
numSlots;
|
|
836
|
+
lastSlotIndex = 0;
|
|
837
|
+
lastAccessMs = 0;
|
|
838
|
+
constructor(maxRequests, windowMs, now = Date.now) {
|
|
839
|
+
this.maxRequests = maxRequests;
|
|
840
|
+
this.windowMs = windowMs;
|
|
841
|
+
this.now = now;
|
|
842
|
+
this.slotMs = windowMs <= 1e4 ? 100 : 1000;
|
|
843
|
+
this.numSlots = Math.max(2, Math.ceil(windowMs / this.slotMs));
|
|
844
|
+
this.slots = new Array(this.numSlots).fill(0);
|
|
845
|
+
this.lastAccessMs = this.now();
|
|
846
|
+
}
|
|
847
|
+
tryAcquire() {
|
|
848
|
+
this.advance();
|
|
849
|
+
const total = this.countAll();
|
|
850
|
+
if (total >= this.maxRequests)
|
|
851
|
+
return false;
|
|
852
|
+
this.slots[this.lastSlotIndex]++;
|
|
853
|
+
return true;
|
|
854
|
+
}
|
|
855
|
+
currentCount() {
|
|
856
|
+
this.advance();
|
|
857
|
+
return this.countAll();
|
|
858
|
+
}
|
|
859
|
+
remaining() {
|
|
860
|
+
return Math.max(0, this.maxRequests - this.currentCount());
|
|
861
|
+
}
|
|
862
|
+
waitTimeMs() {
|
|
863
|
+
this.advance();
|
|
864
|
+
const total = this.countAll();
|
|
865
|
+
if (total < this.maxRequests)
|
|
866
|
+
return 0;
|
|
867
|
+
return this.slotMs;
|
|
868
|
+
}
|
|
869
|
+
async acquire() {
|
|
870
|
+
while (!this.tryAcquire()) {
|
|
871
|
+
const waitMs = Math.max(50, this.waitTimeMs());
|
|
872
|
+
await new Promise((r) => setTimeout(r, waitMs));
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
reset() {
|
|
876
|
+
this.slots.fill(0);
|
|
877
|
+
this.lastAccessMs = this.now();
|
|
878
|
+
}
|
|
879
|
+
advance() {
|
|
880
|
+
const nowMs = this.now();
|
|
881
|
+
const elapsed = nowMs - this.lastAccessMs;
|
|
882
|
+
if (elapsed <= 0)
|
|
883
|
+
return;
|
|
884
|
+
const slotsToAdvance = Math.min(this.numSlots, Math.floor(elapsed / this.slotMs));
|
|
885
|
+
for (let i = 0;i < slotsToAdvance; i++) {
|
|
886
|
+
this.lastSlotIndex = (this.lastSlotIndex + 1) % this.numSlots;
|
|
887
|
+
this.slots[this.lastSlotIndex] = 0;
|
|
888
|
+
}
|
|
889
|
+
this.lastAccessMs = nowMs;
|
|
890
|
+
}
|
|
891
|
+
countAll() {
|
|
892
|
+
let sum = 0;
|
|
893
|
+
for (let i = 0;i < this.numSlots; i++)
|
|
894
|
+
sum += this.slots[i];
|
|
895
|
+
return sum;
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
class DualWindowLimiter {
|
|
900
|
+
burst;
|
|
901
|
+
sustained;
|
|
902
|
+
constructor(burstMax, burstWindowMs, sustainedMax, sustainedWindowMs, now) {
|
|
903
|
+
this.burst = new SlidingWindowLimiter(burstMax, burstWindowMs, now);
|
|
904
|
+
this.sustained = new SlidingWindowLimiter(sustainedMax, sustainedWindowMs, now);
|
|
905
|
+
}
|
|
906
|
+
tryAcquire() {
|
|
907
|
+
if (this.burst.currentCount() >= this.burst.maxRequests)
|
|
908
|
+
return false;
|
|
909
|
+
if (this.sustained.currentCount() >= this.sustained.maxRequests)
|
|
910
|
+
return false;
|
|
911
|
+
this.burst.tryAcquire();
|
|
912
|
+
this.sustained.tryAcquire();
|
|
913
|
+
return true;
|
|
914
|
+
}
|
|
915
|
+
async acquire() {
|
|
916
|
+
while (!this.tryAcquire()) {
|
|
917
|
+
const waitMs = Math.max(50, Math.max(this.burst.waitTimeMs(), this.sustained.waitTimeMs()));
|
|
918
|
+
await new Promise((r) => setTimeout(r, waitMs));
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
remaining() {
|
|
922
|
+
return Math.min(this.burst.remaining(), this.sustained.remaining());
|
|
923
|
+
}
|
|
924
|
+
reset() {
|
|
925
|
+
this.burst.reset();
|
|
926
|
+
this.sustained.reset();
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
function createEndpointLimiters(now) {
|
|
930
|
+
return {
|
|
931
|
+
clob_read: new SlidingWindowLimiter(KEEPER_CLOB_READ_BURST, 1e4, now),
|
|
932
|
+
clob_order_read: new SlidingWindowLimiter(KEEPER_CLOB_ORDER_READ_BURST, 1e4, now),
|
|
933
|
+
clob_post_order: new DualWindowLimiter(KEEPER_CLOB_POST_ORDER_MAX_PER_10S, 1e4, KEEPER_CLOB_POST_ORDER_MAX_PER_10MIN, 600000, now),
|
|
934
|
+
clob_delete_order: new DualWindowLimiter(KEEPER_CLOB_DELETE_ORDER_MAX_PER_10S, 1e4, KEEPER_CLOB_DELETE_ORDER_MAX_PER_10MIN, 600000, now),
|
|
935
|
+
clob_cancel_all: new DualWindowLimiter(KEEPER_CLOB_CANCEL_ALL_MAX_PER_10S, 1e4, KEEPER_CLOB_CANCEL_ALL_MAX_PER_10MIN, 600000, now),
|
|
936
|
+
clob_post_orders: new DualWindowLimiter(Math.max(1, Math.floor(KEEPER_CLOB_POST_ORDER_MAX_PER_10S / 2)), 1e4, Math.max(1, Math.floor(KEEPER_CLOB_POST_ORDER_MAX_PER_10MIN / 2)), 600000, now)
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
function classifyEndpoint(method, path) {
|
|
940
|
+
const p = path.toLowerCase();
|
|
941
|
+
const m = method.toUpperCase();
|
|
942
|
+
if (m === "POST" && (p.includes("/orders") || p === "/orders"))
|
|
943
|
+
return "clob_post_orders";
|
|
944
|
+
if (m === "POST" && (p.includes("/order") || p === "/order"))
|
|
945
|
+
return "clob_post_order";
|
|
946
|
+
if (m === "DELETE" && (p.includes("/cancel-all") || p.includes("/cancel-market")))
|
|
947
|
+
return "clob_cancel_all";
|
|
948
|
+
if (m === "DELETE" && (p.includes("/order") || p.includes("/orders")))
|
|
949
|
+
return "clob_delete_order";
|
|
950
|
+
if (p.includes("/data/order") || p.includes("/data/trade") || p.includes("/notification"))
|
|
951
|
+
return "clob_order_read";
|
|
952
|
+
return "clob_read";
|
|
953
|
+
}
|
|
954
|
+
|
|
775
955
|
// src/utils/concurrency.ts
|
|
776
956
|
class ConcurrencyLimiter {
|
|
777
957
|
maxInFlight;
|
|
@@ -813,8 +993,8 @@ class ConcurrencyLimiter {
|
|
|
813
993
|
// src/clob-client.ts
|
|
814
994
|
var DEFAULT_CONFIG = {
|
|
815
995
|
baseUrl: POLYMARKET_ENDPOINTS.clobBaseUrl,
|
|
816
|
-
requestsPerSecond:
|
|
817
|
-
burstSize:
|
|
996
|
+
requestsPerSecond: KEEPER_CLOB_READ_RPS,
|
|
997
|
+
burstSize: KEEPER_CLOB_READ_BURST,
|
|
818
998
|
requestTimeoutMs: 15000,
|
|
819
999
|
totalTimeoutMs: 60000,
|
|
820
1000
|
maxRetries: 5,
|
|
@@ -1940,6 +2120,8 @@ class ClobTradingClient {
|
|
|
1940
2120
|
requestTimeoutMs;
|
|
1941
2121
|
maxRetries;
|
|
1942
2122
|
log;
|
|
2123
|
+
limiters;
|
|
2124
|
+
rateLimitHits = 0;
|
|
1943
2125
|
creds;
|
|
1944
2126
|
initialized = false;
|
|
1945
2127
|
constructor(walletClient, config = {}) {
|
|
@@ -1948,6 +2130,7 @@ class ClobTradingClient {
|
|
|
1948
2130
|
this.requestTimeoutMs = config.requestTimeoutMs ?? 15000;
|
|
1949
2131
|
this.maxRetries = config.maxRetries ?? 3;
|
|
1950
2132
|
this.log = config.log;
|
|
2133
|
+
this.limiters = createEndpointLimiters(config.now);
|
|
1951
2134
|
const signatureType = config.signatureType ?? SignatureType.EOA;
|
|
1952
2135
|
const authConfig = {
|
|
1953
2136
|
baseUrl: this.baseUrl,
|
|
@@ -2287,13 +2470,43 @@ class ClobTradingClient {
|
|
|
2287
2470
|
}
|
|
2288
2471
|
}
|
|
2289
2472
|
async authenticatedRequest(method, path, body) {
|
|
2473
|
+
const endpointClass = classifyEndpoint(method, path);
|
|
2474
|
+
const limiter = this.limiters[endpointClass];
|
|
2475
|
+
if (limiter && !limiter.tryAcquire()) {
|
|
2476
|
+
this.log?.("warn", "rate limiter budget exhausted; waiting", {
|
|
2477
|
+
endpoint: endpointClass,
|
|
2478
|
+
method,
|
|
2479
|
+
path,
|
|
2480
|
+
remaining: limiter.remaining()
|
|
2481
|
+
});
|
|
2482
|
+
await limiter.acquire();
|
|
2483
|
+
}
|
|
2290
2484
|
let lastError;
|
|
2291
2485
|
for (let attempt = 0;attempt <= this.maxRetries; attempt++) {
|
|
2292
2486
|
try {
|
|
2293
2487
|
return await this.authManager.authenticatedRequest(this.creds, method, path, body);
|
|
2294
2488
|
} catch (error) {
|
|
2295
2489
|
lastError = error instanceof Error ? error : new Error(String(error));
|
|
2296
|
-
|
|
2490
|
+
const msg = lastError.message;
|
|
2491
|
+
if (msg.includes("429")) {
|
|
2492
|
+
this.rateLimitHits++;
|
|
2493
|
+
const retryAfterMs = typeof lastError?.retryAfterMs === "number" ? lastError.retryAfterMs : undefined;
|
|
2494
|
+
const waitMs = retryAfterMs && retryAfterMs > 0 ? retryAfterMs : 1000 * 2 ** attempt;
|
|
2495
|
+
const cappedWait = Math.min(waitMs, 30000);
|
|
2496
|
+
this.log?.("warn", "CLOB 429 rate limited; backing off", {
|
|
2497
|
+
attempt,
|
|
2498
|
+
path,
|
|
2499
|
+
retryAfterMs,
|
|
2500
|
+
waitMs: cappedWait,
|
|
2501
|
+
totalRateLimitHits: this.rateLimitHits
|
|
2502
|
+
});
|
|
2503
|
+
if (attempt < this.maxRetries) {
|
|
2504
|
+
await sleep3(cappedWait);
|
|
2505
|
+
continue;
|
|
2506
|
+
}
|
|
2507
|
+
throw lastError;
|
|
2508
|
+
}
|
|
2509
|
+
if (msg.includes("401") && attempt < this.maxRetries) {
|
|
2297
2510
|
this.log?.("warn", "Auth error, refreshing credentials", { attempt });
|
|
2298
2511
|
try {
|
|
2299
2512
|
this.creds = await this.authManager.createOrDeriveApiKey();
|
|
@@ -2304,15 +2517,17 @@ class ClobTradingClient {
|
|
|
2304
2517
|
});
|
|
2305
2518
|
}
|
|
2306
2519
|
}
|
|
2307
|
-
const isRetryable =
|
|
2520
|
+
const isRetryable = msg.includes("500") || msg.includes("502") || msg.includes("503") || msg.includes("504") || msg.includes("network") || msg.includes("timeout");
|
|
2308
2521
|
if (!isRetryable || attempt >= this.maxRetries) {
|
|
2309
2522
|
throw lastError;
|
|
2310
2523
|
}
|
|
2311
|
-
const
|
|
2524
|
+
const baseDelay = Math.min(1000 * 2 ** attempt, 1e4);
|
|
2525
|
+
const jitter = Math.floor(baseDelay * 0.3 * Math.random());
|
|
2526
|
+
const delay = baseDelay + jitter;
|
|
2312
2527
|
this.log?.("warn", "Request failed, retrying", {
|
|
2313
2528
|
attempt: attempt + 1,
|
|
2314
2529
|
delay,
|
|
2315
|
-
error:
|
|
2530
|
+
error: msg
|
|
2316
2531
|
});
|
|
2317
2532
|
await sleep3(delay);
|
|
2318
2533
|
}
|
|
@@ -3092,6 +3307,7 @@ export {
|
|
|
3092
3307
|
ctfExchangeAbi,
|
|
3093
3308
|
createTrackedOrder,
|
|
3094
3309
|
createMemoryOrderPersistence,
|
|
3310
|
+
createEndpointLimiters,
|
|
3095
3311
|
createClobWsClient,
|
|
3096
3312
|
createClobTradingClient,
|
|
3097
3313
|
createClobOrderBuilder,
|
|
@@ -3103,22 +3319,37 @@ export {
|
|
|
3103
3319
|
collateralPriceFromMarkets,
|
|
3104
3320
|
classifyHttpStatus,
|
|
3105
3321
|
classifyError,
|
|
3322
|
+
classifyEndpoint,
|
|
3106
3323
|
chunk,
|
|
3107
3324
|
calculateAmounts,
|
|
3108
3325
|
VALIDATE_READONLY_API_KEY,
|
|
3326
|
+
USER_PNL_LIMIT_PER_10S,
|
|
3109
3327
|
UPDATE_BALANCE_ALLOWANCE,
|
|
3110
3328
|
TIME,
|
|
3329
|
+
SlidingWindowLimiter,
|
|
3111
3330
|
SignatureType,
|
|
3112
3331
|
ROUNDING_CONFIG,
|
|
3113
3332
|
RFQ_REQUESTS_ACCEPT,
|
|
3114
3333
|
RFQ_QUOTE_APPROVE,
|
|
3115
3334
|
RFQ_CONFIG,
|
|
3116
3335
|
REVOKE_BUILDER_API_KEY,
|
|
3336
|
+
RELAYER_SUBMIT_LIMIT_PER_1MIN,
|
|
3117
3337
|
POST_ORDERS,
|
|
3118
3338
|
POST_ORDER,
|
|
3119
3339
|
POST_HEARTBEAT,
|
|
3340
|
+
POLYMARKET_GENERAL_LIMIT_PER_10S,
|
|
3120
3341
|
POLYMARKET_ENDPOINTS,
|
|
3121
3342
|
OrderValidationError,
|
|
3343
|
+
KEEPER_CLOB_READ_RPS,
|
|
3344
|
+
KEEPER_CLOB_READ_BURST,
|
|
3345
|
+
KEEPER_CLOB_POST_ORDER_MAX_PER_10S,
|
|
3346
|
+
KEEPER_CLOB_POST_ORDER_MAX_PER_10MIN,
|
|
3347
|
+
KEEPER_CLOB_ORDER_READ_RPS,
|
|
3348
|
+
KEEPER_CLOB_ORDER_READ_BURST,
|
|
3349
|
+
KEEPER_CLOB_DELETE_ORDER_MAX_PER_10S,
|
|
3350
|
+
KEEPER_CLOB_DELETE_ORDER_MAX_PER_10MIN,
|
|
3351
|
+
KEEPER_CLOB_CANCEL_ALL_MAX_PER_10S,
|
|
3352
|
+
KEEPER_CLOB_CANCEL_ALL_MAX_PER_10MIN,
|
|
3122
3353
|
IS_ORDER_SCORING,
|
|
3123
3354
|
INITIAL_CURSOR,
|
|
3124
3355
|
GammaClient,
|
|
@@ -3161,12 +3392,24 @@ export {
|
|
|
3161
3392
|
GET_BUILDER_API_KEYS,
|
|
3162
3393
|
GET_BALANCE_ALLOWANCE,
|
|
3163
3394
|
GET_API_KEYS,
|
|
3395
|
+
GAMMA_TAGS_LIMIT_PER_10S,
|
|
3396
|
+
GAMMA_SEARCH_LIMIT_PER_10S,
|
|
3397
|
+
GAMMA_MARKETS_LIMIT_PER_10S,
|
|
3398
|
+
GAMMA_MARKETS_EVENTS_LIMIT_PER_10S,
|
|
3399
|
+
GAMMA_GENERAL_LIMIT_PER_10S,
|
|
3400
|
+
GAMMA_EVENTS_LIMIT_PER_10S,
|
|
3401
|
+
GAMMA_COMMENTS_LIMIT_PER_10S,
|
|
3164
3402
|
END_CURSOR,
|
|
3403
|
+
DualWindowLimiter,
|
|
3165
3404
|
DataApiClient,
|
|
3166
3405
|
DROP_NOTIFICATIONS,
|
|
3167
3406
|
DERIVE_API_KEY,
|
|
3168
3407
|
DELETE_READONLY_API_KEY,
|
|
3169
3408
|
DELETE_API_KEY,
|
|
3409
|
+
DATA_API_TRADES_LIMIT_PER_10S,
|
|
3410
|
+
DATA_API_POSITIONS_LIMIT_PER_10S,
|
|
3411
|
+
DATA_API_GENERAL_LIMIT_PER_10S,
|
|
3412
|
+
DATA_API_CLOSED_POSITIONS_LIMIT_PER_10S,
|
|
3170
3413
|
ClobWsClient,
|
|
3171
3414
|
ClobTradingClient,
|
|
3172
3415
|
ClobTimeoutError,
|
|
@@ -3190,6 +3433,34 @@ export {
|
|
|
3190
3433
|
CONDITIONAL_TOKEN_DECIMALS,
|
|
3191
3434
|
COLLATERAL_TOKEN_DECIMALS,
|
|
3192
3435
|
CLOSED_ONLY,
|
|
3436
|
+
CLOB_TICK_SIZE_LIMIT_PER_10S,
|
|
3437
|
+
CLOB_PRICE_LIMIT_PER_10S,
|
|
3438
|
+
CLOB_PRICE_HISTORY_LIMIT_PER_10S,
|
|
3439
|
+
CLOB_PRICES_LIMIT_PER_10S,
|
|
3440
|
+
CLOB_POST_ORDER_SUSTAINED_PER_10MIN,
|
|
3441
|
+
CLOB_POST_ORDER_BURST_PER_10S,
|
|
3442
|
+
CLOB_POST_ORDERS_SUSTAINED_PER_10MIN,
|
|
3443
|
+
CLOB_POST_ORDERS_BURST_PER_10S,
|
|
3444
|
+
CLOB_NOTIFICATIONS_LIMIT_PER_10S,
|
|
3445
|
+
CLOB_MIDPRICE_LIMIT_PER_10S,
|
|
3446
|
+
CLOB_MIDPRICES_LIMIT_PER_10S,
|
|
3447
|
+
CLOB_LEDGER_LIMIT_PER_10S,
|
|
3448
|
+
CLOB_GENERAL_LIMIT_PER_10S,
|
|
3449
|
+
CLOB_DELETE_ORDER_SUSTAINED_PER_10MIN,
|
|
3450
|
+
CLOB_DELETE_ORDER_BURST_PER_10S,
|
|
3451
|
+
CLOB_DELETE_ORDERS_SUSTAINED_PER_10MIN,
|
|
3452
|
+
CLOB_DELETE_ORDERS_BURST_PER_10S,
|
|
3453
|
+
CLOB_DATA_TRADES_LIMIT_PER_10S,
|
|
3454
|
+
CLOB_DATA_ORDERS_LIMIT_PER_10S,
|
|
3455
|
+
CLOB_CANCEL_MARKET_ORDERS_SUSTAINED_PER_10MIN,
|
|
3456
|
+
CLOB_CANCEL_MARKET_ORDERS_BURST_PER_10S,
|
|
3457
|
+
CLOB_CANCEL_ALL_SUSTAINED_PER_10MIN,
|
|
3458
|
+
CLOB_CANCEL_ALL_BURST_PER_10S,
|
|
3459
|
+
CLOB_BOOK_LIMIT_PER_10S,
|
|
3460
|
+
CLOB_BOOKS_LIMIT_PER_10S,
|
|
3461
|
+
CLOB_BALANCE_ALLOWANCE_UPDATE_LIMIT_PER_10S,
|
|
3462
|
+
CLOB_BALANCE_ALLOWANCE_GET_LIMIT_PER_10S,
|
|
3463
|
+
CLOB_API_KEYS_LIMIT_PER_10S,
|
|
3193
3464
|
CANCEL_RFQ_REQUEST,
|
|
3194
3465
|
CANCEL_RFQ_QUOTE,
|
|
3195
3466
|
CANCEL_ORDERS,
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Polymarket API Rate Limits — Constants & In-Memory Enforcement.
|
|
3
|
+
*
|
|
4
|
+
* Source: https://docs.polymarket.com/quickstart/introduction/rate-limits
|
|
5
|
+
*
|
|
6
|
+
* All limits are enforced via Cloudflare throttling (requests are delayed, not dropped).
|
|
7
|
+
* We enforce conservatively below the published caps so we never trigger throttling.
|
|
8
|
+
*
|
|
9
|
+
* Design decisions:
|
|
10
|
+
* - Constants only (no env vars) — these are upstream API constraints, not tunable config.
|
|
11
|
+
* - In-memory only (no DB) — no historical data, no persistence across restarts.
|
|
12
|
+
* - Sliding window counters — lightweight, O(1) per check, auto-expire.
|
|
13
|
+
*/
|
|
14
|
+
/** General rate limit across all endpoints. */
|
|
15
|
+
export declare const POLYMARKET_GENERAL_LIMIT_PER_10S = 15000;
|
|
16
|
+
export declare const CLOB_GENERAL_LIMIT_PER_10S = 9000;
|
|
17
|
+
export declare const CLOB_BOOK_LIMIT_PER_10S = 1500;
|
|
18
|
+
export declare const CLOB_BOOKS_LIMIT_PER_10S = 500;
|
|
19
|
+
export declare const CLOB_PRICE_LIMIT_PER_10S = 1500;
|
|
20
|
+
export declare const CLOB_PRICES_LIMIT_PER_10S = 500;
|
|
21
|
+
export declare const CLOB_MIDPRICE_LIMIT_PER_10S = 1500;
|
|
22
|
+
export declare const CLOB_MIDPRICES_LIMIT_PER_10S = 500;
|
|
23
|
+
export declare const CLOB_TICK_SIZE_LIMIT_PER_10S = 200;
|
|
24
|
+
export declare const CLOB_PRICE_HISTORY_LIMIT_PER_10S = 1000;
|
|
25
|
+
export declare const CLOB_BALANCE_ALLOWANCE_GET_LIMIT_PER_10S = 200;
|
|
26
|
+
export declare const CLOB_BALANCE_ALLOWANCE_UPDATE_LIMIT_PER_10S = 50;
|
|
27
|
+
export declare const CLOB_LEDGER_LIMIT_PER_10S = 900;
|
|
28
|
+
export declare const CLOB_DATA_ORDERS_LIMIT_PER_10S = 500;
|
|
29
|
+
export declare const CLOB_DATA_TRADES_LIMIT_PER_10S = 500;
|
|
30
|
+
export declare const CLOB_NOTIFICATIONS_LIMIT_PER_10S = 125;
|
|
31
|
+
export declare const CLOB_API_KEYS_LIMIT_PER_10S = 100;
|
|
32
|
+
export declare const CLOB_POST_ORDER_BURST_PER_10S = 3500;
|
|
33
|
+
export declare const CLOB_POST_ORDER_SUSTAINED_PER_10MIN = 36000;
|
|
34
|
+
export declare const CLOB_DELETE_ORDER_BURST_PER_10S = 3000;
|
|
35
|
+
export declare const CLOB_DELETE_ORDER_SUSTAINED_PER_10MIN = 30000;
|
|
36
|
+
export declare const CLOB_POST_ORDERS_BURST_PER_10S = 1000;
|
|
37
|
+
export declare const CLOB_POST_ORDERS_SUSTAINED_PER_10MIN = 15000;
|
|
38
|
+
export declare const CLOB_DELETE_ORDERS_BURST_PER_10S = 1000;
|
|
39
|
+
export declare const CLOB_DELETE_ORDERS_SUSTAINED_PER_10MIN = 15000;
|
|
40
|
+
export declare const CLOB_CANCEL_ALL_BURST_PER_10S = 250;
|
|
41
|
+
export declare const CLOB_CANCEL_ALL_SUSTAINED_PER_10MIN = 6000;
|
|
42
|
+
export declare const CLOB_CANCEL_MARKET_ORDERS_BURST_PER_10S = 1000;
|
|
43
|
+
export declare const CLOB_CANCEL_MARKET_ORDERS_SUSTAINED_PER_10MIN = 1500;
|
|
44
|
+
export declare const DATA_API_GENERAL_LIMIT_PER_10S = 1000;
|
|
45
|
+
export declare const DATA_API_TRADES_LIMIT_PER_10S = 200;
|
|
46
|
+
export declare const DATA_API_POSITIONS_LIMIT_PER_10S = 150;
|
|
47
|
+
export declare const DATA_API_CLOSED_POSITIONS_LIMIT_PER_10S = 150;
|
|
48
|
+
export declare const GAMMA_GENERAL_LIMIT_PER_10S = 4000;
|
|
49
|
+
export declare const GAMMA_EVENTS_LIMIT_PER_10S = 500;
|
|
50
|
+
export declare const GAMMA_MARKETS_LIMIT_PER_10S = 300;
|
|
51
|
+
export declare const GAMMA_MARKETS_EVENTS_LIMIT_PER_10S = 900;
|
|
52
|
+
export declare const GAMMA_COMMENTS_LIMIT_PER_10S = 200;
|
|
53
|
+
export declare const GAMMA_TAGS_LIMIT_PER_10S = 200;
|
|
54
|
+
export declare const GAMMA_SEARCH_LIMIT_PER_10S = 350;
|
|
55
|
+
export declare const RELAYER_SUBMIT_LIMIT_PER_1MIN = 25;
|
|
56
|
+
export declare const USER_PNL_LIMIT_PER_10S = 200;
|
|
57
|
+
/** Internal budget: public CLOB reads (/book, /tick-size, /price). */
|
|
58
|
+
export declare const KEEPER_CLOB_READ_RPS = 5;
|
|
59
|
+
export declare const KEEPER_CLOB_READ_BURST = 15;
|
|
60
|
+
/** Internal budget: authenticated order queries (/data/orders, /order). */
|
|
61
|
+
export declare const KEEPER_CLOB_ORDER_READ_RPS = 2;
|
|
62
|
+
export declare const KEEPER_CLOB_ORDER_READ_BURST = 8;
|
|
63
|
+
/** Internal budget: POST /order (burst per 10s, sustained per 10min). */
|
|
64
|
+
export declare const KEEPER_CLOB_POST_ORDER_MAX_PER_10S = 20;
|
|
65
|
+
export declare const KEEPER_CLOB_POST_ORDER_MAX_PER_10MIN = 200;
|
|
66
|
+
/** Internal budget: DELETE /order(s) (burst per 10s, sustained per 10min). */
|
|
67
|
+
export declare const KEEPER_CLOB_DELETE_ORDER_MAX_PER_10S = 15;
|
|
68
|
+
export declare const KEEPER_CLOB_DELETE_ORDER_MAX_PER_10MIN = 150;
|
|
69
|
+
/** Internal budget: DELETE /cancel-all. */
|
|
70
|
+
export declare const KEEPER_CLOB_CANCEL_ALL_MAX_PER_10S = 3;
|
|
71
|
+
export declare const KEEPER_CLOB_CANCEL_ALL_MAX_PER_10MIN = 30;
|
|
72
|
+
/**
|
|
73
|
+
* Lightweight sliding-window rate limiter.
|
|
74
|
+
*
|
|
75
|
+
* Uses a circular buffer of fixed-size time slots.
|
|
76
|
+
* No historical data is kept — old slots are zeroed on access.
|
|
77
|
+
* Memory: O(windowMs / slotMs) integers per bucket.
|
|
78
|
+
*/
|
|
79
|
+
export declare class SlidingWindowLimiter {
|
|
80
|
+
/** Max requests allowed in the window. */
|
|
81
|
+
readonly maxRequests: number;
|
|
82
|
+
/** Window duration in milliseconds. */
|
|
83
|
+
readonly windowMs: number;
|
|
84
|
+
/** Clock override for testing. */
|
|
85
|
+
private readonly now;
|
|
86
|
+
private readonly slots;
|
|
87
|
+
private readonly slotMs;
|
|
88
|
+
private readonly numSlots;
|
|
89
|
+
private lastSlotIndex;
|
|
90
|
+
private lastAccessMs;
|
|
91
|
+
constructor(
|
|
92
|
+
/** Max requests allowed in the window. */
|
|
93
|
+
maxRequests: number,
|
|
94
|
+
/** Window duration in milliseconds. */
|
|
95
|
+
windowMs: number,
|
|
96
|
+
/** Clock override for testing. */
|
|
97
|
+
now?: () => number);
|
|
98
|
+
/** Try to acquire a permit. Returns true if allowed, false if over budget. */
|
|
99
|
+
tryAcquire(): boolean;
|
|
100
|
+
/** Current request count in the window. */
|
|
101
|
+
currentCount(): number;
|
|
102
|
+
/** Remaining permits in the window. */
|
|
103
|
+
remaining(): number;
|
|
104
|
+
/** Estimated wait time (ms) until a permit becomes available. Returns 0 if available now. */
|
|
105
|
+
waitTimeMs(): number;
|
|
106
|
+
/** Async acquire: waits if over budget. */
|
|
107
|
+
acquire(): Promise<void>;
|
|
108
|
+
/** Reset all counters (useful after long pauses or circuit recovery). */
|
|
109
|
+
reset(): void;
|
|
110
|
+
private advance;
|
|
111
|
+
private countAll;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Enforces both a short burst window and a longer sustained window.
|
|
115
|
+
*
|
|
116
|
+
* Example: POST /order allows 3500/10s burst AND 36000/10min sustained.
|
|
117
|
+
* We use conservative keeper budgets well below these.
|
|
118
|
+
*/
|
|
119
|
+
export declare class DualWindowLimiter {
|
|
120
|
+
readonly burst: SlidingWindowLimiter;
|
|
121
|
+
readonly sustained: SlidingWindowLimiter;
|
|
122
|
+
constructor(burstMax: number, burstWindowMs: number, sustainedMax: number, sustainedWindowMs: number, now?: () => number);
|
|
123
|
+
tryAcquire(): boolean;
|
|
124
|
+
acquire(): Promise<void>;
|
|
125
|
+
remaining(): number;
|
|
126
|
+
reset(): void;
|
|
127
|
+
}
|
|
128
|
+
export type EndpointClass = "clob_read" | "clob_order_read" | "clob_post_order" | "clob_delete_order" | "clob_cancel_all" | "clob_post_orders";
|
|
129
|
+
/**
|
|
130
|
+
* Create a set of endpoint-class limiters with keeper-conservative budgets.
|
|
131
|
+
*
|
|
132
|
+
* Each limiter is independent — /book reads don't eat into /order write budget.
|
|
133
|
+
* Returns a plain object so callers can pick the right limiter per request.
|
|
134
|
+
*/
|
|
135
|
+
export declare function createEndpointLimiters(now?: () => number): Record<EndpointClass, SlidingWindowLimiter | DualWindowLimiter>;
|
|
136
|
+
/**
|
|
137
|
+
* Classify a CLOB path into an endpoint class for rate limiting.
|
|
138
|
+
*/
|
|
139
|
+
export declare function classifyEndpoint(method: string, path: string): EndpointClass;
|
|
140
|
+
//# sourceMappingURL=rate-limits.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limits.d.ts","sourceRoot":"","sources":["../src/rate-limits.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAMH,+CAA+C;AAC/C,eAAO,MAAM,gCAAgC,QAAS,CAAC;AAIvD,eAAO,MAAM,0BAA0B,OAAQ,CAAC;AAChD,eAAO,MAAM,uBAAuB,OAAQ,CAAC;AAC7C,eAAO,MAAM,wBAAwB,MAAM,CAAC;AAC5C,eAAO,MAAM,wBAAwB,OAAQ,CAAC;AAC9C,eAAO,MAAM,yBAAyB,MAAM,CAAC;AAC7C,eAAO,MAAM,2BAA2B,OAAQ,CAAC;AACjD,eAAO,MAAM,4BAA4B,MAAM,CAAC;AAChD,eAAO,MAAM,4BAA4B,MAAM,CAAC;AAChD,eAAO,MAAM,gCAAgC,OAAQ,CAAC;AACtD,eAAO,MAAM,wCAAwC,MAAM,CAAC;AAC5D,eAAO,MAAM,2CAA2C,KAAK,CAAC;AAI9D,eAAO,MAAM,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