@n1xyz/nord-ts 0.1.0 → 0.1.2

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.
@@ -42,6 +42,7 @@ ed.etc.sha512Sync = sha512_1.sha512;
42
42
  const proton_1 = require("@n1xyz/proton");
43
43
  const utils_1 = require("../../utils");
44
44
  const actions_1 = require("../api/actions");
45
+ const triggers_1 = require("../api/triggers");
45
46
  const NordError_1 = require("../utils/NordError");
46
47
  /**
47
48
  * User class for interacting with the Nord protocol
@@ -460,6 +461,102 @@ class NordUser {
460
461
  });
461
462
  }
462
463
  }
464
+ /**
465
+ * Add a trigger for the current session
466
+ *
467
+ * @param params - Trigger parameters including market, side, and prices
468
+ * @returns Object containing the actionId of the submitted trigger
469
+ * @throws {NordError} If the operation fails
470
+ */
471
+ async addTrigger(params) {
472
+ try {
473
+ this.checkSessionValidity();
474
+ const market = (0, utils_1.findMarket)(this.nord.markets, params.marketId);
475
+ if (!market) {
476
+ throw new NordError_1.NordError(`Market with ID ${params.marketId} not found`);
477
+ }
478
+ const result = await (0, actions_1.addTrigger)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
479
+ sessionId: (0, utils_1.optExpect)(this.sessionId, "No session"),
480
+ marketId: params.marketId,
481
+ side: params.side,
482
+ kind: params.kind,
483
+ priceDecimals: market.priceDecimals,
484
+ triggerPrice: params.triggerPrice,
485
+ limitPrice: params.limitPrice,
486
+ accountId: params.accountId,
487
+ });
488
+ return result;
489
+ }
490
+ catch (error) {
491
+ throw new NordError_1.NordError("Failed to add trigger", { cause: error });
492
+ }
493
+ }
494
+ /**
495
+ * Remove a trigger for the current session
496
+ *
497
+ * @param params - Trigger parameters identifying the trigger to remove
498
+ * @returns Object containing the actionId of the removal action
499
+ * @throws {NordError} If the operation fails
500
+ */
501
+ async removeTrigger(params) {
502
+ try {
503
+ this.checkSessionValidity();
504
+ const market = (0, utils_1.findMarket)(this.nord.markets, params.marketId);
505
+ if (!market) {
506
+ throw new NordError_1.NordError(`Market with ID ${params.marketId} not found`);
507
+ }
508
+ const result = await (0, actions_1.removeTrigger)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
509
+ sessionId: (0, utils_1.optExpect)(this.sessionId, "No session"),
510
+ marketId: params.marketId,
511
+ side: params.side,
512
+ kind: params.kind,
513
+ accountId: params.accountId,
514
+ });
515
+ return result;
516
+ }
517
+ catch (error) {
518
+ throw new NordError_1.NordError("Failed to remove trigger", { cause: error });
519
+ }
520
+ }
521
+ /**
522
+ * Fetch active triggers for an account.
523
+ *
524
+ * @param params Optional parameters containing an explicit account id.
525
+ * @throws {NordError} If no account can be resolved or the request fails.
526
+ */
527
+ async getAccountTriggers(params) {
528
+ const accountId = params?.accountId ?? this.accountIds?.[0];
529
+ if (accountId == null) {
530
+ throw new NordError_1.NordError("Account ID is undefined. Make sure to call updateAccountId() before requesting triggers.");
531
+ }
532
+ try {
533
+ return await (0, triggers_1.getAccountTriggers)(this.nord.webServerUrl, accountId);
534
+ }
535
+ catch (error) {
536
+ throw new NordError_1.NordError("Failed to fetch account triggers", { cause: error });
537
+ }
538
+ }
539
+ /**
540
+ * Fetch trigger history for an account.
541
+ *
542
+ * @param params Optional parameters with account id and history query filters.
543
+ * @throws {NordError} If no account can be resolved or the request fails.
544
+ */
545
+ async getAccountTriggerHistory(params) {
546
+ const { accountId: providedAccountId, ...query } = params;
547
+ const accountId = providedAccountId ?? this.accountIds?.[0];
548
+ if (accountId == null) {
549
+ throw new NordError_1.NordError("Account ID is undefined. Make sure to call updateAccountId() before requesting trigger history.");
550
+ }
551
+ try {
552
+ return await (0, triggers_1.getAccountTriggerHistory)(this.nord.webServerUrl, accountId, query);
553
+ }
554
+ catch (error) {
555
+ throw new NordError_1.NordError("Failed to fetch account trigger history", {
556
+ cause: error,
557
+ });
558
+ }
559
+ }
463
560
  /**
464
561
  * Transfer tokens to another account
465
562
  *
package/dist/types.d.ts CHANGED
@@ -72,6 +72,9 @@ export type SideFromApi = components["schemas"]["Side"];
72
72
  export type FillModeFromApi = components["schemas"]["FillMode"];
73
73
  export type PlacementOrigin = components["schemas"]["PlacementOrigin"];
74
74
  export type FinalizationReason = components["schemas"]["FinalizationReason"];
75
+ export type AccountPnlQuery = components["schemas"]["AccountPnlQuery"];
76
+ export type AccountPnl = components["schemas"]["AccountPnl"];
77
+ export type AccountPnlPage = components["schemas"]["PageResult_for_uint64_and_AccountPnl"];
75
78
  /**
76
79
  * Configuration options for the Nord client
77
80
  */
