@n1xyz/nord-ts 0.0.15 → 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 (195) hide show
  1. package/dist/bridge/client.js +45 -6
  2. package/dist/gen/common.js +1 -1
  3. package/dist/gen/nord.d.ts +61 -29
  4. package/dist/gen/nord.js +723 -130
  5. package/dist/idl/bridge.d.ts +569 -2
  6. package/dist/idl/bridge.js +5 -927
  7. package/dist/idl/bridge.json +75 -62
  8. package/dist/idl/index.d.ts +71 -49
  9. package/dist/index.d.ts +1 -3
  10. package/dist/index.js +6 -13
  11. package/dist/nord/api/actions.d.ts +7 -2
  12. package/dist/nord/api/actions.js +7 -1
  13. package/dist/nord/api/core.d.ts +1 -1
  14. package/dist/nord/api/core.js +3 -3
  15. package/dist/nord/api/market.js +1 -3
  16. package/dist/nord/api/queries.js +2 -2
  17. package/dist/nord/client/Nord.d.ts +9 -14
  18. package/dist/nord/client/Nord.js +27 -38
  19. package/dist/nord/client/NordUser.d.ts +7 -23
  20. package/dist/nord/client/NordUser.js +79 -151
  21. package/dist/nord/utils/NordError.js +5 -2
  22. package/dist/types.d.ts +34 -50
  23. package/dist/utils.d.ts +2 -0
  24. package/dist/utils.js +14 -0
  25. package/dist/websocket/NordWebSocketClient.js +0 -2
  26. package/package.json +20 -18
  27. package/protoc-generate.sh +0 -0
  28. package/src/idl/bridge.json +75 -62
  29. package/src/index.ts +9 -12
  30. package/src/nord/api/actions.ts +14 -3
  31. package/src/nord/api/core.ts +4 -2
  32. package/src/nord/api/market.ts +1 -4
  33. package/src/nord/api/queries.ts +2 -2
  34. package/src/nord/client/Nord.ts +45 -50
  35. package/src/nord/client/NordUser.ts +107 -178
  36. package/src/nord/utils/NordError.ts +6 -2
  37. package/src/types.ts +34 -49
  38. package/src/utils.ts +18 -0
  39. package/src/websocket/NordWebSocketClient.ts +0 -2
  40. package/tsconfig.json +1 -1
  41. package/docs/.nojekyll +0 -1
  42. package/docs/assets/hierarchy.js +0 -1
  43. package/docs/assets/highlight.css +0 -92
  44. package/docs/assets/icons.js +0 -18
  45. package/docs/assets/icons.svg +0 -1
  46. package/docs/assets/main.js +0 -60
  47. package/docs/assets/navigation.js +0 -1
  48. package/docs/assets/search.js +0 -1
  49. package/docs/assets/style.css +0 -1611
  50. package/docs/classes/Nord.html +0 -189
  51. package/docs/classes/NordError.html +0 -24
  52. package/docs/classes/NordUser.html +0 -115
  53. package/docs/classes/NordWebSocketClient.html +0 -330
  54. package/docs/classes/SolanaBridgeClient.html +0 -86
  55. package/docs/classes/Subscriber.html +0 -10
  56. package/docs/enums/FillMode.html +0 -5
  57. package/docs/enums/KeyType.html +0 -4
  58. package/docs/enums/MetricPeriod.html +0 -9
  59. package/docs/enums/PdaSeedType.html +0 -9
  60. package/docs/enums/PeakTpsPeriodUnit.html +0 -15
  61. package/docs/enums/Side.html +0 -3
  62. package/docs/enums/WebSocketMessageType.html +0 -7
  63. package/docs/functions/actionQueryRollman.html +0 -6
  64. package/docs/functions/actionsQueryRollman.html +0 -6
  65. package/docs/functions/aggregateMetrics-1.html +0 -7
  66. package/docs/functions/assert.html +0 -1
  67. package/docs/functions/bigIntToProtoU128.html +0 -4
  68. package/docs/functions/bridgeToBN.html +0 -5
  69. package/docs/functions/bufferToHex.html +0 -4
  70. package/docs/functions/cancelOrder.html +0 -1
  71. package/docs/functions/checkPubKeyLength.html +0 -1
  72. package/docs/functions/checkedFetch.html +0 -6
  73. package/docs/functions/createSession.html +0 -1
  74. package/docs/functions/decodeLengthDelimited.html +0 -11
  75. package/docs/functions/encodeLengthDelimited.html +0 -6
  76. package/docs/functions/fillModeToProtoFillMode.html +0 -5
  77. package/docs/functions/findMarket.html +0 -1
  78. package/docs/functions/findPda.html +0 -6
  79. package/docs/functions/findToken.html +0 -1
  80. package/docs/functions/fromBN.html +0 -5
  81. package/docs/functions/getAccount.html +0 -6
  82. package/docs/functions/getActionNonce.html +0 -5
  83. package/docs/functions/getCurrentTps.html +0 -6
  84. package/docs/functions/getInfo.html +0 -5
  85. package/docs/functions/getLastActionId.html +0 -5
  86. package/docs/functions/getMedianLatency.html +0 -6
  87. package/docs/functions/getOrderbook.html +0 -6
  88. package/docs/functions/getPeakTps.html +0 -6
  89. package/docs/functions/getTimestamp.html +0 -5
  90. package/docs/functions/getTotalTransactions.html +0 -5
  91. package/docs/functions/getTrades.html +0 -6
  92. package/docs/functions/getUserAccountIds.html +0 -6
  93. package/docs/functions/hexToBuffer.html +0 -4
  94. package/docs/functions/initWebSocketClient.html +0 -12
  95. package/docs/functions/keypairFromPrivateKey.html +0 -4
  96. package/docs/functions/makeSigningFunction.html +0 -4
  97. package/docs/functions/makeWalletSignFn.html +0 -6
  98. package/docs/functions/marketsStats.html +0 -5
  99. package/docs/functions/optExpect.html +0 -5
  100. package/docs/functions/optMap.html +0 -5
  101. package/docs/functions/optUnwrap.html +0 -2
  102. package/docs/functions/panic.html +0 -1
  103. package/docs/functions/placeOrder.html +0 -1
  104. package/docs/functions/queryAction.html +0 -6
  105. package/docs/functions/queryPrometheus.html +0 -6
  106. package/docs/functions/queryRecentActions.html +0 -7
  107. package/docs/functions/revokeSession.html +0 -1
  108. package/docs/functions/shortenPublicKey.html +0 -5
  109. package/docs/functions/signAction.html +0 -6
  110. package/docs/functions/toBN.html +0 -5
  111. package/docs/functions/toScaledU128.html +0 -8
  112. package/docs/functions/toScaledU64.html +0 -8
  113. package/docs/functions/transfer.html +0 -1
  114. package/docs/functions/withdraw.html +0 -1
  115. package/docs/hierarchy.html +0 -1
  116. package/docs/index.html +0 -40
  117. package/docs/interfaces/Account.html +0 -8
  118. package/docs/interfaces/ActionInfo.html +0 -8
  119. package/docs/interfaces/ActionNonceResponse.html +0 -3
  120. package/docs/interfaces/ActionQuery.html +0 -4
  121. package/docs/interfaces/ActionResponse.html +0 -8
  122. package/docs/interfaces/ActionsExtendedInfo.html +0 -10
  123. package/docs/interfaces/ActionsQuery.html +0 -5
  124. package/docs/interfaces/ActionsResponse.html +0 -6
  125. package/docs/interfaces/AggregateMetrics.html +0 -12
  126. package/docs/interfaces/BlockFacts.html +0 -10
  127. package/docs/interfaces/BlockQuery.html +0 -6
  128. package/docs/interfaces/BlockResponse.html +0 -6
  129. package/docs/interfaces/BlockSummary.html +0 -8
  130. package/docs/interfaces/BlockSummaryResponse.html +0 -6
  131. package/docs/interfaces/DeltaEvent.html +0 -6
  132. package/docs/interfaces/DepositSplParams.html +0 -10
  133. package/docs/interfaces/Info.html +0 -3
  134. package/docs/interfaces/Market.html +0 -8
  135. package/docs/interfaces/MarketStats.html +0 -7
  136. package/docs/interfaces/MarketsStatsResponse.html +0 -2
  137. package/docs/interfaces/NordConfig.html +0 -24
  138. package/docs/interfaces/NordWebSocketClientEvents.html +0 -4
  139. package/docs/interfaces/NordWebSocketEvents.html +0 -8
  140. package/docs/interfaces/Order.html +0 -6
  141. package/docs/interfaces/OrderInfo.html +0 -6
  142. package/docs/interfaces/OrderbookEntry.html +0 -4
  143. package/docs/interfaces/OrderbookQuery.html +0 -6
  144. package/docs/interfaces/OrderbookResponse.html +0 -6
  145. package/docs/interfaces/OrderbookSubscription.html +0 -159
  146. package/docs/interfaces/PerpMarketStats.html +0 -5
  147. package/docs/interfaces/RollmanActionExtendedInfo.html +0 -4
  148. package/docs/interfaces/RollmanActionInfo.html +0 -4
  149. package/docs/interfaces/RollmanActionResponse.html +0 -4
  150. package/docs/interfaces/RollmanActionsResponse.html +0 -2
  151. package/docs/interfaces/RollmanBlockResponse.html +0 -3
  152. package/docs/interfaces/SPLTokenInfo.html +0 -10
  153. package/docs/interfaces/SolanaBridgeConfig.html +0 -10
  154. package/docs/interfaces/StateFacts.html +0 -10
  155. package/docs/interfaces/SubscriberConfig.html +0 -3
  156. package/docs/interfaces/TimestampResponse.html +0 -3
  157. package/docs/interfaces/Token.html +0 -5
  158. package/docs/interfaces/TokenInfo.html +0 -6
  159. package/docs/interfaces/Trade.html +0 -5
  160. package/docs/interfaces/TradeSubscription.html +0 -159
  161. package/docs/interfaces/Trades.html +0 -5
  162. package/docs/interfaces/TradesQuery.html +0 -6
  163. package/docs/interfaces/TradesResponse.html +0 -7
  164. package/docs/interfaces/TransferParams.html +0 -8
  165. package/docs/interfaces/UserAccountIdsQuery.html +0 -3
  166. package/docs/interfaces/UserAccountIdsResponse.html +0 -3
  167. package/docs/interfaces/WebSocketAccountUpdate.html +0 -6
  168. package/docs/interfaces/WebSocketDeltaUpdate.html +0 -9
  169. package/docs/interfaces/WebSocketSubscription.html +0 -4
  170. package/docs/interfaces/WebSocketTradeUpdate.html +0 -6
  171. package/docs/interfaces/WithdrawalClaim.html +0 -14
  172. package/docs/interfaces/WithdrawalParams.html +0 -8
  173. package/docs/modules.html +0 -1
  174. package/docs/types/BigIntValue.html +0 -2
  175. package/docs/types/SubscriptionPattern.html +0 -4
  176. package/docs/types/SubscriptionType.html +0 -2
  177. package/docs/types/WebSocketMessage.html +0 -1
  178. package/docs/variables/DEBUG_KEYS.html +0 -1
  179. package/docs/variables/DEFAULT_FUNDING_AMOUNTS.html +0 -1
  180. package/docs/variables/DEV_TOKEN_INFOS.html +0 -1
  181. package/docs/variables/DEV_URL.html +0 -1
  182. package/docs/variables/MAX_BUFFER_LEN.html +0 -1
  183. package/docs/variables/SESSION_TTL.html +0 -1
  184. package/docs/variables/WEBSERVER_DEV_URL.html +0 -1
  185. package/docs/variables/ZERO_DECIMAL.html +0 -1
  186. package/docs/variables/_private.html +0 -2
  187. package/idl-generate.sh +0 -4
  188. package/src/bridge/client.ts +0 -498
  189. package/src/bridge/const.ts +0 -53
  190. package/src/bridge/index.ts +0 -6
  191. package/src/bridge/types.ts +0 -129
  192. package/src/bridge/utils.ts +0 -140
  193. package/src/gen/common.ts +0 -297
  194. package/src/gen/nord.ts +0 -6723
  195. 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
  */
