@pear-protocol/exchanges-sdk 0.0.7 → 0.0.8-test.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.
@@ -1,7 +1,7 @@
1
1
  import PearSDK from '@pear-protocol/core-sdk';
2
2
  import { Connector } from '@pear-protocol/types';
3
3
  import { Credentials } from '../credentials.js';
4
- import { BalanceCallback, Tracker, AccountBalance, PositionsCallback, AccountPosition, TrackedAssetCallback, TrackedAssetInfo, ExchangeState } from './types.js';
4
+ import { BalanceCallback, Tracker, AccountBalance, PositionsCallback, AccountPosition, TrackedAssetCallback, TrackedAssetInfo, MarginMode, ExchangeState } from './types.js';
5
5
 
6
6
  declare class AccountManager {
7
7
  private credentials;
@@ -18,7 +18,7 @@ declare class AccountManager {
18
18
  trackBalance(cb: BalanceCallback): Tracker<AccountBalance | null>;
19
19
  trackPositions(cb: PositionsCallback): Tracker<AccountPosition[]>;
20
20
  trackAsset(coin: string, cb: TrackedAssetCallback): Tracker<TrackedAssetInfo | null>;
21
- setLeverage(symbol: string, leverage: string): Promise<void>;
21
+ setLeverage(symbol: string, leverage: string, marginMode?: MarginMode): Promise<void>;
22
22
  getLeverage(symbol: string): string | null;
23
23
  getState(): Readonly<ExchangeState> | null;
24
24
  disconnect(): void;
@@ -75,9 +75,9 @@ class AccountManager {
75
75
  }
76
76
  };
77
77
  }
