@pear-protocol/exchanges-sdk 0.6.3 → 0.6.5

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.
@@ -90,6 +90,7 @@ class BaseAccountSyncer {
90
90
  const { firstSyncTrim } = payload;
91
91
  let meta = {
92
92
  ...payload.meta,
93
+ ...payload.meta.externalMaxTs === null ? {} : { newestFillTs: payload.meta.externalMaxTs },
93
94
  ...firstSyncStartedAt === void 0 ? {} : { firstSyncStartedAt }
94
95
  };
95
96
  if (firstSyncTrim) {
@@ -144,8 +145,23 @@ class BaseAccountSyncer {
144
145
  console.error(`${this.connector}: position/fill fetch failed \u2014 sync cycle aborted, retry next tick`, error);
145
146
  return;
146
147
  }
147
- await this.computeAndPostSync(state, snapshot);
148
+ try {
149
+ await this.computeAndPostSync(state, snapshot);
150
+ } catch (error) {
151
+ if (isSyncCreateDeferral(error)) {
152
+ console.log(`${this.connector}: sync cycle deferred by the server (${error.errorCode}) \u2014 retry next tick`);
153
+ return;
154
+ }
155
+ throw error;
156
+ }
157
+ }
158
+ }
159
+ function isSyncCreateDeferral(error) {
160
+ if (typeof error !== "object" || error === null) {
161
+ return false;
148
162
  }
163
+ const { status, errorCode } = error;
164
+ return status === 409 && typeof errorCode === "string" && errorCode.startsWith("SYNC_CREATE_");
149
165
  }
150
166
 
151
167
  export { BaseAccountSyncer };
@@ -1,3 +1,4 @@
1
+ import { FetchError } from '@pear-protocol/core-sdk';
1
2
  import { describe, afterEach, vi, it, expect } from 'vitest';
2
3
  import { BaseAccountSyncer } from './base';
3
4
 
@@ -114,6 +115,13 @@ describe("BaseAccountSyncer \u2014 first-sync plumbing", () => {
114
115
  await syncer.synchronizeFills();
115
116
  expect(sync.fills.create.mock.calls[0]?.[0]?.meta.externalMaxTs).toBe(NEWEST_TS + 6e4 + 1);
116
117
  });
118
+ it("carries the raw newest fill untouched next to a bumped watermark", async () => {
119
+ const { syncer, sync } = makeSyncer({ neverSynced: true });
120
+ syncer.positionsFn.mockResolvedValue(SOL_SNAPSHOT);
121
+ syncer.fillsFn.mockResolvedValue(trimmableFills({ open: 1e6, close: NEWEST_TS - 2e4 }));
122
+ await syncer.synchronizeFills();
123
+ expect(sync.fills.create.mock.calls[0]?.[0]?.meta.newestFillTs).toBe(NEWEST_TS);
124
+ });
117
125
  it("leaves the watermark unbumped when the trimmed era is entirely older than the overlap window", async () => {
118
126
  const { syncer, sync } = makeSyncer({ neverSynced: true });
119
127
  syncer.positionsFn.mockResolvedValue(SOL_SNAPSHOT);
@@ -132,6 +140,28 @@ describe("BaseAccountSyncer \u2014 first-sync plumbing", () => {
132
140
  expect(trimLogs).toHaveLength(1);
133
141
  expect(String(trimLogs[0]?.[0])).toContain("hyperliquid");
134
142
  });
143
+ it("swallows a SYNC_CREATE_* deferral so the next tick retries the same window", async () => {
144
+ const logSpy = vi.spyOn(console, "log").mockImplementation(() => {
145
+ });
146
+ const { syncer, sync } = makeSyncer({ neverSynced: false });
147
+ syncer.positionsFn.mockResolvedValue(SOL_SNAPSHOT);
148
+ syncer.fillsFn.mockResolvedValue([
149
+ extFill({ exchangeFillId: "e", symbol: "SOL", side: "BUY", quantity: "2", timestamp: NEWEST_TS })
150
+ ]);
151
+ sync.fills.create.mockRejectedValue(new FetchError("deferred", "Conflict", 409, "SYNC_CREATE_EXECUTION_IN_FLIGHT"));
152
+ await expect(syncer.synchronizeFills()).resolves.toBeUndefined();
153
+ const deferLogs = logSpy.mock.calls.filter((call) => String(call[0]).includes("SYNC_CREATE_EXECUTION_IN_FLIGHT"));
154
+ expect(deferLogs).toHaveLength(1);
155
+ });
156
+ it("propagates a 409 that is not a sync-create deferral", async () => {
157
+ const { syncer, sync } = makeSyncer({ neverSynced: false });
158
+ syncer.positionsFn.mockResolvedValue(SOL_SNAPSHOT);
159
+ syncer.fillsFn.mockResolvedValue([
160
+ extFill({ exchangeFillId: "e", symbol: "SOL", side: "BUY", quantity: "2", timestamp: NEWEST_TS })
161
+ ]);
162
+ sync.fills.create.mockRejectedValue(new FetchError("conflict", "Conflict", 409, "NOOP_ERROR"));
163
+ await expect(syncer.synchronizeFills()).rejects.toThrow("conflict");
164
+ });
135
165
  it("neverSynced=false: positions fetched once, no bump, no trim log", async () => {
136
166
  const logSpy = vi.spyOn(console, "log").mockImplementation(() => {
137
167
  });
@@ -142,7 +172,7 @@ describe("BaseAccountSyncer \u2014 first-sync plumbing", () => {
142
172
  ]);
143
173
  await syncer.synchronizeFills();
144
174
  expect(syncer.positionsFn).toHaveBeenCalledTimes(1);
145
- expect(sync.fills.create.mock.calls[0]?.[0]?.meta).toEqual({ externalMaxTs: NEWEST_TS });
175
+ expect(sync.fills.create.mock.calls[0]?.[0]?.meta).toEqual({ externalMaxTs: NEWEST_TS, newestFillTs: NEWEST_TS });
146
176
  expect(logSpy).not.toHaveBeenCalled();
147
177
  });
148
178
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pear-protocol/exchanges-sdk",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "description": "Pear Protocol Exchanges SDK",
5
5
  "private": false,
6
6
  "type": "module",
@@ -27,9 +27,9 @@
27
27
  "test:watch": "vitest"
28
28
  },
29
29
  "dependencies": {
30
- "@pear-protocol/core-sdk": "^1.12.0",
31
- "@pear-protocol/types": "^1.15.1",
32
- "@pear-protocol/utils": "^0.4.1",
30
+ "@pear-protocol/core-sdk": "^1.15.0",
31
+ "@pear-protocol/types": "^1.20.0",
32
+ "@pear-protocol/utils": "^0.5.1",
33
33
  "bignumber.js": "^11.1.1",
34
34
  "partysocket": "^1.0.3",
35
35
  "viem": "^2.48.8"