@n1xyz/nord-ts 0.0.21 → 0.0.22

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 (45) hide show
  1. package/dist/api/client.d.ts +14 -0
  2. package/dist/api/client.js +45 -0
  3. package/dist/bridge/client.d.ts +151 -0
  4. package/dist/bridge/client.js +434 -0
  5. package/dist/bridge/const.d.ts +23 -0
  6. package/dist/bridge/const.js +47 -0
  7. package/dist/bridge/index.d.ts +4 -0
  8. package/dist/bridge/index.js +23 -0
  9. package/dist/bridge/types.d.ts +120 -0
  10. package/dist/bridge/types.js +18 -0
  11. package/dist/bridge/utils.d.ts +64 -0
  12. package/dist/bridge/utils.js +131 -0
  13. package/dist/gen/common.d.ts +68 -0
  14. package/dist/gen/common.js +215 -0
  15. package/dist/gen/nord_pb.d.ts +3651 -0
  16. package/dist/gen/nord_pb.js +892 -0
  17. package/dist/gen/openapi.d.ts +241 -2
  18. package/dist/idl/bridge.d.ts +569 -0
  19. package/dist/idl/bridge.js +8 -0
  20. package/dist/idl/bridge.json +1506 -0
  21. package/dist/idl/index.d.ts +607 -0
  22. package/dist/idl/index.js +8 -0
  23. package/dist/nord/api/actions.d.ts +30 -72
  24. package/dist/nord/api/actions.js +179 -200
  25. package/dist/nord/api/market.d.ts +36 -0
  26. package/dist/nord/api/market.js +96 -0
  27. package/dist/nord/api/queries.d.ts +46 -0
  28. package/dist/nord/api/queries.js +109 -0
  29. package/dist/nord/client/Nord.js +3 -3
  30. package/dist/nord/client/NordUser.d.ts +26 -13
  31. package/dist/nord/client/NordUser.js +13 -10
  32. package/dist/types.d.ts +12 -1
  33. package/dist/types.js +29 -2
  34. package/dist/utils.d.ts +6 -20
  35. package/dist/utils.js +17 -35
  36. package/dist/websocket/NordWebSocketClient.js +2 -6
  37. package/package.json +26 -23
  38. package/src/gen/nord_pb.ts +4172 -0
  39. package/src/gen/openapi.ts +241 -2
  40. package/src/nord/api/actions.ts +249 -370
  41. package/src/nord/client/Nord.ts +3 -3
  42. package/src/nord/client/NordUser.ts +40 -19
  43. package/src/types.ts +32 -1
  44. package/src/utils.ts +24 -43
  45. package/src/websocket/NordWebSocketClient.ts +2 -8
@@ -1,55 +1,28 @@
1
1
  import Decimal from "decimal.js";
2
- import * as proto from "../../gen/nord";
3
- import { FillMode, Side } from "../../types";
2
+ import * as proto from "../../gen/nord_pb";
3
+ import { FillMode, Side, QuoteSize } from "../../types";
4
4
  import { BigIntValue } from "../../utils";
5
- declare function createSessionImpl(sendFn: (encoded: Uint8Array) => Promise<Uint8Array>, walletSignFn: (message: string | Uint8Array) => Promise<Uint8Array>, currentTimestamp: bigint, nonce: number, params: {
6
- userPubkey: Uint8Array;
7
- sessionPubkey: Uint8Array;
8
- expiryTimestamp?: bigint;
9
- }): Promise<bigint>;
10
5
  export declare function createSession(serverUrl: string, walletSignFn: (message: string | Uint8Array) => Promise<Uint8Array>, currentTimestamp: bigint, nonce: number, params: {
11
6
  userPubkey: Uint8Array;
12
7
  sessionPubkey: Uint8Array;
13
8
  expiryTimestamp?: bigint;
14
- }): Promise<bigint>;
15
- declare function revokeSessionImpl(sendFn: (encoded: Uint8Array) => Promise<Uint8Array>, walletSignFn: (message: string | Uint8Array) => Promise<Uint8Array>, currentTimestamp: bigint, nonce: number, params: {
16
- sessionId: BigIntValue;
17
- }): Promise<void>;
9
+ }): Promise<{
10
+ actionId: bigint;
11
+ sessionId: bigint;
12
+ }>;
18
13
  export declare function revokeSession(serverUrl: string, walletSignFn: (message: string | Uint8Array) => Promise<Uint8Array>, currentTimestamp: bigint, nonce: number, params: {
19
14
  sessionId: BigIntValue;
20
- }): Promise<void>;
21
- declare function withdrawImpl(sendFn: (encoded: Uint8Array) => Promise<Uint8Array>, signFn: (message: Uint8Array) => Promise<Uint8Array>, currentTimestamp: bigint, nonce: number, params: {
22
- sizeDecimals: number;
23
- sessionId: BigIntValue;
24
- tokenId: number;
25
- amount: Decimal.Value;
26
15
  }): Promise<{
27
16
  actionId: bigint;
28
- } & proto.Receipt_WithdrawResult>;
17
+ }>;
29
18
  export declare function withdraw(serverUrl: string, signFn: (message: Uint8Array) => Promise<Uint8Array>, currentTimestamp: bigint, nonce: number, params: {
30
19
  sizeDecimals: number;
31
20
  sessionId: BigIntValue;
32
21
  tokenId: number;
33
- amount: number;
22
+ amount: Decimal.Value;
34
23
  }): Promise<{
35
24
  actionId: bigint;
36
25
  } & proto.Receipt_WithdrawResult>;
