@pear-protocol/exchanges-sdk 0.7.0 → 0.8.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/dist/index.d.ts +22 -5
- package/dist/syncer/base.js +36 -14
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -562,9 +562,26 @@ interface AccountSyncerConfig {
|
|
|
562
562
|
credentials: SupportedCredentials;
|
|
563
563
|
demo: boolean;
|
|
564
564
|
}
|
|
565
|
+
/**
|
|
566
|
+
* Outcome of one fill sync cycle, so batch/cron callers can tell a completed
|
|
567
|
+
* cycle apart from one that must be retried:
|
|
568
|
+
* - `synced`: the cycle posted, or there was nothing to post.
|
|
569
|
+
* - `deferred`: the server answered 409 SYNC_CREATE_*; the watermark did not
|
|
570
|
+
* advance, so a later cycle retries the same window.
|
|
571
|
+
* - `aborted`: the exchange fetch failed; nothing was posted.
|
|
572
|
+
*/
|
|
573
|
+
type FillSyncOutcome = 'synced' | 'deferred' | 'aborted';
|
|
574
|
+
/**
|
|
575
|
+
* Outcome of one funding sync cycle. Funding has no server deferral contract,
|
|
576
|
+
* so the union is narrower than {@link FillSyncOutcome}:
|
|
577
|
+
* - `synced`: the cycle posted, or there were no new payments.
|
|
578
|
+
* - `aborted`: the exchange funding fetch failed; nothing was posted.
|
|
579
|
+
* A failed post still throws — only the exchange fetch is guarded.
|
|
580
|
+
*/
|
|
581
|
+
type FundingSyncOutcome = 'synced' | 'aborted';
|
|
565
582
|
interface AccountSyncer {
|
|
566
|
-
synchronizeFills(): Promise<
|
|
567
|
-
synchronizeFunding(): Promise<
|
|
583
|
+
synchronizeFills(): Promise<FillSyncOutcome>;
|
|
584
|
+
synchronizeFunding(): Promise<FundingSyncOutcome>;
|
|
568
585
|
}
|
|
569
586
|
interface ListExternalFillsParams {
|
|
570
587
|
since: Date;
|
|
@@ -614,8 +631,8 @@ declare abstract class BaseAccountSyncer<ExternalFill> implements AccountSyncer
|
|
|
614
631
|
* Independent of the fill sync cycle (`synchronizeFills`) — funding rows are
|
|
615
632
|
* immutable and upsert idempotently, so this is driven on its own cadence.
|
|
616
633
|
*/
|
|
617
|
-
synchronizeFunding(): Promise<
|
|
618
|
-
synchronizeFills(): Promise<
|
|
634
|
+
synchronizeFunding(): Promise<FundingSyncOutcome>;
|
|
635
|
+
synchronizeFills(): Promise<FillSyncOutcome>;
|
|
619
636
|
}
|
|
620
637
|
|
|
621
638
|
interface BinanceRawUserTrade {
|
|
@@ -2261,4 +2278,4 @@ declare class ExchangesSDK {
|
|
|
2261
2278
|
private fetchCredentials;
|
|
2262
2279
|
}
|
|
2263
2280
|
|
|
2264
|
-
export { ACCOUNT_MODE_LABELS, ASSET_DECIMALS, AccountBalance, AccountBalanceCore, type AccountHealthStatus, AccountMode, type AccountPosition, type AccountSnapshot, type AccountStatus, type AccountSyncer, type AccountSyncerConfig, type AccountTracker, type AccountTrackerConfig, type AccountWarningReason, type ApiKey, type ApproveIntegratorParams, type ApproveIntegratorResult, AssetId, type AssetSnapshotMap, type BalanceCallback, BaseAccountSyncer, BaseAccountTracker, BaseWs, BasketLeg, BinanceAccountSyncer, BinanceAccountTracker, type BinanceAccountTrackerConfig, BinanceExchangeWs, BinanceMultiAssetBalance, BinanceSingleAssetBalance, BinanceUserDataWs, type BinanceUserDataWsConfig, BinanceWsApi, type BinanceWsApiConfig, BybitAccountSyncer, BybitAccountTracker, type BybitAccountTrackerConfig, BybitExchangeWs, BybitIsolatedMarginBalance, BybitPortfolioMarginBalance, BybitRegularMarginBalance, BybitWs, type BybitWsConfig, type CappedBasketLeg, type CctpDepositHistorySettlement, type CctpDepositIntentParams, type CctpDepositIntentResult, type CctpDepositParams, type CctpDepositProgress, type CctpDepositRegistrationSettlement, type CctpDepositResult, type CctpDepositSettlement, type CctpDepositWalletAdapter, type CheckApiWalletParams, CollateralBalance, type ConnectOptions, type ConnectWalletParams, type ConnectWalletResult, type CreateAccountTrackerConfig, type CreateExchangeWsConfig, DecimalString, type DepositParams, type DepositResult, type ExchangeConnection, type ExchangeHelperExchange, type ExchangeHelperMap, type ExchangeState, ExchangeWs, type ExchangeWsByConnector, ExchangesSDK, type ExchangesSDKConfig, type FastWithdrawParams, type FastWithdrawResult, HyperliquidAccountSyncer, HyperliquidAccountTracker, type HyperliquidAccountTrackerConfig, HyperliquidDexState, HyperliquidExchangeWs, type HyperliquidHelper, HyperliquidPortfolioMarginBalance, HyperliquidPortfolioMarginCollateral, HyperliquidStandardBalance, HyperliquidUnifiedBalance, HyperliquidWs, type IntegratorApprovalRequirements, L2_ASSET_DECIMALS, LIGHTER_CCTP_USDC_ADDRESSES, LIGHTER_CCTP_USDC_DECIMALS, LighterAccountSyncer, LighterAccountTracker, type LighterAccountTrackerConfig, LighterClassicBalance, type LighterDeposit, type LighterDepositNetwork, LighterExchangeWs, type LighterHelper, type LighterLookupResult, type LighterOnboardingStatus, LighterUnifiedBalance, LighterWs, type ListExternalFillsParams, type ListExternalFundingParams, type ListExternalPositionsParams, MAX_API_KEY_INDEX, MIN_LIGHTER_CCTP_DEPOSIT_USDC, MIN_LIGHTER_FAST_WITHDRAW_USDC, MIN_USER_API_KEY_INDEX, MarginMode, type MaxBasketSizeCalculation, MaxBasketSizeInput, MaxBasketSizeResult, type MessageListener, OkxAccountSyncer, OkxAccountTracker, type OkxAccountTrackerConfig, OkxExchangeWs, OkxFuturesBalance, OkxMultiCurrencyMarginBalance, OkxPortfolioMarginBalance, OkxSpotBalance, OkxWs, type OkxWsConfig, type OpenListener, type PositionsCallback, RouteType, Side, SignedDecimalString, type StateUpdate, type SupportedCredentials, type TrackedAssetCallback, type TrackedAssetInfo, type WalletClientWithAccount, type WithdrawParams, type WithdrawResult, type WsMessage, computeMaxBasketSize, createAccountSyncer, createAccountTracker, createExchangeWs, createHyperliquidHelper, createLighterHelper, findAvailableApiKeyIndex, getAssetDecimals, getLighterCctpUsdcAddress, usdcAbi };
|
|
2281
|
+
export { ACCOUNT_MODE_LABELS, ASSET_DECIMALS, AccountBalance, AccountBalanceCore, type AccountHealthStatus, AccountMode, type AccountPosition, type AccountSnapshot, type AccountStatus, type AccountSyncer, type AccountSyncerConfig, type AccountTracker, type AccountTrackerConfig, type AccountWarningReason, type ApiKey, type ApproveIntegratorParams, type ApproveIntegratorResult, AssetId, type AssetSnapshotMap, type BalanceCallback, BaseAccountSyncer, BaseAccountTracker, BaseWs, BasketLeg, BinanceAccountSyncer, BinanceAccountTracker, type BinanceAccountTrackerConfig, BinanceExchangeWs, BinanceMultiAssetBalance, BinanceSingleAssetBalance, BinanceUserDataWs, type BinanceUserDataWsConfig, BinanceWsApi, type BinanceWsApiConfig, BybitAccountSyncer, BybitAccountTracker, type BybitAccountTrackerConfig, BybitExchangeWs, BybitIsolatedMarginBalance, BybitPortfolioMarginBalance, BybitRegularMarginBalance, BybitWs, type BybitWsConfig, type CappedBasketLeg, type CctpDepositHistorySettlement, type CctpDepositIntentParams, type CctpDepositIntentResult, type CctpDepositParams, type CctpDepositProgress, type CctpDepositRegistrationSettlement, type CctpDepositResult, type CctpDepositSettlement, type CctpDepositWalletAdapter, type CheckApiWalletParams, CollateralBalance, type ConnectOptions, type ConnectWalletParams, type ConnectWalletResult, type CreateAccountTrackerConfig, type CreateExchangeWsConfig, DecimalString, type DepositParams, type DepositResult, type ExchangeConnection, type ExchangeHelperExchange, type ExchangeHelperMap, type ExchangeState, ExchangeWs, type ExchangeWsByConnector, ExchangesSDK, type ExchangesSDKConfig, type FastWithdrawParams, type FastWithdrawResult, type FillSyncOutcome, type FundingSyncOutcome, HyperliquidAccountSyncer, HyperliquidAccountTracker, type HyperliquidAccountTrackerConfig, HyperliquidDexState, HyperliquidExchangeWs, type HyperliquidHelper, HyperliquidPortfolioMarginBalance, HyperliquidPortfolioMarginCollateral, HyperliquidStandardBalance, HyperliquidUnifiedBalance, HyperliquidWs, type IntegratorApprovalRequirements, L2_ASSET_DECIMALS, LIGHTER_CCTP_USDC_ADDRESSES, LIGHTER_CCTP_USDC_DECIMALS, LighterAccountSyncer, LighterAccountTracker, type LighterAccountTrackerConfig, LighterClassicBalance, type LighterDeposit, type LighterDepositNetwork, LighterExchangeWs, type LighterHelper, type LighterLookupResult, type LighterOnboardingStatus, LighterUnifiedBalance, LighterWs, type ListExternalFillsParams, type ListExternalFundingParams, type ListExternalPositionsParams, MAX_API_KEY_INDEX, MIN_LIGHTER_CCTP_DEPOSIT_USDC, MIN_LIGHTER_FAST_WITHDRAW_USDC, MIN_USER_API_KEY_INDEX, MarginMode, type MaxBasketSizeCalculation, MaxBasketSizeInput, MaxBasketSizeResult, type MessageListener, OkxAccountSyncer, OkxAccountTracker, type OkxAccountTrackerConfig, OkxExchangeWs, OkxFuturesBalance, OkxMultiCurrencyMarginBalance, OkxPortfolioMarginBalance, OkxSpotBalance, OkxWs, type OkxWsConfig, type OpenListener, type PositionsCallback, RouteType, Side, SignedDecimalString, type StateUpdate, type SupportedCredentials, type TrackedAssetCallback, type TrackedAssetInfo, type WalletClientWithAccount, type WithdrawParams, type WithdrawResult, type WsMessage, computeMaxBasketSize, createAccountSyncer, createAccountTracker, createExchangeWs, createHyperliquidHelper, createLighterHelper, findAvailableApiKeyIndex, getAssetDecimals, getLighterCctpUsdcAddress, usdcAbi };
|
package/dist/syncer/base.js
CHANGED
|
@@ -99,10 +99,24 @@ class BaseAccountSyncer {
|
|
|
99
99
|
return;
|
|
100
100
|
}
|
|
101
101
|
if (payload.reconcilables) {
|
|
102
|
-
|
|
102
|
+
const reconcileBytes = new Blob([
|
|
103
|
+
JSON.stringify(payload.reconcilables)
|
|
104
|
+
]).size;
|
|
105
|
+
if (reconcileBytes > 50 * 1024) {
|
|
106
|
+
await this.sdk.core.sync.fills.reconcileBulk(payload.reconcilables);
|
|
107
|
+
} else {
|
|
108
|
+
await this.sdk.core.sync.fills.reconcile(payload.reconcilables);
|
|
109
|
+
}
|
|
103
110
|
}
|
|
104
111
|
if (payload.confirmables) {
|
|
105
|
-
|
|
112
|
+
const confirmBytes = new Blob([
|
|
113
|
+
JSON.stringify(payload.confirmables)
|
|
114
|
+
]).size;
|
|
115
|
+
if (confirmBytes > 50 * 1024) {
|
|
116
|
+
await this.sdk.core.sync.fills.confirmBulk(payload.confirmables);
|
|
117
|
+
} else {
|
|
118
|
+
await this.sdk.core.sync.fills.confirm(payload.confirmables);
|
|
119
|
+
}
|
|
106
120
|
}
|
|
107
121
|
const { firstSyncTrim } = payload;
|
|
108
122
|
let meta = {
|
|
@@ -128,14 +142,14 @@ class BaseAccountSyncer {
|
|
|
128
142
|
exchangePositions: payload.exchangePositions,
|
|
129
143
|
meta
|
|
130
144
|
};
|
|
131
|
-
const
|
|
145
|
+
const createBytes = new Blob([
|
|
132
146
|
JSON.stringify(createBody)
|
|
133
147
|
]).size;
|
|
134
|
-
if (
|
|
148
|
+
if (createBytes > 50 * 1024) {
|
|
135
149
|
await this.sdk.core.sync.fills.createBulk(createBody);
|
|
136
|
-
|
|
150
|
+
} else {
|
|
151
|
+
await this.sdk.core.sync.fills.create(createBody);
|
|
137
152
|
}
|
|
138
|
-
await this.sdk.core.sync.fills.create(createBody);
|
|
139
153
|
}
|
|
140
154
|
/**
|
|
141
155
|
* Pull historical funding payments since the funding watermark and post them.
|
|
@@ -151,12 +165,18 @@ class BaseAccountSyncer {
|
|
|
151
165
|
overlapMs: 6e4
|
|
152
166
|
})
|
|
153
167
|
]);
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
168
|
+
let payments;
|
|
169
|
+
try {
|
|
170
|
+
payments = await this.listExternalFunding({
|
|
171
|
+
since: new Date(watermark.sinceMs),
|
|
172
|
+
instruments
|
|
173
|
+
});
|
|
174
|
+
} catch (error) {
|
|
175
|
+
console.error(`${this.connector}: funding fetch failed \u2014 funding sync aborted, retry next tick`, error);
|
|
176
|
+
return "aborted";
|
|
177
|
+
}
|
|
158
178
|
if (payments.length === 0) {
|
|
159
|
-
return;
|
|
179
|
+
return "synced";
|
|
160
180
|
}
|
|
161
181
|
const externalMaxTs = Math.max(...payments.map((payment) => payment.timestamp));
|
|
162
182
|
const createBody = {
|
|
@@ -170,9 +190,10 @@ class BaseAccountSyncer {
|
|
|
170
190
|
]).size;
|
|
171
191
|
if (bytes > 50 * 1024) {
|
|
172
192
|
await this.sdk.core.sync.funding.createBulk(createBody);
|
|
173
|
-
return;
|
|
193
|
+
return "synced";
|
|
174
194
|
}
|
|
175
195
|
await this.sdk.core.sync.funding.create(createBody);
|
|
196
|
+
return "synced";
|
|
176
197
|
}
|
|
177
198
|
async synchronizeFills() {
|
|
178
199
|
const state = await this.loadPearState();
|
|
@@ -181,17 +202,18 @@ class BaseAccountSyncer {
|
|
|
181
202
|
snapshot = await this.snapshotExchange(state);
|
|
182
203
|
} catch (error) {
|
|
183
204
|
console.error(`${this.connector}: position/fill fetch failed \u2014 sync cycle aborted, retry next tick`, error);
|
|
184
|
-
return;
|
|
205
|
+
return "aborted";
|
|
185
206
|
}
|
|
186
207
|
try {
|
|
187
208
|
await this.computeAndPostSync(state, snapshot);
|
|
188
209
|
} catch (error) {
|
|
189
210
|
if (isSyncCreateDeferral(error)) {
|
|
190
211
|
console.log(`${this.connector}: sync cycle deferred by the server (${error.errorCode}) \u2014 retry next tick`);
|
|
191
|
-
return;
|
|
212
|
+
return "deferred";
|
|
192
213
|
}
|
|
193
214
|
throw error;
|
|
194
215
|
}
|
|
216
|
+
return "synced";
|
|
195
217
|
}
|
|
196
218
|
}
|
|
197
219
|
function isSyncCreateDeferral(error) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pear-protocol/exchanges-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Pear Protocol Exchanges SDK",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
"typecheck": "tsc --noEmit"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@pear-protocol/core-sdk": "^1.
|
|
29
|
-
"@pear-protocol/types": "^1.
|
|
28
|
+
"@pear-protocol/core-sdk": "^1.18.0",
|
|
29
|
+
"@pear-protocol/types": "^1.27.0",
|
|
30
30
|
"@pear-protocol/utils": "^0.5.1",
|
|
31
31
|
"bignumber.js": "^11.1.1",
|
|
32
32
|
"partysocket": "^1.0.3",
|