@pear-protocol/exchanges-sdk 0.0.8-test.3 → 0.0.8

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, MarginMode, ExchangeState } from './types.js';
4
+ import { BalanceCallback, Tracker, AccountBalance, PositionsCallback, AccountPosition, TrackedAssetCallback, TrackedAssetInfo, 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, marginMode?: MarginMode): Promise<void>;
21
+ setLeverage(symbol: string, leverage: string): 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, marginMode) {
78
+ async setLeverage(symbol, leverage) {
79
79
  if (!this.activeAccount) throw new Error("No active account");
80
- await this.activeAccount.setLeverage(symbol, leverage, marginMode);
80
+ await this.activeAccount.setLeverage(symbol, leverage);
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, MarginMode, ExchangeState } from './types.js';
2
+ import { AccountConfig, SupportedCredentials, BalanceCallback, AccountBalance, PositionsCallback, AccountPosition, TrackedAssetCallback, TrackedAssetInfo, 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, marginMode?: MarginMode): Promise<void>;
30
+ setLeverage(symbol: string, leverage: string): 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, marginMode) {
123
+ async setLeverage(symbol, leverage) {
124
124
  if (!this.orchestrator) throw new Error("Not connected");
125
- await this.orchestrator.setLeverage(symbol, leverage, marginMode);
125
+ await this.orchestrator.setLeverage(symbol, leverage);
126
126
  }
127
127
  getState() {
128
128
  return this.state;
@@ -1,4 +1,4 @@
1
- import { SupportedCredentials, MarginMode, StateUpdate } from '../types.js';
1
+ import { SupportedCredentials, 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, marginMode?: MarginMode) => Promise<void>;
17
+ setLeverage: (symbol: string, leverage: string) => 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, marginMode?: MarginMode): Promise<void>;
39
+ abstract setLeverage(symbol: string, leverage: string): 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, MarginMode } from '../../types.js';
1
+ import { SupportedCredentials } 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, marginMode?: MarginMode): Promise<void>;
24
+ setLeverage(symbol: string, leverage: string): 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, setMarginType, setLeverage } from './rest';
5
+ import { fetchAccountStatus, fetchAllPositionRisk, createListenKey, keepAliveListenKey, fetchPositionRisk, setLeverage } from './rest';
6
6
  import { BinanceWs } from './ws';
7
7
 
8
8
  class BinanceOrchestrator extends BaseOrchestrator {
@@ -100,10 +100,8 @@ 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, marginMode) {
103
+ async setLeverage(symbol, leverage) {
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);
107
105
  await setLeverage(this.creds, symbol, leverage, this.demo);
108
106
  }