@@ -94,8 +97,8 @@ export declare enum KeyType {
94
97
  Bls12_381 = 2
95
98
  }
96
99
  export declare enum Side {
97
- Ask = 0,
98
- Bid = 1
100
+ Ask = "ask",
101
+ Bid = "bid"
99
102
  }
100
103
  export declare enum FillMode {
101
104
  Limit = 0,
@@ -103,6 +106,16 @@ export declare enum FillMode {
103
106
  ImmediateOrCancel = 2,
104
107
  FillOrKill = 3
105
108
  }
109
+ export declare enum TriggerKind {
110
+ StopLoss = 0,
111
+ TakeProfit = 1
112
+ }
113
+ export declare enum TriggerStatus {
114
+ Active = 0,
115
+ Success = 1,
116
+ Cancel = 2,
117
+ Remove = 4
118
+ }
106
119
  export interface SubscriberConfig {
107
120
  streamURL: string;
108
121
  maxBufferLen?: number;
@@ -255,7 +268,7 @@ export declare class QuoteSize {
255
268
  size: Decimal;
256
269
  constructor(quotePrice: Decimal.Value, quoteSize: Decimal.Value);
257
270
  value(): Decimal;
258
- toScaledU64(marketPriceDecimals: number, marketSizeDecimals: number): {
271
+ toWire(marketPriceDecimals: number, marketSizeDecimals: number): {
259
272
  price: bigint;
260
273
  size: bigint;
261
274
  };
package/dist/types.js CHANGED
@@ -36,7 +36,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
36
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.QuoteSize = exports.WebSocketMessageType = exports.FillMode = exports.Side = exports.KeyType = exports.PeakTpsPeriodUnit = void 0;
39
+ exports.QuoteSize = exports.WebSocketMessageType = exports.TriggerStatus = exports.TriggerKind = exports.FillMode = exports.Side = exports.KeyType = exports.PeakTpsPeriodUnit = void 0;
40
40
  exports.fillModeToProtoFillMode = fillModeToProtoFillMode;
41
41
  const proto = __importStar(require("./gen/nord_pb"));
42
42
  const decimal_js_1 = __importDefault(require("decimal.js"));
@@ -66,8 +66,8 @@ var KeyType;
66
66
  })(KeyType || (exports.KeyType = KeyType = {}));
67
67
  var Side;
68
68
  (function (Side) {
69
- Side[Side["Ask"] = 0] = "Ask";
70
- Side[Side["Bid"] = 1] = "Bid";
69
+ Side["Ask"] = "ask";
70
+ Side["Bid"] = "bid";
71
71
  })(Side || (exports.Side = Side = {}));
72
72
  var FillMode;
73
73
  (function (FillMode) {
@@ -76,6 +76,18 @@ var FillMode;
76
76
  FillMode[FillMode["ImmediateOrCancel"] = 2] = "ImmediateOrCancel";
77
77
  FillMode[FillMode["FillOrKill"] = 3] = "FillOrKill";
78
78
  })(FillMode || (exports.FillMode = FillMode = {}));
79
+ var TriggerKind;
80
+ (function (TriggerKind) {
81
+ TriggerKind[TriggerKind["StopLoss"] = 0] = "StopLoss";
82
+ TriggerKind[TriggerKind["TakeProfit"] = 1] = "TakeProfit";
83
+ })(TriggerKind || (exports.TriggerKind = TriggerKind = {}));
84
+ var TriggerStatus;
85
+ (function (TriggerStatus) {
86
+ TriggerStatus[TriggerStatus["Active"] = 0] = "Active";
87
+ TriggerStatus[TriggerStatus["Success"] = 1] = "Success";
88
+ TriggerStatus[TriggerStatus["Cancel"] = 2] = "Cancel";
89
+ TriggerStatus[TriggerStatus["Remove"] = 4] = "Remove";
90
+ })(TriggerStatus || (exports.TriggerStatus = TriggerStatus = {}));
79
91
  /**
80
92
  * Converts a `FillMode` enum to its corresponding protobuf representation.
81
93
  *
@@ -105,7 +117,19 @@ var WebSocketMessageType;
105
117
  WebSocketMessageType["AccountUpdate"] = "account";
106
118
  })(WebSocketMessageType || (exports.WebSocketMessageType = WebSocketMessageType = {}));
107
119
  // Positive decimal price and size.
120
+ // Example:
121
+ // ```
122
+ // const limit = new QuoteSize(new Decimal(114000), new Decimal(0.00035)),
123
+ //```
124
+ // Gives 40$ USD limit.
125
+ //
126
+ // Given price is same(or very close) to the market price,
127
+ // limit gives size tick as close as possible to settlemnt size tick.
128
+ // If you want to get smaller tick on client (on server it will not change),
129
+ // do `new QuoteSize(new Decimal(114000/2), new Decimal(0.00070))`.
130
+ // It will be 40$ limit, but may help if BTC suddently moves fast.
108
131
  class QuoteSize {
132
+ /// Input can be only positive values.
109
133
  constructor(quotePrice, quoteSize) {
110
134
  const p = new decimal_js_1.default(quotePrice);
111
135
  const s = new decimal_js_1.default(quoteSize);
@@ -115,10 +139,12 @@ class QuoteSize {
115
139
  this.price = p;
116
140
  this.size = s;
117
141
  }
142
+ // USD value of limit, use for debug
118
143
  value() {
119
144
  return this.price.mul(this.size);
120
145
  }
121
- toScaledU64(marketPriceDecimals, marketSizeDecimals) {
146
+ // Converts to wire format to be send to server, scaling price and size according to market decimals.
147
+ toWire(marketPriceDecimals, marketSizeDecimals) {
122
148
  return {
123
149
  price: (0, utils_1.toScaledU64)(this.price, marketPriceDecimals),
124
150
  size: (0, utils_1.toScaledU64)(this.size, marketSizeDecimals),
package/dist/utils.js CHANGED
@@ -31,6 +31,7 @@ const bs58_1 = __importDefault(require("bs58"));
31
31
  exports.SESSION_TTL = 60n * 60n * 24n * 30n;
32
32
  exports.ZERO_DECIMAL = new decimal_js_1.Decimal(0);
33
33
  exports.MAX_BUFFER_LEN = 10000;
34
+ // Max size of data returned from Nord endpoints
34
35
  const MAX_PAYLOAD_SIZE = 100 * 1024; // 100 kB
35
36
  function panic(message) {
36
37
  throw new Error(message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@n1xyz/nord-ts",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Typescript for Nord",
5
5
  "keywords": [],
6
6
  "author": "",
@@ -45,7 +45,7 @@
45
45
  },
46
46
  "dependencies": {
47
47
  "@bufbuild/protobuf": "^2.6.3",
48
- "@n1xyz/proton": "0.0.2",
48
+ "@n1xyz/proton": "0.0.3",
49
49
  "@noble/curves": "^1.9.6",
50
50
  "@noble/ed25519": "^2.3.0",
51
51
  "@noble/hashes": "^1.8.0",