@bluefin-exchange/pro-sdk 0.1.27 → 0.1.29
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/src/api.d.ts +87 -39
- package/dist/src/sdk.d.ts +5 -0
- package/dist/src/sdk.js +34 -9
- package/package.json +2 -2
- package/src/api.ts +87 -39
- package/src/sdk.ts +38 -9
package/dist/src/api.d.ts
CHANGED
|
@@ -48,49 +48,49 @@ export interface Account {
|
|
|
48
48
|
* @type {string}
|
|
49
49
|
* @memberof Account
|
|
50
50
|
*/
|
|
51
|
-
'
|
|
51
|
+
'crossEffectiveBalanceE9': string;
|
|
52
52
|
/**
|
|
53
53
|
* The sum of initial margin required across all cross positions (e9 format).
|
|
54
54
|
* @type {string}
|
|
55
55
|
* @memberof Account
|
|
56
56
|
*/
|
|
57
|
-
'
|
|
57
|
+
'crossMarginRequiredE9': string;
|
|
58
58
|
/**
|
|
59
59
|
* The sum of initial margin required across all open orders (e9 format).
|
|
60
60
|
* @type {string}
|
|
61
61
|
* @memberof Account
|
|
62
62
|
*/
|
|
63
|
-
'
|
|
63
|
+
'totalOrderMarginRequiredE9': string;
|
|
64
64
|
/**
|
|
65
65
|
* The amount of margin available to open new positions and orders (e9 format).
|
|
66
66
|
* @type {string}
|
|
67
67
|
* @memberof Account
|
|
68
68
|
*/
|
|
69
|
-
'
|
|
69
|
+
'marginAvailableE9': string;
|
|
70
70
|
/**
|
|
71
71
|
* The sum of maintenance margin required across all cross positions (e9 format).
|
|
72
72
|
* @type {string}
|
|
73
73
|
* @memberof Account
|
|
74
74
|
*/
|
|
75
|
-
'
|
|
75
|
+
'crossMaintenanceMarginRequiredE9': string;
|
|
76
76
|
/**
|
|
77
77
|
* The amount of margin available before liquidation (e9 format).
|
|
78
78
|
* @type {string}
|
|
79
79
|
* @memberof Account
|
|
80
80
|
*/
|
|
81
|
-
'
|
|
81
|
+
'crossMaintenanceMarginAvailableE9': string;
|
|
82
82
|
/**
|
|
83
83
|
* The ratio of the maintenance margin required to the account value (e9 format).
|
|
84
84
|
* @type {string}
|
|
85
85
|
* @memberof Account
|
|
86
86
|
*/
|
|
87
|
-
'
|
|
87
|
+
'crossMaintenanceMarginRatioE9': string;
|
|
88
88
|
/**
|
|
89
89
|
* The leverage of the account (e9 format).
|
|
90
90
|
* @type {string}
|
|
91
91
|
* @memberof Account
|
|
92
92
|
*/
|
|
93
|
-
'
|
|
93
|
+
'crossLeverageE9': string;
|
|
94
94
|
/**
|
|
95
95
|
* Total unrealized profit (e9 format).
|
|
96
96
|
* @type {string}
|
|
@@ -98,11 +98,29 @@ export interface Account {
|
|
|
98
98
|
*/
|
|
99
99
|
'totalUnrealizedPnlE9': string;
|
|
100
100
|
/**
|
|
101
|
-
* Unrealized profit of
|
|
101
|
+
* Unrealized profit of cross positions (e9 format).
|
|
102
102
|
* @type {string}
|
|
103
103
|
* @memberof Account
|
|
104
104
|
*/
|
|
105
|
-
'
|
|
105
|
+
'crossUnrealizedPnlE9': string;
|
|
106
|
+
/**
|
|
107
|
+
* An implicitly negative number that sums only the losses of all cross positions.
|
|
108
|
+
* @type {string}
|
|
109
|
+
* @memberof Account
|
|
110
|
+
*/
|
|
111
|
+
'crossUnrealizedLossE9': string;
|
|
112
|
+
/**
|
|
113
|
+
* The total value of the cross account, combining the cross effective balance and unrealized PnL across all cross positions, and subtracting any pending funding payments on any cross position.
|
|
114
|
+
* @type {string}
|
|
115
|
+
* @memberof Account
|
|
116
|
+
*/
|
|
117
|
+
'crossAccountValueE9': string;
|
|
118
|
+
/**
|
|
119
|
+
* The total value of the account, combining the total effective balance and unrealized PnL across all positions, and subtracting any pending funding payments on any position.
|
|
120
|
+
* @type {string}
|
|
121
|
+
* @memberof Account
|
|
122
|
+
*/
|
|
123
|
+
'totalAccountValueE9': string;
|
|
106
124
|
/**
|
|
107
125
|
* Last update time in milliseconds since Unix epoch.
|
|
108
126
|
* @type {number}
|
|
@@ -435,7 +453,7 @@ export interface AccountPositionUpdate {
|
|
|
435
453
|
* @type {string}
|
|
436
454
|
* @memberof AccountPositionUpdate
|
|
437
455
|
*/
|
|
438
|
-
'
|
|
456
|
+
'clientSetLeverageE9': string;
|
|
439
457
|
/**
|
|
440
458
|
* The liquidation price of the position.
|
|
441
459
|
* @type {string}
|
|
@@ -473,11 +491,11 @@ export interface AccountPositionUpdate {
|
|
|
473
491
|
*/
|
|
474
492
|
'side': PositionSide;
|
|
475
493
|
/**
|
|
476
|
-
* The
|
|
494
|
+
* The margin required for the position.
|
|
477
495
|
* @type {string}
|
|
478
496
|
* @memberof AccountPositionUpdate
|
|
479
497
|
*/
|
|
480
|
-
'
|
|
498
|
+
'marginRequiredE9': string;
|
|
481
499
|
/**
|
|
482
500
|
* The maintenance margin required for the position.
|
|
483
501
|
* @type {string}
|
|
@@ -653,95 +671,119 @@ export interface AccountUpdate {
|
|
|
653
671
|
*/
|
|
654
672
|
'tradingFees'?: TradingFees;
|
|
655
673
|
/**
|
|
656
|
-
*
|
|
674
|
+
* If the user can trade.
|
|
657
675
|
* @type {boolean}
|
|
658
676
|
* @memberof AccountUpdate
|
|
659
677
|
*/
|
|
660
678
|
'canTrade': boolean;
|
|
661
679
|
/**
|
|
662
|
-
*
|
|
680
|
+
* If the current user can deposit to the account.
|
|
663
681
|
* @type {boolean}
|
|
664
682
|
* @memberof AccountUpdate
|
|
665
683
|
*/
|
|
666
684
|
'canDeposit': boolean;
|
|
667
685
|
/**
|
|
668
|
-
*
|
|
686
|
+
* If the current user can withdraw from the account.
|
|
669
687
|
* @type {boolean}
|
|
670
688
|
* @memberof AccountUpdate
|
|
671
689
|
*/
|
|
672
690
|
'canWithdraw': boolean;
|
|
673
691
|
/**
|
|
674
|
-
*
|
|
692
|
+
* Total effective balance in USD (e9 format).
|
|
675
693
|
* @type {string}
|
|
676
694
|
* @memberof AccountUpdate
|
|
677
695
|
*/
|
|
678
|
-
'
|
|
696
|
+
'crossEffectiveBalanceE9': string;
|
|
679
697
|
/**
|
|
680
|
-
* The
|
|
698
|
+
* The sum of initial margin required across all cross positions (e9 format).
|
|
681
699
|
* @type {string}
|
|
682
700
|
* @memberof AccountUpdate
|
|
683
701
|
*/
|
|
684
|
-
'
|
|
702
|
+
'crossMarginRequiredE9': string;
|
|
685
703
|
/**
|
|
686
|
-
* The initial margin required
|
|
704
|
+
* The sum of initial margin required across all open orders (e9 format).
|
|
687
705
|
* @type {string}
|
|
688
706
|
* @memberof AccountUpdate
|
|
689
707
|
*/
|
|
690
|
-
'
|
|
708
|
+
'totalOrderMarginRequiredE9': string;
|
|
691
709
|
/**
|
|
692
|
-
* The available
|
|
710
|
+
* The amount of margin available to open new positions and orders (e9 format).
|
|
693
711
|
* @type {string}
|
|
694
712
|
* @memberof AccountUpdate
|
|
695
713
|
*/
|
|
696
|
-
'
|
|
714
|
+
'marginAvailableE9': string;
|
|
697
715
|
/**
|
|
698
|
-
* The
|
|
716
|
+
* The sum of maintenance margin required across all cross positions (e9 format).
|
|
699
717
|
* @type {string}
|
|
700
718
|
* @memberof AccountUpdate
|
|
701
719
|
*/
|
|
702
|
-
'
|
|
720
|
+
'crossMaintenanceMarginRequiredE9': string;
|
|
703
721
|
/**
|
|
704
|
-
* The available
|
|
722
|
+
* The amount of margin available before liquidation (e9 format).
|
|
705
723
|
* @type {string}
|
|
706
724
|
* @memberof AccountUpdate
|
|
707
725
|
*/
|
|
708
|
-
'
|
|
726
|
+
'crossMaintenanceMarginAvailableE9': string;
|
|
709
727
|
/**
|
|
710
|
-
* The maintenance margin
|
|
728
|
+
* The ratio of the maintenance margin required to the account value (e9 format).
|
|
711
729
|
* @type {string}
|
|
712
730
|
* @memberof AccountUpdate
|
|
713
731
|
*/
|
|
714
|
-
'
|
|
732
|
+
'crossMaintenanceMarginRatioE9': string;
|
|
715
733
|
/**
|
|
716
|
-
* The account
|
|
734
|
+
* The leverage of the account (e9 format).
|
|
717
735
|
* @type {string}
|
|
718
736
|
* @memberof AccountUpdate
|
|
719
737
|
*/
|
|
720
|
-
'
|
|
738
|
+
'crossLeverageE9': string;
|
|
721
739
|
/**
|
|
722
|
-
*
|
|
740
|
+
* Total unrealized profit (e9 format).
|
|
723
741
|
* @type {string}
|
|
724
742
|
* @memberof AccountUpdate
|
|
725
743
|
*/
|
|
726
744
|
'totalUnrealizedPnlE9': string;
|
|
727
745
|
/**
|
|
728
|
-
*
|
|
746
|
+
* Unrealized profit of cross positions (e9 format).
|
|
729
747
|
* @type {string}
|
|
730
748
|
* @memberof AccountUpdate
|
|
731
749
|
*/
|
|
732
|
-
'
|
|
750
|
+
'crossUnrealizedPnlE9': string;
|
|
733
751
|
/**
|
|
734
|
-
*
|
|
752
|
+
* An implicitly negative number that sums only the losses of all cross positions.
|
|
753
|
+
* @type {string}
|
|
754
|
+
* @memberof AccountUpdate
|
|
755
|
+
*/
|
|
756
|
+
'crossUnrealizedLossE9': string;
|
|
757
|
+
/**
|
|
758
|
+
* The total value of the cross account, combining the cross effective balance and unrealized PnL across all cross positions, and subtracting any pending funding payments on any cross position.
|
|
759
|
+
* @type {string}
|
|
760
|
+
* @memberof AccountUpdate
|
|
761
|
+
*/
|
|
762
|
+
'crossAccountValueE9': string;
|
|
763
|
+
/**
|
|
764
|
+
* The total value of the account, combining the total effective balance and unrealized PnL across all positions, and subtracting any pending funding payments on any position.
|
|
765
|
+
* @type {string}
|
|
766
|
+
* @memberof AccountUpdate
|
|
767
|
+
*/
|
|
768
|
+
'totalAccountValueE9': string;
|
|
769
|
+
/**
|
|
770
|
+
* Last update time in milliseconds since Unix epoch.
|
|
735
771
|
* @type {number}
|
|
736
772
|
* @memberof AccountUpdate
|
|
737
773
|
*/
|
|
738
774
|
'updatedAtMillis': number;
|
|
739
775
|
/**
|
|
740
|
-
*
|
|
776
|
+
*
|
|
741
777
|
* @type {Array<Asset>}
|
|
742
778
|
* @memberof AccountUpdate
|
|
743
779
|
*/
|
|
744
780
|
'assets': Array<Asset>;
|
|
781
|
+
/**
|
|
782
|
+
* The accounts that are authorized to trade on behalf of the current account.
|
|
783
|
+
* @type {Array<string>}
|
|
784
|
+
* @memberof AccountUpdate
|
|
785
|
+
*/
|
|
786
|
+
'authorizedAccounts': Array<string>;
|
|
745
787
|
}
|
|
746
788
|
/**
|
|
747
789
|
* Information about an order update.
|
|
@@ -2488,7 +2530,7 @@ export interface Position {
|
|
|
2488
2530
|
* @type {string}
|
|
2489
2531
|
* @memberof Position
|
|
2490
2532
|
*/
|
|
2491
|
-
'
|
|
2533
|
+
'clientSetLeverageE9': string;
|
|
2492
2534
|
/**
|
|
2493
2535
|
* Liquidation price (e9 format).
|
|
2494
2536
|
* @type {string}
|
|
@@ -2530,7 +2572,7 @@ export interface Position {
|
|
|
2530
2572
|
* @type {string}
|
|
2531
2573
|
* @memberof Position
|
|
2532
2574
|
*/
|
|
2533
|
-
'
|
|
2575
|
+
'marginRequiredE9': string;
|
|
2534
2576
|
/**
|
|
2535
2577
|
* Maintenance margin required with current mark price (e9 format).
|
|
2536
2578
|
* @type {string}
|
|
@@ -3294,6 +3336,12 @@ export interface Transaction {
|
|
|
3294
3336
|
* @memberof Transaction
|
|
3295
3337
|
*/
|
|
3296
3338
|
'amountE9': string;
|
|
3339
|
+
/**
|
|
3340
|
+
* Transaction status (SUCCESS, REJECTED).
|
|
3341
|
+
* @type {string}
|
|
3342
|
+
* @memberof Transaction
|
|
3343
|
+
*/
|
|
3344
|
+
'status'?: string;
|
|
3297
3345
|
/**
|
|
3298
3346
|
* Asset bank address.
|
|
3299
3347
|
* @type {string}
|
package/dist/src/sdk.d.ts
CHANGED
|
@@ -22,6 +22,8 @@ export interface BluefinProSdkOptions {
|
|
|
22
22
|
currentAccountAddress?: string;
|
|
23
23
|
refreshToken?: string;
|
|
24
24
|
refreshTokenValidForSeconds?: number;
|
|
25
|
+
disableLoginPromptOnLogout?: boolean;
|
|
26
|
+
onLogout?: () => void;
|
|
25
27
|
authHost?: string;
|
|
26
28
|
apiHost?: string;
|
|
27
29
|
tradeHost?: string;
|
|
@@ -45,6 +47,8 @@ export declare class BluefinProSdk {
|
|
|
45
47
|
private assets;
|
|
46
48
|
private txBuilder;
|
|
47
49
|
private currentAccountAddress;
|
|
50
|
+
private disableLoginPromptOnLogout;
|
|
51
|
+
private onLogout?;
|
|
48
52
|
constructor(bfSigner: IBluefinSigner, environment: "mainnet" | "testnet" | "devnet" | undefined, suiClient: SuiClient, opts?: BluefinProSdkOptions);
|
|
49
53
|
getTokenResponse(): LoginResponse | null;
|
|
50
54
|
private generateSalt;
|
|
@@ -66,5 +70,6 @@ export declare class BluefinProSdk {
|
|
|
66
70
|
refreshToken(): Promise<void>;
|
|
67
71
|
createAccountDataStreamListener(handler: (data: AccountStreamMessage) => Promise<void>): Promise<WebSocket>;
|
|
68
72
|
createMarketDataStreamListener(handler: (data: MarketStreamMessage) => Promise<void>): Promise<WebSocket>;
|
|
73
|
+
logout(): Promise<void>;
|
|
69
74
|
dispose(): Promise<void>;
|
|
70
75
|
}
|
package/dist/src/sdk.js
CHANGED
|
@@ -49,7 +49,7 @@ var Services;
|
|
|
49
49
|
})(Services || (Services = {}));
|
|
50
50
|
class BluefinProSdk {
|
|
51
51
|
constructor(bfSigner, environment = "mainnet", suiClient, opts) {
|
|
52
|
-
var _a, _b, _c, _d, _e;
|
|
52
|
+
var _a, _b, _c, _d, _e, _f;
|
|
53
53
|
this.bfSigner = bfSigner;
|
|
54
54
|
this.environment = environment;
|
|
55
55
|
this.suiClient = suiClient;
|
|
@@ -60,6 +60,9 @@ class BluefinProSdk {
|
|
|
60
60
|
this.contractsConfig = undefined;
|
|
61
61
|
this.tokenResponse = null;
|
|
62
62
|
this.tokenSetAtSeconds = null;
|
|
63
|
+
this.disableLoginPromptOnLogout =
|
|
64
|
+
(_a = opts === null || opts === void 0 ? void 0 : opts.disableLoginPromptOnLogout) !== null && _a !== void 0 ? _a : false;
|
|
65
|
+
this.onLogout = opts === null || opts === void 0 ? void 0 : opts.onLogout;
|
|
63
66
|
if ((opts === null || opts === void 0 ? void 0 : opts.refreshToken) && (opts === null || opts === void 0 ? void 0 : opts.refreshTokenValidForSeconds)) {
|
|
64
67
|
this.tokenResponse = {
|
|
65
68
|
accessToken: "",
|
|
@@ -70,11 +73,11 @@ class BluefinProSdk {
|
|
|
70
73
|
}
|
|
71
74
|
const defaultConfig = environmentConfig[this.environment];
|
|
72
75
|
const basePaths = {
|
|
73
|
-
authHost: (
|
|
74
|
-
apiHost: (
|
|
75
|
-
tradeHost: (
|
|
76
|
-
marketWsHost: (
|
|
77
|
-
accountWsHost: (
|
|
76
|
+
authHost: (_b = opts === null || opts === void 0 ? void 0 : opts.authHost) !== null && _b !== void 0 ? _b : defaultConfig.authHost,
|
|
77
|
+
apiHost: (_c = opts === null || opts === void 0 ? void 0 : opts.apiHost) !== null && _c !== void 0 ? _c : defaultConfig.apiHost,
|
|
78
|
+
tradeHost: (_d = opts === null || opts === void 0 ? void 0 : opts.tradeHost) !== null && _d !== void 0 ? _d : defaultConfig.tradeHost,
|
|
79
|
+
marketWsHost: (_e = opts === null || opts === void 0 ? void 0 : opts.marketWsHost) !== null && _e !== void 0 ? _e : defaultConfig.marketWsHost,
|
|
80
|
+
accountWsHost: (_f = opts === null || opts === void 0 ? void 0 : opts.accountWsHost) !== null && _f !== void 0 ? _f : defaultConfig.accountWsHost,
|
|
78
81
|
};
|
|
79
82
|
let baseOptions = {};
|
|
80
83
|
if (typeof globalThis.navigator !== "undefined") {
|
|
@@ -192,9 +195,15 @@ class BluefinProSdk {
|
|
|
192
195
|
return;
|
|
193
196
|
}
|
|
194
197
|
catch (e) {
|
|
195
|
-
console.error("Error refreshing token:", e);
|
|
198
|
+
console.error("Error refreshing token:", e);
|
|
199
|
+
// If refreshing the token fails, the user should be logged in again
|
|
200
|
+
if (this.disableLoginPromptOnLogout) {
|
|
201
|
+
this.logout();
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
196
204
|
}
|
|
197
205
|
}
|
|
206
|
+
// fallback login (modal) only happens if not disabled
|
|
198
207
|
const loginRequest = {
|
|
199
208
|
accountAddress: this.currentAccountAddress,
|
|
200
209
|
signedAtMillis: Date.now(),
|
|
@@ -402,8 +411,13 @@ class BluefinProSdk {
|
|
|
402
411
|
Date.now() / 1000 - this.tokenSetAtSeconds >
|
|
403
412
|
this.tokenResponse.accessTokenValidForSeconds) {
|
|
404
413
|
console.log("Refreshing token");
|
|
405
|
-
|
|
406
|
-
|
|
414
|
+
try {
|
|
415
|
+
yield this.loginAndUpdateToken();
|
|
416
|
+
this.tokenSetAtSeconds = Date.now() / 1000;
|
|
417
|
+
}
|
|
418
|
+
catch (error) {
|
|
419
|
+
console.log("Error refreshing token:", error);
|
|
420
|
+
}
|
|
407
421
|
}
|
|
408
422
|
});
|
|
409
423
|
}
|
|
@@ -440,6 +454,17 @@ class BluefinProSdk {
|
|
|
440
454
|
});
|
|
441
455
|
});
|
|
442
456
|
}
|
|
457
|
+
logout() {
|
|
458
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
459
|
+
var _a;
|
|
460
|
+
console.log("Logging out");
|
|
461
|
+
this.tokenResponse = null;
|
|
462
|
+
this.tokenSetAtSeconds = null;
|
|
463
|
+
this.isConnected = false;
|
|
464
|
+
(_a = this.onLogout) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
;
|
|
443
468
|
dispose() {
|
|
444
469
|
return __awaiter(this, void 0, void 0, function* () {
|
|
445
470
|
console.log("Disposing SDK resources");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bluefin-exchange/pro-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.29",
|
|
4
4
|
"description": "OpenAPI client for @bluefin-exchange/pro-sdk",
|
|
5
5
|
"author": "Bluefin",
|
|
6
6
|
"repository": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@firefly-exchange/library-sui": "^2.5.8",
|
|
22
22
|
"@mysten/bcs": "^1.2.1",
|
|
23
|
-
"@mysten/sui": "^1.
|
|
23
|
+
"@mysten/sui": "^1.28.2",
|
|
24
24
|
"@noble/hashes": "^1.7.0",
|
|
25
25
|
"@types/ws": "^8.5.13",
|
|
26
26
|
"axios": "^1.6.1",
|
package/src/api.ts
CHANGED
|
@@ -58,49 +58,49 @@ export interface Account {
|
|
|
58
58
|
* @type {string}
|
|
59
59
|
* @memberof Account
|
|
60
60
|
*/
|
|
61
|
-
'
|
|
61
|
+
'crossEffectiveBalanceE9': string;
|
|
62
62
|
/**
|
|
63
63
|
* The sum of initial margin required across all cross positions (e9 format).
|
|
64
64
|
* @type {string}
|
|
65
65
|
* @memberof Account
|
|
66
66
|
*/
|
|
67
|
-
'
|
|
67
|
+
'crossMarginRequiredE9': string;
|
|
68
68
|
/**
|
|
69
69
|
* The sum of initial margin required across all open orders (e9 format).
|
|
70
70
|
* @type {string}
|
|
71
71
|
* @memberof Account
|
|
72
72
|
*/
|
|
73
|
-
'
|
|
73
|
+
'totalOrderMarginRequiredE9': string;
|
|
74
74
|
/**
|
|
75
75
|
* The amount of margin available to open new positions and orders (e9 format).
|
|
76
76
|
* @type {string}
|
|
77
77
|
* @memberof Account
|
|
78
78
|
*/
|
|
79
|
-
'
|
|
79
|
+
'marginAvailableE9': string;
|
|
80
80
|
/**
|
|
81
81
|
* The sum of maintenance margin required across all cross positions (e9 format).
|
|
82
82
|
* @type {string}
|
|
83
83
|
* @memberof Account
|
|
84
84
|
*/
|
|
85
|
-
'
|
|
85
|
+
'crossMaintenanceMarginRequiredE9': string;
|
|
86
86
|
/**
|
|
87
87
|
* The amount of margin available before liquidation (e9 format).
|
|
88
88
|
* @type {string}
|
|
89
89
|
* @memberof Account
|
|
90
90
|
*/
|
|
91
|
-
'
|
|
91
|
+
'crossMaintenanceMarginAvailableE9': string;
|
|
92
92
|
/**
|
|
93
93
|
* The ratio of the maintenance margin required to the account value (e9 format).
|
|
94
94
|
* @type {string}
|
|
95
95
|
* @memberof Account
|
|
96
96
|
*/
|
|
97
|
-
'
|
|
97
|
+
'crossMaintenanceMarginRatioE9': string;
|
|
98
98
|
/**
|
|
99
99
|
* The leverage of the account (e9 format).
|
|
100
100
|
* @type {string}
|
|
101
101
|
* @memberof Account
|
|
102
102
|
*/
|
|
103
|
-
'
|
|
103
|
+
'crossLeverageE9': string;
|
|
104
104
|
/**
|
|
105
105
|
* Total unrealized profit (e9 format).
|
|
106
106
|
* @type {string}
|
|
@@ -108,11 +108,29 @@ export interface Account {
|
|
|
108
108
|
*/
|
|
109
109
|
'totalUnrealizedPnlE9': string;
|
|
110
110
|
/**
|
|
111
|
-
* Unrealized profit of
|
|
111
|
+
* Unrealized profit of cross positions (e9 format).
|
|
112
112
|
* @type {string}
|
|
113
113
|
* @memberof Account
|
|
114
114
|
*/
|
|
115
|
-
'
|
|
115
|
+
'crossUnrealizedPnlE9': string;
|
|
116
|
+
/**
|
|
117
|
+
* An implicitly negative number that sums only the losses of all cross positions.
|
|
118
|
+
* @type {string}
|
|
119
|
+
* @memberof Account
|
|
120
|
+
*/
|
|
121
|
+
'crossUnrealizedLossE9': string;
|
|
122
|
+
/**
|
|
123
|
+
* The total value of the cross account, combining the cross effective balance and unrealized PnL across all cross positions, and subtracting any pending funding payments on any cross position.
|
|
124
|
+
* @type {string}
|
|
125
|
+
* @memberof Account
|
|
126
|
+
*/
|
|
127
|
+
'crossAccountValueE9': string;
|
|
128
|
+
/**
|
|
129
|
+
* The total value of the account, combining the total effective balance and unrealized PnL across all positions, and subtracting any pending funding payments on any position.
|
|
130
|
+
* @type {string}
|
|
131
|
+
* @memberof Account
|
|
132
|
+
*/
|
|
133
|
+
'totalAccountValueE9': string;
|
|
116
134
|
/**
|
|
117
135
|
* Last update time in milliseconds since Unix epoch.
|
|
118
136
|
* @type {number}
|
|
@@ -462,7 +480,7 @@ export interface AccountPositionUpdate {
|
|
|
462
480
|
* @type {string}
|
|
463
481
|
* @memberof AccountPositionUpdate
|
|
464
482
|
*/
|
|
465
|
-
'
|
|
483
|
+
'clientSetLeverageE9': string;
|
|
466
484
|
/**
|
|
467
485
|
* The liquidation price of the position.
|
|
468
486
|
* @type {string}
|
|
@@ -500,11 +518,11 @@ export interface AccountPositionUpdate {
|
|
|
500
518
|
*/
|
|
501
519
|
'side': PositionSide;
|
|
502
520
|
/**
|
|
503
|
-
* The
|
|
521
|
+
* The margin required for the position.
|
|
504
522
|
* @type {string}
|
|
505
523
|
* @memberof AccountPositionUpdate
|
|
506
524
|
*/
|
|
507
|
-
'
|
|
525
|
+
'marginRequiredE9': string;
|
|
508
526
|
/**
|
|
509
527
|
* The maintenance margin required for the position.
|
|
510
528
|
* @type {string}
|
|
@@ -689,95 +707,119 @@ export interface AccountUpdate {
|
|
|
689
707
|
*/
|
|
690
708
|
'tradingFees'?: TradingFees;
|
|
691
709
|
/**
|
|
692
|
-
*
|
|
710
|
+
* If the user can trade.
|
|
693
711
|
* @type {boolean}
|
|
694
712
|
* @memberof AccountUpdate
|
|
695
713
|
*/
|
|
696
714
|
'canTrade': boolean;
|
|
697
715
|
/**
|
|
698
|
-
*
|
|
716
|
+
* If the current user can deposit to the account.
|
|
699
717
|
* @type {boolean}
|
|
700
718
|
* @memberof AccountUpdate
|
|
701
719
|
*/
|
|
702
720
|
'canDeposit': boolean;
|
|
703
721
|
/**
|
|
704
|
-
*
|
|
722
|
+
* If the current user can withdraw from the account.
|
|
705
723
|
* @type {boolean}
|
|
706
724
|
* @memberof AccountUpdate
|
|
707
725
|
*/
|
|
708
726
|
'canWithdraw': boolean;
|
|
709
727
|
/**
|
|
710
|
-
*
|
|
728
|
+
* Total effective balance in USD (e9 format).
|
|
711
729
|
* @type {string}
|
|
712
730
|
* @memberof AccountUpdate
|
|
713
731
|
*/
|
|
714
|
-
'
|
|
732
|
+
'crossEffectiveBalanceE9': string;
|
|
715
733
|
/**
|
|
716
|
-
* The
|
|
734
|
+
* The sum of initial margin required across all cross positions (e9 format).
|
|
717
735
|
* @type {string}
|
|
718
736
|
* @memberof AccountUpdate
|
|
719
737
|
*/
|
|
720
|
-
'
|
|
738
|
+
'crossMarginRequiredE9': string;
|
|
721
739
|
/**
|
|
722
|
-
* The initial margin required
|
|
740
|
+
* The sum of initial margin required across all open orders (e9 format).
|
|
723
741
|
* @type {string}
|
|
724
742
|
* @memberof AccountUpdate
|
|
725
743
|
*/
|
|
726
|
-
'
|
|
744
|
+
'totalOrderMarginRequiredE9': string;
|
|
727
745
|
/**
|
|
728
|
-
* The available
|
|
746
|
+
* The amount of margin available to open new positions and orders (e9 format).
|
|
729
747
|
* @type {string}
|
|
730
748
|
* @memberof AccountUpdate
|
|
731
749
|
*/
|
|
732
|
-
'
|
|
750
|
+
'marginAvailableE9': string;
|
|
733
751
|
/**
|
|
734
|
-
* The
|
|
752
|
+
* The sum of maintenance margin required across all cross positions (e9 format).
|
|
735
753
|
* @type {string}
|
|
736
754
|
* @memberof AccountUpdate
|
|
737
755
|
*/
|
|
738
|
-
'
|
|
756
|
+
'crossMaintenanceMarginRequiredE9': string;
|
|
739
757
|
/**
|
|
740
|
-
* The available
|
|
758
|
+
* The amount of margin available before liquidation (e9 format).
|
|
741
759
|
* @type {string}
|
|
742
760
|
* @memberof AccountUpdate
|
|
743
761
|
*/
|
|
744
|
-
'
|
|
762
|
+
'crossMaintenanceMarginAvailableE9': string;
|
|
745
763
|
/**
|
|
746
|
-
* The maintenance margin
|
|
764
|
+
* The ratio of the maintenance margin required to the account value (e9 format).
|
|
747
765
|
* @type {string}
|
|
748
766
|
* @memberof AccountUpdate
|
|
749
767
|
*/
|
|
750
|
-
'
|
|
768
|
+
'crossMaintenanceMarginRatioE9': string;
|
|
751
769
|
/**
|
|
752
|
-
* The account
|
|
770
|
+
* The leverage of the account (e9 format).
|
|
753
771
|
* @type {string}
|
|
754
772
|
* @memberof AccountUpdate
|
|
755
773
|
*/
|
|
756
|
-
'
|
|
774
|
+
'crossLeverageE9': string;
|
|
757
775
|
/**
|
|
758
|
-
*
|
|
776
|
+
* Total unrealized profit (e9 format).
|
|
759
777
|
* @type {string}
|
|
760
778
|
* @memberof AccountUpdate
|
|
761
779
|
*/
|
|
762
780
|
'totalUnrealizedPnlE9': string;
|
|
763
781
|
/**
|
|
764
|
-
*
|
|
782
|
+
* Unrealized profit of cross positions (e9 format).
|
|
765
783
|
* @type {string}
|
|
766
784
|
* @memberof AccountUpdate
|
|
767
785
|
*/
|
|
768
|
-
'
|
|
786
|
+
'crossUnrealizedPnlE9': string;
|
|
769
787
|
/**
|
|
770
|
-
*
|
|
788
|
+
* An implicitly negative number that sums only the losses of all cross positions.
|
|
789
|
+
* @type {string}
|
|
790
|
+
* @memberof AccountUpdate
|
|
791
|
+
*/
|
|
792
|
+
'crossUnrealizedLossE9': string;
|
|
793
|
+
/**
|
|
794
|
+
* The total value of the cross account, combining the cross effective balance and unrealized PnL across all cross positions, and subtracting any pending funding payments on any cross position.
|
|
795
|
+
* @type {string}
|
|
796
|
+
* @memberof AccountUpdate
|
|
797
|
+
*/
|
|
798
|
+
'crossAccountValueE9': string;
|
|
799
|
+
/**
|
|
800
|
+
* The total value of the account, combining the total effective balance and unrealized PnL across all positions, and subtracting any pending funding payments on any position.
|
|
801
|
+
* @type {string}
|
|
802
|
+
* @memberof AccountUpdate
|
|
803
|
+
*/
|
|
804
|
+
'totalAccountValueE9': string;
|
|
805
|
+
/**
|
|
806
|
+
* Last update time in milliseconds since Unix epoch.
|
|
771
807
|
* @type {number}
|
|
772
808
|
* @memberof AccountUpdate
|
|
773
809
|
*/
|
|
774
810
|
'updatedAtMillis': number;
|
|
775
811
|
/**
|
|
776
|
-
*
|
|
812
|
+
*
|
|
777
813
|
* @type {Array<Asset>}
|
|
778
814
|
* @memberof AccountUpdate
|
|
779
815
|
*/
|
|
780
816
|
'assets': Array<Asset>;
|
|
817
|
+
/**
|
|
818
|
+
* The accounts that are authorized to trade on behalf of the current account.
|
|
819
|
+
* @type {Array<string>}
|
|
820
|
+
* @memberof AccountUpdate
|
|
821
|
+
*/
|
|
822
|
+
'authorizedAccounts': Array<string>;
|
|
781
823
|
}
|
|
782
824
|
/**
|
|
783
825
|
* Information about an order update.
|
|
@@ -2610,7 +2652,7 @@ export interface Position {
|
|
|
2610
2652
|
* @type {string}
|
|
2611
2653
|
* @memberof Position
|
|
2612
2654
|
*/
|
|
2613
|
-
'
|
|
2655
|
+
'clientSetLeverageE9': string;
|
|
2614
2656
|
/**
|
|
2615
2657
|
* Liquidation price (e9 format).
|
|
2616
2658
|
* @type {string}
|
|
@@ -2652,7 +2694,7 @@ export interface Position {
|
|
|
2652
2694
|
* @type {string}
|
|
2653
2695
|
* @memberof Position
|
|
2654
2696
|
*/
|
|
2655
|
-
'
|
|
2697
|
+
'marginRequiredE9': string;
|
|
2656
2698
|
/**
|
|
2657
2699
|
* Maintenance margin required with current mark price (e9 format).
|
|
2658
2700
|
* @type {string}
|
|
@@ -3440,6 +3482,12 @@ export interface Transaction {
|
|
|
3440
3482
|
* @memberof Transaction
|
|
3441
3483
|
*/
|
|
3442
3484
|
'amountE9': string;
|
|
3485
|
+
/**
|
|
3486
|
+
* Transaction status (SUCCESS, REJECTED).
|
|
3487
|
+
* @type {string}
|
|
3488
|
+
* @memberof Transaction
|
|
3489
|
+
*/
|
|
3490
|
+
'status'?: string;
|
|
3443
3491
|
/**
|
|
3444
3492
|
* Asset bank address.
|
|
3445
3493
|
* @type {string}
|
package/src/sdk.ts
CHANGED
|
@@ -97,6 +97,8 @@ export interface BluefinProSdkOptions {
|
|
|
97
97
|
currentAccountAddress?: string;
|
|
98
98
|
refreshToken?: string;
|
|
99
99
|
refreshTokenValidForSeconds?: number;
|
|
100
|
+
disableLoginPromptOnLogout?: boolean;
|
|
101
|
+
onLogout?: () => void;
|
|
100
102
|
|
|
101
103
|
// if needed to point to different services
|
|
102
104
|
authHost?: string;
|
|
@@ -120,6 +122,8 @@ export class BluefinProSdk {
|
|
|
120
122
|
private assets: Array<AssetConfig> | undefined;
|
|
121
123
|
private txBuilder: TxBuilder | undefined;
|
|
122
124
|
private currentAccountAddress: string | undefined;
|
|
125
|
+
private disableLoginPromptOnLogout: boolean;
|
|
126
|
+
private onLogout?: (() => void);
|
|
123
127
|
|
|
124
128
|
constructor(
|
|
125
129
|
private readonly bfSigner: IBluefinSigner,
|
|
@@ -133,6 +137,9 @@ export class BluefinProSdk {
|
|
|
133
137
|
this.contractsConfig = undefined;
|
|
134
138
|
this.tokenResponse = null;
|
|
135
139
|
this.tokenSetAtSeconds = null;
|
|
140
|
+
this.disableLoginPromptOnLogout =
|
|
141
|
+
opts?.disableLoginPromptOnLogout ?? false;
|
|
142
|
+
this.onLogout = opts?.onLogout;
|
|
136
143
|
|
|
137
144
|
if (opts?.refreshToken && opts?.refreshTokenValidForSeconds) {
|
|
138
145
|
this.tokenResponse = {
|
|
@@ -261,13 +268,13 @@ export class BluefinProSdk {
|
|
|
261
268
|
private async login(): Promise<void> {
|
|
262
269
|
console.log("Logging in to get the access token");
|
|
263
270
|
this.tokenSetAtSeconds = Date.now() / 1000;
|
|
264
|
-
|
|
271
|
+
|
|
265
272
|
if (!this.currentAccountAddress) {
|
|
266
273
|
this.currentAccountAddress = this.bfSigner.getAddress();
|
|
267
274
|
}
|
|
268
|
-
|
|
275
|
+
|
|
269
276
|
console.log(`Logging in as ${this.currentAccountAddress}`);
|
|
270
|
-
|
|
277
|
+
|
|
271
278
|
if (this.tokenResponse && this.tokenResponse.refreshTokenValidForSeconds) {
|
|
272
279
|
try {
|
|
273
280
|
const response = await this.authApi.authTokenRefreshPut({
|
|
@@ -276,16 +283,23 @@ export class BluefinProSdk {
|
|
|
276
283
|
this.tokenResponse = response.data;
|
|
277
284
|
return;
|
|
278
285
|
} catch (e) {
|
|
279
|
-
console.error("Error refreshing token:", e);
|
|
286
|
+
console.error("Error refreshing token:", e);
|
|
287
|
+
|
|
288
|
+
// If refreshing the token fails, the user should be logged in again
|
|
289
|
+
if (this.disableLoginPromptOnLogout) {
|
|
290
|
+
this.logout();
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
280
293
|
}
|
|
281
294
|
}
|
|
282
|
-
|
|
295
|
+
|
|
296
|
+
// fallback login (modal) only happens if not disabled
|
|
283
297
|
const loginRequest: LoginRequest = {
|
|
284
298
|
accountAddress: this.currentAccountAddress,
|
|
285
299
|
signedAtMillis: Date.now(),
|
|
286
300
|
audience: "api",
|
|
287
301
|
};
|
|
288
|
-
|
|
302
|
+
|
|
289
303
|
const signature = await this.bfSigner.signLoginRequest(loginRequest);
|
|
290
304
|
const response = await this.authApi.authV2TokenPost(
|
|
291
305
|
signature,
|
|
@@ -528,8 +542,9 @@ export class BluefinProSdk {
|
|
|
528
542
|
|
|
529
543
|
public async refreshToken(): Promise<void> {
|
|
530
544
|
if (!this.isConnected) return;
|
|
531
|
-
|
|
545
|
+
|
|
532
546
|
console.log("Checking token for refresh");
|
|
547
|
+
|
|
533
548
|
if (
|
|
534
549
|
!this.tokenResponse ||
|
|
535
550
|
!this.tokenSetAtSeconds ||
|
|
@@ -537,8 +552,13 @@ export class BluefinProSdk {
|
|
|
537
552
|
this.tokenResponse.accessTokenValidForSeconds
|
|
538
553
|
) {
|
|
539
554
|
console.log("Refreshing token");
|
|
540
|
-
|
|
541
|
-
|
|
555
|
+
|
|
556
|
+
try {
|
|
557
|
+
await this.loginAndUpdateToken();
|
|
558
|
+
this.tokenSetAtSeconds = Date.now() / 1000;
|
|
559
|
+
} catch (error) {
|
|
560
|
+
console.log("Error refreshing token:", error);
|
|
561
|
+
}
|
|
542
562
|
}
|
|
543
563
|
}
|
|
544
564
|
|
|
@@ -582,6 +602,15 @@ export class BluefinProSdk {
|
|
|
582
602
|
});
|
|
583
603
|
}
|
|
584
604
|
|
|
605
|
+
public async logout(): Promise<void> {
|
|
606
|
+
console.log("Logging out");
|
|
607
|
+
this.tokenResponse = null;
|
|
608
|
+
this.tokenSetAtSeconds = null;
|
|
609
|
+
this.isConnected = false;
|
|
610
|
+
|
|
611
|
+
this.onLogout?.();
|
|
612
|
+
};
|
|
613
|
+
|
|
585
614
|
public async dispose(): Promise<void> {
|
|
586
615
|
console.log("Disposing SDK resources");
|
|
587
616
|
|