@@ -36,10 +36,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.NordUser = void 0;
37
37
  const spl_token_1 = require("@solana/spl-token");
38
38
  const web3_js_1 = require("@solana/web3.js");
39
- const nacl = __importStar(require("tweetnacl"));
40
- const client_1 = require("../../bridge/client");
41
- const utils_1 = require("../../bridge/utils");
42
- const utils_2 = require("../../utils");
39
+ const ed = __importStar(require("@noble/ed25519"));
40
+ const sha512_1 = require("@noble/hashes/sha512");
41
+ ed.etc.sha512Sync = sha512_1.sha512;
42
+ const utils_1 = require("../../utils");
43
43
  const actions_1 = require("../api/actions");
44
44
  const NordError_1 = require("../utils/NordError");
45
45
  /**
@@ -52,7 +52,7 @@ class NordUser {
52
52
  * @param params - Parameters for creating a NordUser
53
53
  * @throws {NordError} If required parameters are missing
54
54
  */
55
- constructor(params) {
55
+ constructor({ address, nord, publicKey, sessionPubKey, sessionSignFn, transactionSignFn, walletSignFn, connection, sessionId, }) {
56
56
  /** User balances by token symbol */
57
57
  this.balances = {};
58
58
  /** User orders by market symbol */
@@ -67,43 +67,35 @@ class NordUser {
67
67
  this.lastNonce = 0;
68
68
  /** SPL token information */
69
69
  this.splTokenInfos = [];
70
- if (!params.nord) {
71
- throw new NordError_1.NordError("Nord instance is required");
72
- }
73
- if (!params.address) {
74
- throw new NordError_1.NordError("Address is required");
75
- }
76
- if (!params.walletSignFn) {
70
+ if (!walletSignFn) {
77
71
  throw new NordError_1.NordError("Wallet sign function is required");
78
72
  }
79
- if (!params.sessionSignFn) {
73
+ if (!sessionSignFn) {
80
74
  throw new NordError_1.NordError("Session sign function is required");
81
75
  }
82
- if (!params.sessionPubKey) {
76
+ if (!sessionPubKey) {
83
77
  throw new NordError_1.NordError("Session public key is required");
84
78
  }
85
- this.nord = params.nord;
86
- this.address = params.address;
87
- this.walletSignFn = params.walletSignFn;
88
- this.sessionSignFn = params.sessionSignFn;
89
- this.transactionSignFn = params.transactionSignFn;
90
- this.sessionPubKey = params.sessionPubKey;
91
- 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;
92
91
  this.connection =
93
- params.connection ||
94
- new web3_js_1.Connection(params.nord.solanaUrl, {
92
+ connection ||
93
+ new web3_js_1.Connection(nord.solanaUrl, {
95
94
  commitment: "confirmed",
96
95
  });
97
96
  // Set sessionId if provided
98
- if (params.sessionId !== undefined) {
99
- this.sessionId = params.sessionId;
100
- }
101
- // Initialize bridge client if needed
102
- try {
103
- this.initBridgeClient();
104
- }
105
- catch (error) {
106
- console.warn("Failed to initialize bridge client:", error);
97
+ if (sessionId !== undefined) {
98
+ this.sessionId = sessionId;
107
99
  }
108
100
  // Convert tokens from info endpoint to SPLTokenInfo
109
101
  if (this.nord.tokens && this.nord.tokens.length > 0) {
@@ -123,7 +115,7 @@ class NordUser {
123
115
  clone() {
124
116
  const cloned = new NordUser({
125
117
  nord: this.nord,
126
- address: this.address,
118
+ address: this.address.toBase58(),
127
119
  walletSignFn: this.walletSignFn,
128
120
  sessionSignFn: this.sessionSignFn,
129
121
  transactionSignFn: this.transactionSignFn,
@@ -144,44 +136,6 @@ class NordUser {
144
136
  cloned.splTokenInfos = [...this.splTokenInfos];
145
137
  return cloned;
146
138
  }
147
- /**
148
- * Initialize the Solana bridge client
149
- *
150
- * @private
151
- * @throws {NordError} If required parameters are missing
152
- */
153
- initBridgeClient() {
154
- if (!this.getSolanaPublicKey() ||
155
- !this.connection ||
156
- !this.nord.getSolanaProgramId()) {
157
- throw new NordError_1.NordError("Solana public key, connection, and program ID are required to initialize bridge client");
158
- }
159
- // Create an Anchor wallet that uses walletSignFn for signing
160
- const wallet = {
161
- publicKey: this.getSolanaPublicKey(),
162
- signTransaction: async (tx) => {
163
- return await this.transactionSignFn(tx);
164
- },
165
- signAllTransactions: async (txs) => {
166
- return Promise.all(txs.map(async (tx) => {
167
- return await this.transactionSignFn(tx);
168
- }));
169
- },
170
- // Create a keypair-like object with just the public key
171
- payer: {
172
- publicKey: this.getSolanaPublicKey(),
173
- secretKey: new Uint8Array(64), // Dummy secret key to satisfy the type
174
- },
175
- };
176
- // Initialize the bridge client
177
- this.bridgeClient = new client_1.SolanaBridgeClient({
178
- rpcUrl: this.connection.rpcEndpoint,
179
- programId: this.nord.getSolanaProgramId(),
180
- commitment: "confirmed",
181
- tokenInfos: this.splTokenInfos,
182
- bridgeVk: this.nord.bridgeVk,
183
- }, wallet);
184
- }
185
139
  /**
186
140
  * Create a NordUser from a private key
187
141
  *
@@ -197,23 +151,26 @@ class NordUser {
197
151
  const publicKey = keypair.publicKey;
198
152
  // Create a signing function that uses the keypair but doesn't expose it
199
153
  const walletSignFn = async (message) => {
200
- const messageBuffer = typeof message === "string"
201
- ? Buffer.from(message)
202
- : Buffer.from(message);
203
- // Use the keypair to sign the message
204
- const signature = nacl.sign.detached(messageBuffer, keypair.secretKey);
154
+ function toHex(buffer) {
155
+ return Array.from(buffer)
156
+ .map((byte) => byte.toString(16).padStart(2, "0"))
157
+ .join("");
158
+ }
159
+ const messageBuffer = new TextEncoder().encode(toHex(message));
160
+ // Use ed25519 to sign the message
161
+ const signature = ed.sign(messageBuffer, keypair.secretKey.slice(0, 32));
205
162
  return signature;
206
163
  };
207
164
  const sessionSignFn = async (message) => {
208
- // Use the keypair to sign the message
209
- return nacl.sign.detached(message, keypair.secretKey);
165
+ // Use ed25519 to sign the message
166
+ return ed.sign(message, keypair.secretKey.slice(0, 32));
210
167
  };
211
168
  // Create a transaction signing function
212
169
  const transactionSignFn = async (transaction) => {
213
170
  // This is a basic implementation - actual implementation would depend on the transaction type
214
- if (transaction.serializeMessage) {
171
+ if (transaction.sign) {
215
172
  // Solana transaction
216
- transaction.sign([keypair]);
173
+ transaction.sign(keypair);
217
174
  return transaction;
218
175
  }
219
176
  // For other transaction types, would need specific implementation
@@ -248,7 +205,19 @@ class NordUser {
248
205
  throw new NordError_1.NordError("Solana public key is required to get associated token account");
249
206
  }
250
207
  try {
251
- 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;
252
221
  }
253
222
  catch (error) {
254
223
  throw new NordError_1.NordError("Failed to get associated token account", {
@@ -265,9 +234,6 @@ class NordUser {
265
234
  * @throws {NordError} If required parameters are missing or operation fails
266
235
  */
267
236
  async depositSpl(amount, tokenId) {
268
- if (!this.bridgeClient || !this.getSolanaPublicKey() || !this.connection) {
269
- throw new NordError_1.NordError("Bridge client, Solana public key, and connection are required for deposit");
270
- }
271
237
  try {
272
238
  // Find the token info
273
239
  const tokenInfo = this.splTokenInfos.find((t) => t.tokenId === tokenId);
@@ -278,60 +244,26 @@ class NordUser {
278
244
  // Get the user's token account
279
245
  const fromAccount = await this.getAssociatedTokenAccount(mint);
280
246
  // Convert amount to BN with proper decimals
281
- const amountBN = (0, utils_2.toBN)(amount, tokenInfo.precision);
282
- // Deposit tokens
283
- return await this.bridgeClient.depositSpl({
247
+ const amountBN = (0, utils_1.toBN)(amount, tokenInfo.precision);
248
+ // Create deposit parameters
249
+ const depositParams = {
284
250
  amount: amountBN,
285
251
  mint,
286
252
  fromAccount,
287
- });
288
- }
289
- catch (error) {
290
- throw new NordError_1.NordError(`Failed to deposit ${amount} of token ID ${tokenId}`, { cause: error });
291
- }
292
- }
293
- /**
294
- * Withdraw SPL tokens from the bridge
295
- *
296
- * @param claim - Claim data
297
- * @returns Transaction signature
298
- * @throws {NordError} If required parameters are missing or operation fails
299
- */
300
- async withdrawSpl(claim) {
301
- if (!this.bridgeClient || !this.getSolanaPublicKey() || !this.connection) {
302
- throw new NordError_1.NordError("Bridge client, Solana public key, and connection are required for withdrawal");
303
- }
304
- try {
305
- // Find the token info
306
- const tokenInfo = this.splTokenInfos.find((t) => t.tokenId === claim.tokenId);
307
- if (!tokenInfo) {
308
- throw new NordError_1.NordError(`Token with ID ${claim.tokenId} not found`);
309
- }
310
- const mint = new web3_js_1.PublicKey(tokenInfo.mint);
311
- // Get the user's token account
312
- const toAccount = await this.getAssociatedTokenAccount(mint);
313
- // Get the bridge's token account
314
- const [authority] = await this.bridgeClient.findAuthorityPda();
315
- const fromAccount = await (0, spl_token_1.getAssociatedTokenAddress)(mint, authority, true, spl_token_1.TOKEN_2022_PROGRAM_ID);
316
- // Create a transaction signer that uses walletSignFn
317
- const transactionSigner = {
318
- publicKey: this.getSolanaPublicKey(),
319
- secretKey: new Uint8Array(64), // Dummy secret key, not actually used
320
- sign: async (tx) => {
321
- const message = tx.serializeMessage();
322
- await this.walletSignFn(message);
323
- return tx;
324
- },
325
253
  };
326
- // Withdraw tokens
327
- return await this.bridgeClient.withdraw({
328
- claim,
329
- fromAccount,
330
- toAccount,
331
- }, 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;
332
264
  }
333
265
  catch (error) {
334
- 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 });
335
267
  }
336
268
  }
337
269
  /**
@@ -369,7 +301,7 @@ class NordUser {
369
301
  async fetchInfo() {
370
302
  if (this.accountIds !== undefined) {
371
303
  const accountsData = await Promise.all(this.accountIds.map(async (accountId) => {
372
- 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}`);
373
305
  const accountData = (await response.json());
374
306
  // Ensure we have the correct accountId
375
307
  return {
@@ -412,7 +344,7 @@ class NordUser {
412
344
  async refreshSession() {
413
345
  console.log(this.publicKey);
414
346
  this.sessionId = await (0, actions_1.createSession)(this.nord.webServerUrl, this.walletSignFn, await this.nord.getTimestamp(), this.getNonce(), {
415
- 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"),
416
348
  sessionPubkey: this.sessionPubKey,
417
349
  });
418
350
  }
@@ -454,12 +386,13 @@ class NordUser {
454
386
  async withdraw(tokenId, amount) {
455
387
  try {
456
388
  this.checkSessionValidity();
457
- await (0, actions_1.withdraw)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
458
- sizeDecimals: (0, utils_2.findToken)(this.nord.tokens, tokenId).decimals,
459
- 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"),
460
392
  tokenId: tokenId,
461
393
  amount,
462
394
  });
395
+ return { actionId };
463
396
  }
464
397
  catch (error) {
465
398
  throw new NordError_1.NordError(`Failed to withdraw ${amount} of token ID ${tokenId}`, { cause: error });
@@ -475,12 +408,12 @@ class NordUser {
475
408
  async placeOrder(params) {
476
409
  try {
477
410
  this.checkSessionValidity();
478
- const market = (0, utils_2.findMarket)(this.nord.markets, params.marketId);
411
+ const market = (0, utils_1.findMarket)(this.nord.markets, params.marketId);
479
412
  if (!market) {
480
413
  throw new NordError_1.NordError(`Market with ID ${params.marketId} not found`);
481
414
  }
482
415
  return (0, actions_1.placeOrder)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
483
- sessionId: (0, utils_2.optExpect)(this.sessionId, "No session"),
416
+ sessionId: (0, utils_1.optExpect)(this.sessionId, "No session"),
484
417
  senderId: params.accountId,
485
418
  sizeDecimals: market.sizeDecimals,
486
419
  priceDecimals: market.priceDecimals,
@@ -501,7 +434,7 @@ class NordUser {
501
434
  * Cancel an order
502
435
  *
503
436
  * @param orderId - Order ID to cancel
504
- * @param accountId - Account ID that placed the order
437
+ * @param providedAccountId - Account ID that placed the order
505
438
  * @returns Action ID if successful
506
439
  * @throws {NordError} If the operation fails
507
440
  */
@@ -510,7 +443,7 @@ class NordUser {
510
443
  try {
511
444
  this.checkSessionValidity();
512
445
  return (0, actions_1.cancelOrder)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
513
- sessionId: (0, utils_2.optExpect)(this.sessionId, "No session"),
446
+ sessionId: (0, utils_1.optExpect)(this.sessionId, "No session"),
514
447
  senderId: accountId,
515
448
  orderId,
516
449
  });
@@ -530,11 +463,11 @@ class NordUser {
530
463
  async transferToAccount(params) {
531
464
  try {
532
465
  this.checkSessionValidity();
533
- const token = (0, utils_2.findToken)(this.nord.tokens, params.tokenId);
466
+ const token = (0, utils_1.findToken)(this.nord.tokens, params.tokenId);
534
467
  await (0, actions_1.transfer)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
535
- sessionId: (0, utils_2.optExpect)(this.sessionId, "No session"),
536
- fromAccountId: (0, utils_2.optExpect)(params.fromAccountId, "No source account"),
537
- 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"),
538
471
  tokenId: params.tokenId,
539
472
  tokenDecimals: token.decimals,
540
473
  amount: params.amount,
@@ -655,12 +588,7 @@ class NordUser {
655
588
  * @returns The Solana public key
656
589
  */
657
590
  getSolanaPublicKey() {
658
- try {
659
- return new web3_js_1.PublicKey(this.address);
660
- }
661
- catch (error) {
662
- throw new NordError_1.NordError("Invalid Solana address", { cause: error });
663
- }
591
+ return this.address;
664
592
  }
665
593
  }
666
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
  }