37
- declare function placeOrderImpl(sendFn: (encoded: Uint8Array) => Promise<Uint8Array>, signFn: (message: Uint8Array) => Promise<Uint8Array>, currentTimestamp: bigint, nonce: number, params: {
38
- sessionId: BigIntValue;
39
- senderId?: number;
40
- liquidateeId?: number;
41
- sizeDecimals: number;
42
- priceDecimals: number;
43
- marketId: number;
44
- side: Side;
45
- fillMode: FillMode;
46
- isReduceOnly: boolean;
47
- size?: Decimal.Value;
48
- price?: Decimal.Value;
49
- quoteSizeSize?: Decimal.Value;
50
- quoteSizePrice?: Decimal.Value;
51
- clientOrderId?: BigIntValue;
52
- }): Promise<bigint | undefined>;
53
26
  export declare function placeOrder(serverUrl: string, signFn: (message: Uint8Array) => Promise<Uint8Array>, currentTimestamp: bigint, nonce: number, params: {
54
27
  sessionId: BigIntValue;
55
28
  senderId?: number;
@@ -61,38 +34,39 @@ export declare function placeOrder(serverUrl: string, signFn: (message: Uint8Arr
61
34
  isReduceOnly: boolean;
62
35
  size?: Decimal.Value;
63
36
  price?: Decimal.Value;
64
- quoteSize?: Decimal.Value;
37
+ quoteSize?: QuoteSize;
65
38
  liquidateeId?: number;
66
39
  clientOrderId?: BigIntValue;
67
- }): Promise<bigint | undefined>;
68
- declare function cancelOrderImpl(sendFn: (encoded: Uint8Array) => Promise<Uint8Array>, signFn: (message: Uint8Array) => Promise<Uint8Array>, currentTimestamp: bigint, nonce: number, params: {
69
- sessionId: BigIntValue;
70
- senderId?: number;
71
- orderId: BigIntValue;
72
- liquidateeId?: number;
73
- }): Promise<bigint>;
40
+ }): Promise<{
41
+ actionId: bigint;
42
+ orderId?: bigint;
43
+ fills: proto.Receipt_Trade[];
44
+ }>;
74
45
  export declare function cancelOrder(serverUrl: string, signFn: (message: Uint8Array) => Promise<Uint8Array>, currentTimestamp: bigint, nonce: number, params: {
75
46
  sessionId: BigIntValue;
76
47
  senderId?: number;
77
48
  orderId: BigIntValue;
78
49
  liquidateeId?: number;
79
- }): Promise<bigint>;
80
- declare function transferImpl(sendFn: (encoded: Uint8Array) => Promise<Uint8Array>, signFn: (message: Uint8Array) => Promise<Uint8Array>, currentTimestamp: bigint, nonce: number, params: {
50
+ }): Promise<{
51
+ actionId: bigint;
52
+ orderId: bigint;
53
+ accountId: number;
54
+ }>;
55
+ export declare function transfer(serverUrl: string, signFn: (message: Uint8Array) => Promise<Uint8Array>, currentTimestamp: bigint, nonce: number, params: {
81
56
  sessionId: BigIntValue;
82
57
  fromAccountId: number;
83
58
  toAccountId?: number;
84
59
  tokenId: number;
85
60
  tokenDecimals: number;
86
61
  amount: Decimal.Value;
87
- }): Promise<number | undefined>;
88
- export declare function transfer(serverUrl: string, signFn: (message: Uint8Array) => Promise<Uint8Array>, currentTimestamp: bigint, nonce: number, params: {
89
- sessionId: BigIntValue;
62
+ }): Promise<{
63
+ actionId: bigint;
90
64
  fromAccountId: number;
91
65
  toAccountId?: number;
92
66
  tokenId: number;
93
- tokenDecimals: number;
94
- amount: Decimal.Value;
95
- }): Promise<number | undefined>;
67
+ amount: bigint;
68
+ accountCreated: boolean;
69
+ }>;
96
70
  export type AtomicSubaction = {
97
71
  kind: "place";
98
72
  marketId: number;
@@ -103,33 +77,17 @@ export type AtomicSubaction = {
103
77
  priceDecimals: number;
104
78
  size?: Decimal.Value;
105
79
  price?: Decimal.Value;
106
- quoteSizeSize?: Decimal.Value;
107
- quoteSizePrice?: Decimal.Value;
80
+ quoteSize?: QuoteSize;
108
81
  clientOrderId?: BigIntValue;
109
82
  } | {
110
83
  kind: "cancel";
111
84
  orderId: BigIntValue;
112
85
  };
113
- declare function atomicImpl(sendFn: (encoded: Uint8Array) => Promise<Uint8Array>, signFn: (message: Uint8Array) => Promise<Uint8Array>, currentTimestamp: bigint, nonce: number, params: {
114
- sessionId: BigIntValue;
115
- accountId?: number;
116
- actions: AtomicSubaction[];
117
- }): Promise<proto.Receipt_AtomicResult>;
118
86
  export declare function atomic(serverUrl: string, signFn: (message: Uint8Array) => Promise<Uint8Array>, currentTimestamp: bigint, nonce: number, params: {
119
87
  sessionId: BigIntValue;
120
88
  accountId?: number;
121
89
  actions: AtomicSubaction[];
122
- }): Promise<proto.Receipt_AtomicResult>;
123
- /**
124
- * For testing purposes
125
- */
126
- export declare const _private: {
127
- createSessionImpl: typeof createSessionImpl;
128
- revokeSessionImpl: typeof revokeSessionImpl;
129
- withdrawImpl: typeof withdrawImpl;
130
- placeOrderImpl: typeof placeOrderImpl;
131
- cancelOrderImpl: typeof cancelOrderImpl;
132
- transferImpl: typeof transferImpl;
133
- atomicImpl: typeof atomicImpl;
134
- };
135
- export {};
90
+ }): Promise<{
91
+ actionId: bigint;
92
+ results: proto.Receipt_AtomicSubactionResultKind[];
93
+ }>;
@@ -33,7 +33,6 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports._private = void 0;
37
36
  exports.createSession = createSession;
