@n1xyz/nord-ts 0.1.6 → 0.1.8
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.d.ts +57 -0
- package/dist/actions.js +186 -0
- package/dist/bundle.js +79181 -0
- package/dist/{nord/client → client}/Nord.d.ts +90 -107
- package/dist/{nord/client → client}/Nord.js +157 -218
- package/dist/{nord/client → client}/NordAdmin.d.ts +68 -102
- package/dist/{nord/client → client}/NordAdmin.js +109 -140
- package/dist/{nord/client → client}/NordUser.d.ts +86 -98
- package/dist/{nord/client → client}/NordUser.js +204 -271
- package/dist/const.js +5 -8
- package/dist/{nord/utils/NordError.js → error.js} +7 -5
- package/dist/gen/nord_pb.js +88 -92
- package/dist/gen/openapi.d.ts +39 -0
- package/dist/gen/openapi.js +1 -2
- package/dist/index.d.ts +6 -1
- package/dist/index.js +10 -21
- package/dist/types.d.ts +5 -50
- package/dist/types.js +21 -83
- package/dist/utils.d.ts +8 -11
- package/dist/utils.js +55 -90
- package/dist/websocket/NordWebSocketClient.js +12 -17
- package/dist/{nord/models → websocket}/Subscriber.d.ts +1 -1
- package/dist/{nord/models → websocket}/Subscriber.js +6 -7
- package/dist/websocket/events.js +1 -2
- package/dist/websocket/index.d.ts +19 -2
- package/dist/websocket/index.js +80 -5
- package/package.json +2 -3
- 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/NordClient.d.ts +0 -33
- package/dist/nord/client/NordClient.js +0 -45
- package/dist/nord/index.d.ts +0 -11
- package/dist/nord/index.js +0 -36
- package/src/const.ts +0 -34
- package/src/gen/.gitkeep +0 -0
- package/src/gen/nord_pb.ts +0 -5053
- package/src/gen/openapi.ts +0 -2864
- package/src/index.ts +0 -5
- package/src/nord/api/actions.ts +0 -648
- package/src/nord/api/core.ts +0 -96
- package/src/nord/api/metrics.ts +0 -269
- package/src/nord/client/Nord.ts +0 -937
- package/src/nord/client/NordAdmin.ts +0 -514
- package/src/nord/client/NordClient.ts +0 -79
- package/src/nord/client/NordUser.ts +0 -1211
- package/src/nord/index.ts +0 -25
- package/src/nord/models/Subscriber.ts +0 -56
- package/src/nord/utils/NordError.ts +0 -76
- package/src/types.ts +0 -377
- package/src/utils.ts +0 -269
- package/src/websocket/NordWebSocketClient.ts +0 -316
- package/src/websocket/events.ts +0 -31
- package/src/websocket/index.ts +0 -2
- /package/dist/{nord/utils/NordError.d.ts → error.d.ts} +0 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import Decimal from "decimal.js";
|
|
2
|
+
import * as proto from "./gen/nord_pb";
|
|
3
|
+
import { paths } from "./gen/openapi";
|
|
4
|
+
import { Client } from "openapi-fetch";
|
|
5
|
+
import { FillMode, Side, QuoteSize } from "./types";
|
|
6
|
+
import { BigIntValue } from "./utils";
|
|
7
|
+
import { PublicKey, Transaction } from "@solana/web3.js";
|
|
8
|
+
type ReceiptKind = NonNullable<proto.Receipt["kind"]>;
|
|
9
|
+
type ExtractReceiptKind<K extends ReceiptKind["case"]> = Extract<ReceiptKind, {
|
|
10
|
+
case: K;
|
|
11
|
+
}>;
|
|
12
|
+
export declare function formatReceiptError(receipt: proto.Receipt): string;
|
|
13
|
+
export declare function expectReceiptKind<K extends ReceiptKind["case"]>(receipt: proto.Receipt, expected: K, action: string): asserts receipt is proto.Receipt & {
|
|
14
|
+
kind: ExtractReceiptKind<K>;
|
|
15
|
+
};
|
|
16
|
+
export declare function createAction(currentTimestamp: bigint, nonce: number, kind: proto.Action["kind"]): proto.Action;
|
|
17
|
+
export declare function sendAction(client: Client<paths>, makeSignedMessage: (message: Uint8Array) => Promise<Uint8Array>, action: proto.Action): Promise<proto.Receipt>;
|
|
18
|
+
export declare function prepareAction(action: proto.Action, makeSignedMessage: (message: Uint8Array) => Promise<Uint8Array>): Promise<Uint8Array<ArrayBufferLike>>;
|
|
19
|
+
export declare function createSession(client: Client<paths>, signTransaction: (tx: Transaction) => Promise<Transaction>, currentTimestamp: bigint, nonce: number, params: {
|
|
20
|
+
userPubkey: PublicKey;
|
|
21
|
+
sessionPubkey: PublicKey;
|
|
22
|
+
expiryTimestamp?: bigint;
|
|
23
|
+
}): Promise<{
|
|
24
|
+
actionId: bigint;
|
|
25
|
+
sessionId: bigint;
|
|
26
|
+
}>;
|
|
27
|
+
export declare function revokeSession(client: Client<paths>, signTransaction: (tx: Transaction) => Promise<Transaction>, currentTimestamp: bigint, nonce: number, params: {
|
|
28
|
+
sessionId: BigIntValue;
|
|
29
|
+
userPubkey: PublicKey;
|
|
30
|
+
}): Promise<{
|
|
31
|
+
actionId: bigint;
|
|
32
|
+
}>;
|
|
33
|
+
export type AtomicSubaction = {
|
|
34
|
+
kind: "place";
|
|
35
|
+
marketId: number;
|
|
36
|
+
side: Side;
|
|
37
|
+
fillMode: FillMode;
|
|
38
|
+
isReduceOnly: boolean;
|
|
39
|
+
sizeDecimals: number;
|
|
40
|
+
priceDecimals: number;
|
|
41
|
+
size?: Decimal.Value;
|
|
42
|
+
price?: Decimal.Value;
|
|
43
|
+
quoteSize?: QuoteSize;
|
|
44
|
+
clientOrderId?: BigIntValue;
|
|
45
|
+
} | {
|
|
46
|
+
kind: "cancel";
|
|
47
|
+
orderId: BigIntValue;
|
|
48
|
+
};
|
|
49
|
+
export declare function atomic(client: Client<paths>, signFn: (message: Uint8Array) => Promise<Uint8Array>, currentTimestamp: bigint, nonce: number, params: {
|
|
50
|
+
sessionId: BigIntValue;
|
|
51
|
+
accountId?: number;
|
|
52
|
+
actions: AtomicSubaction[];
|
|
53
|
+
}): Promise<{
|
|
54
|
+
actionId: bigint;
|
|
55
|
+
results: proto.Receipt_AtomicSubactionResultKind[];
|
|
56
|
+
}>;
|
|
57
|
+
export {};
|
package/dist/actions.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
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) {
|
|
8
|
+
if (receipt.kind?.case === "err") {
|
|
9
|
+
const err = receipt.kind.value;
|
|
10
|
+
return proto.Error[err] ?? err.toString();
|
|
11
|
+
}
|
|
12
|
+
return receipt.kind?.case ?? "unknown";
|
|
13
|
+
}
|
|
14
|
+
export function expectReceiptKind(receipt, expected, action) {
|
|
15
|
+
if (receipt.kind?.case !== expected) {
|
|
16
|
+
const label = formatReceiptError(receipt);
|
|
17
|
+
throw new NordError(`Failed to ${action}: ${label}`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async function sessionSign(signFn, message) {
|
|
21
|
+
const signature = await signFn(message);
|
|
22
|
+
return new Uint8Array([...message, ...signature]);
|
|
23
|
+
}
|
|
24
|
+
// Helper to create an action with common fields
|
|
25
|
+
export function createAction(currentTimestamp, nonce, kind) {
|
|
26
|
+
return create(proto.ActionSchema, {
|
|
27
|
+
currentTimestamp,
|
|
28
|
+
nonce,
|
|
29
|
+
kind,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
export async function sendAction(client, makeSignedMessage, action) {
|
|
33
|
+
const body = await prepareAction(action, makeSignedMessage);
|
|
34
|
+
const response = await client.POST("/action", {
|
|
35
|
+
params: {
|
|
36
|
+
header: {
|
|
37
|
+
"content-type": "application/octet-stream",
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
body: body,
|
|
41
|
+
// NOTE: openapi-fetch ignores headers and types/const headers in schema, and always assume all things are JSON
|
|
42
|
+
// to handle multi type bodies, need these overrides and later adhoc parsing
|
|
43
|
+
bodySerializer: (body) => body,
|
|
44
|
+
parseAs: "stream",
|
|
45
|
+
});
|
|
46
|
+
if (response.error) {
|
|
47
|
+
throw new Error(`Failed to ${action.kind.case}, HTTP status ${JSON.stringify(response.error)}`);
|
|
48
|
+
}
|
|
49
|
+
const rawResp = new Uint8Array(await response.response.arrayBuffer());
|
|
50
|
+
const resp = decodeLengthDelimited(rawResp, proto.ReceiptSchema);
|
|
51
|
+
if (resp.kind?.case === "err") {
|
|
52
|
+
throw new Error(`Could not execute ${action.kind.case}, reason: ${proto.Error[resp.kind.value]}`);
|
|
53
|
+
}
|
|
54
|
+
return resp;
|
|
55
|
+
}
|
|
56
|
+
// Given action and signature function, prepare the signed message to send to server as `body`.
|
|
57
|
+
// `makeSignedMessage` must include the original message and signature.
|
|
58
|
+
export async function prepareAction(action, makeSignedMessage) {
|
|
59
|
+
const encoded = sizeDelimitedEncode(proto.ActionSchema, action);
|
|
60
|
+
// NOTE(agent): keep in sync with MAX_ENCODED_ACTION_SIZE in Rust code
|
|
61
|
+
const MAX_ENCODED_ACTION_SIZE = 1024;
|
|
62
|
+
if (encoded.byteLength > MAX_ENCODED_ACTION_SIZE) {
|
|
63
|
+
console.warn("Encoded message:", encoded);
|
|
64
|
+
throw new Error(`Encoded message size (${encoded.byteLength} bytes) is greater than max payload size (${MAX_ENCODED_ACTION_SIZE} bytes).`);
|
|
65
|
+
}
|
|
66
|
+
const body = await makeSignedMessage(encoded);
|
|
67
|
+
if (body.byteLength > MAX_ENCODED_ACTION_SIZE) {
|
|
68
|
+
console.warn("Encoded length:", encoded.byteLength);
|
|
69
|
+
throw new Error(`Signed message size (${body.byteLength} bytes) is greater than max payload size (${MAX_ENCODED_ACTION_SIZE} bytes).`);
|
|
70
|
+
}
|
|
71
|
+
return body;
|
|
72
|
+
}
|
|
73
|
+
export async function createSession(client, signTransaction, currentTimestamp, nonce, params) {
|
|
74
|
+
let expiry = 0n;
|
|
75
|
+
if (params.expiryTimestamp !== undefined) {
|
|
76
|
+
expiry = params.expiryTimestamp;
|
|
77
|
+
assert(expiry > currentTimestamp, "Cannot set expiry timestamp in the past");
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
expiry = currentTimestamp + SESSION_TTL;
|
|
81
|
+
}
|
|
82
|
+
const action = createAction(currentTimestamp, nonce, {
|
|
83
|
+
case: "createSession",
|
|
84
|
+
value: create(proto.Action_CreateSessionSchema, {
|
|
85
|
+
userPubkey: params.userPubkey.toBytes(),
|
|
86
|
+
blstPubkey: params.sessionPubkey.toBytes(),
|
|
87
|
+
expiryTimestamp: expiry,
|
|
88
|
+
}),
|
|
89
|
+
});
|
|
90
|
+
const resp = await sendAction(client, async (payload) => {
|
|
91
|
+
return new Uint8Array([
|
|
92
|
+
...payload,
|
|
93
|
+
...(await signUserPayload({
|
|
94
|
+
payload,
|
|
95
|
+
user: params.userPubkey,
|
|
96
|
+
signTransaction,
|
|
97
|
+
})),
|
|
98
|
+
]);
|
|
99
|
+
}, action);
|
|
100
|
+
if (resp.kind?.case === "createSessionResult") {
|
|
101
|
+
return {
|
|
102
|
+
actionId: resp.actionId,
|
|
103
|
+
sessionId: resp.kind.value.sessionId,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
export async function revokeSession(client, signTransaction, currentTimestamp, nonce, params) {
|
|
111
|
+
const action = createAction(currentTimestamp, nonce, {
|
|
112
|
+
case: "revokeSession",
|
|
113
|
+
value: create(proto.Action_RevokeSessionSchema, {
|
|
114
|
+
sessionId: BigInt(params.sessionId),
|
|
115
|
+
}),
|
|
116
|
+
});
|
|
117
|
+
const resp = await sendAction(client, async (payload) => {
|
|
118
|
+
return new Uint8Array([
|
|
119
|
+
...payload,
|
|
120
|
+
...(await signUserPayload({
|
|
121
|
+
payload,
|
|
122
|
+
user: params.userPubkey,
|
|
123
|
+
signTransaction,
|
|
124
|
+
})),
|
|
125
|
+
]);
|
|
126
|
+
}, action);
|
|
127
|
+
return { actionId: resp.actionId };
|
|
128
|
+
}
|
|
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");
|
|
131
|
+
const subactions = params.actions.map((a) => {
|
|
132
|
+
if (a.kind === "place") {
|
|
133
|
+
const price = toScaledU64(a.price ?? 0, a.priceDecimals);
|
|
134
|
+
const size = toScaledU64(a.size ?? 0, a.sizeDecimals);
|
|
135
|
+
const scaledQuote = a.quoteSize
|
|
136
|
+
? a.quoteSize.toWire(a.priceDecimals, a.sizeDecimals)
|
|
137
|
+
: undefined;
|
|
138
|
+
// Require at least one limit to be set (non-zero size, non-zero price, or quoteSize)
|
|
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, {
|
|
141
|
+
marketId: a.marketId,
|
|
142
|
+
orderType: create(proto.OrderTypeSchema, {
|
|
143
|
+
side: a.side === Side.Bid ? proto.Side.BID : proto.Side.ASK,
|
|
144
|
+
fillMode: fillModeToProtoFillMode(a.fillMode),
|
|
145
|
+
isReduceOnly: a.isReduceOnly,
|
|
146
|
+
}),
|
|
147
|
+
limit: create(proto.OrderLimitSchema, {
|
|
148
|
+
price,
|
|
149
|
+
size,
|
|
150
|
+
quoteSize: scaledQuote === undefined
|
|
151
|
+
? undefined
|
|
152
|
+
: create(proto.QuoteSizeSchema, {
|
|
153
|
+
size: scaledQuote.size,
|
|
154
|
+
price: scaledQuote.price,
|
|
155
|
+
}),
|
|
156
|
+
}),
|
|
157
|
+
clientOrderId: a.clientOrderId === undefined ? undefined : BigInt(a.clientOrderId),
|
|
158
|
+
});
|
|
159
|
+
return create(proto.AtomicSubactionKindSchema, {
|
|
160
|
+
inner: { case: "tradeOrPlace", value: tradeOrPlace },
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
return create(proto.AtomicSubactionKindSchema, {
|
|
164
|
+
inner: {
|
|
165
|
+
case: "cancelOrder",
|
|
166
|
+
value: create(proto.CancelOrderSchema, { orderId: BigInt(a.orderId) }),
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
const action = createAction(currentTimestamp, nonce, {
|
|
171
|
+
case: "atomic",
|
|
172
|
+
value: create(proto.AtomicSchema, {
|
|
173
|
+
sessionId: BigInt(params.sessionId),
|
|
174
|
+
accountId: params.accountId, // optional
|
|
175
|
+
actions: subactions,
|
|
176
|
+
}),
|
|
177
|
+
});
|
|
178
|
+
const resp = await sendAction(client, (m) => sessionSign(signFn, m), action);
|
|
179
|
+
if (resp.kind?.case === "atomic") {
|
|
180
|
+
return {
|
|
181
|
+
actionId: resp.actionId,
|
|
182
|
+
results: resp.kind.value.results,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
|
|
186
|
+
}
|