@n1xyz/nord-ts 0.1.7 → 0.1.9
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/actions.js +39 -82
- package/dist/bundle.js +79181 -0
- package/dist/client/Nord.d.ts +13 -2
- package/dist/client/Nord.js +68 -78
- package/dist/client/NordAdmin.d.ts +2 -2
- package/dist/client/NordAdmin.js +57 -89
- package/dist/client/NordUser.js +118 -147
- package/dist/const.js +5 -8
- package/dist/error.js +7 -5
- package/dist/gen/nord_pb.js +88 -92
- package/dist/gen/openapi.d.ts +83 -6
- package/dist/gen/openapi.js +1 -2
- package/dist/index.js +10 -49
- package/dist/types.d.ts +3 -0
- package/dist/types.js +21 -60
- package/dist/utils.js +38 -86
- package/dist/websocket/NordWebSocketClient.js +12 -17
- package/dist/websocket/Subscriber.js +6 -7
- package/dist/websocket/events.js +1 -2
- package/dist/websocket/index.js +10 -15
- package/package.json +3 -4
- package/dist/api/client.d.ts +0 -14
- package/dist/api/client.js +0 -45
- package/dist/bridge/client.d.ts +0 -151
- package/dist/bridge/client.js +0 -434
- package/dist/bridge/const.d.ts +0 -23
- package/dist/bridge/const.js +0 -47
- package/dist/bridge/index.d.ts +0 -4
- package/dist/bridge/index.js +0 -23
- package/dist/bridge/types.d.ts +0 -120
- package/dist/bridge/types.js +0 -18
- package/dist/bridge/utils.d.ts +0 -64
- package/dist/bridge/utils.js +0 -131
- package/dist/gen/common.d.ts +0 -68
- package/dist/gen/common.js +0 -215
- package/dist/gen/nord.d.ts +0 -882
- package/dist/gen/nord.js +0 -6520
- package/dist/idl/bridge.d.ts +0 -569
- package/dist/idl/bridge.js +0 -8
- package/dist/idl/bridge.json +0 -1506
- package/dist/idl/index.d.ts +0 -607
- package/dist/idl/index.js +0 -8
- package/dist/nord/api/actions.d.ts +0 -126
- package/dist/nord/api/actions.js +0 -397
- package/dist/nord/api/core.d.ts +0 -16
- package/dist/nord/api/core.js +0 -81
- package/dist/nord/api/market.d.ts +0 -36
- package/dist/nord/api/market.js +0 -96
- package/dist/nord/api/metrics.d.ts +0 -67
- package/dist/nord/api/metrics.js +0 -229
- package/dist/nord/api/queries.d.ts +0 -46
- package/dist/nord/api/queries.js +0 -109
- package/dist/nord/api/triggers.d.ts +0 -7
- package/dist/nord/api/triggers.js +0 -38
- package/dist/nord/client/Nord.d.ts +0 -396
- package/dist/nord/client/Nord.js +0 -747
- package/dist/nord/client/NordAdmin.d.ts +0 -259
- package/dist/nord/client/NordAdmin.js +0 -395
- package/dist/nord/client/NordClient.d.ts +0 -33
- package/dist/nord/client/NordClient.js +0 -45
- package/dist/nord/client/NordUser.d.ts +0 -362
- package/dist/nord/client/NordUser.js +0 -781
- package/dist/nord/index.d.ts +0 -11
- package/dist/nord/index.js +0 -36
- package/dist/nord/models/Subscriber.d.ts +0 -37
- package/dist/nord/models/Subscriber.js +0 -25
- package/dist/nord/utils/NordError.d.ts +0 -35
- package/dist/nord/utils/NordError.js +0 -49
- package/src/actions.ts +0 -333
- package/src/client/Nord.ts +0 -934
- package/src/client/NordAdmin.ts +0 -484
- package/src/client/NordUser.ts +0 -1122
- package/src/const.ts +0 -34
- package/src/error.ts +0 -76
- package/src/gen/.gitkeep +0 -0
- package/src/gen/nord_pb.ts +0 -5053
- package/src/gen/openapi.ts +0 -2904
- package/src/index.ts +0 -11
- package/src/types.ts +0 -327
- package/src/utils.ts +0 -266
- package/src/websocket/NordWebSocketClient.ts +0 -316
- package/src/websocket/Subscriber.ts +0 -56
- package/src/websocket/events.ts +0 -31
- package/src/websocket/index.ts +0 -105
package/dist/actions.js
CHANGED
|
@@ -1,63 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.formatReceiptError = formatReceiptError;
|
|
37
|
-
exports.expectReceiptKind = expectReceiptKind;
|
|
38
|
-
exports.createAction = createAction;
|
|
39
|
-
exports.sendAction = sendAction;
|
|
40
|
-
exports.prepareAction = prepareAction;
|
|
41
|
-
exports.createSession = createSession;
|
|
42
|
-
exports.revokeSession = revokeSession;
|
|
43
|
-
exports.atomic = atomic;
|
|
44
|
-
const proto = __importStar(require("./gen/nord_pb"));
|
|
45
|
-
const protobuf_1 = require("@bufbuild/protobuf");
|
|
46
|
-
const types_1 = require("./types");
|
|
47
|
-
const utils_1 = require("./utils");
|
|
48
|
-
const wire_1 = require("@bufbuild/protobuf/wire");
|
|
49
|
-
const error_1 = require("./error");
|
|
50
|
-
function formatReceiptError(receipt) {
|
|
1
|
+
import * as proto from "./gen/nord_pb";
|
|
2
|
+
import { create } from "@bufbuild/protobuf";
|
|
3
|
+
import { fillModeToProtoFillMode, Side } from "./types";
|
|
4
|
+
import { assert, decodeLengthDelimited, SESSION_TTL, toScaledU64, signUserPayload, } from "./utils";
|
|
5
|
+
import { sizeDelimitedEncode } from "@bufbuild/protobuf/wire";
|
|
6
|
+
import { NordError } from "./error";
|
|
7
|
+
export function formatReceiptError(receipt) {
|
|
51
8
|
if (receipt.kind?.case === "err") {
|
|
52
9
|
const err = receipt.kind.value;
|
|
53
10
|
return proto.Error[err] ?? err.toString();
|
|
54
11
|
}
|
|
55
12
|
return receipt.kind?.case ?? "unknown";
|
|
56
13
|
}
|
|
57
|
-
function expectReceiptKind(receipt, expected, action) {
|
|
14
|
+
export function expectReceiptKind(receipt, expected, action) {
|
|
58
15
|
if (receipt.kind?.case !== expected) {
|
|
59
16
|
const label = formatReceiptError(receipt);
|
|
60
|
-
throw new
|
|
17
|
+
throw new NordError(`Failed to ${action}: ${label}`);
|
|
61
18
|
}
|
|
62
19
|
}
|
|
63
20
|
async function sessionSign(signFn, message) {
|
|
@@ -65,14 +22,14 @@ async function sessionSign(signFn, message) {
|
|
|
65
22
|
return new Uint8Array([...message, ...signature]);
|
|
66
23
|
}
|
|
67
24
|
// Helper to create an action with common fields
|
|
68
|
-
function createAction(currentTimestamp, nonce, kind) {
|
|
69
|
-
return
|
|
25
|
+
export function createAction(currentTimestamp, nonce, kind) {
|
|
26
|
+
return create(proto.ActionSchema, {
|
|
70
27
|
currentTimestamp,
|
|
71
28
|
nonce,
|
|
72
29
|
kind,
|
|
73
30
|
});
|
|
74
31
|
}
|
|
75
|
-
async function sendAction(client, makeSignedMessage, action) {
|
|
32
|
+
export async function sendAction(client, makeSignedMessage, action) {
|
|
76
33
|
const body = await prepareAction(action, makeSignedMessage);
|
|
77
34
|
const response = await client.POST("/action", {
|
|
78
35
|
params: {
|
|
@@ -89,8 +46,8 @@ async function sendAction(client, makeSignedMessage, action) {
|
|
|
89
46
|
if (response.error) {
|
|
90
47
|
throw new Error(`Failed to ${action.kind.case}, HTTP status ${JSON.stringify(response.error)}`);
|
|
91
48
|
}
|
|
92
|
-
const rawResp = new Uint8Array(await response.response.
|
|
93
|
-
const resp =
|
|
49
|
+
const rawResp = new Uint8Array(await response.response.arrayBuffer());
|
|
50
|
+
const resp = decodeLengthDelimited(rawResp, proto.ReceiptSchema);
|
|
94
51
|
if (resp.kind?.case === "err") {
|
|
95
52
|
throw new Error(`Could not execute ${action.kind.case}, reason: ${proto.Error[resp.kind.value]}`);
|
|
96
53
|
}
|
|
@@ -98,8 +55,8 @@ async function sendAction(client, makeSignedMessage, action) {
|
|
|
98
55
|
}
|
|
99
56
|
// Given action and signature function, prepare the signed message to send to server as `body`.
|
|
100
57
|
// `makeSignedMessage` must include the original message and signature.
|
|
101
|
-
async function prepareAction(action, makeSignedMessage) {
|
|
102
|
-
const encoded =
|
|
58
|
+
export async function prepareAction(action, makeSignedMessage) {
|
|
59
|
+
const encoded = sizeDelimitedEncode(proto.ActionSchema, action);
|
|
103
60
|
// NOTE(agent): keep in sync with MAX_ENCODED_ACTION_SIZE in Rust code
|
|
104
61
|
const MAX_ENCODED_ACTION_SIZE = 1024;
|
|
105
62
|
if (encoded.byteLength > MAX_ENCODED_ACTION_SIZE) {
|
|
@@ -113,18 +70,18 @@ async function prepareAction(action, makeSignedMessage) {
|
|
|
113
70
|
}
|
|
114
71
|
return body;
|
|
115
72
|
}
|
|
116
|
-
async function createSession(client, signTransaction, currentTimestamp, nonce, params) {
|
|
73
|
+
export async function createSession(client, signTransaction, currentTimestamp, nonce, params) {
|
|
117
74
|
let expiry = 0n;
|
|
118
75
|
if (params.expiryTimestamp !== undefined) {
|
|
119
76
|
expiry = params.expiryTimestamp;
|
|
120
|
-
|
|
77
|
+
assert(expiry > currentTimestamp, "Cannot set expiry timestamp in the past");
|
|
121
78
|
}
|
|
122
79
|
else {
|
|
123
|
-
expiry = currentTimestamp +
|
|
80
|
+
expiry = currentTimestamp + SESSION_TTL;
|
|
124
81
|
}
|
|
125
82
|
const action = createAction(currentTimestamp, nonce, {
|
|
126
83
|
case: "createSession",
|
|
127
|
-
value:
|
|
84
|
+
value: create(proto.Action_CreateSessionSchema, {
|
|
128
85
|
userPubkey: params.userPubkey.toBytes(),
|
|
129
86
|
blstPubkey: params.sessionPubkey.toBytes(),
|
|
130
87
|
expiryTimestamp: expiry,
|
|
@@ -133,7 +90,7 @@ async function createSession(client, signTransaction, currentTimestamp, nonce, p
|
|
|
133
90
|
const resp = await sendAction(client, async (payload) => {
|
|
134
91
|
return new Uint8Array([
|
|
135
92
|
...payload,
|
|
136
|
-
...(await
|
|
93
|
+
...(await signUserPayload({
|
|
137
94
|
payload,
|
|
138
95
|
user: params.userPubkey,
|
|
139
96
|
signTransaction,
|
|
@@ -150,17 +107,17 @@ async function createSession(client, signTransaction, currentTimestamp, nonce, p
|
|
|
150
107
|
throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
|
|
151
108
|
}
|
|
152
109
|
}
|
|
153
|
-
async function revokeSession(client, signTransaction, currentTimestamp, nonce, params) {
|
|
110
|
+
export async function revokeSession(client, signTransaction, currentTimestamp, nonce, params) {
|
|
154
111
|
const action = createAction(currentTimestamp, nonce, {
|
|
155
112
|
case: "revokeSession",
|
|
156
|
-
value:
|
|
113
|
+
value: create(proto.Action_RevokeSessionSchema, {
|
|
157
114
|
sessionId: BigInt(params.sessionId),
|
|
158
115
|
}),
|
|
159
116
|
});
|
|
160
117
|
const resp = await sendAction(client, async (payload) => {
|
|
161
118
|
return new Uint8Array([
|
|
162
119
|
...payload,
|
|
163
|
-
...(await
|
|
120
|
+
...(await signUserPayload({
|
|
164
121
|
payload,
|
|
165
122
|
user: params.userPubkey,
|
|
166
123
|
signTransaction,
|
|
@@ -169,50 +126,50 @@ async function revokeSession(client, signTransaction, currentTimestamp, nonce, p
|
|
|
169
126
|
}, action);
|
|
170
127
|
return { actionId: resp.actionId };
|
|
171
128
|
}
|
|
172
|
-
async function atomic(client, signFn, currentTimestamp, nonce, params) {
|
|
173
|
-
|
|
129
|
+
export async function atomic(client, signFn, currentTimestamp, nonce, params) {
|
|
130
|
+
assert(params.actions.length > 0 && params.actions.length <= 4, "Atomic action must contain between 1 and 4 sub-actions");
|
|
174
131
|
const subactions = params.actions.map((a) => {
|
|
175
132
|
if (a.kind === "place") {
|
|
176
|
-
const price =
|
|
177
|
-
const size =
|
|
133
|
+
const price = toScaledU64(a.price ?? 0, a.priceDecimals);
|
|
134
|
+
const size = toScaledU64(a.size ?? 0, a.sizeDecimals);
|
|
178
135
|
const scaledQuote = a.quoteSize
|
|
179
136
|
? a.quoteSize.toWire(a.priceDecimals, a.sizeDecimals)
|
|
180
137
|
: undefined;
|
|
181
138
|
// Require at least one limit to be set (non-zero size, non-zero price, or quoteSize)
|
|
182
|
-
|
|
183
|
-
const tradeOrPlace =
|
|
139
|
+
assert(price > 0n || size > 0n || scaledQuote !== undefined, "OrderLimit must include at least one of: size, price, or quoteSize");
|
|
140
|
+
const tradeOrPlace = create(proto.TradeOrPlaceSchema, {
|
|
184
141
|
marketId: a.marketId,
|
|
185
|
-
orderType:
|
|
186
|
-
side: a.side ===
|
|
187
|
-
fillMode:
|
|
142
|
+
orderType: create(proto.OrderTypeSchema, {
|
|
143
|
+
side: a.side === Side.Bid ? proto.Side.BID : proto.Side.ASK,
|
|
144
|
+
fillMode: fillModeToProtoFillMode(a.fillMode),
|
|
188
145
|
isReduceOnly: a.isReduceOnly,
|
|
189
146
|
}),
|
|
190
|
-
limit:
|
|
147
|
+
limit: create(proto.OrderLimitSchema, {
|
|
191
148
|
price,
|
|
192
149
|
size,
|
|
193
150
|
quoteSize: scaledQuote === undefined
|
|
194
151
|
? undefined
|
|
195
|
-
:
|
|
152
|
+
: create(proto.QuoteSizeSchema, {
|
|
196
153
|
size: scaledQuote.size,
|
|
197
154
|
price: scaledQuote.price,
|
|
198
155
|
}),
|
|
199
156
|
}),
|
|
200
157
|
clientOrderId: a.clientOrderId === undefined ? undefined : BigInt(a.clientOrderId),
|
|
201
158
|
});
|
|
202
|
-
return
|
|
159
|
+
return create(proto.AtomicSubactionKindSchema, {
|
|
203
160
|
inner: { case: "tradeOrPlace", value: tradeOrPlace },
|
|
204
161
|
});
|
|
205
162
|
}
|
|
206
|
-
return
|
|
163
|
+
return create(proto.AtomicSubactionKindSchema, {
|
|
207
164
|
inner: {
|
|
208
165
|
case: "cancelOrder",
|
|
209
|
-
value:
|
|
166
|
+
value: create(proto.CancelOrderSchema, { orderId: BigInt(a.orderId) }),
|
|
210
167
|
},
|
|
211
168
|
});
|
|
212
169
|
});
|
|
213
170
|
const action = createAction(currentTimestamp, nonce, {
|
|
214
171
|
case: "atomic",
|
|
215
|
-
value:
|
|
172
|
+
value: create(proto.AtomicSchema, {
|
|
216
173
|
sessionId: BigInt(params.sessionId),
|
|
217
174
|
accountId: params.accountId, // optional
|
|
218
175
|
actions: subactions,
|