@n1xyz/nord-ts 0.0.16 → 0.0.17

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 (192) hide show
  1. package/dist/bridge/client.js +45 -6
  2. package/dist/gen/nord.d.ts +48 -4
  3. package/dist/gen/nord.js +633 -33
  4. package/dist/idl/bridge.d.ts +569 -0
  5. package/dist/idl/bridge.js +8 -0
  6. package/dist/idl/bridge.json +75 -62
  7. package/dist/idl/index.d.ts +71 -49
  8. package/dist/index.d.ts +1 -3
  9. package/dist/index.js +6 -13
  10. package/dist/nord/api/actions.d.ts +7 -2
  11. package/dist/nord/api/actions.js +7 -1
  12. package/dist/nord/api/core.d.ts +1 -1
  13. package/dist/nord/api/core.js +3 -3
  14. package/dist/nord/api/market.js +1 -3
  15. package/dist/nord/api/queries.js +2 -2
  16. package/dist/nord/client/Nord.d.ts +9 -14
  17. package/dist/nord/client/Nord.js +27 -38
  18. package/dist/nord/client/NordUser.d.ts +7 -23
  19. package/dist/nord/client/NordUser.js +66 -143
  20. package/dist/nord/utils/NordError.js +5 -2
  21. package/dist/types.d.ts +34 -50
  22. package/dist/utils.d.ts +2 -0
  23. package/dist/utils.js +14 -0
  24. package/package.json +18 -18
  25. package/protoc-generate.sh +0 -0
  26. package/src/idl/bridge.json +75 -62
  27. package/src/index.ts +9 -12
  28. package/src/nord/api/actions.ts +14 -3
  29. package/src/nord/api/core.ts +4 -2
  30. package/src/nord/api/market.ts +1 -4
  31. package/src/nord/api/queries.ts +2 -2
  32. package/src/nord/client/Nord.ts +45 -50
  33. package/src/nord/client/NordUser.ts +89 -169
  34. package/src/nord/utils/NordError.ts +6 -2
  35. package/src/types.ts +34 -49
  36. package/src/utils.ts +18 -0
  37. package/codebase.md +0 -18886
  38. package/docs/.nojekyll +0 -1
  39. package/docs/assets/hierarchy.js +0 -1
  40. package/docs/assets/highlight.css +0 -92
  41. package/docs/assets/icons.js +0 -18
  42. package/docs/assets/icons.svg +0 -1
  43. package/docs/assets/main.js +0 -60
  44. package/docs/assets/navigation.js +0 -1
  45. package/docs/assets/search.js +0 -1
  46. package/docs/assets/style.css +0 -1611
  47. package/docs/classes/Nord.html +0 -189
  48. package/docs/classes/NordError.html +0 -24
  49. package/docs/classes/NordUser.html +0 -115
  50. package/docs/classes/NordWebSocketClient.html +0 -330
  51. package/docs/classes/SolanaBridgeClient.html +0 -91
  52. package/docs/classes/Subscriber.html +0 -10
  53. package/docs/enums/FillMode.html +0 -5
  54. package/docs/enums/KeyType.html +0 -4
  55. package/docs/enums/MetricPeriod.html +0 -9
  56. package/docs/enums/PdaSeedType.html +0 -11
  57. package/docs/enums/PeakTpsPeriodUnit.html +0 -15
  58. package/docs/enums/Side.html +0 -3
  59. package/docs/enums/WebSocketMessageType.html +0 -7
  60. package/docs/functions/actionQueryRollman.html +0 -6
  61. package/docs/functions/actionsQueryRollman.html +0 -6
  62. package/docs/functions/aggregateMetrics-1.html +0 -7
  63. package/docs/functions/assert.html +0 -1
  64. package/docs/functions/bigIntToProtoU128.html +0 -4
  65. package/docs/functions/bridgeToBN.html +0 -5
  66. package/docs/functions/bufferToHex.html +0 -4
  67. package/docs/functions/cancelOrder.html +0 -1
  68. package/docs/functions/checkPubKeyLength.html +0 -1
  69. package/docs/functions/checkedFetch.html +0 -6
  70. package/docs/functions/createSession.html +0 -1
  71. package/docs/functions/decodeLengthDelimited.html +0 -11
  72. package/docs/functions/encodeLengthDelimited.html +0 -6
  73. package/docs/functions/fillModeToProtoFillMode.html +0 -5
  74. package/docs/functions/findMarket.html +0 -1
  75. package/docs/functions/findPda.html +0 -6
  76. package/docs/functions/findToken.html +0 -1
  77. package/docs/functions/fromBN.html +0 -5
  78. package/docs/functions/getAccount.html +0 -6
  79. package/docs/functions/getActionNonce.html +0 -5
  80. package/docs/functions/getCurrentTps.html +0 -6
  81. package/docs/functions/getInfo.html +0 -5
  82. package/docs/functions/getLastActionId.html +0 -5
  83. package/docs/functions/getMedianLatency.html +0 -6
  84. package/docs/functions/getOrderbook.html +0 -6
  85. package/docs/functions/getPeakTps.html +0 -6
  86. package/docs/functions/getTimestamp.html +0 -5
  87. package/docs/functions/getTotalTransactions.html +0 -5
  88. package/docs/functions/getTrades.html +0 -6
  89. package/docs/functions/getUserAccountIds.html +0 -6
  90. package/docs/functions/hexToBuffer.html +0 -4
  91. package/docs/functions/initWebSocketClient.html +0 -12
  92. package/docs/functions/keypairFromPrivateKey.html +0 -4
  93. package/docs/functions/makeSigningFunction.html +0 -4
  94. package/docs/functions/makeWalletSignFn.html +0 -6
  95. package/docs/functions/marketsStats.html +0 -5
  96. package/docs/functions/optExpect.html +0 -5
  97. package/docs/functions/optMap.html +0 -5
  98. package/docs/functions/optUnwrap.html +0 -2
  99. package/docs/functions/panic.html +0 -1
  100. package/docs/functions/placeOrder.html +0 -1
  101. package/docs/functions/queryAction.html +0 -6
  102. package/docs/functions/queryPrometheus.html +0 -6
  103. package/docs/functions/queryRecentActions.html +0 -7
  104. package/docs/functions/revokeSession.html +0 -1
  105. package/docs/functions/shortenPublicKey.html +0 -5
  106. package/docs/functions/signAction.html +0 -6
  107. package/docs/functions/toBN.html +0 -5
  108. package/docs/functions/toScaledU128.html +0 -8
  109. package/docs/functions/toScaledU64.html +0 -8
  110. package/docs/functions/transfer.html +0 -1
  111. package/docs/functions/withdraw.html +0 -1
  112. package/docs/hierarchy.html +0 -1
  113. package/docs/index.html +0 -40
  114. package/docs/interfaces/Account.html +0 -8
  115. package/docs/interfaces/ActionInfo.html +0 -8
  116. package/docs/interfaces/ActionNonceResponse.html +0 -3
  117. package/docs/interfaces/ActionQuery.html +0 -4
  118. package/docs/interfaces/ActionResponse.html +0 -8
  119. package/docs/interfaces/ActionsExtendedInfo.html +0 -10
  120. package/docs/interfaces/ActionsQuery.html +0 -5
  121. package/docs/interfaces/ActionsResponse.html +0 -6
  122. package/docs/interfaces/AggregateMetrics.html +0 -12
  123. package/docs/interfaces/BlockFacts.html +0 -10
  124. package/docs/interfaces/BlockQuery.html +0 -6
  125. package/docs/interfaces/BlockResponse.html +0 -6
  126. package/docs/interfaces/BlockSummary.html +0 -8
  127. package/docs/interfaces/BlockSummaryResponse.html +0 -6
  128. package/docs/interfaces/DeltaEvent.html +0 -6
  129. package/docs/interfaces/DepositSplParams.html +0 -8
  130. package/docs/interfaces/Info.html +0 -3
  131. package/docs/interfaces/Market.html +0 -8
  132. package/docs/interfaces/MarketStats.html +0 -7
  133. package/docs/interfaces/MarketsStatsResponse.html +0 -2
  134. package/docs/interfaces/NordConfig.html +0 -24
  135. package/docs/interfaces/NordWebSocketClientEvents.html +0 -4
  136. package/docs/interfaces/NordWebSocketEvents.html +0 -8
  137. package/docs/interfaces/Order.html +0 -6
  138. package/docs/interfaces/OrderInfo.html +0 -6
  139. package/docs/interfaces/OrderbookEntry.html +0 -4
  140. package/docs/interfaces/OrderbookQuery.html +0 -6
  141. package/docs/interfaces/OrderbookResponse.html +0 -6
  142. package/docs/interfaces/OrderbookSubscription.html +0 -159
  143. package/docs/interfaces/PerpMarketStats.html +0 -5
  144. package/docs/interfaces/RollmanActionExtendedInfo.html +0 -4
  145. package/docs/interfaces/RollmanActionInfo.html +0 -4
  146. package/docs/interfaces/RollmanActionResponse.html +0 -4
  147. package/docs/interfaces/RollmanActionsResponse.html +0 -2
  148. package/docs/interfaces/RollmanBlockResponse.html +0 -3
  149. package/docs/interfaces/SPLTokenInfo.html +0 -10
  150. package/docs/interfaces/SolanaBridgeConfig.html +0 -12
  151. package/docs/interfaces/StateFacts.html +0 -10
  152. package/docs/interfaces/SubscriberConfig.html +0 -3
  153. package/docs/interfaces/TimestampResponse.html +0 -3
  154. package/docs/interfaces/Token.html +0 -5
  155. package/docs/interfaces/TokenInfo.html +0 -6
  156. package/docs/interfaces/Trade.html +0 -5
  157. package/docs/interfaces/TradeSubscription.html +0 -159
  158. package/docs/interfaces/Trades.html +0 -5
  159. package/docs/interfaces/TradesQuery.html +0 -6
  160. package/docs/interfaces/TradesResponse.html +0 -7
  161. package/docs/interfaces/TransferParams.html +0 -8
  162. package/docs/interfaces/UserAccountIdsQuery.html +0 -3
  163. package/docs/interfaces/UserAccountIdsResponse.html +0 -3
  164. package/docs/interfaces/WebSocketAccountUpdate.html +0 -6
  165. package/docs/interfaces/WebSocketDeltaUpdate.html +0 -9
  166. package/docs/interfaces/WebSocketSubscription.html +0 -4
  167. package/docs/interfaces/WebSocketTradeUpdate.html +0 -6
  168. package/docs/interfaces/WithdrawalClaim.html +0 -14
  169. package/docs/interfaces/WithdrawalParams.html +0 -8
  170. package/docs/modules.html +0 -1
  171. package/docs/types/BigIntValue.html +0 -2
  172. package/docs/types/SubscriptionPattern.html +0 -4
  173. package/docs/types/SubscriptionType.html +0 -2
  174. package/docs/types/WebSocketMessage.html +0 -1
  175. package/docs/variables/DEBUG_KEYS.html +0 -1
  176. package/docs/variables/DEFAULT_FUNDING_AMOUNTS.html +0 -1
  177. package/docs/variables/DEV_TOKEN_INFOS.html +0 -1
  178. package/docs/variables/DEV_URL.html +0 -1
  179. package/docs/variables/MAX_BUFFER_LEN.html +0 -1
  180. package/docs/variables/SESSION_TTL.html +0 -1
  181. package/docs/variables/WEBSERVER_DEV_URL.html +0 -1
  182. package/docs/variables/ZERO_DECIMAL.html +0 -1
  183. package/docs/variables/_private.html +0 -2
  184. package/idl-generate.sh +0 -4
  185. package/src/bridge/client.ts +0 -498
  186. package/src/bridge/const.ts +0 -53
  187. package/src/bridge/index.ts +0 -6
  188. package/src/bridge/types.ts +0 -129
  189. package/src/bridge/utils.ts +0 -140
  190. package/src/gen/common.ts +0 -297
  191. package/src/gen/nord.ts +0 -6716
  192. package/src/idl/index.ts +0 -2
