@n1xyz/nord-ts 0.0.16 → 0.0.18-8121ed05.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bridge/client.js +45 -6
- package/dist/gen/nord.d.ts +48 -4
- package/dist/gen/nord.js +634 -34
- package/dist/idl/bridge.d.ts +569 -0
- package/dist/idl/bridge.js +8 -0
- package/dist/idl/bridge.json +75 -62
- package/dist/idl/index.d.ts +71 -49
- package/dist/index.d.ts +1 -3
- package/dist/index.js +6 -13
- package/dist/nord/api/actions.d.ts +7 -2
- package/dist/nord/api/actions.js +7 -1
- package/dist/nord/api/core.d.ts +1 -1
- package/dist/nord/api/core.js +3 -3
- package/dist/nord/api/market.js +1 -3
- package/dist/nord/api/queries.js +2 -2
- package/dist/nord/client/Nord.d.ts +9 -14
- package/dist/nord/client/Nord.js +27 -38
- package/dist/nord/client/NordUser.d.ts +7 -23
- package/dist/nord/client/NordUser.js +67 -143
- package/dist/nord/utils/NordError.js +5 -2
- package/dist/types.d.ts +34 -50
- package/dist/utils.d.ts +2 -9
- package/dist/utils.js +13 -18
- package/package.json +17 -18
- package/protoc-generate.sh +0 -0
- package/src/idl/bridge.json +75 -62
- package/src/index.ts +9 -12
- package/src/nord/api/actions.ts +14 -3
- package/src/nord/api/core.ts +4 -2
- package/src/nord/api/market.ts +1 -4
- package/src/nord/api/queries.ts +2 -2
- package/src/nord/client/Nord.ts +45 -50
- package/src/nord/client/NordUser.ts +90 -171
- package/src/nord/utils/NordError.ts +6 -2
- package/src/types.ts +34 -49
- package/src/utils.ts +16 -19
- package/codebase.md +0 -18886
- package/docs/.nojekyll +0 -1
- package/docs/assets/hierarchy.js +0 -1
- package/docs/assets/highlight.css +0 -92
- package/docs/assets/icons.js +0 -18
- package/docs/assets/icons.svg +0 -1
- package/docs/assets/main.js +0 -60
- package/docs/assets/navigation.js +0 -1
- package/docs/assets/search.js +0 -1
- package/docs/assets/style.css +0 -1611
- package/docs/classes/Nord.html +0 -189
- package/docs/classes/NordError.html +0 -24
- package/docs/classes/NordUser.html +0 -115
- package/docs/classes/NordWebSocketClient.html +0 -330
- package/docs/classes/SolanaBridgeClient.html +0 -91
- package/docs/classes/Subscriber.html +0 -10
- package/docs/enums/FillMode.html +0 -5
- package/docs/enums/KeyType.html +0 -4
- package/docs/enums/MetricPeriod.html +0 -9
- package/docs/enums/PdaSeedType.html +0 -11
- package/docs/enums/PeakTpsPeriodUnit.html +0 -15
- package/docs/enums/Side.html +0 -3
- package/docs/enums/WebSocketMessageType.html +0 -7
- package/docs/functions/actionQueryRollman.html +0 -6
- package/docs/functions/actionsQueryRollman.html +0 -6
- package/docs/functions/aggregateMetrics-1.html +0 -7
- package/docs/functions/assert.html +0 -1
- package/docs/functions/bigIntToProtoU128.html +0 -4
- package/docs/functions/bridgeToBN.html +0 -5
- package/docs/functions/bufferToHex.html +0 -4
- package/docs/functions/cancelOrder.html +0 -1
- package/docs/functions/checkPubKeyLength.html +0 -1
- package/docs/functions/checkedFetch.html +0 -6
- package/docs/functions/createSession.html +0 -1
- package/docs/functions/decodeLengthDelimited.html +0 -11
- package/docs/functions/encodeLengthDelimited.html +0 -6
- package/docs/functions/fillModeToProtoFillMode.html +0 -5
- package/docs/functions/findMarket.html +0 -1
- package/docs/functions/findPda.html +0 -6
- package/docs/functions/findToken.html +0 -1
- package/docs/functions/fromBN.html +0 -5
- package/docs/functions/getAccount.html +0 -6
- package/docs/functions/getActionNonce.html +0 -5
- package/docs/functions/getCurrentTps.html +0 -6
- package/docs/functions/getInfo.html +0 -5
- package/docs/functions/getLastActionId.html +0 -5
- package/docs/functions/getMedianLatency.html +0 -6
- package/docs/functions/getOrderbook.html +0 -6
- package/docs/functions/getPeakTps.html +0 -6
- package/docs/functions/getTimestamp.html +0 -5
- package/docs/functions/getTotalTransactions.html +0 -5
- package/docs/functions/getTrades.html +0 -6
- package/docs/functions/getUserAccountIds.html +0 -6
- package/docs/functions/hexToBuffer.html +0 -4
- package/docs/functions/initWebSocketClient.html +0 -12
- package/docs/functions/keypairFromPrivateKey.html +0 -4
- package/docs/functions/makeSigningFunction.html +0 -4
- package/docs/functions/makeWalletSignFn.html +0 -6
- package/docs/functions/marketsStats.html +0 -5
- package/docs/functions/optExpect.html +0 -5
- package/docs/functions/optMap.html +0 -5
- package/docs/functions/optUnwrap.html +0 -2
- package/docs/functions/panic.html +0 -1
- package/docs/functions/placeOrder.html +0 -1
- package/docs/functions/queryAction.html +0 -6
- package/docs/functions/queryPrometheus.html +0 -6
- package/docs/functions/queryRecentActions.html +0 -7
- package/docs/functions/revokeSession.html +0 -1
- package/docs/functions/shortenPublicKey.html +0 -5
- package/docs/functions/signAction.html +0 -6
- package/docs/functions/toBN.html +0 -5
- package/docs/functions/toScaledU128.html +0 -8
- package/docs/functions/toScaledU64.html +0 -8
- package/docs/functions/transfer.html +0 -1
- package/docs/functions/withdraw.html +0 -1
- package/docs/hierarchy.html +0 -1
- package/docs/index.html +0 -40
- package/docs/interfaces/Account.html +0 -8
- package/docs/interfaces/ActionInfo.html +0 -8
- package/docs/interfaces/ActionNonceResponse.html +0 -3
- package/docs/interfaces/ActionQuery.html +0 -4
- package/docs/interfaces/ActionResponse.html +0 -8
- package/docs/interfaces/ActionsExtendedInfo.html +0 -10
- package/docs/interfaces/ActionsQuery.html +0 -5
- package/docs/interfaces/ActionsResponse.html +0 -6
- package/docs/interfaces/AggregateMetrics.html +0 -12
- package/docs/interfaces/BlockFacts.html +0 -10
- package/docs/interfaces/BlockQuery.html +0 -6
- package/docs/interfaces/BlockResponse.html +0 -6
- package/docs/interfaces/BlockSummary.html +0 -8
- package/docs/interfaces/BlockSummaryResponse.html +0 -6
- package/docs/interfaces/DeltaEvent.html +0 -6
- package/docs/interfaces/DepositSplParams.html +0 -8
- package/docs/interfaces/Info.html +0 -3
- package/docs/interfaces/Market.html +0 -8
- package/docs/interfaces/MarketStats.html +0 -7
- package/docs/interfaces/MarketsStatsResponse.html +0 -2
- package/docs/interfaces/NordConfig.html +0 -24
- package/docs/interfaces/NordWebSocketClientEvents.html +0 -4
- package/docs/interfaces/NordWebSocketEvents.html +0 -8
- package/docs/interfaces/Order.html +0 -6
- package/docs/interfaces/OrderInfo.html +0 -6
- package/docs/interfaces/OrderbookEntry.html +0 -4
- package/docs/interfaces/OrderbookQuery.html +0 -6
- package/docs/interfaces/OrderbookResponse.html +0 -6
- package/docs/interfaces/OrderbookSubscription.html +0 -159
- package/docs/interfaces/PerpMarketStats.html +0 -5
- package/docs/interfaces/RollmanActionExtendedInfo.html +0 -4
- package/docs/interfaces/RollmanActionInfo.html +0 -4
- package/docs/interfaces/RollmanActionResponse.html +0 -4
- package/docs/interfaces/RollmanActionsResponse.html +0 -2
- package/docs/interfaces/RollmanBlockResponse.html +0 -3
- package/docs/interfaces/SPLTokenInfo.html +0 -10
- package/docs/interfaces/SolanaBridgeConfig.html +0 -12
- package/docs/interfaces/StateFacts.html +0 -10
- package/docs/interfaces/SubscriberConfig.html +0 -3
- package/docs/interfaces/TimestampResponse.html +0 -3
- package/docs/interfaces/Token.html +0 -5
- package/docs/interfaces/TokenInfo.html +0 -6
- package/docs/interfaces/Trade.html +0 -5
- package/docs/interfaces/TradeSubscription.html +0 -159
- package/docs/interfaces/Trades.html +0 -5
- package/docs/interfaces/TradesQuery.html +0 -6
- package/docs/interfaces/TradesResponse.html +0 -7
- package/docs/interfaces/TransferParams.html +0 -8
- package/docs/interfaces/UserAccountIdsQuery.html +0 -3
- package/docs/interfaces/UserAccountIdsResponse.html +0 -3
- package/docs/interfaces/WebSocketAccountUpdate.html +0 -6
- package/docs/interfaces/WebSocketDeltaUpdate.html +0 -9
- package/docs/interfaces/WebSocketSubscription.html +0 -4
- package/docs/interfaces/WebSocketTradeUpdate.html +0 -6
- package/docs/interfaces/WithdrawalClaim.html +0 -14
- package/docs/interfaces/WithdrawalParams.html +0 -8
- package/docs/modules.html +0 -1
- package/docs/types/BigIntValue.html +0 -2
- package/docs/types/SubscriptionPattern.html +0 -4
- package/docs/types/SubscriptionType.html +0 -2
- package/docs/types/WebSocketMessage.html +0 -1
- package/docs/variables/DEBUG_KEYS.html +0 -1
- package/docs/variables/DEFAULT_FUNDING_AMOUNTS.html +0 -1
- package/docs/variables/DEV_TOKEN_INFOS.html +0 -1
- package/docs/variables/DEV_URL.html +0 -1
- package/docs/variables/MAX_BUFFER_LEN.html +0 -1
- package/docs/variables/SESSION_TTL.html +0 -1
- package/docs/variables/WEBSERVER_DEV_URL.html +0 -1
- package/docs/variables/ZERO_DECIMAL.html +0 -1
- package/docs/variables/_private.html +0 -2
- package/idl-generate.sh +0 -4
- package/src/bridge/client.ts +0 -498
- package/src/bridge/const.ts +0 -53
- package/src/bridge/index.ts +0 -6
- package/src/bridge/types.ts +0 -129
- package/src/bridge/utils.ts +0 -140
- package/src/gen/common.ts +0 -297
- package/src/gen/nord.ts +0 -6716
- package/src/idl/index.ts +0 -2
package/dist/nord/client/Nord.js
CHANGED
|
@@ -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(
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
|
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.
|
|
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(
|
|
178
|
-
|
|
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
|
|
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 {
|
|
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:
|
|
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(
|
|
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<
|
|
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
|
|
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,8 @@ 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
|
|
43
|
-
const utils_1 = require("../../
|
|
44
|
-
const utils_2 = require("../../utils");
|
|
42
|
+
const proton_1 = require("@n1xyz/proton");
|
|
43
|
+
const utils_1 = require("../../utils");
|
|
45
44
|
const actions_1 = require("../api/actions");
|
|
46
45
|
const NordError_1 = require("../utils/NordError");
|
|
47
46
|
/**
|
|
@@ -54,7 +53,7 @@ class NordUser {
|
|
|
54
53
|
* @param params - Parameters for creating a NordUser
|
|
55
54
|
* @throws {NordError} If required parameters are missing
|
|
56
55
|
*/
|
|
57
|
-
constructor(
|
|
56
|
+
constructor({ address, nord, publicKey, sessionPubKey, sessionSignFn, transactionSignFn, walletSignFn, connection, sessionId, }) {
|
|
58
57
|
/** User balances by token symbol */
|
|
59
58
|
this.balances = {};
|
|
60
59
|
/** User orders by market symbol */
|
|
@@ -69,43 +68,35 @@ class NordUser {
|
|
|
69
68
|
this.lastNonce = 0;
|
|
70
69
|
/** SPL token information */
|
|
71
70
|
this.splTokenInfos = [];
|
|
72
|
-
if (!
|
|
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) {
|
|
71
|
+
if (!walletSignFn) {
|
|
79
72
|
throw new NordError_1.NordError("Wallet sign function is required");
|
|
80
73
|
}
|
|
81
|
-
if (!
|
|
74
|
+
if (!sessionSignFn) {
|
|
82
75
|
throw new NordError_1.NordError("Session sign function is required");
|
|
83
76
|
}
|
|
84
|
-
if (!
|
|
77
|
+
if (!sessionPubKey) {
|
|
85
78
|
throw new NordError_1.NordError("Session public key is required");
|
|
86
79
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
this.
|
|
80
|
+
try {
|
|
81
|
+
this.address = new web3_js_1.PublicKey(address);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
throw new NordError_1.NordError("Invalid Solana address", { cause: error });
|
|
85
|
+
}
|
|
86
|
+
this.nord = nord;
|
|
87
|
+
this.walletSignFn = walletSignFn;
|
|
88
|
+
this.sessionSignFn = sessionSignFn;
|
|
89
|
+
this.transactionSignFn = transactionSignFn;
|
|
90
|
+
this.sessionPubKey = sessionPubKey;
|
|
91
|
+
this.publicKey = publicKey;
|
|
94
92
|
this.connection =
|
|
95
|
-
|
|
96
|
-
new web3_js_1.Connection(
|
|
93
|
+
connection ||
|
|
94
|
+
new web3_js_1.Connection(nord.solanaUrl, {
|
|
97
95
|
commitment: "confirmed",
|
|
98
96
|
});
|
|
99
97
|
// Set sessionId if provided
|
|
100
|
-
if (
|
|
101
|
-
this.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);
|
|
98
|
+
if (sessionId !== undefined) {
|
|
99
|
+
this.sessionId = sessionId;
|
|
109
100
|
}
|
|
110
101
|
// Convert tokens from info endpoint to SPLTokenInfo
|
|
111
102
|
if (this.nord.tokens && this.nord.tokens.length > 0) {
|
|
@@ -125,7 +116,7 @@ class NordUser {
|
|
|
125
116
|
clone() {
|
|
126
117
|
const cloned = new NordUser({
|
|
127
118
|
nord: this.nord,
|
|
128
|
-
address: this.address,
|
|
119
|
+
address: this.address.toBase58(),
|
|
129
120
|
walletSignFn: this.walletSignFn,
|
|
130
121
|
sessionSignFn: this.sessionSignFn,
|
|
131
122
|
transactionSignFn: this.transactionSignFn,
|
|
@@ -146,44 +137,6 @@ class NordUser {
|
|
|
146
137
|
cloned.splTokenInfos = [...this.splTokenInfos];
|
|
147
138
|
return cloned;
|
|
148
139
|
}
|
|
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
140
|
/**
|
|
188
141
|
* Create a NordUser from a private key
|
|
189
142
|
*
|
|
@@ -216,9 +169,9 @@ class NordUser {
|
|
|
216
169
|
// Create a transaction signing function
|
|
217
170
|
const transactionSignFn = async (transaction) => {
|
|
218
171
|
// This is a basic implementation - actual implementation would depend on the transaction type
|
|
219
|
-
if (transaction.
|
|
172
|
+
if (transaction.sign) {
|
|
220
173
|
// Solana transaction
|
|
221
|
-
transaction.sign(
|
|
174
|
+
transaction.sign(keypair);
|
|
222
175
|
return transaction;
|
|
223
176
|
}
|
|
224
177
|
// For other transaction types, would need specific implementation
|
|
@@ -253,7 +206,19 @@ class NordUser {
|
|
|
253
206
|
throw new NordError_1.NordError("Solana public key is required to get associated token account");
|
|
254
207
|
}
|
|
255
208
|
try {
|
|
256
|
-
|
|
209
|
+
// Get the token program ID from the mint account
|
|
210
|
+
const mintAccount = await this.connection.getAccountInfo(mint);
|
|
211
|
+
if (!mintAccount) {
|
|
212
|
+
throw new NordError_1.NordError("Mint account not found");
|
|
213
|
+
}
|
|
214
|
+
const tokenProgramId = mintAccount.owner;
|
|
215
|
+
// Validate that the mint is owned by a supported SPL token program
|
|
216
|
+
if (!tokenProgramId.equals(spl_token_1.TOKEN_PROGRAM_ID) &&
|
|
217
|
+
!tokenProgramId.equals(spl_token_1.TOKEN_2022_PROGRAM_ID)) {
|
|
218
|
+
throw new NordError_1.NordError("Mint Account is not owned by a supported SPL token program");
|
|
219
|
+
}
|
|
220
|
+
const associatedTokenAddress = await (0, spl_token_1.getAssociatedTokenAddress)(mint, this.getSolanaPublicKey(), false, tokenProgramId, spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID);
|
|
221
|
+
return associatedTokenAddress;
|
|
257
222
|
}
|
|
258
223
|
catch (error) {
|
|
259
224
|
throw new NordError_1.NordError("Failed to get associated token account", {
|
|
@@ -270,9 +235,6 @@ class NordUser {
|
|
|
270
235
|
* @throws {NordError} If required parameters are missing or operation fails
|
|
271
236
|
*/
|
|
272
237
|
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
238
|
try {
|
|
277
239
|
// Find the token info
|
|
278
240
|
const tokenInfo = this.splTokenInfos.find((t) => t.tokenId === tokenId);
|
|
@@ -283,60 +245,26 @@ class NordUser {
|
|
|
283
245
|
// Get the user's token account
|
|
284
246
|
const fromAccount = await this.getAssociatedTokenAccount(mint);
|
|
285
247
|
// Convert amount to BN with proper decimals
|
|
286
|
-
const amountBN = (0,
|
|
287
|
-
//
|
|
288
|
-
|
|
248
|
+
const amountBN = (0, proton_1.floatToBn)(amount, tokenInfo.precision);
|
|
249
|
+
// Create deposit parameters
|
|
250
|
+
const depositParams = {
|
|
289
251
|
amount: amountBN,
|
|
290
252
|
mint,
|
|
291
253
|
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
254
|
};
|
|
331
|
-
//
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
255
|
+
// Build the deposit transaction using proton client
|
|
256
|
+
const depositTx = await this.nord.protonClient.buildDepositTx(depositParams, this.getSolanaPublicKey(), this.connection);
|
|
257
|
+
const { blockhash } = await this.connection.getLatestBlockhash();
|
|
258
|
+
depositTx.recentBlockhash = blockhash;
|
|
259
|
+
depositTx.feePayer = this.getSolanaPublicKey();
|
|
260
|
+
const signedTx = await this.transactionSignFn(depositTx);
|
|
261
|
+
// TODO: should use `VersionedTransaction` and remove any for `transactionSignFn`,
|
|
262
|
+
// this is incredibly annoying to debug and i could've saved 30 mins.
|
|
263
|
+
const signature = await this.connection.sendRawTransaction(signedTx.serialize());
|
|
264
|
+
return signature;
|
|
337
265
|
}
|
|
338
266
|
catch (error) {
|
|
339
|
-
throw new NordError_1.NordError(
|
|
267
|
+
throw new NordError_1.NordError(`Failed to deposit ${amount} of token ID ${tokenId}`, { cause: error });
|
|
340
268
|
}
|
|
341
269
|
}
|
|
342
270
|
/**
|
|
@@ -374,7 +302,7 @@ class NordUser {
|
|
|
374
302
|
async fetchInfo() {
|
|
375
303
|
if (this.accountIds !== undefined) {
|
|
376
304
|
const accountsData = await Promise.all(this.accountIds.map(async (accountId) => {
|
|
377
|
-
const response = await (0,
|
|
305
|
+
const response = await (0, utils_1.checkedFetch)(`${this.nord.webServerUrl}/account/${accountId}`);
|
|
378
306
|
const accountData = (await response.json());
|
|
379
307
|
// Ensure we have the correct accountId
|
|
380
308
|
return {
|
|
@@ -417,7 +345,7 @@ class NordUser {
|
|
|
417
345
|
async refreshSession() {
|
|
418
346
|
console.log(this.publicKey);
|
|
419
347
|
this.sessionId = await (0, actions_1.createSession)(this.nord.webServerUrl, this.walletSignFn, await this.nord.getTimestamp(), this.getNonce(), {
|
|
420
|
-
userPubkey: (0,
|
|
348
|
+
userPubkey: (0, utils_1.optExpect)(this.publicKey.toBytes(), "No user's public key"),
|
|
421
349
|
sessionPubkey: this.sessionPubKey,
|
|
422
350
|
});
|
|
423
351
|
}
|
|
@@ -459,12 +387,13 @@ class NordUser {
|
|
|
459
387
|
async withdraw(tokenId, amount) {
|
|
460
388
|
try {
|
|
461
389
|
this.checkSessionValidity();
|
|
462
|
-
await (0, actions_1.withdraw)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
|
|
463
|
-
sizeDecimals: (0,
|
|
464
|
-
sessionId: (0,
|
|
390
|
+
const { actionId } = await (0, actions_1.withdraw)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
|
|
391
|
+
sizeDecimals: (0, utils_1.findToken)(this.nord.tokens, tokenId).decimals,
|
|
392
|
+
sessionId: (0, utils_1.optExpect)(this.sessionId, "No session"),
|
|
465
393
|
tokenId: tokenId,
|
|
466
394
|
amount,
|
|
467
395
|
});
|
|
396
|
+
return { actionId };
|
|
468
397
|
}
|
|
469
398
|
catch (error) {
|
|
470
399
|
throw new NordError_1.NordError(`Failed to withdraw ${amount} of token ID ${tokenId}`, { cause: error });
|
|
@@ -480,12 +409,12 @@ class NordUser {
|
|
|
480
409
|
async placeOrder(params) {
|
|
481
410
|
try {
|
|
482
411
|
this.checkSessionValidity();
|
|
483
|
-
const market = (0,
|
|
412
|
+
const market = (0, utils_1.findMarket)(this.nord.markets, params.marketId);
|
|
484
413
|
if (!market) {
|
|
485
414
|
throw new NordError_1.NordError(`Market with ID ${params.marketId} not found`);
|
|
486
415
|
}
|
|
487
416
|
return (0, actions_1.placeOrder)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
|
|
488
|
-
sessionId: (0,
|
|
417
|
+
sessionId: (0, utils_1.optExpect)(this.sessionId, "No session"),
|
|
489
418
|
senderId: params.accountId,
|
|
490
419
|
sizeDecimals: market.sizeDecimals,
|
|
491
420
|
priceDecimals: market.priceDecimals,
|
|
@@ -506,7 +435,7 @@ class NordUser {
|
|
|
506
435
|
* Cancel an order
|
|
507
436
|
*
|
|
508
437
|
* @param orderId - Order ID to cancel
|
|
509
|
-
* @param
|
|
438
|
+
* @param providedAccountId - Account ID that placed the order
|
|
510
439
|
* @returns Action ID if successful
|
|
511
440
|
* @throws {NordError} If the operation fails
|
|
512
441
|
*/
|
|
@@ -515,7 +444,7 @@ class NordUser {
|
|
|
515
444
|
try {
|
|
516
445
|
this.checkSessionValidity();
|
|
517
446
|
return (0, actions_1.cancelOrder)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
|
|
518
|
-
sessionId: (0,
|
|
447
|
+
sessionId: (0, utils_1.optExpect)(this.sessionId, "No session"),
|
|
519
448
|
senderId: accountId,
|
|
520
449
|
orderId,
|
|
521
450
|
});
|
|
@@ -535,11 +464,11 @@ class NordUser {
|
|
|
535
464
|
async transferToAccount(params) {
|
|
536
465
|
try {
|
|
537
466
|
this.checkSessionValidity();
|
|
538
|
-
const token = (0,
|
|
467
|
+
const token = (0, utils_1.findToken)(this.nord.tokens, params.tokenId);
|
|
539
468
|
await (0, actions_1.transfer)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
|
|
540
|
-
sessionId: (0,
|
|
541
|
-
fromAccountId: (0,
|
|
542
|
-
toAccountId: (0,
|
|
469
|
+
sessionId: (0, utils_1.optExpect)(this.sessionId, "No session"),
|
|
470
|
+
fromAccountId: (0, utils_1.optExpect)(params.fromAccountId, "No source account"),
|
|
471
|
+
toAccountId: (0, utils_1.optExpect)(params.toAccountId, "No target account"),
|
|
543
472
|
tokenId: params.tokenId,
|
|
544
473
|
tokenDecimals: token.decimals,
|
|
545
474
|
amount: params.amount,
|
|
@@ -660,12 +589,7 @@ class NordUser {
|
|
|
660
589
|
* @returns The Solana public key
|
|
661
590
|
*/
|
|
662
591
|
getSolanaPublicKey() {
|
|
663
|
-
|
|
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
|
-
}
|
|
592
|
+
return this.address;
|
|
669
593
|
}
|
|
670
594
|
}
|
|
671
595
|
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 += `
|
|
38
|
+
result += ` \nstatus: ${this.statusCode}`;
|
|
39
39
|
}
|
|
40
40
|
if (this.details && Object.keys(this.details).length > 0) {
|
|
41
|
-
result +=
|
|
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
|
}
|