@bluefin-exchange/pro-sdk 1.17.0 → 2.0.0-beta.1

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.
Files changed (62) hide show
  1. package/README.md +4 -4
  2. package/dist/esm/index.js +1 -17
  3. package/dist/esm/index.js.map +1 -1
  4. package/dist/esm/src/api.js +1041 -1277
  5. package/dist/esm/src/api.js.map +1 -1
  6. package/dist/esm/src/base.js +12 -17
  7. package/dist/esm/src/base.js.map +1 -1
  8. package/dist/esm/src/common.js +31 -59
  9. package/dist/esm/src/common.js.map +1 -1
  10. package/dist/esm/src/configuration.js +63 -7
  11. package/dist/esm/src/configuration.js.map +1 -1
  12. package/dist/esm/src/index.js +6 -22
  13. package/dist/esm/src/index.js.map +1 -1
  14. package/dist/esm/src/request-signer.js +65 -90
  15. package/dist/esm/src/request-signer.js.map +1 -1
  16. package/dist/esm/src/sdk.js +549 -595
  17. package/dist/esm/src/sdk.js.map +1 -1
  18. package/dist/esm/src/utils.js +1 -4
  19. package/dist/esm/src/utils.js.map +1 -1
  20. package/dist/esm/src/websocket.js +72 -92
  21. package/dist/esm/src/websocket.js.map +1 -1
  22. package/dist/types/index.d.ts +1 -1
  23. package/dist/types/index.d.ts.map +1 -1
  24. package/dist/types/src/api.d.ts +3 -3
  25. package/dist/types/src/api.d.ts.map +1 -1
  26. package/dist/types/src/base.d.ts +1 -1
  27. package/dist/types/src/base.d.ts.map +1 -1
  28. package/dist/types/src/common.d.ts +2 -2
  29. package/dist/types/src/common.d.ts.map +1 -1
  30. package/dist/types/src/index.d.ts +6 -6
  31. package/dist/types/src/index.d.ts.map +1 -1
  32. package/dist/types/src/request-signer.d.ts +4 -3
  33. package/dist/types/src/request-signer.d.ts.map +1 -1
  34. package/dist/types/src/sdk.d.ts +11 -10
  35. package/dist/types/src/sdk.d.ts.map +1 -1
  36. package/package.json +16 -13
  37. package/dist/cjs/example.js +0 -277
  38. package/dist/cjs/example.js.map +0 -1
  39. package/dist/cjs/index.js +0 -18
  40. package/dist/cjs/index.js.map +0 -1
  41. package/dist/cjs/src/api.js +0 -5729
  42. package/dist/cjs/src/api.js.map +0 -1
  43. package/dist/cjs/src/base.js +0 -69
  44. package/dist/cjs/src/base.js.map +0 -1
  45. package/dist/cjs/src/common.js +0 -162
  46. package/dist/cjs/src/common.js.map +0 -1
  47. package/dist/cjs/src/configuration.js +0 -45
  48. package/dist/cjs/src/configuration.js.map +0 -1
  49. package/dist/cjs/src/index.js +0 -23
  50. package/dist/cjs/src/index.js.map +0 -1
  51. package/dist/cjs/src/request-signer.js +0 -234
  52. package/dist/cjs/src/request-signer.js.map +0 -1
  53. package/dist/cjs/src/sdk.js +0 -864
  54. package/dist/cjs/src/sdk.js.map +0 -1
  55. package/dist/cjs/src/utils.js +0 -10
  56. package/dist/cjs/src/utils.js.map +0 -1
  57. package/dist/cjs/src/websocket.js +0 -159
  58. package/dist/cjs/src/websocket.js.map +0 -1
  59. package/dist/esm/example.js +0 -277
  60. package/dist/esm/example.js.map +0 -1
  61. package/dist/types/example.d.ts +0 -2
  62. package/dist/types/example.d.ts.map +0 -1