@@ -35,13 +35,14 @@ var __importStar = (this && this.__importStar) || (function () {
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.Nord = void 0;
37
37
  const events_1 = require("events");
38
- const web3_js_1 = require("@solana/web3.js");
39
38
  const types_1 = require("../../types");
39
+ const proton_1 = require("@n1xyz/proton");
40
40
  const core = __importStar(require("../api/core"));
41
41
  const market = __importStar(require("../api/market"));
42
42
  const metrics = __importStar(require("../api/metrics"));
43
43
  const queries = __importStar(require("../api/queries"));
44
44
  const NordError_1 = require("../utils/NordError");
45
+ const web3_js_1 = require("@solana/web3.js");
45
46
  /**
46
47
  * Main Nord client class for interacting with the Nord API
47
48
  */
@@ -55,25 +56,17 @@ class Nord {
55
56
  * @param config.solanaUrl - Solana cluster URL
56
57
  * @throws {Error} If required configuration is missing
57
58
  */
58
- constructor(config) {
59
+ constructor({ bridgeVk, solanaUrl, webServerUrl, protonClient, }) {
59
60
  /** Available markets */
60
61
  this.markets = [];
61
62
  /** Available tokens */
62
63
  this.tokens = [];
63
64
  /** Map of symbol to market_id */
64
65
  this.symbolToMarketId = new Map();
65
- if (!config.webServerUrl) {
66
- throw new NordError_1.NordError("webServerUrl is required");
67
- }
68
- if (!config.bridgeVk) {
69
- throw new NordError_1.NordError("bridgeVk is required");
70
- }
71
- if (!config.solanaUrl) {
72
- throw new NordError_1.NordError("solanaUrl is required");
73
- }
74
- this.webServerUrl = config.webServerUrl;
75
- this.bridgeVk = config.bridgeVk;
76
- this.solanaUrl = config.solanaUrl;
66
+ this.webServerUrl = webServerUrl;
67
+ this.bridgeVk = bridgeVk;
68
+ this.solanaUrl = solanaUrl;
69
+ this.protonClient = protonClient;
77
70
  }
78
71
  /**
79
72
  * Create a WebSocket client with specific subscriptions
@@ -136,13 +129,13 @@ class Nord {
136
129
  return core.getTimestamp(this.webServerUrl);
137
130
  }
138
131
  /**
139
- * Get the next action nonce from the Nord server
132
+ * Get the last event nonce from the Nord server
140
133
  *
141
134
  * @returns Next action nonce
142
135
  * @throws {NordError} If the request fails
143
136
  */
144
137
  async getActionNonce() {
145
- return core.getActionNonce(this.webServerUrl);
138
+ return core.getLastEventNonce(this.webServerUrl);
146
139
  }
147
140
  /**
148
141
  * Fetch information about Nord markets and tokens
@@ -174,37 +167,33 @@ class Nord {
174
167
  * @returns Initialized Nord client
175
168
  * @throws {NordError} If initialization fails
176
169
  */
177
- static async initNord(nordConfig) {
178
- const nord = new Nord(nordConfig);
170
+ static async initNord({ bridgeVk: bridgeVk_, solanaUrl, webServerUrl, }) {
171
+ // TODO: we should parametrize the connectionn not have it done here.
172
+ // this is a dogshit api, only here to be compatible with the shitty
173
+ // vibecoded code and not break zero one team's workflow.
174
+ const connection = new web3_js_1.Connection(solanaUrl, { commitment: "confirmed" });
175
+ const bridgeVk = new web3_js_1.PublicKey(bridgeVk_);
176
+ const protonClient = await proton_1.ProtonClient.init({
177
+ protonUrl: webServerUrl,
178
+ bridgeVk,
179
+ solConn: connection,
180
+ });
181
+ const nord = new Nord({
182
+ bridgeVk,
183
+ protonClient,
184
+ solanaUrl,
185
+ webServerUrl,
186
+ });
179
187
  await nord.init();
180
188
  return nord;
181
189
  }
182
190
  /**
183
- * Initialize the Nord client by deriving program ID and fetching info
191
+ * Initialize the Nord client
184
192
  * @private
185
193
  */
186
194
  async init() {
187
- const connection = new web3_js_1.Connection(this.solanaUrl);
188
- const bridgeVkPubkey = new web3_js_1.PublicKey(this.bridgeVk);
189
- const bridgeAccount = await connection.getAccountInfo(bridgeVkPubkey);
190
- if (!bridgeAccount) {
191
- throw new NordError_1.NordError(`Bridge account ${this.bridgeVk} not found`);
192
- }
193
- this._solanaProgramId = bridgeAccount.owner.toString();
194
195
  await this.fetchNordInfo();
195
196
  }
196
- /**
197
- * Get the Solana program ID derived from bridge VK
198
- *
199
- * @returns Program ID string
200
- * @throws {NordError} If program ID hasn't been initialized
201
- */
202
- getSolanaProgramId() {
203
- if (!this._solanaProgramId) {
204
- throw new NordError_1.NordError("Solana program ID not initialized. Have you called Nord.initNord()?");
205
- }
206
- return this._solanaProgramId;
207
- }
208
197
  /**
209
198
  * Get market statistics
210
199
  *
@@ -1,7 +1,6 @@
1
1
  import { Connection, PublicKey } from "@solana/web3.js";
2
2
  import Decimal from "decimal.js";
3
- import { SolanaBridgeClient } from "../../bridge/client";
4
- import { SPLTokenInfo } from "../../bridge/types";
3
+ import { SPLTokenInfo } from "@n1xyz/proton";
5
4
  import { FillMode, Order, Side } from "../../types";
6
5
  import { BigIntValue } from "../../utils";
7
6
  import { Nord } from "./Nord";
@@ -71,7 +70,7 @@ export declare class NordUser {
71
70
  /** Nord client instance */
72
71
  readonly nord: Nord;
73
72
  /** User's blockchain address */
74
- readonly address: string;
73
+ readonly address: PublicKey;
75
74
  /** Function to sign messages with the user's wallet */
76
75
  readonly walletSignFn: (message: Uint8Array | string) => Promise<Uint8Array>;
77
76
  /** Function to sign messages with the user's session key */
@@ -131,8 +130,6 @@ export declare class NordUser {
131
130
  lastTs: number;
132
131
  /** Last nonce used */
133
132
  lastNonce: number;
134
- /** Solana bridge client */
135
- bridgeClient?: SolanaBridgeClient;
136
133
  /** Solana connection */
137
134
  connection: Connection;
138
135
  /** SPL token information */
@@ -143,20 +140,13 @@ export declare class NordUser {
143
140
  * @param params - Parameters for creating a NordUser
144
141
  * @throws {NordError} If required parameters are missing
145
142
  */
146
- constructor(params: NordUserParams);
143
+ constructor({ address, nord, publicKey, sessionPubKey, sessionSignFn, transactionSignFn, walletSignFn, connection, sessionId, }: NordUserParams);
147
144
  /**
148
145
  * Create a clone of this NordUser instance
149
146
  *
150
147
  * @returns A new NordUser instance with the same properties
151
148
  */
152
149
  clone(): NordUser;
153
- /**
154
- * Initialize the Solana bridge client
155
- *
156
- * @private
157
- * @throws {NordError} If required parameters are missing
158
- */
159
- private initBridgeClient;
160
150
  /**
161
151
  * Create a NordUser from a private key
162
152
  *
@@ -184,14 +174,6 @@ export declare class NordUser {
184
174
  * @throws {NordError} If required parameters are missing or operation fails
185
175
  */
186
176
  depositSpl(amount: number, tokenId: number): Promise<string>;
187
- /**
188
- * Withdraw SPL tokens from the bridge
189
- *
190
- * @param claim - Claim data
191
- * @returns Transaction signature
192
- * @throws {NordError} If required parameters are missing or operation fails
193
- */
194
- withdrawSpl(claim: any): Promise<string>;
195
177
  /**
196
178
  * Get a new nonce for actions
197
179
  *
@@ -236,7 +218,9 @@ export declare class NordUser {
236
218
  * @param amount - Amount to withdraw
237
219
  * @throws {NordError} If the operation fails
238
220
  */
239
- withdraw(tokenId: number, amount: number): Promise<void>;
221
+ withdraw(tokenId: number, amount: number): Promise<{
222
+ actionId: bigint;
223
+ }>;
240
224
  /**
241
225
  * Place an order on the exchange
242
226
  *
@@ -249,7 +233,7 @@ export declare class NordUser {
249
233
  * Cancel an order
250
234
  *
251
235
  * @param orderId - Order ID to cancel
252
- * @param accountId - Account ID that placed the order
236
+ * @param providedAccountId - Account ID that placed the order
253
237
  * @returns Action ID if successful
254
238
  * @throws {NordError} If the operation fails
255
239
  */
@@ -39,9 +39,7 @@ const web3_js_1 = require("@solana/web3.js");
39
39
  const ed = __importStar(require("@noble/ed25519"));
40
40
  const sha512_1 = require("@noble/hashes/sha512");
41
41
  ed.etc.sha512Sync = sha512_1.sha512;
42
- const client_1 = require("../../bridge/client");
43
- const utils_1 = require("../../bridge/utils");
44
- const utils_2 = require("../../utils");
42
+ const utils_1 = require("../../utils");
45
43
  const actions_1 = require("../api/actions");
46
44
  const NordError_1 = require("../utils/NordError");
47
45
  /**
@@ -54,7 +52,7 @@ class NordUser {
54
52
  * @param params - Parameters for creating a NordUser
55
53
  * @throws {NordError} If required parameters are missing
56
54
  */
57
- constructor(params) {
55
+ constructor({ address, nord, publicKey, sessionPubKey, sessionSignFn, transactionSignFn, walletSignFn, connection, sessionId, }) {
58
56
  /** User balances by token symbol */
59
57
  this.balances = {};
60
58
  /** User orders by market symbol */
@@ -69,43 +67,35 @@ class NordUser {
69
67
  this.lastNonce = 0;
70
68
  /** SPL token information */
71
69
  this.splTokenInfos = [];
72
- if (!params.nord) {
73
- throw new NordError_1.NordError("Nord instance is required");
74
- }
75
- if (!params.address) {
76
- throw new NordError_1.NordError("Address is required");
77
- }
78
- if (!params.walletSignFn) {
70
+ if (!walletSignFn) {
79
71
  throw new NordError_1.NordError("Wallet sign function is required");
80
72
  }
81
- if (!params.sessionSignFn) {
73
+ if (!sessionSignFn) {
82
74
  throw new NordError_1.NordError("Session sign function is required");
83
75
  }
84
- if (!params.sessionPubKey) {
76
+ if (!sessionPubKey) {
85
77
  throw new NordError_1.NordError("Session public key is required");
86
78
  }
87
- this.nord = params.nord;
88
- this.address = params.address;
89
- this.walletSignFn = params.walletSignFn;
90
- this.sessionSignFn = params.sessionSignFn;
91
- this.transactionSignFn = params.transactionSignFn;
92
- this.sessionPubKey = params.sessionPubKey;
93
- this.publicKey = params.publicKey;
79
+ try {
80
+ this.address = new web3_js_1.PublicKey(address);
81
+ }
82
+ catch (error) {
83
+ throw new NordError_1.NordError("Invalid Solana address", { cause: error });
84
+ }
85
+ this.nord = nord;
86
+ this.walletSignFn = walletSignFn;
87
+ this.sessionSignFn = sessionSignFn;
88
+ this.transactionSignFn = transactionSignFn;
89
+ this.sessionPubKey = sessionPubKey;
90
+ this.publicKey = publicKey;
94
91
  this.connection =
95
- params.connection ||
96
- new web3_js_1.Connection(params.nord.solanaUrl, {
92
+ connection ||
93
+ new web3_js_1.Connection(nord.solanaUrl, {
97
94
  commitment: "confirmed",
98
95
  });
99
96
  // Set sessionId if provided
100
- if (params.sessionId !== undefined) {
101
- this.sessionId = params.sessionId;
102
- }
103
- // Initialize bridge client if needed
104
- try {
105
- this.initBridgeClient();
106
- }
107
- catch (error) {
108
- console.warn("Failed to initialize bridge client:", error);
97
+ if (sessionId !== undefined) {
98
+ this.sessionId = sessionId;
109
99
  }
110
100
  // Convert tokens from info endpoint to SPLTokenInfo
111
101
  if (this.nord.tokens && this.nord.tokens.length > 0) {
@@ -125,7 +115,7 @@ class NordUser {
125
115
  clone() {
126
116
  const cloned = new NordUser({
127
117
  nord: this.nord,
128
- address: this.address,
118
+ address: this.address.toBase58(),
129
119
  walletSignFn: this.walletSignFn,
130
120
  sessionSignFn: this.sessionSignFn,
131
121
  transactionSignFn: this.transactionSignFn,
@@ -146,44 +136,6 @@ class NordUser {
146
136
  cloned.splTokenInfos = [...this.splTokenInfos];
147
137
  return cloned;
148
138
  }
149
- /**
150
- * Initialize the Solana bridge client
151
- *
152
- * @private
153
- * @throws {NordError} If required parameters are missing
154
- */
155
- initBridgeClient() {
156
- if (!this.getSolanaPublicKey() ||
157
- !this.connection ||
158
- !this.nord.getSolanaProgramId()) {
159
- throw new NordError_1.NordError("Solana public key, connection, and program ID are required to initialize bridge client");
160
- }
161
- // Create an Anchor wallet that uses walletSignFn for signing
162
- const wallet = {
163
- publicKey: this.getSolanaPublicKey(),
164
- signTransaction: async (tx) => {
165
- return await this.transactionSignFn(tx);
166
- },
167
- signAllTransactions: async (txs) => {
168
- return Promise.all(txs.map(async (tx) => {
169
- return await this.transactionSignFn(tx);
170
- }));
171
- },
172
- // Create a keypair-like object with just the public key
173
- payer: {
174
- publicKey: this.getSolanaPublicKey(),
175
- secretKey: new Uint8Array(64), // Dummy secret key to satisfy the type
176
- },
177
- };
178
- // Initialize the bridge client
179
- this.bridgeClient = new client_1.SolanaBridgeClient({
180
- rpcUrl: this.connection.rpcEndpoint,
181
- programId: this.nord.getSolanaProgramId(),
182
- commitment: "confirmed",
183
- tokenInfos: this.splTokenInfos,
184
- bridgeVk: this.nord.bridgeVk,
185
- }, wallet);
186
- }
187
139
  /**
188
140
  * Create a NordUser from a private key
189
141
  *
@@ -216,9 +168,9 @@ class NordUser {
216
168
  // Create a transaction signing function
217
169
  const transactionSignFn = async (transaction) => {
218
170
  // This is a basic implementation - actual implementation would depend on the transaction type
219
- if (transaction.serializeMessage) {
171
+ if (transaction.sign) {
220
172
  // Solana transaction
221
- transaction.sign([keypair]);
173
+ transaction.sign(keypair);
222
174
  return transaction;
223
175
  }
224
176
  // For other transaction types, would need specific implementation
@@ -253,7 +205,19 @@ class NordUser {
253
205
  throw new NordError_1.NordError("Solana public key is required to get associated token account");
254
206
  }
255
207
  try {
256
- return await (0, spl_token_1.getAssociatedTokenAddress)(mint, this.getSolanaPublicKey(), false, spl_token_1.TOKEN_2022_PROGRAM_ID, spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID);
208
+ // Get the token program ID from the mint account
209
+ const mintAccount = await this.connection.getAccountInfo(mint);
210
+ if (!mintAccount) {
211
+ throw new NordError_1.NordError("Mint account not found");
212
+ }
213
+ const tokenProgramId = mintAccount.owner;
214
+ // Validate that the mint is owned by a supported SPL token program
215
+ if (!tokenProgramId.equals(spl_token_1.TOKEN_PROGRAM_ID) &&
216
+ !tokenProgramId.equals(spl_token_1.TOKEN_2022_PROGRAM_ID)) {
217
+ throw new NordError_1.NordError("Mint Account is not owned by a supported SPL token program");
218
+ }
219
+ const associatedTokenAddress = await (0, spl_token_1.getAssociatedTokenAddress)(mint, this.getSolanaPublicKey(), false, tokenProgramId, spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID);
220
+ return associatedTokenAddress;
257
221
  }
258
222
  catch (error) {
259
223
  throw new NordError_1.NordError("Failed to get associated token account", {
@@ -270,9 +234,6 @@ class NordUser {
270
234
  * @throws {NordError} If required parameters are missing or operation fails
271
235
  */
272
236
  async depositSpl(amount, tokenId) {
273
- if (!this.bridgeClient || !this.getSolanaPublicKey() || !this.connection) {
274
- throw new NordError_1.NordError("Bridge client, Solana public key, and connection are required for deposit");
275
- }
276
237
  try {
277
238
  // Find the token info
278
239
  const tokenInfo = this.splTokenInfos.find((t) => t.tokenId === tokenId);
@@ -283,60 +244,26 @@ class NordUser {
283
244
  // Get the user's token account
284
245
  const fromAccount = await this.getAssociatedTokenAccount(mint);
285
246
  // Convert amount to BN with proper decimals
286
- const amountBN = (0, utils_2.toBN)(amount, tokenInfo.precision);
287
- // Deposit tokens
288
- return await this.bridgeClient.depositSpl({
247
+ const amountBN = (0, utils_1.toBN)(amount, tokenInfo.precision);
248
+ // Create deposit parameters
249
+ const depositParams = {
289
250
  amount: amountBN,
290
251
  mint,
291
252
  fromAccount,
292
- });
293
- }
294
- catch (error) {
295
- throw new NordError_1.NordError(`Failed to deposit ${amount} of token ID ${tokenId}`, { cause: error });
296
- }
297
- }
298
- /**
299
- * Withdraw SPL tokens from the bridge
300
- *
301
- * @param claim - Claim data
302
- * @returns Transaction signature
303
- * @throws {NordError} If required parameters are missing or operation fails
304
- */
305
- async withdrawSpl(claim) {
306
- if (!this.bridgeClient || !this.getSolanaPublicKey() || !this.connection) {
307
- throw new NordError_1.NordError("Bridge client, Solana public key, and connection are required for withdrawal");
308
- }
309
- try {
310
- // Find the token info
311
- const tokenInfo = this.splTokenInfos.find((t) => t.tokenId === claim.tokenId);
312
- if (!tokenInfo) {
313
- throw new NordError_1.NordError(`Token with ID ${claim.tokenId} not found`);
314
- }
315
- const mint = new web3_js_1.PublicKey(tokenInfo.mint);
316
- // Get the user's token account
317
- const toAccount = await this.getAssociatedTokenAccount(mint);
318
- // Get the bridge's token account
319
- const [authority] = await this.bridgeClient.findAuthorityPda();
320
- const fromAccount = await (0, spl_token_1.getAssociatedTokenAddress)(mint, authority, true, spl_token_1.TOKEN_2022_PROGRAM_ID);
321
- // Create a transaction signer that uses walletSignFn
322
- const transactionSigner = {
323
- publicKey: this.getSolanaPublicKey(),
324
- secretKey: new Uint8Array(64), // Dummy secret key, not actually used
325
- sign: async (tx) => {
326
- const message = tx.serializeMessage();
327
- await this.walletSignFn(message);
328
- return tx;
329
- },
330
253
  };
331
- // Withdraw tokens
332
- return await this.bridgeClient.withdraw({
333
- claim,
334
- fromAccount,
335
- toAccount,
336
- }, transactionSigner);
254
+ // Build the deposit transaction using proton client
255
+ const depositTx = await this.nord.protonClient.buildDepositTx(depositParams, this.getSolanaPublicKey(), this.connection);
256
+ const { blockhash } = await this.connection.getLatestBlockhash();
257
+ depositTx.recentBlockhash = blockhash;
258
+ depositTx.feePayer = this.getSolanaPublicKey();
259
+ const signedTx = await this.transactionSignFn(depositTx);
260
+ // TODO: should use `VersionedTransaction` and remove any for `transactionSignFn`,
261
+ // this is incredibly annoying to debug and i could've saved 30 mins.
262
+ const signature = await this.connection.sendRawTransaction(signedTx.serialize());
263
+ return signature;
337
264
  }
338
265
  catch (error) {
339
- throw new NordError_1.NordError("Failed to withdraw SPL tokens", { cause: error });
266
+ throw new NordError_1.NordError(`Failed to deposit ${amount} of token ID ${tokenId}`, { cause: error });
340
267
  }
341
268
  }
342
269
  /**
@@ -374,7 +301,7 @@ class NordUser {
374
301
  async fetchInfo() {
375
302
  if (this.accountIds !== undefined) {
376
303
  const accountsData = await Promise.all(this.accountIds.map(async (accountId) => {
377
- const response = await (0, utils_2.checkedFetch)(`${this.nord.webServerUrl}/account/${accountId}`);
304
+ const response = await (0, utils_1.checkedFetch)(`${this.nord.webServerUrl}/account/${accountId}`);
378
305
  const accountData = (await response.json());
379
306
  // Ensure we have the correct accountId
380
307
  return {
@@ -417,7 +344,7 @@ class NordUser {
417
344
  async refreshSession() {
418
345
  console.log(this.publicKey);
419
346
  this.sessionId = await (0, actions_1.createSession)(this.nord.webServerUrl, this.walletSignFn, await this.nord.getTimestamp(), this.getNonce(), {
420
- userPubkey: (0, utils_2.optExpect)(this.publicKey.toBytes(), "No user's public key"),
347
+ userPubkey: (0, utils_1.optExpect)(this.publicKey.toBytes(), "No user's public key"),
421
348
  sessionPubkey: this.sessionPubKey,
422
349
  });
423
350
  }
@@ -459,12 +386,13 @@ class NordUser {
459
386
  async withdraw(tokenId, amount) {
460
387
  try {
461
388
  this.checkSessionValidity();
462
- await (0, actions_1.withdraw)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
463
- sizeDecimals: (0, utils_2.findToken)(this.nord.tokens, tokenId).decimals,
464
- sessionId: (0, utils_2.optExpect)(this.sessionId, "No session"),
389
+ const { actionId } = await (0, actions_1.withdraw)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
390
+ sizeDecimals: (0, utils_1.findToken)(this.nord.tokens, tokenId).decimals,
391
+ sessionId: (0, utils_1.optExpect)(this.sessionId, "No session"),
465
392
  tokenId: tokenId,
466
393
  amount,
467
394
  });
395
+ return { actionId };
468
396
  }
469
397
  catch (error) {
470
398
  throw new NordError_1.NordError(`Failed to withdraw ${amount} of token ID ${tokenId}`, { cause: error });
@@ -480,12 +408,12 @@ class NordUser {
480
408
  async placeOrder(params) {
481
409
  try {
482
410
  this.checkSessionValidity();
483
- const market = (0, utils_2.findMarket)(this.nord.markets, params.marketId);
411
+ const market = (0, utils_1.findMarket)(this.nord.markets, params.marketId);
484
412
  if (!market) {
485
413
  throw new NordError_1.NordError(`Market with ID ${params.marketId} not found`);
486
414
  }
487
415
  return (0, actions_1.placeOrder)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
488
- sessionId: (0, utils_2.optExpect)(this.sessionId, "No session"),
416
+ sessionId: (0, utils_1.optExpect)(this.sessionId, "No session"),
489
417
  senderId: params.accountId,
490
418
  sizeDecimals: market.sizeDecimals,
491
419
  priceDecimals: market.priceDecimals,
@@ -506,7 +434,7 @@ class NordUser {
506
434
  * Cancel an order
507
435
  *
508
436
  * @param orderId - Order ID to cancel
509
- * @param accountId - Account ID that placed the order
437
+ * @param providedAccountId - Account ID that placed the order
510
438
  * @returns Action ID if successful
511
439
  * @throws {NordError} If the operation fails
512
440
  */
@@ -515,7 +443,7 @@ class NordUser {
515
443
  try {
516
444
  this.checkSessionValidity();
517
445
  return (0, actions_1.cancelOrder)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
518
- sessionId: (0, utils_2.optExpect)(this.sessionId, "No session"),
446
+ sessionId: (0, utils_1.optExpect)(this.sessionId, "No session"),
519
447
  senderId: accountId,
520
448
  orderId,
521
449
  });
@@ -535,11 +463,11 @@ class NordUser {
535
463
  async transferToAccount(params) {
536
464
  try {
537
465
  this.checkSessionValidity();
538
- const token = (0, utils_2.findToken)(this.nord.tokens, params.tokenId);
466
+ const token = (0, utils_1.findToken)(this.nord.tokens, params.tokenId);
539
467
  await (0, actions_1.transfer)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
540
- sessionId: (0, utils_2.optExpect)(this.sessionId, "No session"),
541
- fromAccountId: (0, utils_2.optExpect)(params.fromAccountId, "No source account"),
542
- toAccountId: (0, utils_2.optExpect)(params.toAccountId, "No target account"),
468
+ sessionId: (0, utils_1.optExpect)(this.sessionId, "No session"),
469
+ fromAccountId: (0, utils_1.optExpect)(params.fromAccountId, "No source account"),
470
+ toAccountId: (0, utils_1.optExpect)(params.toAccountId, "No target account"),
543
471
  tokenId: params.tokenId,
544
472
  tokenDecimals: token.decimals,
545
473
  amount: params.amount,
@@ -660,12 +588,7 @@ class NordUser {
660
588
  * @returns The Solana public key
661
589
  */
662
590
  getSolanaPublicKey() {
663
- try {
664
- return new web3_js_1.PublicKey(this.address);
665
- }
666
- catch (error) {
667
- throw new NordError_1.NordError("Invalid Solana address", { cause: error });
668
- }
591
+ return this.address;
669
592
  }
670
593
  }
671
594
  exports.NordUser = NordUser;
@@ -35,10 +35,13 @@ class NordError extends Error {
35
35
  toString() {
36
36
  let result = `${this.name}: ${this.message}`;
37
37
  if (this.statusCode) {
38
- result += ` (Status: ${this.statusCode})`;
38
+ result += ` \nstatus: ${this.statusCode}`;
39
39
  }
40
40
  if (this.details && Object.keys(this.details).length > 0) {
41
- result += `\nDetails: ${JSON.stringify(this.details, null, 2)}`;
41
+ result += ` \ndetails: ${JSON.stringify(this.details, null, 2)}`;
42
+ }
43
+ if (this.cause) {
44
+ result += ` \ncause: ${this.cause.toString()}`;
42
45
  }
43
46
  return result;
44
47
  }