78
- async setLeverage(symbol, leverage) {
78
+ async setLeverage(symbol, leverage, marginMode) {
79
79
  if (!this.activeAccount) throw new Error("No active account");
80
- await this.activeAccount.setLeverage(symbol, leverage);
80
+ await this.activeAccount.setLeverage(symbol, leverage, marginMode);
81
81
  }
82
82
  getLeverage(symbol) {
83
83
  return this.activeAccount?.getLeverage(symbol) ?? null;
@@ -1,5 +1,5 @@
1
1
  import { Connector } from '@pear-protocol/types';
2
- import { AccountConfig, SupportedCredentials, BalanceCallback, AccountBalance, PositionsCallback, AccountPosition, TrackedAssetCallback, TrackedAssetInfo, ExchangeState } from './types.js';
2
+ import { AccountConfig, SupportedCredentials, BalanceCallback, AccountBalance, PositionsCallback, AccountPosition, TrackedAssetCallback, TrackedAssetInfo, MarginMode, ExchangeState } from './types.js';
3
3
 
4
4
  declare class ExchangeAccount {
5
5
  private exchange;
@@ -27,7 +27,7 @@ declare class ExchangeAccount {
27
27
  untrackAsset(id: string): void;
28
28
  getTrackedAsset(coin: string): TrackedAssetInfo | null;
29
29
  getLeverage(symbol: string): string | null;
30
- setLeverage(symbol: string, leverage: string): Promise<void>;
30
+ setLeverage(symbol: string, leverage: string, marginMode?: MarginMode): Promise<void>;
31
31
  getState(): Readonly<ExchangeState>;
32
32
  getExchange(): Connector;
33
33
  private applyStateUpdate;
@@ -120,9 +120,9 @@ class ExchangeAccount {
120
120
  }
121
121
  return null;
122
122
  }
123
- async setLeverage(symbol, leverage) {
123
+ async setLeverage(symbol, leverage, marginMode) {
124
124
  if (!this.orchestrator) throw new Error("Not connected");
125
- await this.orchestrator.setLeverage(symbol, leverage);
125
+ await this.orchestrator.setLeverage(symbol, leverage, marginMode);
126
126
  }
127
127
  getState() {
128
128
  return this.state;
@@ -1,4 +1,4 @@
1
- import { SupportedCredentials, StateUpdate } from '../types.js';
1
+ import { SupportedCredentials, MarginMode, StateUpdate } from '../types.js';
2
2
  import '@pear-protocol/types';
3
3
 
4
4
  type StateUpdateHandler = (update: StateUpdate) => void;
@@ -14,7 +14,7 @@ interface Orchestrator {
14
14
  isConnected: boolean;
15
15
  onAssetTracked: (coin: string) => void;
16
16
  onAssetUntracked: (coin: string) => void;
17
- setLeverage: (symbol: string, leverage: string) => Promise<void>;
17
+ setLeverage: (symbol: string, leverage: string, marginMode?: MarginMode) => Promise<void>;
18
18
  trackAsset: (coin: string) => void;
19
19
  untrackAsset: (coin: string) => boolean;
20
20
  getTrackedCoins: () => ReadonlySet<string>;
@@ -36,7 +36,7 @@ declare abstract class BaseOrchestrator implements Orchestrator {
36
36
  abstract get isConnected(): boolean;
37
37
  abstract onAssetTracked(coin: string): void;
38
38
  abstract onAssetUntracked(coin: string): void;
39
- abstract setLeverage(symbol: string, leverage: string): Promise<void>;
39
+ abstract setLeverage(symbol: string, leverage: string, marginMode?: MarginMode): Promise<void>;
40
40
  trackAsset(coin: string): void;
41
41
  untrackAsset(coin: string): boolean;
42
42
  getTrackedCoins(): ReadonlySet<string>;
@@ -1,4 +1,4 @@
1
- import { SupportedCredentials } from '../../types.js';
1
+ import { SupportedCredentials, MarginMode } from '../../types.js';
2
2
  import { BaseOrchestrator, OrchestratorConfig } from '../base.js';
3
3
  import '@pear-protocol/types';
4
4
 
@@ -21,7 +21,7 @@ declare class BinanceOrchestrator extends BaseOrchestrator {
21
21
  get isConnected(): boolean;
22
22
  onAssetTracked(coin: string): void;
23
23
  onAssetUntracked(coin: string): void;
24
- setLeverage(symbol: string, leverage: string): Promise<void>;
24
+ setLeverage(symbol: string, leverage: string, marginMode?: MarginMode): Promise<void>;
25
25
  private stopAll;
26
26
  private pollAccountStatus;
27
27
  private handleUserDataEvent;
@@ -2,7 +2,7 @@ import { positionKey } from '../../utils';
2
2
  import { BaseOrchestrator } from '../base';
3
3
  import { DEFAULT_LISTEN_KEY_REFRESH_MS, DEFAULT_ACCOUNT_POLL_MS } from './const';
4
4
  import { mapBalance, mapPositions, mergePositions } from './mapper';
5
- import { fetchAccountStatus, fetchAllPositionRisk, createListenKey, keepAliveListenKey, fetchPositionRisk, setLeverage } from './rest';
5
+ import { fetchAccountStatus, fetchAllPositionRisk, createListenKey, keepAliveListenKey, fetchPositionRisk, setMarginType, setLeverage } from './rest';
6
6
  import { BinanceWs } from './ws';
7
7
 
8
8
  class BinanceOrchestrator extends BaseOrchestrator {
@@ -100,8 +100,10 @@ class BinanceOrchestrator extends BaseOrchestrator {
100
100
  this.trackedAssetInfos.delete(coin);
101
101
  this.onStateUpdate({ trackedAssets: new Map(this.trackedAssetInfos) });
102
102
  }
103
- async setLeverage(symbol, leverage) {
103
+ async setLeverage(symbol, leverage, marginMode) {
104
104
  if (!this.creds) throw new Error("Not connected");
105
+ const mode = marginMode ?? "cross";
106
+ await setMarginType(this.creds, symbol, mode === "isolated" ? "ISOLATED" : "CROSSED", this.demo);
105
107
  await setLeverage(this.creds, symbol, leverage, this.demo);
106
108
  }
107
109
  stopAll() {
@@ -8,6 +8,7 @@ declare function fetchAccountStatus(creds: BinanceUSDMCredentialsDTO, demo: bool
8
8
  declare function fetchPositionRisk(creds: BinanceUSDMCredentialsDTO, symbol: string, demo: boolean): Promise<BinanceRawPositionRisk | undefined>;
9
9
  declare function fetchAllPositionRisk(creds: BinanceUSDMCredentialsDTO, demo: boolean): Promise<BinanceRawPositionRisk[]>;
10
10
  declare function setLeverage(creds: BinanceUSDMCredentialsDTO, symbol: string, leverage: string, demo: boolean): Promise<BinanceRawLeverageResponse>;
11
+ declare function setMarginType(creds: BinanceUSDMCredentialsDTO, symbol: string, marginType: 'ISOLATED' | 'CROSSED', demo: boolean): Promise<void>;
11
12
  declare function fetchAssetIndex(demo: boolean): Promise<BinanceAssetIndex[]>;
12
13
 
13
- export { createListenKey, fetchAccountStatus, fetchAllPositionRisk, fetchAssetIndex, fetchPositionRisk, keepAliveListenKey, setLeverage };
14
+ export { createListenKey, fetchAccountStatus, fetchAllPositionRisk, fetchAssetIndex, fetchPositionRisk, keepAliveListenKey, setLeverage, setMarginType };
@@ -69,6 +69,9 @@ async function fetchAllPositionRisk(creds, demo) {
69
69
  async function setLeverage(creds, symbol, leverage, demo) {
70
70
  return signedPost(creds, "/fapi/v1/leverage", { symbol, leverage }, demo);
71
71
  }
72
+ async function setMarginType(creds, symbol, marginType, demo) {
73
+ await signedPost(creds, "/fapi/v1/marginType", { symbol, marginType }, demo);
74
+ }
72
75
  async function fetchAssetIndex(demo) {
73
76
  const baseUrl = demo ? BASE_URL_TESTNET : BASE_URL;
74
77
  const response = await fetch(`${baseUrl}/fapi/v1/assetIndex`);
@@ -78,4 +81,4 @@ async function fetchAssetIndex(demo) {
78
81
  return await response.json();
79
82
  }
80
83
 
81
- export { createListenKey, fetchAccountStatus, fetchAllPositionRisk, fetchAssetIndex, fetchPositionRisk, keepAliveListenKey, setLeverage };
84
+ export { createListenKey, fetchAccountStatus, fetchAllPositionRisk, fetchAssetIndex, fetchPositionRisk, keepAliveListenKey, setLeverage, setMarginType };
@@ -1,4 +1,4 @@
1
- import { SupportedCredentials } from '../../types.js';
1
+ import { SupportedCredentials, MarginMode } from '../../types.js';
2
2
  import { BaseOrchestrator } from '../base.js';
3
3
  import '@pear-protocol/types';
4
4
 
@@ -13,7 +13,7 @@ declare class BybitOrchestrator extends BaseOrchestrator {
13
13
  get isConnected(): boolean;
14
14
  onAssetTracked(coin: string): void;
15
15
  onAssetUntracked(coin: string): void;
16
- setLeverage(symbol: string, leverage: string): Promise<void>;
16
+ setLeverage(symbol: string, leverage: string, marginMode?: MarginMode): Promise<void>;
17
17
  private stopAll;
18
18
  private handleMessage;
19
19
  private handleWalletUpdate;
@@ -1,6 +1,6 @@
1
1
  import { BaseOrchestrator } from '../base';
2
2
  import { mergeRawCoins, buildBalance, mapPositions, mapPositionUpdates } from './mapper';
3
- import { fetchWalletBalance, fetchPositions, fetchAccountInfo, fetchPositionLeverage, setLeverage } from './rest';
3
+ import { fetchWalletBalance, fetchPositions, fetchAccountInfo, fetchPositionLeverage, switchMarginMode } from './rest';
4
4
  import { BybitWs } from './ws';
5
5
 
6
6
  class BybitOrchestrator extends BaseOrchestrator {
@@ -93,10 +93,11 @@ class BybitOrchestrator extends BaseOrchestrator {
93
93
  this.trackedAssetInfos.delete(coin);
94
94
  this.onStateUpdate({ trackedAssets: new Map(this.trackedAssetInfos) });
95
95
  }
96
- async setLeverage(symbol, leverage) {
96
+ async setLeverage(symbol, leverage, marginMode) {
97
97
  const creds = this.credentials;
98
98
  if (!creds) throw new Error("Not connected");
99
- await setLeverage(creds, symbol, leverage, this.demo);
99
+ const mode = marginMode ?? "cross";
100
+ await switchMarginMode(creds, symbol, mode === "isolated" ? 1 : 0, leverage, this.demo);
100
101
  }
101
102
  stopAll() {
102
103
  this.clearReconnectTimer();
@@ -5,7 +5,8 @@ declare function fetchWalletBalance(creds: BybitCredentialsDTO, demo: boolean):
5
5
  declare function fetchPositions(creds: BybitCredentialsDTO, demo: boolean): Promise<BybitRawPosition[]>;
6
6
  declare function fetchPositionLeverage(creds: BybitCredentialsDTO, symbol: string, demo: boolean): Promise<BybitRawPosition | undefined>;
7
7
  declare function setLeverage(creds: BybitCredentialsDTO, symbol: string, leverage: string, demo: boolean): Promise<void>;
8
+ declare function switchMarginMode(creds: BybitCredentialsDTO, symbol: string, tradeMode: 0 | 1, leverage: string, demo: boolean): Promise<void>;
8
9
  declare function fetchAccountInfo(creds: BybitCredentialsDTO, demo: boolean): Promise<BybitRawAccountInfo>;
9
10
  declare function fetchCollateralRatios(creds: BybitCredentialsDTO, demo: boolean): Promise<Map<string, string>>;
10
11
 
11
- export { fetchAccountInfo, fetchCollateralRatios, fetchPositionLeverage, fetchPositions, fetchWalletBalance, setLeverage };
12
+ export { fetchAccountInfo, fetchCollateralRatios, fetchPositionLeverage, fetchPositions, fetchWalletBalance, setLeverage, switchMarginMode };
@@ -87,6 +87,14 @@ async function setLeverage(creds, symbol, leverage, demo) {
87
87
  demo
88
88
  );
89
89
  }
90
+ async function switchMarginMode(creds, symbol, tradeMode, leverage, demo) {
91
+ await signedPost(
92
+ creds,
93
+ "/v5/position/switch-isolated",
94
+ { category: "linear", symbol, tradeMode, buyLeverage: leverage, sellLeverage: leverage },
95
+ demo
96
+ );
97
+ }
90
98
  async function fetchAccountInfo(creds, demo) {
91
99
  return signedGet(creds, "/v5/account/account-info", {}, demo);
92
100
  }
@@ -107,4 +115,4 @@ async function fetchCollateralRatios(creds, demo) {
107
115
  return ratios;
108
116
  }
109
117
 
110
- export { fetchAccountInfo, fetchCollateralRatios, fetchPositionLeverage, fetchPositions, fetchWalletBalance, setLeverage };
118
+ export { fetchAccountInfo, fetchCollateralRatios, fetchPositionLeverage, fetchPositions, fetchWalletBalance, setLeverage, switchMarginMode };
@@ -1,5 +1,5 @@
1
1
  import { InstrumentId } from '@pear-protocol/types';
2
- import { SupportedCredentials } from '../../types.js';
2
+ import { SupportedCredentials, MarginMode } from '../../types.js';
3
3
  import { BaseOrchestrator } from '../base.js';
4
4
 
5
5
  declare class HyperliquidOrchestrator extends BaseOrchestrator {
@@ -21,7 +21,7 @@ declare class HyperliquidOrchestrator extends BaseOrchestrator {
21
21
  get isConnected(): boolean;
22
22
  onAssetTracked(coin: string): void;
23
23
  onAssetUntracked(coin: string): void;
24
- setLeverage(id: InstrumentId, leverage: string): Promise<void>;
24
+ setLeverage(id: InstrumentId, leverage: string, marginMode?: MarginMode): Promise<void>;
25
25
  protected canUntrackAsset(coin: string): boolean;
26
26
  private fetchPerpMetas;
27
27
  private subscribeActiveAssetData;
@@ -105,13 +105,12 @@ class HyperliquidOrchestrator extends BaseOrchestrator {
105
105
  const trackedAssets = this.buildTrackedAssetsMap();
106
106
  this.onStateUpdate({ trackedAssets });
107
107
  }
108
- async setLeverage(id, leverage) {
108
+ async setLeverage(id, leverage, marginMode) {
109
109
  const creds = this.credentials;
110
110
  if (!creds) throw new Error("Not connected");
111
111
  const asset = Number.parseInt(id, 10);
112
112
  if (Number.isNaN(asset)) throw new Error(`Invalid Hyperliquid asset index: ${id}`);
113
- const tracked = this.trackedAssetData.get(id);
114
- const isCross = tracked?.leverage?.type === "cross";
113
+ const isCross = (marginMode ?? "cross") == "cross";
115
114
  await setLeverage(creds, asset, Number.parseFloat(leverage), isCross);
116
115
  }
117
116
  canUntrackAsset(coin) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pear-protocol/exchanges-sdk",
3
- "version": "0.0.7",
3
+ "version": "0.0.8-test.1",
4
4
  "description": "Pear Protocol Exchanges SDK",
5
5
  "private": false,
6
6
  "type": "module",
@@ -22,7 +22,8 @@
22
22
  "scripts": {
23
23
  "build": "tsup",
24
24
  "clean": "rm -rf dist .turbo",
25
- "typecheck": "tsc --noEmit"
25
+ "typecheck": "tsc --noEmit",
26
+ "publish:test": "tsup && bun publish --tag test"
26
27
  },
27
28
  "dependencies": {
28
29
  "@nktkas/hyperliquid": "0.30.2",
@@ -31,7 +32,7 @@
31
32
  "viem": "^2.43.5"
32
33
  },
33
34
  "devDependencies": {
34
- "@backend/typescript-config": "workspace:*",
35
+ "@backend/typescript-config": "0.0.0",
35
36
  "tsup": "8.5.1",
36
37
  "typescript": "5.9.3"
37
38
  }