@@ -1,864 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- var _a;
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.BluefinProSdk = void 0;
17
- const api_1 = require("./api");
18
- const configuration_1 = require("./configuration");
19
- const ws_1 = require("ws");
20
- const v3_1 = require("@firefly-exchange/library-sui/v3");
21
- const library_sui_1 = require("@firefly-exchange/library-sui");
22
- // RewardsDistributorInteractor for reward claiming
23
- const index_1 = require("@firefly-exchange/library-sui/index");
24
- const bcs_1 = require("@mysten/bcs");
25
- const axios_1 = __importDefault(require("axios"));
26
- // Keep HTTP/2 as default in Node.js. Consumers can opt out with
27
- // BLUEFIN_SDK_HTTP_VERSION=1.
28
- if (typeof process !== 'undefined' &&
29
- process.versions &&
30
- process.versions.node) {
31
- const requestedHttpVersion = Number.parseInt((_a = process.env.BLUEFIN_SDK_HTTP_VERSION) !== null && _a !== void 0 ? _a : '2', 10);
32
- axios_1.default.defaults.httpVersion = requestedHttpVersion === 2 ? 2 : 1;
33
- }
34
- const environmentConfig = {
35
- mainnet: {
36
- authHost: 'https://auth.api.sui-prod.bluefin.io',
37
- apiHost: 'https://api.sui-prod.bluefin.io',
38
- tradeHost: 'https://trade.api.sui-prod.bluefin.io',
39
- marketWsHost: 'wss://stream.api.sui-prod.bluefin.io/ws/market',
40
- accountWsHost: 'wss://stream.api.sui-prod.bluefin.io/ws/account',
41
- },
42
- testnet: {
43
- authHost: 'https://auth.api.sui-staging.bluefin.io',
44
- apiHost: 'https://api.sui-staging.bluefin.io',
45
- tradeHost: 'https://trade.api.sui-staging.bluefin.io',
46
- marketWsHost: 'wss://stream.api.sui-staging.bluefin.io/ws/market',
47
- accountWsHost: 'wss://stream.api.sui-staging.bluefin.io/ws/account',
48
- },
49
- devnet: {
50
- authHost: 'https://auth.api.sui-dev.bluefin.io',
51
- apiHost: 'https://api.sui-dev.bluefin.io',
52
- tradeHost: 'https://trade.api.sui-dev.bluefin.io',
53
- marketWsHost: 'wss://stream.api.sui-dev.bluefin.io/ws/market',
54
- accountWsHost: 'wss://stream.api.sui-dev.bluefin.io/ws/account',
55
- },
56
- };
57
- var Services;
58
- (function (Services) {
59
- Services[Services["Account"] = 0] = "Account";
60
- Services[Services["Exchange"] = 1] = "Exchange";
61
- Services[Services["Rewards"] = 2] = "Rewards";
62
- Services[Services["Trade"] = 3] = "Trade";
63
- Services[Services["Auth"] = 4] = "Auth";
64
- Services[Services["MarketWebsocket"] = 5] = "MarketWebsocket";
65
- Services[Services["AccountWebsocket"] = 6] = "AccountWebsocket";
66
- })(Services || (Services = {}));
67
- class BluefinProSdk {
68
- constructor(bfSigner, environment = 'mainnet', suiClient, opts) {
69
- var _a, _b, _c, _d, _e, _f;
70
- this.bfSigner = bfSigner;
71
- this.environment = environment;
72
- this.suiClient = suiClient;
73
- this.configs = {};
74
- /**
75
- * @description
76
- * build gasless transaction payload bytes
77
- * @param tx transcation block
78
- * @returns string
79
- * */
80
- this.buildGaslessTxPayloadBytes = (txb) => __awaiter(this, void 0, void 0, function* () {
81
- try {
82
- return yield library_sui_1.SuiBlocks.buildGaslessTxPayloadBytes(txb, this.suiClient);
83
- }
84
- catch (error) {
85
- throw new Error(error instanceof Error
86
- ? error.message
87
- : 'Build gasless tx payload bytes failed');
88
- }
89
- });
90
- this.currentAccountAddress = opts === null || opts === void 0 ? void 0 : opts.currentAccountAddress;
91
- this.isConnected = false;
92
- this.updateTokenTimeout = null;
93
- this.contractsConfig = undefined;
94
- this.tokenResponse = null;
95
- this.tokenSetAtSeconds = null;
96
- this.disableLoginPromptOnLogout = (_a = opts === null || opts === void 0 ? void 0 : opts.disableLoginPromptOnLogout) !== null && _a !== void 0 ? _a : false;
97
- this.onLogout = opts === null || opts === void 0 ? void 0 : opts.onLogout;
98
- this.onAccessTokenUpdate = opts === null || opts === void 0 ? void 0 : opts.onAccessTokenUpdate;
99
- this.isRefreshing = false;
100
- this.refreshTokenPromise = null;
101
- this.visibilityChangeHandler = undefined;
102
- this.onlineHandler = undefined;
103
- this.offlineHandler = undefined;
104
- // Initialize time offset based on provided currentTimeMs
105
- if ((opts === null || opts === void 0 ? void 0 : opts.currentTimeMs) !== undefined) {
106
- this.timeOffsetMs = opts.currentTimeMs - Date.now();
107
- }
108
- else {
109
- this.timeOffsetMs = 0;
110
- }
111
- if ((opts === null || opts === void 0 ? void 0 : opts.refreshToken) && (opts === null || opts === void 0 ? void 0 : opts.refreshTokenValidForSeconds)) {
112
- this.tokenResponse = {
113
- accessToken: '',
114
- accessTokenValidForSeconds: 0,
115
- refreshToken: opts.refreshToken,
116
- refreshTokenValidForSeconds: opts.refreshTokenValidForSeconds,
117
- };
118
- // Set timestamp when refresh token was provided
119
- this.tokenSetAtSeconds = Date.now() / 1000;
120
- }
121
- const defaultConfig = environmentConfig[this.environment];
122
- const basePaths = {
123
- authHost: (_b = opts === null || opts === void 0 ? void 0 : opts.authHost) !== null && _b !== void 0 ? _b : defaultConfig.authHost,
124
- apiHost: (_c = opts === null || opts === void 0 ? void 0 : opts.apiHost) !== null && _c !== void 0 ? _c : defaultConfig.apiHost,
125
- tradeHost: (_d = opts === null || opts === void 0 ? void 0 : opts.tradeHost) !== null && _d !== void 0 ? _d : defaultConfig.tradeHost,
126
- marketWsHost: (_e = opts === null || opts === void 0 ? void 0 : opts.marketWsHost) !== null && _e !== void 0 ? _e : defaultConfig.marketWsHost,
127
- accountWsHost: (_f = opts === null || opts === void 0 ? void 0 : opts.accountWsHost) !== null && _f !== void 0 ? _f : defaultConfig.accountWsHost,
128
- };
129
- const authApiConfig = new configuration_1.Configuration({
130
- basePath: basePaths.authHost,
131
- });
132
- this.configs[Services.Auth] = authApiConfig;
133
- this.authApi = new api_1.AuthApi(authApiConfig);
134
- const boundGetAccessToken = this.getAccessToken.bind(this);
135
- const exchangeApiConfig = new configuration_1.Configuration({
136
- basePath: basePaths.apiHost,
137
- });
138
- exchangeApiConfig.accessToken = boundGetAccessToken;
139
- this.configs[Services.Exchange] = exchangeApiConfig;
140
- this.exchangeDataApi = new api_1.ExchangeApi(exchangeApiConfig);
141
- const rewardsApiConfig = new configuration_1.Configuration({
142
- basePath: basePaths.apiHost,
143
- });
144
- rewardsApiConfig.accessToken = boundGetAccessToken;
145
- this.configs[Services.Rewards] = rewardsApiConfig;
146
- this.rewardsDataApi = new api_1.RewardsApi(rewardsApiConfig);
147
- const accountDataApiConfig = new configuration_1.Configuration({
148
- basePath: basePaths.apiHost,
149
- });
150
- accountDataApiConfig.accessToken = boundGetAccessToken;
151
- this.configs[Services.Account] = accountDataApiConfig;
152
- this.accountDataApi = new api_1.AccountDataApi(accountDataApiConfig);
153
- const tradeApiConfig = new configuration_1.Configuration({
154
- basePath: basePaths.tradeHost,
155
- });
156
- tradeApiConfig.accessToken = boundGetAccessToken;
157
- this.configs[Services.Trade] = tradeApiConfig;
158
- this.tradeApi = new api_1.TradeApi(tradeApiConfig);
159
- const marketWsConfig = new configuration_1.Configuration({
160
- basePath: basePaths.marketWsHost,
161
- });
162
- this.configs[Services.MarketWebsocket] = marketWsConfig;
163
- const accountWsConfig = new configuration_1.Configuration({
164
- basePath: basePaths.accountWsHost,
165
- });
166
- this.configs[Services.AccountWebsocket] = accountWsConfig;
167
- }
168
- createWallet() {
169
- const wallet = library_sui_1.Ed25519Keypair.generate();
170
- const signerKey = wallet.getSecretKey();
171
- const keyPair = (0, library_sui_1.decodeSuiPrivateKey)(signerKey);
172
- const publicAddress = wallet.toSuiAddress();
173
- return {
174
- privateKey: (0, bcs_1.toHex)(keyPair.secretKey),
175
- publicAddress,
176
- };
177
- }
178
- getTokenResponse() {
179
- return this.tokenResponse;
180
- }
181
- generateSalt() {
182
- return (Date.now() + Math.floor(Math.random() * 1000000)).toString();
183
- }
184
- getCurrentTimeMs() {
185
- return Date.now() + this.timeOffsetMs;
186
- }
187
- updateCurrentTimeMs(currentTimeMs) {
188
- this.timeOffsetMs = currentTimeMs - Date.now();
189
- }
190
- isRefreshTokenValid() {
191
- var _a;
192
- if (!((_a = this.tokenResponse) === null || _a === void 0 ? void 0 : _a.refreshToken) || !this.tokenSetAtSeconds) {
193
- return false;
194
- }
195
- const currentTimeSeconds = Date.now() / 1000;
196
- const refreshTokenExpiryTime = this.tokenSetAtSeconds + this.tokenResponse.refreshTokenValidForSeconds;
197
- // Add 60 second buffer to prevent using token right at expiry
198
- return currentTimeSeconds < refreshTokenExpiryTime - 60;
199
- }
200
- isAccessTokenExpired() {
201
- var _a;
202
- if (!((_a = this.tokenResponse) === null || _a === void 0 ? void 0 : _a.accessToken) || !this.tokenSetAtSeconds) {
203
- return true;
204
- }
205
- const currentTimeSeconds = Date.now() / 1000;
206
- const tokenLifetimeSeconds = this.tokenResponse.accessTokenValidForSeconds;
207
- const refreshAtLifetimePercentage = BluefinProSdk.TOKEN_REFRESH_THRESHOLD_PERCENTAGE;
208
- const refreshAtSeconds = this.tokenSetAtSeconds +
209
- tokenLifetimeSeconds * refreshAtLifetimePercentage;
210
- // Token is considered "expired" if we've passed the 80% lifetime mark
211
- return currentTimeSeconds >= refreshAtSeconds;
212
- }
213
- initializeTxBuilder() {
214
- return __awaiter(this, void 0, void 0, function* () {
215
- var _a, _b, _c, _d, _e, _f, _g, _h;
216
- this.txBuilder = new v3_1.TxBuilder({
217
- AdminCap: '',
218
- ExternalDataStore: ((_a = this.contractsConfig) === null || _a === void 0 ? void 0 : _a.edsId) || '',
219
- InternalDataStore: ((_b = this.contractsConfig) === null || _b === void 0 ? void 0 : _b.idsId) || '',
220
- Operators: {
221
- admin: ((_c = this.contractsConfig) === null || _c === void 0 ? void 0 : _c.operators.admin) || '',
222
- fee: ((_d = this.contractsConfig) === null || _d === void 0 ? void 0 : _d.operators.fee) || '',
223
- funding: ((_e = this.contractsConfig) === null || _e === void 0 ? void 0 : _e.operators.funding) || '',
224
- pruning: '',
225
- sequencer: ((_f = this.contractsConfig) === null || _f === void 0 ? void 0 : _f.operators.sequencer) || '',
226
- },
227
- Package: ((_g = this.contractsConfig) === null || _g === void 0 ? void 0 : _g.currentContractAddress) || '',
228
- Perpetuals: {},
229
- SupportedAssets: ((_h = this.assets) === null || _h === void 0 ? void 0 : _h.reduce((agg, x) => {
230
- agg[x.symbol] = Object.assign(Object.assign({}, x), { coinType: x.assetType });
231
- return agg;
232
- }, {})) || {},
233
- TreasuryCap: '',
234
- UpgradeCap: '',
235
- });
236
- });
237
- }
238
- initialize(options) {
239
- return __awaiter(this, void 0, void 0, function* () {
240
- this.initializeOptions = options;
241
- yield this.setContractsConfig();
242
- yield this.initializeTxBuilder();
243
- yield this.loginAndUpdateToken();
244
- this.setupVisibilityChangeListener();
245
- this.setupNetworkChangeListener();
246
- });
247
- }
248
- setContractsConfig() {
249
- return __awaiter(this, void 0, void 0, function* () {
250
- const response = yield this.exchangeDataApi.getExchangeInfo();
251
- this.contractsConfig = response.data.contractsConfig;
252
- this.assets = response.data.assets;
253
- });
254
- }
255
- loginAndUpdateToken() {
256
- return __awaiter(this, void 0, void 0, function* () {
257
- var _a;
258
- yield this.login();
259
- // Safety check - if login failed or logout was called, tokenResponse might be null
260
- if (!this.tokenResponse) {
261
- throw new Error('Login failed - no token response available');
262
- }
263
- this.isConnected = true;
264
- // Notify about token refresh
265
- (_a = this.onAccessTokenUpdate) === null || _a === void 0 ? void 0 : _a.call(this, this.tokenResponse.accessToken);
266
- // Schedule the next token refresh
267
- this.scheduleTokenRefresh();
268
- });
269
- }
270
- scheduleTokenRefresh() {
271
- // Clear any existing timeout
272
- if (this.updateTokenTimeout) {
273
- clearTimeout(this.updateTokenTimeout);
274
- this.updateTokenTimeout = null;
275
- }
276
- if (!this.isConnected || !this.tokenResponse || !this.tokenSetAtSeconds) {
277
- return;
278
- }
279
- // Calculate when to refresh: at 80% of token lifetime (or 20% before expiry)
280
- const currentTimeSeconds = Date.now() / 1000;
281
- const tokenLifetimeSeconds = this.tokenResponse.accessTokenValidForSeconds;
282
- const refreshAtLifetimePercentage = BluefinProSdk.TOKEN_REFRESH_THRESHOLD_PERCENTAGE;
283
- const refreshAtSeconds = this.tokenSetAtSeconds +
284
- tokenLifetimeSeconds * refreshAtLifetimePercentage;
285
- const millisecondsUntilRefresh = (refreshAtSeconds - currentTimeSeconds) * 1000;
286
- const delayMs = Math.max(millisecondsUntilRefresh, 0);
287
- console.log(`Scheduling token refresh in ${Math.round(delayMs / 1000)} seconds`);
288
- this.updateTokenTimeout = setTimeout(() => {
289
- this.refreshToken(true);
290
- }, delayMs);
291
- }
292
- setupVisibilityChangeListener() {
293
- // Only set up in browser environment
294
- if (typeof document !== 'undefined') {
295
- this.visibilityChangeHandler = () => {
296
- if (document.visibilityState === 'visible' && this.isConnected) {
297
- console.log('Page became visible, checking token status');
298
- this.handleVisible();
299
- }
300
- };
301
- document.addEventListener('visibilitychange', this.visibilityChangeHandler);
302
- }
303
- }
304
- setupNetworkChangeListener() {
305
- // Only set up in browser environment
306
- if (typeof navigator !== 'undefined' && 'onLine' in navigator) {
307
- window.addEventListener('online', this.handleOnline);
308
- window.addEventListener('offline', this.handleOffline);
309
- }
310
- }
311
- handleVisible() {
312
- return __awaiter(this, void 0, void 0, function* () {
313
- if (!this.tokenResponse || !this.tokenSetAtSeconds) {
314
- return;
315
- }
316
- this.refreshToken();
317
- });
318
- }
319
- handleOnline() {
320
- return __awaiter(this, void 0, void 0, function* () {
321
- if (!this.isConnected || !this.tokenResponse || !this.tokenSetAtSeconds) {
322
- return;
323
- }
324
- console.log('Network reconnected, resuming token refresh and checking token status');
325
- this.refreshToken(true);
326
- });
327
- }
328
- handleOffline() {
329
- if (this.updateTokenTimeout) {
330
- clearTimeout(this.updateTokenTimeout);
331
- this.updateTokenTimeout = null;
332
- }
333
- }
334
- login() {
335
- return __awaiter(this, void 0, void 0, function* () {
336
- var _a, _b, _c;
337
- console.log('Logging in to get the access token');
338
- if (!this.currentAccountAddress) {
339
- this.currentAccountAddress = this.bfSigner.getAddress();
340
- }
341
- console.log(`Logging in as ${this.currentAccountAddress}`);
342
- // Check if we have a valid refresh token first
343
- if (((_a = this.tokenResponse) === null || _a === void 0 ? void 0 : _a.refreshToken) && this.isRefreshTokenValid()) {
344
- try {
345
- console.log('Attempting to refresh token using refresh token');
346
- const response = yield this.authApi.authTokenRefreshPut({
347
- refreshToken: this.tokenResponse.refreshToken,
348
- });
349
- this.tokenResponse = response.data;
350
- this.tokenSetAtSeconds = Date.now() / 1000;
351
- console.log('Token refreshed successfully');
352
- return;
353
- }
354
- catch (e) {
355
- console.error('Error refreshing token:', e);
356
- // Throw the error to let the caller handle it
357
- throw new Error(`Token refresh failed: ${e instanceof Error ? e.message : 'Unknown error'}`);
358
- }
359
- }
360
- // Fallback to full login only if refresh token is not available or invalid
361
- // This should only happen during initial login or when refresh token is truly expired
362
- try {
363
- const loginRequest = {
364
- accountAddress: this.currentAccountAddress,
365
- signedAtMillis: this.getCurrentTimeMs(),
366
- audience: 'api',
367
- };
368
- const signature = yield this.bfSigner.signLoginRequest(loginRequest);
369
- const response = yield this.authApi.authV2TokenPost(signature, loginRequest, (_b = this.initializeOptions) === null || _b === void 0 ? void 0 : _b.refreshTokenValidForSeconds, (_c = this.initializeOptions) === null || _c === void 0 ? void 0 : _c.readOnly);
370
- this.tokenResponse = response.data;
371
- this.tokenSetAtSeconds = Date.now() / 1000;
372
- }
373
- catch (e) {
374
- console.error('Full login failed:', e);
375
- // If this fails and we're in a mode where logout should be disabled, don't logout
376
- if (this.disableLoginPromptOnLogout) {
377
- throw new Error(`Login failed: ${e instanceof Error ? e.message : 'Unknown error'}`);
378
- }
379
- // Otherwise, throw the error and let the caller decide
380
- throw new Error(`Login failed: ${e instanceof Error ? e.message : 'Unknown error'}`);
381
- }
382
- });
383
- }
384
- getAccessToken() {
385
- return __awaiter(this, void 0, void 0, function* () {
386
- if (!this.tokenResponse) {
387
- yield this.login();
388
- }
389
- if (!this.tokenResponse) {
390
- throw new Error('Unable to obtain access token');
391
- }
392
- if (this.isConnected) {
393
- if (this.isAccessTokenExpired()) {
394
- yield this.refreshToken();
395
- }
396
- }
397
- else if (this.isAccessTokenExpired()) {
398
- yield this.login();
399
- }
400
- if (!this.tokenResponse) {
401
- throw new Error('Access token unavailable after refresh');
402
- }
403
- return this.tokenResponse.accessToken;
404
- });
405
- }
406
- getOpenOrders(symbol) {
407
- return __awaiter(this, void 0, void 0, function* () {
408
- return yield this.tradeApi.getOpenOrders(symbol);
409
- });
410
- }
411
- getStandbyOrders(symbol) {
412
- return __awaiter(this, void 0, void 0, function* () {
413
- return yield this.tradeApi.getStandbyOrders(symbol);
414
- });
415
- }
416
- updateLeverage(symbol, leverageE9) {
417
- return __awaiter(this, void 0, void 0, function* () {
418
- if (!this.contractsConfig) {
419
- throw new Error('Missing contracts config');
420
- }
421
- const signedFields = {
422
- accountAddress: this.currentAccountAddress,
423
- idsId: this.contractsConfig.idsId,
424
- symbol: symbol,
425
- leverageE9: leverageE9,
426
- salt: this.generateSalt(),
427
- signedAtMillis: this.getCurrentTimeMs(),
428
- };
429
- const request = yield this.bfSigner.signLeverageUpdateRequest(signedFields);
430
- return yield this.tradeApi.putLeverageUpdate({
431
- signedFields,
432
- signature: request,
433
- });
434
- });
435
- }
436
- createOrder(params) {
437
- return __awaiter(this, void 0, void 0, function* () {
438
- var _a, _b;
439
- if (!this.contractsConfig) {
440
- throw new Error('Missing contracts config');
441
- }
442
- const signedFields = {
443
- symbol: params.symbol,
444
- idsId: this.contractsConfig.idsId,
445
- accountAddress: this.currentAccountAddress,
446
- priceE9: params.priceE9,
447
- quantityE9: params.quantityE9,
448
- side: params.side,
449
- leverageE9: params.leverageE9,
450
- isIsolated: params.isIsolated,
451
- salt: this.generateSalt(),
452
- expiresAtMillis: params.expiresAtMillis,
453
- signedAtMillis: this.getCurrentTimeMs(),
454
- };
455
- const signature = yield this.bfSigner.signOrderRequest(signedFields);
456
- const createOrderRequest = {
457
- signedFields,
458
- signature,
459
- clientOrderId: params.clientOrderId,
460
- type: params.type,
461
- reduceOnly: (_a = params.reduceOnly) !== null && _a !== void 0 ? _a : false,
462
- postOnly: (_b = params.postOnly) !== null && _b !== void 0 ? _b : false,
463
- timeInForce: params.timeInForce,
464
- triggerPriceE9: params.triggerPriceE9,
465
- selfTradePreventionType: params.selfTradePreventionType,
466
- twapConfig: params.twapConfig,
467
- };
468
- console.log('Creating order:', createOrderRequest);
469
- return yield this.tradeApi.postCreateOrder(createOrderRequest);
470
- });
471
- }
472
- cancelOrder(cancelOrdersRequest) {
473
- return __awaiter(this, void 0, void 0, function* () {
474
- return yield this.tradeApi.cancelOrders(cancelOrdersRequest);
475
- });
476
- }
477
- cancelStandbyOrder(cancelOrdersRequest) {
478
- return __awaiter(this, void 0, void 0, function* () {
479
- return yield this.tradeApi.cancelStandbyOrders(cancelOrdersRequest);
480
- });
481
- }
482
- withdraw(assetSymbol, amountE9) {
483
- return __awaiter(this, void 0, void 0, function* () {
484
- const exchangeInfo = yield this.exchangeDataApi.getExchangeInfo();
485
- const asset = exchangeInfo.data.assets.find((asset) => asset.symbol === assetSymbol);
486
- if (!asset) {
487
- throw new Error(`Asset ${assetSymbol} not found`);
488
- }
489
- if (!this.contractsConfig) {
490
- throw new Error('Missing contractsConfig');
491
- }
492
- const signedFields = {
493
- assetSymbol,
494
- edsId: this.contractsConfig.edsId,
495
- accountAddress: this.currentAccountAddress,
496
- amountE9,
497
- salt: this.generateSalt(),
498
- signedAtMillis: this.getCurrentTimeMs(),
499
- };
500
- const signature = yield this.bfSigner.signWithdrawRequest(signedFields);
501
- yield this.tradeApi.postWithdraw({
502
- signedFields,
503
- signature,
504
- });
505
- console.log('Withdraw request sent:', signedFields);
506
- });
507
- }
508
- authorizeAccount(accountAddress, alias) {
509
- return __awaiter(this, void 0, void 0, function* () {
510
- if (!this.contractsConfig) {
511
- throw new Error('Missing contractsConfig');
512
- }
513
- const signedFields = {
514
- accountAddress: this.currentAccountAddress,
515
- idsId: this.contractsConfig.idsId,
516
- authorizedAccountAddress: accountAddress,
517
- salt: this.generateSalt(),
518
- signedAtMillis: this.getCurrentTimeMs(),
519
- };
520
- const signature = yield this.bfSigner.signAccountAuthorizationRequest(signedFields, true);
521
- yield this.tradeApi.putAuthorizeAccount({
522
- signedFields,
523
- signature,
524
- alias,
525
- });
526
- console.log('Authorize account request sent:', signedFields);
527
- });
528
- }
529
- deauthorizeAccount(accountAddress) {
530
- return __awaiter(this, void 0, void 0, function* () {
531
- if (!this.contractsConfig) {
532
- throw new Error('Missing contractsConfig');
533
- }
534
- const signedFields = {
535
- accountAddress: this.currentAccountAddress,
536
- idsId: this.contractsConfig.idsId,
537
- authorizedAccountAddress: accountAddress,
538
- salt: this.generateSalt(),
539
- signedAtMillis: this.getCurrentTimeMs(),
540
- };
541
- const signature = yield this.bfSigner.signAccountAuthorizationRequest(signedFields, false);
542
- yield this.tradeApi.putDeauthorizeAccount({
543
- signedFields,
544
- signature,
545
- });
546
- console.log('Deauthorize account request sent:', signedFields);
547
- });
548
- }
549
- adjustIsolatedMargin(symbol, amountE9, add) {
550
- return __awaiter(this, void 0, void 0, function* () {
551
- if (!this.contractsConfig) {
552
- throw new Error('Missing contractsConfig');
553
- }
554
- const signedFields = {
555
- symbol,
556
- idsId: this.contractsConfig.idsId,
557
- accountAddress: this.currentAccountAddress,
558
- operation: add
559
- ? api_1.AdjustMarginOperation.Add
560
- : api_1.AdjustMarginOperation.Subtract,
561
- quantityE9: amountE9,
562
- salt: this.generateSalt(),
563
- signedAtMillis: this.getCurrentTimeMs(),
564
- };
565
- const signature = yield this.bfSigner.signAdjustIsolatedMarginRequest(signedFields);
566
- yield this.tradeApi.putAdjustIsolatedMargin({
567
- signedFields,
568
- signature,
569
- });
570
- console.log('Adjust isolated margin request sent:', signedFields);
571
- });
572
- }
573
- getAccountPreferences() {
574
- return __awaiter(this, void 0, void 0, function* () {
575
- return yield this.accountDataApi.getAccountPreferences();
576
- });
577
- }
578
- updateAccountPreferences(updateAccountPreferenceRequest) {
579
- return __awaiter(this, void 0, void 0, function* () {
580
- return yield this.accountDataApi.putAccountPreferences(updateAccountPreferenceRequest);
581
- });
582
- }
583
- // Update Account Group ID.
584
- // If groupID is null, it will remove the account from its group.
585
- // An account may only belong to 1 group at a time.
586
- updateAccountGroupId(updateAccountGroupIdRequest) {
587
- return __awaiter(this, void 0, void 0, function* () {
588
- return yield this.accountDataApi.patchAccountGroupID(updateAccountGroupIdRequest);
589
- });
590
- }
591
- deposit(amountE9, accountAddress, args) {
592
- return __awaiter(this, void 0, void 0, function* () {
593
- var _a, _b, _c;
594
- const assetSymbol = 'USDC';
595
- const txb = new library_sui_1.TransactionBlock();
596
- const assetType = (_b = (_a = this.assets) === null || _a === void 0 ? void 0 : _a.find((x) => x.symbol === assetSymbol)) === null || _b === void 0 ? void 0 : _b.assetType;
597
- if (!assetType) {
598
- throw new Error('Missing USDC asset type');
599
- }
600
- const [splitCoin, mergedCoin] = yield library_sui_1.CoinUtils.createCoinWithBalance(this.suiClient, txb, amountE9, assetType, this.currentAccountAddress || this.bfSigner.getAddress());
601
- //build the tx
602
- (_c = this.txBuilder) === null || _c === void 0 ? void 0 : _c.depositToAssetBank(assetSymbol, accountAddress ||
603
- this.currentAccountAddress ||
604
- this.bfSigner.getAddress(), amountE9, splitCoin, {
605
- txBlock: txb,
606
- });
607
- //add the transfer objects to the tx
608
- if (mergedCoin) {
609
- txb.transferObjects([mergedCoin], this.currentAccountAddress || this.bfSigner.getAddress());
610
- }
611
- if (splitCoin) {
612
- txb.transferObjects([splitCoin], this.currentAccountAddress || this.bfSigner.getAddress());
613
- }
614
- if (args && args.sponsored) {
615
- // build the gasless tx payload bytes
616
- const gaslessTxPayloadBytes = yield this.buildGaslessTxPayloadBytes(txb);
617
- try {
618
- const request = {
619
- txBytes: gaslessTxPayloadBytes,
620
- };
621
- // sponsor gas for the transaction
622
- const sponsorTxApiResponse = yield this.accountDataApi.sponsorTx(request);
623
- const txBlockFromBytes = library_sui_1.TransactionBlock.from(sponsorTxApiResponse.data.txBytes);
624
- // sign the transaction with user's wallet
625
- const userSignedTx = yield this.bfSigner.signTx(txBlockFromBytes, this.suiClient);
626
- // execute the transaction with both user's signature and sponsor's signature
627
- const response = yield this.bfSigner.executeSponsoredTx(userSignedTx.bytes, userSignedTx.signature, sponsorTxApiResponse.data.signature, this.suiClient);
628
- return response;
629
- }
630
- catch (error) {
631
- if (args === null || args === void 0 ? void 0 : args.fallbackToExecuteTx) {
632
- return this.bfSigner.executeTx(txb, this.suiClient);
633
- }
634
- throw error;
635
- }
636
- }
637
- else {
638
- return this.bfSigner.executeTx(txb, this.suiClient);
639
- }
640
- });
641
- }
642
- /**
643
- * Batch claim rewards on-chain using RewardsDistributorInteractor
644
- * @param payload Array of claim payloads with signatures
645
- * @returns Transaction response
646
- */
647
- batchClaimRewards(payload) {
648
- return __awaiter(this, void 0, void 0, function* () {
649
- // Get the rewards contract config from API (same as useClaimConfigQuery)
650
- const { data } = yield this.rewardsDataApi.getContractConfig();
651
- if (!data) {
652
- throw new Error('Failed to get rewards contract config');
653
- }
654
- // Get the signer from BluefinRequestSigner
655
- const signer = this.bfSigner.wallet;
656
- // Create the RewardsDistributorInteractor
657
- const interactor = new index_1.RewardsDistributorInteractor(this.suiClient, data, signer, this.bfSigner.isUIWallet());
658
- // Transform payload to the format expected by claimRewardsBatch
659
- const batch = payload.map(({ signature, sigPayload }) => ({
660
- signature,
661
- payload: sigPayload,
662
- }));
663
- // Execute the batch claim
664
- const tx = yield interactor.claimRewardsBatch(batch);
665
- return tx;
666
- });
667
- }
668
- refreshToken() {
669
- return __awaiter(this, arguments, void 0, function* (force = false) {
670
- if (!this.isConnected)
671
- return;
672
- // If already refreshing, wait for that operation to complete
673
- if (this.isRefreshing) {
674
- if (this.refreshTokenPromise) {
675
- yield this.refreshTokenPromise;
676
- }
677
- return;
678
- }
679
- // Check if token needs refreshing
680
- if (!this.isAccessTokenExpired() && !force) {
681
- return;
682
- }
683
- this.isRefreshing = true;
684
- try {
685
- this.refreshTokenPromise = this.performTokenRefresh();
686
- yield this.refreshTokenPromise;
687
- }
688
- finally {
689
- this.isRefreshing = false;
690
- this.refreshTokenPromise = null;
691
- }
692
- });
693
- }
694
- performTokenRefresh() {
695
- return __awaiter(this, void 0, void 0, function* () {
696
- const maxRetries = 5;
697
- let retryCount = 0;
698
- let consecutiveNetworkErrors = 0;
699
- while (retryCount < maxRetries) {
700
- try {
701
- console.log(`Refreshing token (attempt ${retryCount + 1}/${maxRetries})`);
702
- // Check if refresh token is still valid
703
- if (!this.isRefreshTokenValid()) {
704
- console.log('Refresh token is expired or invalid, triggering logout');
705
- this.logout();
706
- return;
707
- }
708
- yield this.loginAndUpdateToken();
709
- console.log('Token refreshed successfully');
710
- consecutiveNetworkErrors = 0; // Reset network error counter on success
711
- return;
712
- }
713
- catch (error) {
714
- retryCount++;
715
- console.error(`Error refreshing token (attempt ${retryCount}):`, error);
716
- // Check if this is a network error that we should retry
717
- const isNetworkError = error instanceof Error &&
718
- (error.message.includes('Network Error') ||
719
- error.message.includes('ERR_NETWORK') ||
720
- error.message.includes('ECONNREFUSED') ||
721
- error.message.includes('timeout') ||
722
- error.message.includes('ERR_NETWORK_IO_SUSPENDED'));
723
- // Check if we're currently offline (browser environment only)
724
- const isOffline = typeof navigator !== 'undefined' &&
725
- 'onLine' in navigator &&
726
- !navigator.onLine;
727
- if (isNetworkError) {
728
- consecutiveNetworkErrors++;
729
- }
730
- // If we're offline, don't count this as a "real" error - just wait for network to come back
731
- if (isOffline) {
732
- console.log('User is offline, will retry when network comes back online');
733
- return;
734
- }
735
- // If it's not a network error and login failed (tokenResponse is null), logout immediately
736
- if (!isNetworkError && !this.tokenResponse) {
737
- console.error('Login failed permanently, triggering logout');
738
- this.logout();
739
- return;
740
- }
741
- // If we've had too many consecutive network errors, pause refresh attempts temporarily
742
- if (consecutiveNetworkErrors >= 10) {
743
- // Clear any scheduled refresh
744
- if (this.updateTokenTimeout) {
745
- clearTimeout(this.updateTokenTimeout);
746
- this.updateTokenTimeout = null;
747
- }
748
- setTimeout(() => {
749
- console.log('Resuming token refresh attempts');
750
- this.scheduleTokenRefresh(); // Reschedule refresh when resuming
751
- }, 300000); // 5 minutes
752
- return;
753
- }
754
- if (retryCount >= maxRetries && !isOffline) {
755
- if (isNetworkError) {
756
- console.warn('Max retries reached due to network errors, will retry later');
757
- return; // Don't logout for network issues, just wait for next interval
758
- }
759
- else {
760
- console.error('Max retries reached, triggering logout');
761
- this.logout();
762
- return;
763
- }
764
- }
765
- // Exponential backoff: wait 2^retryCount seconds, but cap at 30 seconds for network errors
766
- const baseDelay = isNetworkError ? 5000 : 2000; // Start with 5s for network errors, 2s for others
767
- const delayMs = Math.min(baseDelay * Math.pow(2, retryCount - 1), 30000);
768
- console.log(`Retrying in ${delayMs}ms...`);
769
- yield new Promise((resolve) => setTimeout(resolve, delayMs));
770
- }
771
- }
772
- });
773
- }
774
- createAccountDataStreamListener(handler) {
775
- return __awaiter(this, void 0, void 0, function* () {
776
- const accessToken = yield this.getAccessToken();
777
- return new Promise((resolve) => {
778
- const ws = new ws_1.WebSocket(this.configs[Services.AccountWebsocket].basePath, {
779
- headers: {
780
- Authorization: `Bearer ${accessToken}`,
781
- },
782
- });
783
- ws.onmessage = (event) => __awaiter(this, void 0, void 0, function* () {
784
- yield handler(JSON.parse(event.data));
785
- });
786
- ws.on('open', () => {
787
- resolve(ws);
788
- });
789
- });
790
- });
791
- }
792
- createMarketDataStreamListener(handler) {
793
- return __awaiter(this, void 0, void 0, function* () {
794
- return new Promise((resolve) => {
795
- const ws = new ws_1.WebSocket(this.configs[Services.MarketWebsocket].basePath);
796
- ws.onmessage = (event) => __awaiter(this, void 0, void 0, function* () {
797
- yield handler(JSON.parse(event.data));
798
- });
799
- ws.on('open', () => {
800
- resolve(ws);
801
- });
802
- });
803
- });
804
- }
805
- logout() {
806
- return __awaiter(this, void 0, void 0, function* () {
807
- var _a;
808
- console.log('Logging out');
809
- if (this.updateTokenTimeout) {
810
- clearTimeout(this.updateTokenTimeout);
811
- this.updateTokenTimeout = null;
812
- }
813
- this.tokenResponse = null;
814
- this.tokenSetAtSeconds = null;
815
- this.isConnected = false;
816
- (_a = this.onLogout) === null || _a === void 0 ? void 0 : _a.call(this);
817
- });
818
- }
819
- dispose() {
820
- return __awaiter(this, void 0, void 0, function* () {
821
- console.log('Disposing SDK resources');
822
- if (this.updateTokenTimeout) {
823
- clearTimeout(this.updateTokenTimeout);
824
- this.updateTokenTimeout = null;
825
- }
826
- // Clean up visibility change listener
827
- if (this.visibilityChangeHandler && typeof document !== 'undefined') {
828
- document.removeEventListener('visibilitychange', this.visibilityChangeHandler);
829
- this.visibilityChangeHandler = undefined;
830
- }
831
- // Clean up network change listeners
832
- if (typeof window !== 'undefined') {
833
- if (this.onlineHandler) {
834
- window.removeEventListener('online', this.onlineHandler);
835
- this.onlineHandler = undefined;
836
- }
837
- if (this.offlineHandler) {
838
- window.removeEventListener('offline', this.offlineHandler);
839
- this.offlineHandler = undefined;
840
- }
841
- }
842
- this.isConnected = false;
843
- });
844
- }
845
- /**
846
- * @description
847
- * Get the transaction builder instance
848
- * @returns TxBuilder instance or undefined if not initialized
849
- */
850
- getTxBuilder() {
851
- return this.txBuilder;
852
- }
853
- /**
854
- * @description
855
- * Get the Bluefin signer instance
856
- * @returns IBluefinSigner instance
857
- */
858
- getSigner() {
859
- return this.bfSigner;
860
- }
861
- }
862
- exports.BluefinProSdk = BluefinProSdk;
863
- BluefinProSdk.TOKEN_REFRESH_THRESHOLD_PERCENTAGE = 0.8;
864
- //# sourceMappingURL=sdk.js.map