109
107
  stopAll() {
@@ -1,14 +1,13 @@
1
- import { BinanceUSDMCredentialsDTO } from '@pear-protocol/types';
1
+ import { BinanceCredentialsDTO } from '@pear-protocol/types';
2
2
  import { BinanceRawAccountStatus, BinanceRawPositionRisk, BinanceAssetIndex, BinanceRawLeverageResponse } from './types.js';
3
3
  import '../../types.js';
4
4
 
5
5
  declare function createListenKey(apiKey: string, demo?: boolean): Promise<string>;
6
6
  declare function keepAliveListenKey(apiKey: string, demo?: boolean): Promise<void>;
7
- declare function fetchAccountStatus(creds: BinanceUSDMCredentialsDTO, demo: boolean): Promise<BinanceRawAccountStatus>;
8
- declare function fetchPositionRisk(creds: BinanceUSDMCredentialsDTO, symbol: string, demo: boolean): Promise<BinanceRawPositionRisk | undefined>;
9
- declare function fetchAllPositionRisk(creds: BinanceUSDMCredentialsDTO, demo: boolean): Promise<BinanceRawPositionRisk[]>;
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>;
7
+ declare function fetchAccountStatus(creds: BinanceCredentialsDTO, demo: boolean): Promise<BinanceRawAccountStatus>;
8
+ declare function fetchPositionRisk(creds: BinanceCredentialsDTO, symbol: string, demo: boolean): Promise<BinanceRawPositionRisk | undefined>;
9
+ declare function fetchAllPositionRisk(creds: BinanceCredentialsDTO, demo: boolean): Promise<BinanceRawPositionRisk[]>;
10
+ declare function setLeverage(creds: BinanceCredentialsDTO, symbol: string, leverage: string, demo: boolean): Promise<BinanceRawLeverageResponse>;
12
11
  declare function fetchAssetIndex(demo: boolean): Promise<BinanceAssetIndex[]>;
13
12
 
14
- export { createListenKey, fetchAccountStatus, fetchAllPositionRisk, fetchAssetIndex, fetchPositionRisk, keepAliveListenKey, setLeverage, setMarginType };
13
+ export { createListenKey, fetchAccountStatus, fetchAllPositionRisk, fetchAssetIndex, fetchPositionRisk, keepAliveListenKey, setLeverage };
@@ -69,9 +69,6 @@ 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
- }
75
72
  async function fetchAssetIndex(demo) {
76
73
  const baseUrl = demo ? BASE_URL_TESTNET : BASE_URL;
77
74
  const response = await fetch(`${baseUrl}/fapi/v1/assetIndex`);
@@ -81,4 +78,4 @@ async function fetchAssetIndex(demo) {
81
78
  return await response.json();
82
79
  }
83
80
 
84
- export { createListenKey, fetchAccountStatus, fetchAllPositionRisk, fetchAssetIndex, fetchPositionRisk, keepAliveListenKey, setLeverage, setMarginType };
81
+ export { createListenKey, fetchAccountStatus, fetchAllPositionRisk, fetchAssetIndex, fetchPositionRisk, keepAliveListenKey, setLeverage };
@@ -1,4 +1,4 @@
1
- import { BinanceUSDMCredentialsDTO } from '@pear-protocol/types';
1
+ import { BinanceCredentialsDTO } from '@pear-protocol/types';
2
2
  import { BinanceWsHandlers } from './types.js';
3
3
  import '../../types.js';
4
4
 
@@ -11,7 +11,7 @@ declare class BinanceWs {
11
11
  private creds;
12
12
  constructor(handlers: BinanceWsHandlers, demo: boolean);
13
13
  connectUserDataStream(listenKey: string): Promise<void>;
14
- connectWsApi(creds: BinanceUSDMCredentialsDTO): Promise<void>;
14
+ connectWsApi(creds: BinanceCredentialsDTO): Promise<void>;
15
15
  nextRequestId(): number;
16
16
  sendSignedRequest(id: number, method: string, params?: Record<string, unknown>): Promise<void>;
17
17
  close(): void;
@@ -1,4 +1,4 @@
1
- import { SupportedCredentials, MarginMode } from '../../types.js';
1
+ import { SupportedCredentials } 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, marginMode?: MarginMode): Promise<void>;
16
+ setLeverage(symbol: string, leverage: string): 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, switchMarginMode } from './rest';
3
+ import { fetchWalletBalance, fetchPositions, fetchAccountInfo, fetchPositionLeverage, setLeverage } from './rest';
4
4
  import { BybitWs } from './ws';
5
5
 
6
6
  class BybitOrchestrator extends BaseOrchestrator {
@@ -93,11 +93,10 @@ 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, marginMode) {
96
+ async setLeverage(symbol, leverage) {
97
97
  const creds = this.credentials;
98
98
  if (!creds) throw new Error("Not connected");
99
- const mode = marginMode ?? "cross";
100
- await switchMarginMode(creds, symbol, mode === "isolated" ? 1 : 0, leverage, this.demo);
99
+ await setLeverage(creds, symbol, leverage, this.demo);
101
100
  }
102
101
  stopAll() {
103
102
  this.clearReconnectTimer();
@@ -5,8 +5,7 @@ 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>;
9
8
  declare function fetchAccountInfo(creds: BybitCredentialsDTO, demo: boolean): Promise<BybitRawAccountInfo>;
10
9
  declare function fetchCollateralRatios(creds: BybitCredentialsDTO, demo: boolean): Promise<Map<string, string>>;
11
10
 
12
- export { fetchAccountInfo, fetchCollateralRatios, fetchPositionLeverage, fetchPositions, fetchWalletBalance, setLeverage, switchMarginMode };
11
+ export { fetchAccountInfo, fetchCollateralRatios, fetchPositionLeverage, fetchPositions, fetchWalletBalance, setLeverage };
@@ -87,14 +87,6 @@ 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
- }
98
90
  async function fetchAccountInfo(creds, demo) {
99
91
  return signedGet(creds, "/v5/account/account-info", {}, demo);
100
92
  }
@@ -115,4 +107,4 @@ async function fetchCollateralRatios(creds, demo) {
115
107
  return ratios;
116
108
  }
117
109
 
118
- export { fetchAccountInfo, fetchCollateralRatios, fetchPositionLeverage, fetchPositions, fetchWalletBalance, setLeverage, switchMarginMode };
110
+ export { fetchAccountInfo, fetchCollateralRatios, fetchPositionLeverage, fetchPositions, fetchWalletBalance, setLeverage };
@@ -1,5 +1,5 @@
1
1
  import { InstrumentId } from '@pear-protocol/types';
2
- import { SupportedCredentials, MarginMode } from '../../types.js';
2
+ import { SupportedCredentials } 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, marginMode?: MarginMode): Promise<void>;
24
+ setLeverage(id: InstrumentId, leverage: string): Promise<void>;
25
25
  protected canUntrackAsset(coin: string): boolean;