38
37
  exports.revokeSession = revokeSession;
39
38
  exports.withdraw = withdraw;
@@ -41,9 +40,11 @@ exports.placeOrder = placeOrder;
41
40
  exports.cancelOrder = cancelOrder;
42
41
  exports.transfer = transfer;
43
42
  exports.atomic = atomic;
44
- const proto = __importStar(require("../../gen/nord"));
43
+ const proto = __importStar(require("../../gen/nord_pb"));
44
+ const protobuf_1 = require("@bufbuild/protobuf");
45
45
  const types_1 = require("../../types");
46
46
  const utils_1 = require("../../utils");
47
+ const wire_1 = require("@bufbuild/protobuf/wire");
47
48
  async function sessionSign(signFn, message) {
48
49
  const signature = await signFn(message);
49
50
  return new Uint8Array([...message, ...signature]);
@@ -52,30 +53,38 @@ async function walletSign(walletSignFn, message) {
52
53
  const signature = await walletSignFn(message);
53
54
  return new Uint8Array([...message, ...signature]);
54
55
  }
55
- function makeSendHttp(serverUrl) {
56
- return async (body) => {
57
- // TODO: this should be changed to use openapi
58
- const response = await (0, utils_1.checkedFetch)(`${serverUrl}/action`, {
59
- method: "POST",
60
- headers: {
61
- "Content-Type": "application/json",
62
- },
63
- body,
64
- });
65
- return new Uint8Array(await response.arrayBuffer());
66
- };
56
+ // Helper to create an action with common fields
57
+ function createAction(currentTimestamp, nonce, kind) {
58
+ return (0, protobuf_1.create)(proto.ActionSchema, {
59
+ currentTimestamp,
60
+ nonce,
61
+ kind,
62
+ });
67
63
  }
68
- async function sendAction(sendFn, makeSignedMessage, action, actionErrorDesc) {
69
- const encoded = (0, utils_1.encodeLengthDelimited)(action, proto.Action);
64
+ async function sendAction(serverUrl, makeSignedMessage, action, actionErrorDesc) {
65
+ const encoded = (0, wire_1.sizeDelimitedEncode)(proto.ActionSchema, action);
66
+ // validate the payload size
67
+ const MAX_PAYLOAD_SIZE = 100 * 1024; // 100 kB
68
+ if (encoded.byteLength > MAX_PAYLOAD_SIZE) {
69
+ throw new Error(`Encoded message size (${encoded.byteLength} bytes) is greater than max payload size (${MAX_PAYLOAD_SIZE} bytes).`);
70
+ }
70
71
  const body = await makeSignedMessage(encoded);
71
- const rawResp = await sendFn(body);
72
- const resp = (0, utils_1.decodeLengthDelimited)(rawResp, proto.Receipt);
73
- if (resp.kind?.$case === "err") {
74
- throw new Error(`Could not ${actionErrorDesc}, reason: ${proto.errorToJSON(resp.kind.value)}`);
72
+ // TODO: this should be changed to use openapi
73
+ const response = await (0, utils_1.checkedFetch)(`${serverUrl}/action`, {
74
+ method: "POST",
75
+ headers: {
76
+ "Content-Type": "application/json",
77
+ },
78
+ body,
79
+ });
80
+ const rawResp = new Uint8Array(await response.arrayBuffer());
81
+ const resp = (0, utils_1.decodeLengthDelimited)(rawResp, proto.ReceiptSchema);
82
+ if (resp.kind?.case === "err") {
83
+ throw new Error(`Could not ${actionErrorDesc}, reason: ${proto.Error[resp.kind.value]}`);
75
84
  }
76
85
  return resp;
77
86
  }
78
- async function createSessionImpl(sendFn, walletSignFn, currentTimestamp, nonce, params) {
87
+ async function createSession(serverUrl, walletSignFn, currentTimestamp, nonce, params) {
79
88
  (0, utils_1.checkPubKeyLength)(types_1.KeyType.Ed25519, params.userPubkey.length);
80
89
  (0, utils_1.checkPubKeyLength)(types_1.KeyType.Ed25519, params.sessionPubkey.length);
81
90
  let expiry = 0n;
@@ -86,228 +95,198 @@ async function createSessionImpl(sendFn, walletSignFn, currentTimestamp, nonce,
86
95
  else {
87
96
  expiry = currentTimestamp + utils_1.SESSION_TTL;
88
97
  }
89
- const action = {
90
- currentTimestamp,
91
- nonce,
92
- kind: {
93
- $case: "createSession",
94
- value: {
95
- userPubkey: params.userPubkey,
96
- blstPubkey: params.sessionPubkey,
97
- expiryTimestamp: expiry,
98
- },
99
- },
100
- };
101
- const resp = await sendAction(sendFn, (m) => walletSign(walletSignFn, m), action, "create a new session");
102
- if (resp.kind?.$case === "createSessionResult") {
103
- return resp.kind.value.sessionId;
98
+ const action = createAction(currentTimestamp, nonce, {
99
+ case: "createSession",
100
+ value: (0, protobuf_1.create)(proto.Action_CreateSessionSchema, {
101
+ userPubkey: params.userPubkey,
102
+ blstPubkey: params.sessionPubkey,
103
+ expiryTimestamp: expiry,
104
+ }),
105
+ });
106
+ const resp = await sendAction(serverUrl, (m) => walletSign(walletSignFn, m), action, "create a new session");
107
+ if (resp.kind?.case === "createSessionResult") {
108
+ return {
109
+ actionId: resp.actionId,
110
+ sessionId: resp.kind.value.sessionId,
111
+ };
104
112
  }
105
113
  else {
106
- throw new Error(`Unexpected receipt kind ${resp.kind?.$case}`);
114
+ throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
107
115
  }
108
116
  }
109
- async function createSession(serverUrl, walletSignFn, currentTimestamp, nonce, params) {
110
- return createSessionImpl(makeSendHttp(serverUrl), walletSignFn, currentTimestamp, nonce, params);
111
- }
112
- async function revokeSessionImpl(sendFn, walletSignFn, currentTimestamp, nonce, params) {
113
- const action = {
114
- currentTimestamp,
115
- nonce,
116
- kind: {
117
- $case: "revokeSession",
118
- value: {
119
- sessionId: BigInt(params.sessionId),
120
- },
121
- },
122
- };
123
- await sendAction(sendFn, (m) => walletSign(walletSignFn, m), action, "create a new session");
124
- }
125
117
  async function revokeSession(serverUrl, walletSignFn, currentTimestamp, nonce, params) {
126
- return revokeSessionImpl(makeSendHttp(serverUrl), walletSignFn, currentTimestamp, nonce, params);
118
+ const action = createAction(currentTimestamp, nonce, {
119
+ case: "revokeSession",
120
+ value: (0, protobuf_1.create)(proto.Action_RevokeSessionSchema, {
121
+ sessionId: BigInt(params.sessionId),
122
+ }),
123
+ });
124
+ const resp = await sendAction(serverUrl, (m) => walletSign(walletSignFn, m), action, "revoke session");
125
+ return { actionId: resp.actionId };
127
126
  }
128
- async function withdrawImpl(sendFn, signFn, currentTimestamp, nonce, params) {
127
+ async function withdraw(serverUrl, signFn, currentTimestamp, nonce, params) {
129
128
  const amount = (0, utils_1.toScaledU64)(params.amount, params.sizeDecimals);
130
129
  if (amount <= 0) {
131
130
  throw new Error("Withdraw amount must be positive");
132
131
  }
133
- const action = {
134
- currentTimestamp,
135
- nonce,
136
- kind: {
137
- $case: "withdraw",
138
- value: {
139
- sessionId: BigInt(params.sessionId),
140
- tokenId: params.tokenId,
141
- amount,
142
- },
143
- },
144
- };
145
- const resp = await sendAction(sendFn, (m) => sessionSign(signFn, m), action, "withdraw");
146
- if (resp.kind?.$case === "withdrawResult") {
132
+ const action = createAction(currentTimestamp, nonce, {
133
+ case: "withdraw",
134
+ value: (0, protobuf_1.create)(proto.Action_WithdrawSchema, {
135
+ sessionId: BigInt(params.sessionId),
136
+ tokenId: params.tokenId,
137
+ amount,
138
+ }),
139
+ });
140
+ const resp = await sendAction(serverUrl, (m) => sessionSign(signFn, m), action, "withdraw");
141
+ if (resp.kind?.case === "withdrawResult") {
147
142
  return { actionId: resp.actionId, ...resp.kind.value };
148
143
  }
149
144
  else {
150
- throw new Error(`Unexpected receipt kind ${resp.kind?.$case}`);
145
+ throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
151
146
  }
152
147
  }
153
- async function withdraw(serverUrl, signFn, currentTimestamp, nonce, params) {
154
- return withdrawImpl(makeSendHttp(serverUrl), signFn, currentTimestamp, nonce, params);
155
- }
156
- async function placeOrderImpl(sendFn, signFn, currentTimestamp, nonce, params) {
148
+ async function placeOrder(serverUrl, signFn, currentTimestamp, nonce, params) {
157
149
  const price = (0, utils_1.toScaledU64)(params.price ?? 0, params.priceDecimals);
158
150
  const size = (0, utils_1.toScaledU64)(params.size ?? 0, params.sizeDecimals);
159
- const quoteSize = (0, utils_1.toScaledU64)(params.quoteSizeSize ?? 0, params.sizeDecimals);
160
- const quotePrice = (0, utils_1.toScaledU64)(params.quoteSizePrice ?? 0, params.priceDecimals);
161
- // Compose action object
162
- const action = {
163
- currentTimestamp,
164
- nonce,
165
- kind: {
166
- $case: "placeOrder",
167
- value: {
168
- sessionId: BigInt(params.sessionId),
169
- senderAccountId: params.senderId,
170
- marketId: params.marketId,
171
- side: params.side === types_1.Side.Bid ? proto.Side.BID : proto.Side.ASK,
172
- fillMode: (0, types_1.fillModeToProtoFillMode)(params.fillMode),
173
- isReduceOnly: params.isReduceOnly,
174
- price,
175
- size,
176
- quoteSize: { size: quoteSize, price: quotePrice },
177
- clientOrderId: params.clientOrderId === undefined
178
- ? undefined
179
- : BigInt(params.clientOrderId),
180
- delegatorAccountId: params.liquidateeId,
181
- },
182
- },
183
- };
184
- const resp = await sendAction(sendFn, (m) => sessionSign(signFn, m), action, "place the order");
185
- if (resp.kind?.$case === "placeOrderResult") {
186
- return resp.kind.value.posted?.orderId;
151
+ const scaledQuote = params.quoteSize
152
+ ? params.quoteSize.toScaledU64(params.priceDecimals, params.sizeDecimals)
153
+ : undefined;
154
+ const action = createAction(currentTimestamp, nonce, {
155
+ case: "placeOrder",
156
+ value: (0, protobuf_1.create)(proto.Action_PlaceOrderSchema, {
157
+ sessionId: BigInt(params.sessionId),
158
+ senderAccountId: params.senderId,
159
+ marketId: params.marketId,
160
+ side: params.side === types_1.Side.Bid ? proto.Side.BID : proto.Side.ASK,
161
+ fillMode: (0, types_1.fillModeToProtoFillMode)(params.fillMode),
162
+ isReduceOnly: params.isReduceOnly,
163
+ price,
164
+ size,
165
+ quoteSize: scaledQuote === undefined
166
+ ? undefined
167
+ : (0, protobuf_1.create)(proto.QuoteSizeSchema, {
168
+ size: scaledQuote.size,
169
+ price: scaledQuote.price,
170
+ }),
171
+ clientOrderId: params.clientOrderId === undefined
172
+ ? undefined
173
+ : BigInt(params.clientOrderId),
174
+ delegatorAccountId: params.liquidateeId,
175
+ }),
176
+ });
177
+ const resp = await sendAction(serverUrl, (m) => sessionSign(signFn, m), action, "place order");
178
+ if (resp.kind?.case === "placeOrderResult") {
179
+ return {
180
+ actionId: resp.actionId,
181
+ orderId: resp.kind.value.posted?.orderId,
182
+ fills: resp.kind.value.fills,
183
+ };
187
184
  }
188
185
  else {
189
- throw new Error(`Unexpected receipt kind ${resp.kind?.$case}`);
186
+ throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
190
187
  }
191
188
  }
192
- async function placeOrder(serverUrl, signFn, currentTimestamp, nonce, params) {
193
- return placeOrderImpl(makeSendHttp(serverUrl), signFn, currentTimestamp, nonce, params);
194
- }
195
- async function cancelOrderImpl(sendFn, signFn, currentTimestamp, nonce, params) {
196
- const action = {
197
- currentTimestamp,
198
- nonce: nonce,
199
- kind: {
200
- $case: "cancelOrderById",
201
- value: {
202
- orderId: BigInt(params.orderId),
203
- sessionId: BigInt(params.sessionId),
204
- senderAccountId: params.senderId,
205
- delegatorAccountId: params.liquidateeId,
206
- },
207
- },
208
- };
209
- const resp = await sendAction(sendFn, (m) => sessionSign(signFn, m), action, "cancel the order");
210
- if (resp.kind?.$case === "cancelOrderResult") {
211
- return resp.kind.value.orderId;
189
+ async function cancelOrder(serverUrl, signFn, currentTimestamp, nonce, params) {
190
+ const action = createAction(currentTimestamp, nonce, {
191
+ case: "cancelOrderById",
192
+ value: (0, protobuf_1.create)(proto.Action_CancelOrderByIdSchema, {
193
+ orderId: BigInt(params.orderId),
194
+ sessionId: BigInt(params.sessionId),
195
+ senderAccountId: params.senderId,
196
+ delegatorAccountId: params.liquidateeId,
197
+ }),
198
+ });
199
+ const resp = await sendAction(serverUrl, (m) => sessionSign(signFn, m), action, "cancel order");
200
+ if (resp.kind?.case === "cancelOrderResult") {
201
+ return {
202
+ actionId: resp.actionId,
203
+ orderId: resp.kind.value.orderId,
204
+ accountId: resp.kind.value.accountId,
205
+ };
212
206
  }
213
207
  else {
214
- throw new Error(`Unexpected receipt kind ${resp.kind?.$case}`);
208
+ throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
215
209
  }
216
210
  }
217
- async function cancelOrder(serverUrl, signFn, currentTimestamp, nonce, params) {
218
- return cancelOrderImpl(makeSendHttp(serverUrl), signFn, currentTimestamp, nonce, params);
219
- }
220
- async function transferImpl(sendFn, signFn, currentTimestamp, nonce, params) {
221
- const action = {
222
- currentTimestamp,
223
- nonce: nonce,
224
- kind: {
225
- $case: "transfer",
226
- value: {
227
- sessionId: BigInt(params.sessionId),
228
- fromAccountId: params.fromAccountId,
229
- toAccountId: params.toAccountId,
230
- tokenId: params.tokenId,
231
- amount: (0, utils_1.toScaledU64)(params.amount ?? 0, params.tokenDecimals),
232
- },
233
- },
234
- };
235
- const resp = await sendAction(sendFn, (m) => sessionSign(signFn, m), action, "transfer asset to other account");
236
- if (resp.kind?.$case === "transferred") {
237
- if (resp.kind.value.accountCreated) {
238
- return resp.kind.value.toUserAccount;
239
- }
240
- else {
241
- return undefined;
242
- }
211
+ async function transfer(serverUrl, signFn, currentTimestamp, nonce, params) {
212
+ const action = createAction(currentTimestamp, nonce, {
213
+ case: "transfer",
214
+ value: (0, protobuf_1.create)(proto.Action_TransferSchema, {
215
+ sessionId: BigInt(params.sessionId),
216
+ fromAccountId: params.fromAccountId,
217
+ toAccountId: params.toAccountId,
218
+ tokenId: params.tokenId,
219
+ amount: (0, utils_1.toScaledU64)(params.amount ?? 0, params.tokenDecimals),
220
+ }),
221
+ });
222
+ const resp = await sendAction(serverUrl, (m) => sessionSign(signFn, m), action, "transfer");
223
+ if (resp.kind?.case === "transferred") {
224
+ return {
225
+ actionId: resp.actionId,
226
+ fromAccountId: resp.kind.value.fromAccountId,
227
+ toAccountId: resp.kind.value.toUserAccount,
228
+ tokenId: resp.kind.value.tokenId,
229
+ amount: resp.kind.value.amount,
230
+ accountCreated: resp.kind.value.accountCreated,
231
+ };
243
232
  }
244
233
  else {
245
- throw new Error(`Unexpected receipt kind ${resp.kind?.$case}`);
234
+ throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
246
235
  }
247
236
  }
248
- async function transfer(serverUrl, signFn, currentTimestamp, nonce, params) {
249
- return transferImpl(makeSendHttp(serverUrl), signFn, currentTimestamp, nonce, params);
250
- }
251
- async function atomicImpl(sendFn, signFn, currentTimestamp, nonce, params) {
237
+ async function atomic(serverUrl, signFn, currentTimestamp, nonce, params) {
252
238
  (0, utils_1.assert)(params.actions.length > 0 && params.actions.length <= 4, "Atomic action must contain between 1 and 4 sub-actions");
253
239
  const subactions = params.actions.map((a) => {
254
240
  if (a.kind === "place") {
255
241
  const price = (0, utils_1.toScaledU64)(a.price ?? 0, a.priceDecimals);
256
242
  const size = (0, utils_1.toScaledU64)(a.size ?? 0, a.sizeDecimals);
257
- const quoteSizeSize = (0, utils_1.toScaledU64)(a.quoteSizeSize ?? 0, a.sizeDecimals);
258
- const quoteSizePrice = (0, utils_1.toScaledU64)(a.quoteSizePrice ?? 0, a.priceDecimals);
259
- const tradeOrPlace = {
243
+ const scaledQuote = a.quoteSize
244
+ ? a.quoteSize.toScaledU64(a.priceDecimals, a.sizeDecimals)
245
+ : undefined;
246
+ const tradeOrPlace = (0, protobuf_1.create)(proto.TradeOrPlaceSchema, {
260
247
  marketId: a.marketId,
261
- orderType: {
248
+ orderType: (0, protobuf_1.create)(proto.OrderTypeSchema, {
262
249
  side: a.side === types_1.Side.Bid ? proto.Side.BID : proto.Side.ASK,
263
250
  fillMode: (0, types_1.fillModeToProtoFillMode)(a.fillMode),
264
251
  isReduceOnly: a.isReduceOnly,
265
- },
266
- limit: {
252
+ }),
253
+ limit: (0, protobuf_1.create)(proto.OrderLimitSchema, {
267
254
  price,
268
255
  size,
269
- quoteSize: { size: quoteSizeSize, price: quoteSizePrice },
270
- },
256
+ quoteSize: scaledQuote === undefined
257
+ ? undefined
258
+ : (0, protobuf_1.create)(proto.QuoteSizeSchema, {
259
+ size: scaledQuote.size,
260
+ price: scaledQuote.price,
261
+ }),
262
+ }),
271
263
  clientOrderId: a.clientOrderId === undefined ? undefined : BigInt(a.clientOrderId),
272
- };
273
- return {
274
- inner: { $case: "tradeOrPlace", value: tradeOrPlace },
275
- };
264
+ });
265
+ return (0, protobuf_1.create)(proto.AtomicSubactionKindSchema, {
266
+ inner: { case: "tradeOrPlace", value: tradeOrPlace },
267
+ });
276
268
  }
269
+ return (0, protobuf_1.create)(proto.AtomicSubactionKindSchema, {
270
+ inner: {
271
+ case: "cancelOrder",
272
+ value: (0, protobuf_1.create)(proto.CancelOrderSchema, { orderId: BigInt(a.orderId) }),
273
+ },
274
+ });
275
+ });
276
+ const action = createAction(currentTimestamp, nonce, {
277
+ case: "atomic",
278
+ value: (0, protobuf_1.create)(proto.AtomicSchema, {
279
+ sessionId: BigInt(params.sessionId),
280
+ accountId: params.accountId, // optional
281
+ actions: subactions,
282
+ }),
283
+ });
284
+ const resp = await sendAction(serverUrl, (m) => sessionSign(signFn, m), action, "execute atomic action");
285
+ if (resp.kind?.case === "atomic") {
277
286
  return {
278
- inner: { $case: "cancelOrder", value: { orderId: BigInt(a.orderId) } },
287
+ actionId: resp.actionId,
288
+ results: resp.kind.value.results,
279
289
  };
280
- });
281
- const action = {
282
- currentTimestamp,
283
- nonce,
284
- kind: {
285
- $case: "atomic",
286
- value: {
287
- sessionId: BigInt(params.sessionId),
288
- accountId: params.accountId, // optional
289
- actions: subactions,
290
- },
291
- },
292
- };
293
- const resp = await sendAction(sendFn, (m) => sessionSign(signFn, m), action, "execute atomic action");
294
- if (resp.kind?.$case === "atomic") {
295
- return resp.kind.value;
296
290
  }
297
- throw new Error(`Unexpected receipt kind ${resp.kind?.$case}`);
298
- }
299
- async function atomic(serverUrl, signFn, currentTimestamp, nonce, params) {
300
- return atomicImpl(makeSendHttp(serverUrl), signFn, currentTimestamp, nonce, params);
291
+ throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
301
292
  }
302
- /**
303
- * For testing purposes
304
- */
305
- exports._private = {
306
- createSessionImpl,
307
- revokeSessionImpl,
308
- withdrawImpl,
309
- placeOrderImpl,
310
- cancelOrderImpl,
311
- transferImpl,
312
- atomicImpl,
313
- };
@@ -0,0 +1,36 @@
1
+ import { MarketsStatsResponse, OrderbookQuery, OrderbookResponse, TradesQuery, TradesResponse, UserAccountIdsQuery, UserAccountIdsResponse } from "../../types";
2
+ /**
3
+ * Get market statistics
4
+ *
5
+ * @param webServerUrl - Base URL for the Nord web server
6
+ * @returns Market statistics response
7
+ * @throws {NordError} If the request fails
8
+ */
9
+ export declare function marketsStats(webServerUrl: string): Promise<MarketsStatsResponse>;
10
+ /**
11
+ * Get trades for a market
12
+ *
13
+ * @param webServerUrl - Base URL for the Nord web server
14
+ * @param query - Trades query parameters
15
+ * @returns Trades response
16
+ * @throws {NordError} If the request fails
17
+ */
18
+ export declare function getTrades(webServerUrl: string, query: TradesQuery): Promise<TradesResponse>;
19
+ /**
20
+ * Get user account IDs
21
+ *
22
+ * @param webServerUrl - Base URL for the Nord web server
23
+ * @param query - User account IDs query parameters
24
+ * @returns User account IDs response
25
+ * @throws {NordError} If the request fails
26
+ */
27
+ export declare function getUserAccountIds(webServerUrl: string, query: UserAccountIdsQuery): Promise<UserAccountIdsResponse>;
28
+ /**
29
+ * Get orderbook for a market
30
+ *
31
+ * @param webServerUrl - Base URL for the Nord web server
32
+ * @param query - Orderbook query parameters
33
+ * @returns Orderbook response
34
+ * @throws {NordError} If the request fails
35
+ */
36
+ export declare function getOrderbook(webServerUrl: string, query: OrderbookQuery): Promise<OrderbookResponse>;