@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.
- package/dist/api/client.d.ts +14 -0
- package/dist/api/client.js +45 -0
- package/dist/bridge/client.d.ts +151 -0
- package/dist/bridge/client.js +434 -0
- package/dist/bridge/const.d.ts +23 -0
- package/dist/bridge/const.js +47 -0
- package/dist/bridge/index.d.ts +4 -0
- package/dist/bridge/index.js +23 -0
- package/dist/bridge/types.d.ts +120 -0
- package/dist/bridge/types.js +18 -0
- package/dist/bridge/utils.d.ts +64 -0
- package/dist/bridge/utils.js +131 -0
- package/dist/gen/common.d.ts +68 -0
- package/dist/gen/common.js +215 -0
- package/dist/gen/nord_pb.d.ts +3651 -0
- package/dist/gen/nord_pb.js +892 -0
- package/dist/gen/openapi.d.ts +241 -2
- package/dist/idl/bridge.d.ts +569 -0
- package/dist/idl/bridge.js +8 -0
- package/dist/idl/bridge.json +1506 -0
- package/dist/idl/index.d.ts +607 -0
- package/dist/idl/index.js +8 -0
- package/dist/nord/api/actions.d.ts +30 -72
- package/dist/nord/api/actions.js +179 -200
- package/dist/nord/api/market.d.ts +36 -0
- package/dist/nord/api/market.js +96 -0
- package/dist/nord/api/queries.d.ts +46 -0
- package/dist/nord/api/queries.js +109 -0
- package/dist/nord/client/Nord.js +3 -3
- package/dist/nord/client/NordUser.d.ts +26 -13
- package/dist/nord/client/NordUser.js +13 -10
- package/dist/types.d.ts +12 -1
- package/dist/types.js +29 -2
- package/dist/utils.d.ts +6 -20
- package/dist/utils.js +17 -35
- package/dist/websocket/NordWebSocketClient.js +2 -6
- package/package.json +26 -23
- package/src/gen/nord_pb.ts +4172 -0
- package/src/gen/openapi.ts +241 -2
- package/src/nord/api/actions.ts +249 -370
- package/src/nord/client/Nord.ts +3 -3
- package/src/nord/client/NordUser.ts +40 -19
- package/src/types.ts +32 -1
- package/src/utils.ts +24 -43
- package/src/websocket/NordWebSocketClient.ts +2 -8
package/src/nord/api/actions.ts
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
import Decimal from "decimal.js";
|
|
2
|
-
import * as proto from "../../gen/
|
|
3
|
-
import {
|
|
2
|
+
import * as proto from "../../gen/nord_pb";
|
|
3
|
+
import { create } from "@bufbuild/protobuf";
|
|
4
|
+
import {
|
|
5
|
+
FillMode,
|
|
6
|
+
fillModeToProtoFillMode,
|
|
7
|
+
KeyType,
|
|
8
|
+
Side,
|
|
9
|
+
QuoteSize,
|
|
10
|
+
} from "../../types";
|
|
4
11
|
import {
|
|
5
12
|
assert,
|
|
6
13
|
BigIntValue,
|
|
7
14
|
checkedFetch,
|
|
8
15
|
checkPubKeyLength,
|
|
9
16
|
decodeLengthDelimited,
|
|
10
|
-
encodeLengthDelimited,
|
|
11
17
|
SESSION_TTL,
|
|
12
18
|
toScaledU64,
|
|
13
19
|
} from "../../utils";
|
|
20
|
+
import { sizeDelimitedEncode } from "@bufbuild/protobuf/wire";
|
|
14
21
|
|
|
15
22
|
async function sessionSign(
|
|
16
23
|
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
@@ -28,44 +35,61 @@ async function walletSign(
|
|
|
28
35
|
return new Uint8Array([...message, ...signature]);
|
|
29
36
|
}
|
|
30
37
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
});
|
|
43
|
-
return new Uint8Array(await response.arrayBuffer());
|
|
44
|
-
};
|
|
38
|
+
// Helper to create an action with common fields
|
|
39
|
+
function createAction(
|
|
40
|
+
currentTimestamp: bigint,
|
|
41
|
+
nonce: number,
|
|
42
|
+
kind: proto.Action["kind"],
|
|
43
|
+
): proto.Action {
|
|
44
|
+
return create(proto.ActionSchema, {
|
|
45
|
+
currentTimestamp,
|
|
46
|
+
nonce,
|
|
47
|
+
kind,
|
|
48
|
+
});
|
|
45
49
|
}
|
|
46
50
|
|
|
47
51
|
async function sendAction(
|
|
48
|
-
|
|
52
|
+
serverUrl: string,
|
|
49
53
|
makeSignedMessage: (message: Uint8Array) => Promise<Uint8Array>,
|
|
50
54
|
action: proto.Action,
|
|
51
55
|
actionErrorDesc: string,
|
|
52
56
|
): Promise<proto.Receipt> {
|
|
53
|
-
const encoded =
|
|
57
|
+
const encoded = sizeDelimitedEncode(proto.ActionSchema, action);
|
|
58
|
+
// validate the payload size
|
|
59
|
+
const MAX_PAYLOAD_SIZE = 100 * 1024; // 100 kB
|
|
60
|
+
if (encoded.byteLength > MAX_PAYLOAD_SIZE) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
`Encoded message size (${encoded.byteLength} bytes) is greater than max payload size (${MAX_PAYLOAD_SIZE} bytes).`,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
54
65
|
const body = await makeSignedMessage(encoded);
|
|
55
|
-
const rawResp = await sendFn(body);
|
|
56
|
-
const resp: proto.Receipt = decodeLengthDelimited(rawResp, proto.Receipt);
|
|
57
66
|
|
|
58
|
-
|
|
67
|
+
// TODO: this should be changed to use openapi
|
|
68
|
+
const response = await checkedFetch(`${serverUrl}/action`, {
|
|
69
|
+
method: "POST",
|
|
70
|
+
headers: {
|
|
71
|
+
"Content-Type": "application/json",
|
|
72
|
+
},
|
|
73
|
+
body,
|
|
74
|
+
});
|
|
75
|
+
const rawResp = new Uint8Array(await response.arrayBuffer());
|
|
76
|
+
|
|
77
|
+
const resp: proto.Receipt = decodeLengthDelimited(
|
|
78
|
+
rawResp,
|
|
79
|
+
proto.ReceiptSchema,
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
if (resp.kind?.case === "err") {
|
|
59
83
|
throw new Error(
|
|
60
|
-
`Could not ${actionErrorDesc}, reason: ${proto.
|
|
84
|
+
`Could not ${actionErrorDesc}, reason: ${proto.Error[resp.kind.value]}`,
|
|
61
85
|
);
|
|
62
86
|
}
|
|
63
87
|
|
|
64
88
|
return resp;
|
|
65
89
|
}
|
|
66
90
|
|
|
67
|
-
async function
|
|
68
|
-
|
|
91
|
+
export async function createSession(
|
|
92
|
+
serverUrl: string,
|
|
69
93
|
walletSignFn: (message: string | Uint8Array) => Promise<Uint8Array>,
|
|
70
94
|
currentTimestamp: bigint,
|
|
71
95
|
nonce: number,
|
|
@@ -75,7 +99,7 @@ async function createSessionImpl(
|
|
|
75
99
|
// If not specified, set to current moment plus default session TTL
|
|
76
100
|
expiryTimestamp?: bigint;
|
|
77
101
|
},
|
|
78
|
-
): Promise<bigint> {
|
|
102
|
+
): Promise<{ actionId: bigint; sessionId: bigint }> {
|
|
79
103
|
checkPubKeyLength(KeyType.Ed25519, params.userPubkey.length);
|
|
80
104
|
checkPubKeyLength(KeyType.Ed25519, params.sessionPubkey.length);
|
|
81
105
|
|
|
@@ -91,102 +115,60 @@ async function createSessionImpl(
|
|
|
91
115
|
expiry = currentTimestamp + SESSION_TTL;
|
|
92
116
|
}
|
|
93
117
|
|
|
94
|
-
const action
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
expiryTimestamp: expiry,
|
|
103
|
-
},
|
|
104
|
-
},
|
|
105
|
-
};
|
|
118
|
+
const action = createAction(currentTimestamp, nonce, {
|
|
119
|
+
case: "createSession",
|
|
120
|
+
value: create(proto.Action_CreateSessionSchema, {
|
|
121
|
+
userPubkey: params.userPubkey,
|
|
122
|
+
blstPubkey: params.sessionPubkey,
|
|
123
|
+
expiryTimestamp: expiry,
|
|
124
|
+
}),
|
|
125
|
+
});
|
|
106
126
|
|
|
107
127
|
const resp = await sendAction(
|
|
108
|
-
|
|
128
|
+
serverUrl,
|
|
109
129
|
(m) => walletSign(walletSignFn, m),
|
|
110
130
|
action,
|
|
111
131
|
"create a new session",
|
|
112
132
|
);
|
|
113
133
|
|
|
114
|
-
if (resp.kind
|
|
115
|
-
return
|
|
134
|
+
if (resp.kind?.case === "createSessionResult") {
|
|
135
|
+
return {
|
|
136
|
+
actionId: resp.actionId,
|
|
137
|
+
sessionId: resp.kind.value.sessionId,
|
|
138
|
+
};
|
|
116
139
|
} else {
|
|
117
|
-
throw new Error(`Unexpected receipt kind ${resp.kind
|
|
140
|
+
throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
|
|
118
141
|
}
|
|
119
142
|
}
|
|
120
143
|
|
|
121
|
-
export async function
|
|
144
|
+
export async function revokeSession(
|
|
122
145
|
serverUrl: string,
|
|
123
146
|
walletSignFn: (message: string | Uint8Array) => Promise<Uint8Array>,
|
|
124
147
|
currentTimestamp: bigint,
|
|
125
148
|
nonce: number,
|
|
126
|
-
params: {
|
|
127
|
-
userPubkey: Uint8Array;
|
|
128
|
-
sessionPubkey: Uint8Array;
|
|
129
|
-
// If not specified, set to current moment plus default session TTL
|
|
130
|
-
expiryTimestamp?: bigint;
|
|
131
|
-
},
|
|
132
|
-
): Promise<bigint> {
|
|
133
|
-
return createSessionImpl(
|
|
134
|
-
makeSendHttp(serverUrl),
|
|
135
|
-
walletSignFn,
|
|
136
|
-
currentTimestamp,
|
|
137
|
-
nonce,
|
|
138
|
-
params,
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
async function revokeSessionImpl(
|
|
143
|
-
sendFn: (encoded: Uint8Array) => Promise<Uint8Array>,
|
|
144
|
-
walletSignFn: (message: string | Uint8Array) => Promise<Uint8Array>,
|
|
145
|
-
currentTimestamp: bigint,
|
|
146
|
-
nonce: number,
|
|
147
149
|
params: {
|
|
148
150
|
sessionId: BigIntValue;
|
|
149
151
|
},
|
|
150
|
-
): Promise<
|
|
151
|
-
const action
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
sessionId: BigInt(params.sessionId),
|
|
158
|
-
},
|
|
159
|
-
},
|
|
160
|
-
};
|
|
152
|
+
): Promise<{ actionId: bigint }> {
|
|
153
|
+
const action = createAction(currentTimestamp, nonce, {
|
|
154
|
+
case: "revokeSession",
|
|
155
|
+
value: create(proto.Action_RevokeSessionSchema, {
|
|
156
|
+
sessionId: BigInt(params.sessionId),
|
|
157
|
+
}),
|
|
158
|
+
});
|
|
161
159
|
|
|
162
|
-
await sendAction(
|
|
163
|
-
|
|
160
|
+
const resp = await sendAction(
|
|
161
|
+
serverUrl,
|
|
164
162
|
(m) => walletSign(walletSignFn, m),
|
|
165
163
|
action,
|
|
166
|
-
"
|
|
164
|
+
"revoke session",
|
|
167
165
|
);
|
|
168
|
-
}
|
|
169
166
|
|
|
170
|
-
|
|
171
|
-
serverUrl: string,
|
|
172
|
-
walletSignFn: (message: string | Uint8Array) => Promise<Uint8Array>,
|
|
173
|
-
currentTimestamp: bigint,
|
|
174
|
-
nonce: number,
|
|
175
|
-
params: {
|
|
176
|
-
sessionId: BigIntValue;
|
|
177
|
-
},
|
|
178
|
-
): Promise<void> {
|
|
179
|
-
return revokeSessionImpl(
|
|
180
|
-
makeSendHttp(serverUrl),
|
|
181
|
-
walletSignFn,
|
|
182
|
-
currentTimestamp,
|
|
183
|
-
nonce,
|
|
184
|
-
params,
|
|
185
|
-
);
|
|
167
|
+
return { actionId: resp.actionId };
|
|
186
168
|
}
|
|
187
169
|
|
|
188
|
-
async function
|
|
189
|
-
|
|
170
|
+
export async function withdraw(
|
|
171
|
+
serverUrl: string,
|
|
190
172
|
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
191
173
|
currentTimestamp: bigint,
|
|
192
174
|
nonce: number,
|
|
@@ -203,63 +185,37 @@ async function withdrawImpl(
|
|
|
203
185
|
throw new Error("Withdraw amount must be positive");
|
|
204
186
|
}
|
|
205
187
|
|
|
206
|
-
const action
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
amount,
|
|
215
|
-
},
|
|
216
|
-
},
|
|
217
|
-
};
|
|
188
|
+
const action = createAction(currentTimestamp, nonce, {
|
|
189
|
+
case: "withdraw",
|
|
190
|
+
value: create(proto.Action_WithdrawSchema, {
|
|
191
|
+
sessionId: BigInt(params.sessionId),
|
|
192
|
+
tokenId: params.tokenId,
|
|
193
|
+
amount,
|
|
194
|
+
}),
|
|
195
|
+
});
|
|
218
196
|
|
|
219
197
|
const resp = await sendAction(
|
|
220
|
-
|
|
198
|
+
serverUrl,
|
|
221
199
|
(m) => sessionSign(signFn, m),
|
|
222
200
|
action,
|
|
223
201
|
"withdraw",
|
|
224
202
|
);
|
|
225
203
|
|
|
226
|
-
if (resp.kind
|
|
204
|
+
if (resp.kind?.case === "withdrawResult") {
|
|
227
205
|
return { actionId: resp.actionId, ...resp.kind.value };
|
|
228
206
|
} else {
|
|
229
|
-
throw new Error(`Unexpected receipt kind ${resp.kind
|
|
207
|
+
throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
|
|
230
208
|
}
|
|
231
209
|
}
|
|
232
210
|
|
|
233
|
-
export async function
|
|
211
|
+
export async function placeOrder(
|
|
234
212
|
serverUrl: string,
|
|
235
213
|
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
236
214
|
currentTimestamp: bigint,
|
|
237
215
|
nonce: number,
|
|
238
|
-
params: {
|
|
239
|
-
sizeDecimals: number;
|
|
240
|
-
sessionId: BigIntValue;
|
|
241
|
-
tokenId: number;
|
|
242
|
-
amount: number;
|
|
243
|
-
},
|
|
244
|
-
): Promise<{ actionId: bigint } & proto.Receipt_WithdrawResult> {
|
|
245
|
-
return withdrawImpl(
|
|
246
|
-
makeSendHttp(serverUrl),
|
|
247
|
-
signFn,
|
|
248
|
-
currentTimestamp,
|
|
249
|
-
nonce,
|
|
250
|
-
params,
|
|
251
|
-
);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
async function placeOrderImpl(
|
|
255
|
-
sendFn: (encoded: Uint8Array) => Promise<Uint8Array>,
|
|
256
|
-
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
257
|
-
currentTimestamp: bigint,
|
|
258
|
-
nonce: number,
|
|
259
216
|
params: {
|
|
260
217
|
sessionId: BigIntValue;
|
|
261
218
|
senderId?: number;
|
|
262
|
-
liquidateeId?: number;
|
|
263
219
|
sizeDecimals: number;
|
|
264
220
|
priceDecimals: number;
|
|
265
221
|
marketId: number;
|
|
@@ -269,154 +225,115 @@ async function placeOrderImpl(
|
|
|
269
225
|
// NOTE: if `size` equals 1.0, it will sell whole unit, for example 1.0 BTC
|
|
270
226
|
size?: Decimal.Value;
|
|
271
227
|
price?: Decimal.Value;
|
|
272
|
-
|
|
273
|
-
|
|
228
|
+
quoteSize?: QuoteSize;
|
|
229
|
+
liquidateeId?: number;
|
|
274
230
|
clientOrderId?: BigIntValue;
|
|
275
231
|
},
|
|
276
|
-
): Promise<
|
|
232
|
+
): Promise<{
|
|
233
|
+
actionId: bigint;
|
|
234
|
+
orderId?: bigint;
|
|
235
|
+
fills: proto.Receipt_Trade[];
|
|
236
|
+
}> {
|
|
277
237
|
const price = toScaledU64(params.price ?? 0, params.priceDecimals);
|
|
278
238
|
const size = toScaledU64(params.size ?? 0, params.sizeDecimals);
|
|
279
|
-
const quoteSize = toScaledU64(params.quoteSizeSize ?? 0, params.sizeDecimals);
|
|
280
|
-
const quotePrice = toScaledU64(
|
|
281
|
-
params.quoteSizePrice ?? 0,
|
|
282
|
-
params.priceDecimals,
|
|
283
|
-
);
|
|
284
239
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
240
|
+
const scaledQuote = params.quoteSize
|
|
241
|
+
? params.quoteSize.toScaledU64(params.priceDecimals, params.sizeDecimals)
|
|
242
|
+
: undefined;
|
|
243
|
+
|
|
244
|
+
const action = createAction(currentTimestamp, nonce, {
|
|
245
|
+
case: "placeOrder",
|
|
246
|
+
value: create(proto.Action_PlaceOrderSchema, {
|
|
247
|
+
sessionId: BigInt(params.sessionId),
|
|
248
|
+
senderAccountId: params.senderId,
|
|
249
|
+
marketId: params.marketId,
|
|
250
|
+
side: params.side === Side.Bid ? proto.Side.BID : proto.Side.ASK,
|
|
251
|
+
fillMode: fillModeToProtoFillMode(params.fillMode),
|
|
252
|
+
isReduceOnly: params.isReduceOnly,
|
|
253
|
+
price,
|
|
254
|
+
size,
|
|
255
|
+
quoteSize:
|
|
256
|
+
scaledQuote === undefined
|
|
257
|
+
? undefined
|
|
258
|
+
: create(proto.QuoteSizeSchema, {
|
|
259
|
+
size: scaledQuote.size,
|
|
260
|
+
price: scaledQuote.price,
|
|
261
|
+
}),
|
|
262
|
+
clientOrderId:
|
|
263
|
+
params.clientOrderId === undefined
|
|
264
|
+
? undefined
|
|
265
|
+
: BigInt(params.clientOrderId),
|
|
266
|
+
delegatorAccountId: params.liquidateeId,
|
|
267
|
+
}),
|
|
268
|
+
});
|
|
309
269
|
|
|
310
270
|
const resp = await sendAction(
|
|
311
|
-
|
|
271
|
+
serverUrl,
|
|
312
272
|
(m) => sessionSign(signFn, m),
|
|
313
273
|
action,
|
|
314
|
-
"place
|
|
274
|
+
"place order",
|
|
315
275
|
);
|
|
316
276
|
|
|
317
|
-
if (resp.kind
|
|
318
|
-
return
|
|
277
|
+
if (resp.kind?.case === "placeOrderResult") {
|
|
278
|
+
return {
|
|
279
|
+
actionId: resp.actionId,
|
|
280
|
+
orderId: resp.kind.value.posted?.orderId,
|
|
281
|
+
fills: resp.kind.value.fills,
|
|
282
|
+
};
|
|
319
283
|
} else {
|
|
320
|
-
throw new Error(`Unexpected receipt kind ${resp.kind
|
|
284
|
+
throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
|
|
321
285
|
}
|
|
322
286
|
}
|
|
323
287
|
|
|
324
|
-
export async function
|
|
288
|
+
export async function cancelOrder(
|
|
325
289
|
serverUrl: string,
|
|
326
290
|
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
327
291
|
currentTimestamp: bigint,
|
|
328
292
|
nonce: number,
|
|
329
|
-
params: {
|
|
330
|
-
sessionId: BigIntValue;
|
|
331
|
-
senderId?: number;
|
|
332
|
-
sizeDecimals: number;
|
|
333
|
-
priceDecimals: number;
|
|
334
|
-
marketId: number;
|
|
335
|
-
side: Side;
|
|
336
|
-
fillMode: FillMode;
|
|
337
|
-
isReduceOnly: boolean;
|
|
338
|
-
size?: Decimal.Value;
|
|
339
|
-
price?: Decimal.Value;
|
|
340
|
-
quoteSize?: Decimal.Value;
|
|
341
|
-
liquidateeId?: number;
|
|
342
|
-
clientOrderId?: BigIntValue;
|
|
343
|
-
},
|
|
344
|
-
): Promise<bigint | undefined> {
|
|
345
|
-
return placeOrderImpl(
|
|
346
|
-
makeSendHttp(serverUrl),
|
|
347
|
-
signFn,
|
|
348
|
-
currentTimestamp,
|
|
349
|
-
nonce,
|
|
350
|
-
params,
|
|
351
|
-
);
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
async function cancelOrderImpl(
|
|
355
|
-
sendFn: (encoded: Uint8Array) => Promise<Uint8Array>,
|
|
356
|
-
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
357
|
-
currentTimestamp: bigint,
|
|
358
|
-
nonce: number,
|
|
359
293
|
params: {
|
|
360
294
|
sessionId: BigIntValue;
|
|
361
295
|
senderId?: number;
|
|
362
296
|
orderId: BigIntValue;
|
|
363
297
|
liquidateeId?: number;
|
|
364
298
|
},
|
|
365
|
-
): Promise<
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
},
|
|
378
|
-
};
|
|
299
|
+
): Promise<{
|
|
300
|
+
actionId: bigint;
|
|
301
|
+
orderId: bigint;
|
|
302
|
+
accountId: number;
|
|
303
|
+
}> {
|
|
304
|
+
const action = createAction(currentTimestamp, nonce, {
|
|
305
|
+
case: "cancelOrderById",
|
|
306
|
+
value: create(proto.Action_CancelOrderByIdSchema, {
|
|
307
|
+
orderId: BigInt(params.orderId),
|
|
308
|
+
sessionId: BigInt(params.sessionId),
|
|
309
|
+
senderAccountId: params.senderId,
|
|
310
|
+
delegatorAccountId: params.liquidateeId,
|
|
311
|
+
}),
|
|
312
|
+
});
|
|
379
313
|
|
|
380
314
|
const resp = await sendAction(
|
|
381
|
-
|
|
315
|
+
serverUrl,
|
|
382
316
|
(m) => sessionSign(signFn, m),
|
|
383
317
|
action,
|
|
384
|
-
"cancel
|
|
318
|
+
"cancel order",
|
|
385
319
|
);
|
|
386
320
|
|
|
387
|
-
if (resp.kind
|
|
388
|
-
return
|
|
321
|
+
if (resp.kind?.case === "cancelOrderResult") {
|
|
322
|
+
return {
|
|
323
|
+
actionId: resp.actionId,
|
|
324
|
+
orderId: resp.kind.value.orderId,
|
|
325
|
+
accountId: resp.kind.value.accountId,
|
|
326
|
+
};
|
|
389
327
|
} else {
|
|
390
|
-
throw new Error(`Unexpected receipt kind ${resp.kind
|
|
328
|
+
throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
|
|
391
329
|
}
|
|
392
330
|
}
|
|
393
331
|
|
|
394
|
-
export async function
|
|
332
|
+
export async function transfer(
|
|
395
333
|
serverUrl: string,
|
|
396
334
|
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
397
335
|
currentTimestamp: bigint,
|
|
398
336
|
nonce: number,
|
|
399
|
-
params: {
|
|
400
|
-
sessionId: BigIntValue;
|
|
401
|
-
senderId?: number;
|
|
402
|
-
orderId: BigIntValue;
|
|
403
|
-
liquidateeId?: number;
|
|
404
|
-
},
|
|
405
|
-
): Promise<bigint> {
|
|
406
|
-
return cancelOrderImpl(
|
|
407
|
-
makeSendHttp(serverUrl),
|
|
408
|
-
signFn,
|
|
409
|
-
currentTimestamp,
|
|
410
|
-
nonce,
|
|
411
|
-
params,
|
|
412
|
-
);
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
async function transferImpl(
|
|
416
|
-
sendFn: (encoded: Uint8Array) => Promise<Uint8Array>,
|
|
417
|
-
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
418
|
-
currentTimestamp: bigint,
|
|
419
|
-
nonce: number,
|
|
420
337
|
params: {
|
|
421
338
|
sessionId: BigIntValue;
|
|
422
339
|
fromAccountId: number;
|
|
@@ -425,63 +342,46 @@ async function transferImpl(
|
|
|
425
342
|
tokenDecimals: number;
|
|
426
343
|
amount: Decimal.Value;
|
|
427
344
|
},
|
|
428
|
-
): Promise<
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
345
|
+
): Promise<{
|
|
346
|
+
actionId: bigint;
|
|
347
|
+
fromAccountId: number;
|
|
348
|
+
toAccountId?: number;
|
|
349
|
+
tokenId: number;
|
|
350
|
+
amount: bigint;
|
|
351
|
+
accountCreated: boolean;
|
|
352
|
+
}> {
|
|
353
|
+
const action = createAction(currentTimestamp, nonce, {
|
|
354
|
+
case: "transfer",
|
|
355
|
+
value: create(proto.Action_TransferSchema, {
|
|
356
|
+
sessionId: BigInt(params.sessionId),
|
|
357
|
+
fromAccountId: params.fromAccountId,
|
|
358
|
+
toAccountId: params.toAccountId,
|
|
359
|
+
tokenId: params.tokenId,
|
|
360
|
+
amount: toScaledU64(params.amount ?? 0, params.tokenDecimals),
|
|
361
|
+
}),
|
|
362
|
+
});
|
|
443
363
|
|
|
444
364
|
const resp = await sendAction(
|
|
445
|
-
|
|
365
|
+
serverUrl,
|
|
446
366
|
(m) => sessionSign(signFn, m),
|
|
447
367
|
action,
|
|
448
|
-
"transfer
|
|
368
|
+
"transfer",
|
|
449
369
|
);
|
|
450
370
|
|
|
451
|
-
if (resp.kind
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
371
|
+
if (resp.kind?.case === "transferred") {
|
|
372
|
+
return {
|
|
373
|
+
actionId: resp.actionId,
|
|
374
|
+
fromAccountId: resp.kind.value.fromAccountId,
|
|
375
|
+
toAccountId: resp.kind.value.toUserAccount,
|
|
376
|
+
tokenId: resp.kind.value.tokenId,
|
|
377
|
+
amount: resp.kind.value.amount,
|
|
378
|
+
accountCreated: resp.kind.value.accountCreated,
|
|
379
|
+
};
|
|
457
380
|
} else {
|
|
458
|
-
throw new Error(`Unexpected receipt kind ${resp.kind
|
|
381
|
+
throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
|
|
459
382
|
}
|
|
460
383
|
}
|
|
461
384
|
|
|
462
|
-
export async function transfer(
|
|
463
|
-
serverUrl: string,
|
|
464
|
-
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
465
|
-
currentTimestamp: bigint,
|
|
466
|
-
nonce: number,
|
|
467
|
-
params: {
|
|
468
|
-
sessionId: BigIntValue;
|
|
469
|
-
fromAccountId: number;
|
|
470
|
-
toAccountId?: number;
|
|
471
|
-
tokenId: number;
|
|
472
|
-
tokenDecimals: number;
|
|
473
|
-
amount: Decimal.Value;
|
|
474
|
-
},
|
|
475
|
-
): Promise<number | undefined> {
|
|
476
|
-
return transferImpl(
|
|
477
|
-
makeSendHttp(serverUrl),
|
|
478
|
-
signFn,
|
|
479
|
-
currentTimestamp,
|
|
480
|
-
nonce,
|
|
481
|
-
params,
|
|
482
|
-
);
|
|
483
|
-
}
|
|
484
|
-
|
|
485
385
|
export type AtomicSubaction =
|
|
486
386
|
| {
|
|
487
387
|
kind: "place";
|
|
@@ -496,8 +396,7 @@ export type AtomicSubaction =
|
|
|
496
396
|
// at least one of the three has to be specified; 0 treated as "not set"
|
|
497
397
|
size?: Decimal.Value;
|
|
498
398
|
price?: Decimal.Value;
|
|
499
|
-
|
|
500
|
-
quoteSizePrice?: Decimal.Value;
|
|
399
|
+
quoteSize?: QuoteSize;
|
|
501
400
|
clientOrderId?: BigIntValue;
|
|
502
401
|
}
|
|
503
402
|
| {
|
|
@@ -505,8 +404,8 @@ export type AtomicSubaction =
|
|
|
505
404
|
orderId: BigIntValue;
|
|
506
405
|
};
|
|
507
406
|
|
|
508
|
-
async function
|
|
509
|
-
|
|
407
|
+
export async function atomic(
|
|
408
|
+
serverUrl: string,
|
|
510
409
|
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
511
410
|
currentTimestamp: bigint,
|
|
512
411
|
nonce: number,
|
|
@@ -515,7 +414,10 @@ async function atomicImpl(
|
|
|
515
414
|
accountId?: number;
|
|
516
415
|
actions: AtomicSubaction[];
|
|
517
416
|
},
|
|
518
|
-
): Promise<
|
|
417
|
+
): Promise<{
|
|
418
|
+
actionId: bigint;
|
|
419
|
+
results: proto.Receipt_AtomicSubactionResultKind[];
|
|
420
|
+
}> {
|
|
519
421
|
assert(
|
|
520
422
|
params.actions.length > 0 && params.actions.length <= 4,
|
|
521
423
|
"Atomic action must contain between 1 and 4 sub-actions",
|
|
@@ -525,89 +427,66 @@ async function atomicImpl(
|
|
|
525
427
|
if (a.kind === "place") {
|
|
526
428
|
const price = toScaledU64(a.price ?? 0, a.priceDecimals);
|
|
527
429
|
const size = toScaledU64(a.size ?? 0, a.sizeDecimals);
|
|
528
|
-
const
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
430
|
+
const scaledQuote = a.quoteSize
|
|
431
|
+
? a.quoteSize.toScaledU64(a.priceDecimals, a.sizeDecimals)
|
|
432
|
+
: undefined;
|
|
433
|
+
|
|
434
|
+
const tradeOrPlace: proto.TradeOrPlace = create(
|
|
435
|
+
proto.TradeOrPlaceSchema,
|
|
436
|
+
{
|
|
437
|
+
marketId: a.marketId,
|
|
438
|
+
orderType: create(proto.OrderTypeSchema, {
|
|
439
|
+
side: a.side === Side.Bid ? proto.Side.BID : proto.Side.ASK,
|
|
440
|
+
fillMode: fillModeToProtoFillMode(a.fillMode),
|
|
441
|
+
isReduceOnly: a.isReduceOnly,
|
|
442
|
+
}),
|
|
443
|
+
limit: create(proto.OrderLimitSchema, {
|
|
444
|
+
price,
|
|
445
|
+
size,
|
|
446
|
+
quoteSize:
|
|
447
|
+
scaledQuote === undefined
|
|
448
|
+
? undefined
|
|
449
|
+
: create(proto.QuoteSizeSchema, {
|
|
450
|
+
size: scaledQuote.size,
|
|
451
|
+
price: scaledQuote.price,
|
|
452
|
+
}),
|
|
453
|
+
}),
|
|
454
|
+
clientOrderId:
|
|
455
|
+
a.clientOrderId === undefined ? undefined : BigInt(a.clientOrderId),
|
|
544
456
|
},
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
inner: { $case: "tradeOrPlace", value: tradeOrPlace },
|
|
550
|
-
} as proto.AtomicSubactionKind;
|
|
457
|
+
);
|
|
458
|
+
return create(proto.AtomicSubactionKindSchema, {
|
|
459
|
+
inner: { case: "tradeOrPlace", value: tradeOrPlace },
|
|
460
|
+
});
|
|
551
461
|
}
|
|
552
|
-
return {
|
|
553
|
-
inner: {
|
|
554
|
-
|
|
462
|
+
return create(proto.AtomicSubactionKindSchema, {
|
|
463
|
+
inner: {
|
|
464
|
+
case: "cancelOrder",
|
|
465
|
+
value: create(proto.CancelOrderSchema, { orderId: BigInt(a.orderId) }),
|
|
466
|
+
},
|
|
467
|
+
});
|
|
555
468
|
});
|
|
556
469
|
|
|
557
|
-
const action
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
actions: subactions,
|
|
566
|
-
},
|
|
567
|
-
},
|
|
568
|
-
};
|
|
470
|
+
const action = createAction(currentTimestamp, nonce, {
|
|
471
|
+
case: "atomic",
|
|
472
|
+
value: create(proto.AtomicSchema, {
|
|
473
|
+
sessionId: BigInt(params.sessionId),
|
|
474
|
+
accountId: params.accountId, // optional
|
|
475
|
+
actions: subactions,
|
|
476
|
+
}),
|
|
477
|
+
});
|
|
569
478
|
|
|
570
479
|
const resp = await sendAction(
|
|
571
|
-
|
|
480
|
+
serverUrl,
|
|
572
481
|
(m) => sessionSign(signFn, m),
|
|
573
482
|
action,
|
|
574
483
|
"execute atomic action",
|
|
575
484
|
);
|
|
576
|
-
if (resp.kind
|
|
577
|
-
return
|
|
485
|
+
if (resp.kind?.case === "atomic") {
|
|
486
|
+
return {
|
|
487
|
+
actionId: resp.actionId,
|
|
488
|
+
results: resp.kind.value.results,
|
|
489
|
+
};
|
|
578
490
|
}
|
|
579
|
-
throw new Error(`Unexpected receipt kind ${resp.kind
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
export async function atomic(
|
|
583
|
-
serverUrl: string,
|
|
584
|
-
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
585
|
-
currentTimestamp: bigint,
|
|
586
|
-
nonce: number,
|
|
587
|
-
params: {
|
|
588
|
-
sessionId: BigIntValue;
|
|
589
|
-
accountId?: number;
|
|
590
|
-
actions: AtomicSubaction[];
|
|
591
|
-
},
|
|
592
|
-
): Promise<proto.Receipt_AtomicResult> {
|
|
593
|
-
return atomicImpl(
|
|
594
|
-
makeSendHttp(serverUrl),
|
|
595
|
-
signFn,
|
|
596
|
-
currentTimestamp,
|
|
597
|
-
nonce,
|
|
598
|
-
params,
|
|
599
|
-
);
|
|
491
|
+
throw new Error(`Unexpected receipt kind ${resp.kind?.case}`);
|
|
600
492
|
}
|
|
601
|
-
|
|
602
|
-
/**
|
|
603
|
-
* For testing purposes
|
|
604
|
-
*/
|
|
605
|
-
export const _private = {
|
|
606
|
-
createSessionImpl,
|
|
607
|
-
revokeSessionImpl,
|
|
608
|
-
withdrawImpl,
|
|
609
|
-
placeOrderImpl,
|
|
610
|
-
cancelOrderImpl,
|
|
611
|
-
transferImpl,
|
|
612
|
-
atomicImpl,
|
|
613
|
-
};
|