26
26
  private fetchPerpMetas;
27
27
  private subscribeActiveAssetData;
@@ -105,12 +105,13 @@ class HyperliquidOrchestrator extends BaseOrchestrator {
105
105
  const trackedAssets = this.buildTrackedAssetsMap();
106
106
  this.onStateUpdate({ trackedAssets });
107
107
  }
108
- async setLeverage(id, leverage, marginMode) {
108
+ async setLeverage(id, leverage) {
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 isCross = (marginMode ?? "cross") === "cross";
113
+ const tracked = this.trackedAssetData.get(id);
114
+ const isCross = tracked?.leverage?.type === "cross";
114
115
  await setLeverage(creds, asset, Number.parseFloat(leverage), isCross);
115
116
  }
116
117
  canUntrackAsset(coin) {
@@ -5,7 +5,7 @@ import { HyperliquidOrchestrator } from './hyperliquid/orchestrator';
5
5
 
6
6
  function createOrchestrator(exchange, config) {
7
7
  switch (exchange) {
8
- case "binanceusdm":
8
+ case "binance":
9
9
  return new BinanceOrchestrator(config);
10
10
  case "bybit":
11
11
  return new BybitOrchestrator(config);
@@ -1,4 +1,4 @@
1
- import { Connector, BinanceUSDMCredentialsDTO, BybitCredentialsDTO, HyperliquidCredentialsDTO } from '@pear-protocol/types';
1
+ import { Connector, BinanceCredentialsDTO, BybitCredentialsDTO, HyperliquidCredentialsDTO } from '@pear-protocol/types';
2
2
 
3
3
  type MarginMode = 'cross' | 'isolated';
4
4
  interface AssetBalance {
@@ -62,7 +62,7 @@ interface AccountConfig {
62
62
  type HyperliquidAccountCredentials = HyperliquidCredentialsDTO & {
63
63
  address: string;
64
64
  };
65
- type SupportedCredentials = BinanceUSDMCredentialsDTO | BybitCredentialsDTO | HyperliquidAccountCredentials;
65
+ type SupportedCredentials = BinanceCredentialsDTO | BybitCredentialsDTO | HyperliquidAccountCredentials;
66
66
  interface StateUpdate {
67
67
  balance?: AccountBalance;
68
68
  positions?: Map<string, AccountPosition>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pear-protocol/exchanges-sdk",
3
- "version": "0.0.8-test.3",
3
+ "version": "0.0.8",
4
4
  "description": "Pear Protocol Exchanges SDK",
5
5
  "private": false,
6
6
  "type": "module",
@@ -22,17 +22,16 @@
22
22
  "scripts": {
23
23
  "build": "tsup",
24
24
  "clean": "rm -rf dist .turbo",
25
- "typecheck": "tsc --noEmit",
26
- "publish:test": "tsup && bun publish --tag test"
25
+ "typecheck": "tsc --noEmit"
27
26
  },
28
27
  "dependencies": {
29
28
  "@nktkas/hyperliquid": "0.30.2",
30
- "@pear-protocol/core-sdk": "0.0.8",
31
- "@pear-protocol/types": "0.0.7",
29
+ "@pear-protocol/core-sdk": "0.0.9",
30
+ "@pear-protocol/types": "0.0.8",
32
31
  "viem": "^2.43.5"
33
32
  },
34
33
  "devDependencies": {
35
- "@backend/typescript-config": "0.0.0",
34
+ "@backend/typescript-config": "workspace:*",
36
35
  "tsup": "8.5.1",
37
36
  "typescript": "5.9.3"
38
37
  }