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