@n1xyz/nord-ts 0.0.1
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/.eslintignore +1 -0
- package/.eslintrc.js +20 -0
- package/.prettierignore +1 -0
- package/README.md +110 -0
- package/dist/abis/ERC20_ABI.d.ts +39 -0
- package/dist/abis/ERC20_ABI.js +313 -0
- package/dist/abis/NORD_GETTERS_FACET_ABI.d.ts +34 -0
- package/dist/abis/NORD_GETTERS_FACET_ABI.js +195 -0
- package/dist/abis/NORD_RAMP_FACET_ABI.d.ts +35 -0
- package/dist/abis/NORD_RAMP_FACET_ABI.js +144 -0
- package/dist/abis/index.d.ts +3 -0
- package/dist/abis/index.js +9 -0
- package/dist/const.d.ts +11 -0
- package/dist/const.js +34 -0
- package/dist/gen/common.d.ts +63 -0
- package/dist/gen/common.js +205 -0
- package/dist/gen/nord.d.ts +705 -0
- package/dist/gen/nord.js +4784 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +21 -0
- package/dist/nord/Nord.d.ts +76 -0
- package/dist/nord/Nord.js +376 -0
- package/dist/nord/NordImpl.d.ts +7 -0
- package/dist/nord/NordImpl.js +6 -0
- package/dist/nord/NordUser.d.ts +77 -0
- package/dist/nord/NordUser.js +249 -0
- package/dist/nord/actions.d.ts +101 -0
- package/dist/nord/actions.js +254 -0
- package/dist/nord/index.d.ts +2 -0
- package/dist/nord/index.js +9 -0
- package/dist/types.d.ts +343 -0
- package/dist/types.js +92 -0
- package/dist/utils.d.ts +114 -0
- package/dist/utils.js +257 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +92 -0
- package/docs/assets/icons.js +15 -0
- package/docs/assets/icons.svg +1 -0
- package/docs/assets/main.js +59 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1415 -0
- package/docs/classes/Nord.html +44 -0
- package/docs/classes/NordUser.html +35 -0
- package/docs/classes/Subscriber.html +6 -0
- package/docs/enums/FillMode.html +5 -0
- package/docs/enums/KeyType.html +4 -0
- package/docs/enums/PeakTpsPeriodUnit.html +15 -0
- package/docs/enums/Side.html +3 -0
- package/docs/functions/assert.html +1 -0
- package/docs/functions/bigIntToProtoU128.html +4 -0
- package/docs/functions/checkPubKeyLength.html +1 -0
- package/docs/functions/checkedFetch.html +6 -0
- package/docs/functions/createWebSocketSubscription.html +12 -0
- package/docs/functions/decodeLengthDelimited.html +10 -0
- package/docs/functions/encodeLengthDelimited.html +6 -0
- package/docs/functions/fillModeToProtoFillMode.html +5 -0
- package/docs/functions/findMarket.html +1 -0
- package/docs/functions/findToken.html +1 -0
- package/docs/functions/makeWalletSignFn.html +6 -0
- package/docs/functions/optExpect.html +5 -0
- package/docs/functions/optMap.html +5 -0
- package/docs/functions/optUnwrap.html +2 -0
- package/docs/functions/panic.html +1 -0
- package/docs/functions/signAction.html +6 -0
- package/docs/functions/toScaledU128.html +8 -0
- package/docs/functions/toScaledU64.html +8 -0
- package/docs/index.html +21 -0
- package/docs/interfaces/Account.html +8 -0
- package/docs/interfaces/ActionInfo.html +8 -0
- package/docs/interfaces/ActionQuery.html +4 -0
- package/docs/interfaces/ActionResponse.html +8 -0
- package/docs/interfaces/ActionsExtendedInfo.html +10 -0
- package/docs/interfaces/ActionsQuery.html +5 -0
- package/docs/interfaces/ActionsResponse.html +6 -0
- package/docs/interfaces/AggregateMetrics.html +12 -0
- package/docs/interfaces/BlockQuery.html +6 -0
- package/docs/interfaces/BlockResponse.html +6 -0
- package/docs/interfaces/BlockSummary.html +8 -0
- package/docs/interfaces/BlockSummaryResponse.html +6 -0
- package/docs/interfaces/DeltaEvent.html +6 -0
- package/docs/interfaces/ERC20TokenInfo.html +5 -0
- package/docs/interfaces/Info.html +3 -0
- package/docs/interfaces/Market.html +6 -0
- package/docs/interfaces/MarketStats.html +7 -0
- package/docs/interfaces/MarketsStatsResponse.html +2 -0
- package/docs/interfaces/NordConfig.html +5 -0
- package/docs/interfaces/Order.html +6 -0
- package/docs/interfaces/OrderInfo.html +6 -0
- package/docs/interfaces/OrderbookOrder.html +6 -0
- package/docs/interfaces/OrderbookResponse.html +10 -0
- package/docs/interfaces/PerpMarketStats.html +5 -0
- package/docs/interfaces/RollmanActionExtendedInfo.html +4 -0
- package/docs/interfaces/RollmanActionInfo.html +4 -0
- package/docs/interfaces/RollmanActionResponse.html +4 -0
- package/docs/interfaces/RollmanActionsResponse.html +2 -0
- package/docs/interfaces/RollmanBlockResponse.html +3 -0
- package/docs/interfaces/SubscriberConfig.html +3 -0
- package/docs/interfaces/Token.html +5 -0
- package/docs/interfaces/Trade.html +5 -0
- package/docs/interfaces/TradeInfo.html +20 -0
- package/docs/interfaces/Trades.html +5 -0
- package/docs/interfaces/TradesQueryParams.html +10 -0
- package/docs/interfaces/TradesResponse.html +12 -0
- package/docs/modules.html +77 -0
- package/docs/types/BigIntValue.html +2 -0
- package/docs/variables/DEBUG_KEYS.html +1 -0
- package/docs/variables/DEFAULT_FUNDING_AMOUNTS.html +1 -0
- package/docs/variables/DEV_CONTRACT_ADDRESS.html +1 -0
- package/docs/variables/DEV_TOKEN_INFOS.html +1 -0
- package/docs/variables/DEV_URL.html +1 -0
- package/docs/variables/ERC20_ABI.html +1 -0
- package/docs/variables/EVM_DEV_URL.html +1 -0
- package/docs/variables/FAUCET_PRIVATE_ADDRESS.html +1 -0
- package/docs/variables/MAX_BUFFER_LEN.html +1 -0
- package/docs/variables/NORD_GETTERS_FACET_ABI.html +1 -0
- package/docs/variables/NORD_RAMP_FACET_ABI.html +1 -0
- package/docs/variables/SESSION_TTL.html +1 -0
- package/docs/variables/WEBSERVER_DEV_URL.html +1 -0
- package/docs/variables/ZERO_DECIMAL.html +1 -0
- package/jest.config.ts +9 -0
- package/nodemon.json +4 -0
- package/package.json +61 -0
- package/protoc-generate.sh +23 -0
- package/src/abis/ERC20_ABI.ts +310 -0
- package/src/abis/NORD_GETTERS_FACET_ABI.ts +192 -0
- package/src/abis/NORD_RAMP_FACET_ABI.ts +141 -0
- package/src/abis/index.ts +3 -0
- package/src/const.ts +39 -0
- package/src/gen/common.ts +280 -0
- package/src/gen/nord.ts +5666 -0
- package/src/index.ts +5 -0
- package/src/nord/Nord.ts +504 -0
- package/src/nord/NordImpl.ts +8 -0
- package/src/nord/NordUser.ts +469 -0
- package/src/nord/actions.ts +484 -0
- package/src/nord/index.ts +2 -0
- package/src/types.ts +393 -0
- package/src/utils.ts +300 -0
- package/tests/utils.spec.ts +154 -0
- package/tsconfig.json +24 -0
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
import { ethers } from "ethers";
|
|
2
|
+
import { FillMode, fillModeToProtoFillMode, KeyType, Side } from "../types";
|
|
3
|
+
import {
|
|
4
|
+
assert,
|
|
5
|
+
bigIntToProtoU128,
|
|
6
|
+
BigIntValue,
|
|
7
|
+
checkedFetch,
|
|
8
|
+
checkPubKeyLength,
|
|
9
|
+
decodeLengthDelimited,
|
|
10
|
+
encodeLengthDelimited,
|
|
11
|
+
optMap,
|
|
12
|
+
SESSION_TTL,
|
|
13
|
+
toScaledU128,
|
|
14
|
+
toScaledU64,
|
|
15
|
+
} from "../utils";
|
|
16
|
+
import * as proto from "../gen/nord";
|
|
17
|
+
import Decimal from "decimal.js";
|
|
18
|
+
|
|
19
|
+
async function sessionSign(
|
|
20
|
+
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
21
|
+
message: Uint8Array,
|
|
22
|
+
): Promise<Uint8Array> {
|
|
23
|
+
const signature = await signFn(message);
|
|
24
|
+
return new Uint8Array([...message, ...signature]);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function walletSign(
|
|
28
|
+
walletSignFn: (message: string | Uint8Array) => Promise<string>,
|
|
29
|
+
message: Uint8Array,
|
|
30
|
+
): Promise<Uint8Array> {
|
|
31
|
+
const signature = await walletSignFn(message);
|
|
32
|
+
return new Uint8Array([
|
|
33
|
+
...message,
|
|
34
|
+
...ethers.getBytes(signature.slice(0, -2)),
|
|
35
|
+
]);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function makeSendHttp(
|
|
39
|
+
serverUrl: string,
|
|
40
|
+
): (encoded: Uint8Array) => Promise<Uint8Array> {
|
|
41
|
+
return async (body) => {
|
|
42
|
+
const response = await checkedFetch(`${serverUrl}/action`, {
|
|
43
|
+
method: "POST",
|
|
44
|
+
headers: {
|
|
45
|
+
"Content-Type": "application/json",
|
|
46
|
+
},
|
|
47
|
+
body,
|
|
48
|
+
});
|
|
49
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function sendAction(
|
|
54
|
+
sendFn: (encoded: Uint8Array) => Promise<Uint8Array>,
|
|
55
|
+
makeSignedMessage: (message: Uint8Array) => Promise<Uint8Array>,
|
|
56
|
+
action: proto.Action,
|
|
57
|
+
actionErrorDesc: string,
|
|
58
|
+
): Promise<proto.Receipt> {
|
|
59
|
+
const encoded = encodeLengthDelimited(action, proto.Action);
|
|
60
|
+
const body = await makeSignedMessage(encoded);
|
|
61
|
+
const rawResp = await sendFn(body);
|
|
62
|
+
const resp: proto.Receipt = decodeLengthDelimited(rawResp, proto.Receipt);
|
|
63
|
+
|
|
64
|
+
if (resp.kind?.$case === "err") {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`Could not ${actionErrorDesc}, reason: ${proto.errorToJSON(resp.kind.value)}`,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return resp;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function createSessionImpl(
|
|
74
|
+
sendFn: (encoded: Uint8Array) => Promise<Uint8Array>,
|
|
75
|
+
walletSignFn: (message: string | Uint8Array) => Promise<string>,
|
|
76
|
+
currentTimestamp: bigint,
|
|
77
|
+
nonce: number,
|
|
78
|
+
params: {
|
|
79
|
+
userPubkey: Uint8Array;
|
|
80
|
+
sessionPubkey: Uint8Array;
|
|
81
|
+
// If not specified, set to current moment plus default session TTL
|
|
82
|
+
expiryTimestamp?: bigint;
|
|
83
|
+
},
|
|
84
|
+
): Promise<bigint> {
|
|
85
|
+
checkPubKeyLength(KeyType.Secp256k1, params.userPubkey.length);
|
|
86
|
+
checkPubKeyLength(KeyType.Ed25519, params.sessionPubkey.length);
|
|
87
|
+
|
|
88
|
+
let expiry = 0n;
|
|
89
|
+
|
|
90
|
+
if (params.expiryTimestamp !== undefined) {
|
|
91
|
+
expiry = params.expiryTimestamp;
|
|
92
|
+
assert(
|
|
93
|
+
expiry > currentTimestamp,
|
|
94
|
+
"Cannot set expiry timestamp in the past",
|
|
95
|
+
);
|
|
96
|
+
} else {
|
|
97
|
+
expiry = currentTimestamp + SESSION_TTL;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const action: proto.Action = {
|
|
101
|
+
currentTimestamp,
|
|
102
|
+
nonce,
|
|
103
|
+
kind: {
|
|
104
|
+
$case: "createSession",
|
|
105
|
+
value: {
|
|
106
|
+
userPubkey: params.userPubkey,
|
|
107
|
+
blstPubkey: params.sessionPubkey,
|
|
108
|
+
expiryTimestamp: expiry,
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const resp = await sendAction(
|
|
114
|
+
sendFn,
|
|
115
|
+
(m) => walletSign(walletSignFn, m),
|
|
116
|
+
action,
|
|
117
|
+
"create a new session",
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
if (resp.kind?.$case === "createSessionResult") {
|
|
121
|
+
return resp.kind.value.sessionId;
|
|
122
|
+
} else {
|
|
123
|
+
throw new Error(`Unexpected receipt kind ${resp.kind?.$case}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export async function createSession(
|
|
128
|
+
serverUrl: string,
|
|
129
|
+
walletSignFn: (message: string | Uint8Array) => Promise<string>,
|
|
130
|
+
currentTimestamp: bigint,
|
|
131
|
+
nonce: number,
|
|
132
|
+
params: {
|
|
133
|
+
userPubkey: Uint8Array;
|
|
134
|
+
sessionPubkey: Uint8Array;
|
|
135
|
+
// If not specified, set to current moment plus default session TTL
|
|
136
|
+
expiryTimestamp?: bigint;
|
|
137
|
+
},
|
|
138
|
+
): Promise<bigint> {
|
|
139
|
+
return createSessionImpl(
|
|
140
|
+
makeSendHttp(serverUrl),
|
|
141
|
+
walletSignFn,
|
|
142
|
+
currentTimestamp,
|
|
143
|
+
nonce,
|
|
144
|
+
params,
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async function revokeSessionImpl(
|
|
149
|
+
sendFn: (encoded: Uint8Array) => Promise<Uint8Array>,
|
|
150
|
+
walletSignFn: (message: string | Uint8Array) => Promise<string>,
|
|
151
|
+
currentTimestamp: bigint,
|
|
152
|
+
nonce: number,
|
|
153
|
+
params: {
|
|
154
|
+
sessionId: BigIntValue;
|
|
155
|
+
},
|
|
156
|
+
): Promise<void> {
|
|
157
|
+
const action: proto.Action = {
|
|
158
|
+
currentTimestamp,
|
|
159
|
+
nonce,
|
|
160
|
+
kind: {
|
|
161
|
+
$case: "revokeSession",
|
|
162
|
+
value: {
|
|
163
|
+
sessionId: BigInt(params.sessionId),
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
await sendAction(
|
|
169
|
+
sendFn,
|
|
170
|
+
(m) => walletSign(walletSignFn, m),
|
|
171
|
+
action,
|
|
172
|
+
"create a new session",
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export async function revokeSession(
|
|
177
|
+
serverUrl: string,
|
|
178
|
+
walletSignFn: (message: string | Uint8Array) => Promise<string>,
|
|
179
|
+
currentTimestamp: bigint,
|
|
180
|
+
nonce: number,
|
|
181
|
+
params: {
|
|
182
|
+
sessionId: BigIntValue;
|
|
183
|
+
},
|
|
184
|
+
): Promise<void> {
|
|
185
|
+
return revokeSessionImpl(
|
|
186
|
+
makeSendHttp(serverUrl),
|
|
187
|
+
walletSignFn,
|
|
188
|
+
currentTimestamp,
|
|
189
|
+
nonce,
|
|
190
|
+
params,
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function withdrawImpl(
|
|
195
|
+
sendFn: (encoded: Uint8Array) => Promise<Uint8Array>,
|
|
196
|
+
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
197
|
+
currentTimestamp: bigint,
|
|
198
|
+
nonce: number,
|
|
199
|
+
params: {
|
|
200
|
+
sizeDecimals: number;
|
|
201
|
+
sessionId: BigIntValue;
|
|
202
|
+
tokenId: number;
|
|
203
|
+
amount: Decimal.Value;
|
|
204
|
+
},
|
|
205
|
+
): Promise<void> {
|
|
206
|
+
const amount = toScaledU64(params.amount, params.sizeDecimals);
|
|
207
|
+
|
|
208
|
+
if (amount <= 0) {
|
|
209
|
+
throw new Error("Withdraw amount must be positive");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const action: proto.Action = {
|
|
213
|
+
currentTimestamp,
|
|
214
|
+
nonce,
|
|
215
|
+
kind: {
|
|
216
|
+
$case: "withdraw",
|
|
217
|
+
value: {
|
|
218
|
+
sessionId: BigInt(params.sessionId),
|
|
219
|
+
tokenId: params.tokenId,
|
|
220
|
+
amount,
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
await sendAction(sendFn, (m) => sessionSign(signFn, m), action, "withdraw");
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export async function withdraw(
|
|
229
|
+
serverUrl: string,
|
|
230
|
+
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
231
|
+
currentTimestamp: bigint,
|
|
232
|
+
nonce: number,
|
|
233
|
+
params: {
|
|
234
|
+
sizeDecimals: number;
|
|
235
|
+
sessionId: BigIntValue;
|
|
236
|
+
tokenId: number;
|
|
237
|
+
amount: number;
|
|
238
|
+
},
|
|
239
|
+
): Promise<void> {
|
|
240
|
+
return withdrawImpl(
|
|
241
|
+
makeSendHttp(serverUrl),
|
|
242
|
+
signFn,
|
|
243
|
+
currentTimestamp,
|
|
244
|
+
nonce,
|
|
245
|
+
params,
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async function placeOrderImpl(
|
|
250
|
+
sendFn: (encoded: Uint8Array) => Promise<Uint8Array>,
|
|
251
|
+
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
252
|
+
currentTimestamp: bigint,
|
|
253
|
+
nonce: number,
|
|
254
|
+
params: {
|
|
255
|
+
sessionId: BigIntValue;
|
|
256
|
+
senderId?: number;
|
|
257
|
+
liquidateeId?: number;
|
|
258
|
+
sizeDecimals: number;
|
|
259
|
+
priceDecimals: number;
|
|
260
|
+
marketId: number;
|
|
261
|
+
side: Side;
|
|
262
|
+
fillMode: FillMode;
|
|
263
|
+
isReduceOnly: boolean;
|
|
264
|
+
// NOTE: if `size` equals 1.0, it will sell whole unit, for example 1.0 BTC
|
|
265
|
+
size?: Decimal.Value;
|
|
266
|
+
price?: Decimal.Value;
|
|
267
|
+
quoteSize?: Decimal.Value;
|
|
268
|
+
clientOrderId?: BigIntValue;
|
|
269
|
+
},
|
|
270
|
+
): Promise<bigint | undefined> {
|
|
271
|
+
const price = toScaledU64(params.price ?? 0, params.priceDecimals);
|
|
272
|
+
const size = toScaledU64(params.size ?? 0, params.sizeDecimals);
|
|
273
|
+
const quoteSize = toScaledU128(
|
|
274
|
+
params.quoteSize ?? 0,
|
|
275
|
+
params.priceDecimals + params.sizeDecimals,
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
// Compose action object
|
|
279
|
+
const action: proto.Action = {
|
|
280
|
+
currentTimestamp,
|
|
281
|
+
nonce,
|
|
282
|
+
kind: {
|
|
283
|
+
$case: "placeOrder",
|
|
284
|
+
value: {
|
|
285
|
+
sessionId: BigInt(params.sessionId),
|
|
286
|
+
senderAccountId: params.senderId,
|
|
287
|
+
marketId: params.marketId,
|
|
288
|
+
side: params.side === Side.Bid ? proto.Side.BID : proto.Side.ASK,
|
|
289
|
+
fillMode: fillModeToProtoFillMode(params.fillMode),
|
|
290
|
+
isReduceOnly: params.isReduceOnly,
|
|
291
|
+
price,
|
|
292
|
+
size,
|
|
293
|
+
quoteSize: bigIntToProtoU128(quoteSize),
|
|
294
|
+
clientOrderId: optMap(params.clientOrderId, (x) => BigInt(x)),
|
|
295
|
+
delegatorAccountId: params.liquidateeId,
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
const resp = await sendAction(
|
|
301
|
+
sendFn,
|
|
302
|
+
(m) => sessionSign(signFn, m),
|
|
303
|
+
action,
|
|
304
|
+
"place the order",
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
if (resp.kind?.$case === "placeOrderResult") {
|
|
308
|
+
return resp.kind.value.posted?.orderId;
|
|
309
|
+
} else {
|
|
310
|
+
throw new Error(`Unexpected receipt kind ${resp.kind?.$case}`);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export async function placeOrder(
|
|
315
|
+
serverUrl: string,
|
|
316
|
+
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
317
|
+
currentTimestamp: bigint,
|
|
318
|
+
nonce: number,
|
|
319
|
+
params: {
|
|
320
|
+
sessionId: BigIntValue;
|
|
321
|
+
senderId?: number;
|
|
322
|
+
sizeDecimals: number;
|
|
323
|
+
priceDecimals: number;
|
|
324
|
+
marketId: number;
|
|
325
|
+
side: Side;
|
|
326
|
+
fillMode: FillMode;
|
|
327
|
+
isReduceOnly: boolean;
|
|
328
|
+
size?: Decimal.Value;
|
|
329
|
+
price?: Decimal.Value;
|
|
330
|
+
quoteSize?: Decimal.Value;
|
|
331
|
+
liquidateeId?: number;
|
|
332
|
+
clientOrderId?: BigIntValue;
|
|
333
|
+
},
|
|
334
|
+
): Promise<bigint | undefined> {
|
|
335
|
+
return placeOrderImpl(
|
|
336
|
+
makeSendHttp(serverUrl),
|
|
337
|
+
signFn,
|
|
338
|
+
currentTimestamp,
|
|
339
|
+
nonce,
|
|
340
|
+
params,
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async function cancelOrderImpl(
|
|
345
|
+
sendFn: (encoded: Uint8Array) => Promise<Uint8Array>,
|
|
346
|
+
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
347
|
+
currentTimestamp: bigint,
|
|
348
|
+
nonce: number,
|
|
349
|
+
params: {
|
|
350
|
+
sessionId: BigIntValue;
|
|
351
|
+
senderId?: number;
|
|
352
|
+
orderId: BigIntValue;
|
|
353
|
+
liquidateeId?: number;
|
|
354
|
+
},
|
|
355
|
+
): Promise<bigint> {
|
|
356
|
+
const action: proto.Action = {
|
|
357
|
+
currentTimestamp,
|
|
358
|
+
nonce: nonce,
|
|
359
|
+
kind: {
|
|
360
|
+
$case: "cancelOrderById",
|
|
361
|
+
value: {
|
|
362
|
+
orderId: BigInt(params.orderId),
|
|
363
|
+
sessionId: BigInt(params.sessionId),
|
|
364
|
+
senderAccountId: params.senderId,
|
|
365
|
+
delegatorAccountId: params.liquidateeId,
|
|
366
|
+
},
|
|
367
|
+
},
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
const resp = await sendAction(
|
|
371
|
+
sendFn,
|
|
372
|
+
(m) => sessionSign(signFn, m),
|
|
373
|
+
action,
|
|
374
|
+
"cancel the order",
|
|
375
|
+
);
|
|
376
|
+
|
|
377
|
+
if (resp.kind?.$case === "cancelOrderResult") {
|
|
378
|
+
return resp.kind.value.orderId;
|
|
379
|
+
} else {
|
|
380
|
+
throw new Error(`Unexpected receipt kind ${resp.kind?.$case}`);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
export async function cancelOrder(
|
|
385
|
+
serverUrl: string,
|
|
386
|
+
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
387
|
+
currentTimestamp: bigint,
|
|
388
|
+
nonce: number,
|
|
389
|
+
params: {
|
|
390
|
+
sessionId: BigIntValue;
|
|
391
|
+
senderId?: number;
|
|
392
|
+
orderId: BigIntValue;
|
|
393
|
+
liquidateeId?: number;
|
|
394
|
+
},
|
|
395
|
+
): Promise<bigint> {
|
|
396
|
+
return cancelOrderImpl(
|
|
397
|
+
makeSendHttp(serverUrl),
|
|
398
|
+
signFn,
|
|
399
|
+
currentTimestamp,
|
|
400
|
+
nonce,
|
|
401
|
+
params,
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
async function transferImpl(
|
|
406
|
+
sendFn: (encoded: Uint8Array) => Promise<Uint8Array>,
|
|
407
|
+
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
408
|
+
currentTimestamp: bigint,
|
|
409
|
+
nonce: number,
|
|
410
|
+
params: {
|
|
411
|
+
sessionId: BigIntValue;
|
|
412
|
+
fromAccountId: number;
|
|
413
|
+
toAccountId?: number;
|
|
414
|
+
tokenId: number;
|
|
415
|
+
tokenDecimals: number;
|
|
416
|
+
amount: Decimal.Value;
|
|
417
|
+
},
|
|
418
|
+
): Promise<number | undefined> {
|
|
419
|
+
const action: proto.Action = {
|
|
420
|
+
currentTimestamp,
|
|
421
|
+
nonce: nonce,
|
|
422
|
+
kind: {
|
|
423
|
+
$case: "transfer",
|
|
424
|
+
value: {
|
|
425
|
+
sessionId: BigInt(params.sessionId),
|
|
426
|
+
fromAccountId: params.fromAccountId,
|
|
427
|
+
toAccountId: params.toAccountId,
|
|
428
|
+
tokenId: params.tokenId,
|
|
429
|
+
amount: toScaledU64(params.amount ?? 0, params.tokenDecimals),
|
|
430
|
+
},
|
|
431
|
+
},
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
const resp = await sendAction(
|
|
435
|
+
sendFn,
|
|
436
|
+
(m) => sessionSign(signFn, m),
|
|
437
|
+
action,
|
|
438
|
+
"transfer asset to other account",
|
|
439
|
+
);
|
|
440
|
+
|
|
441
|
+
if (resp.kind?.$case === "transferred") {
|
|
442
|
+
if (resp.kind.value.accountCreated) {
|
|
443
|
+
return resp.kind.value.toAccountId;
|
|
444
|
+
} else {
|
|
445
|
+
return undefined;
|
|
446
|
+
}
|
|
447
|
+
} else {
|
|
448
|
+
throw new Error(`Unexpected receipt kind ${resp.kind?.$case}`);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
export async function transfer(
|
|
453
|
+
serverUrl: string,
|
|
454
|
+
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
455
|
+
currentTimestamp: bigint,
|
|
456
|
+
nonce: number,
|
|
457
|
+
params: {
|
|
458
|
+
sessionId: BigIntValue;
|
|
459
|
+
fromAccountId: number;
|
|
460
|
+
toAccountId?: number;
|
|
461
|
+
tokenId: number;
|
|
462
|
+
tokenDecimals: number;
|
|
463
|
+
amount: Decimal.Value;
|
|
464
|
+
},
|
|
465
|
+
): Promise<number | undefined> {
|
|
466
|
+
return transferImpl(
|
|
467
|
+
makeSendHttp(serverUrl),
|
|
468
|
+
signFn,
|
|
469
|
+
currentTimestamp,
|
|
470
|
+
nonce,
|
|
471
|
+
params,
|
|
472
|
+
);
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* For testing purposes
|
|
476
|
+
*/
|
|
477
|
+
export const _private = {
|
|
478
|
+
createSessionImpl,
|
|
479
|
+
revokeSessionImpl,
|
|
480
|
+
withdrawImpl,
|
|
481
|
+
placeOrderImpl,
|
|
482
|
+
cancelOrderImpl,
|
|
483
|
+
transferImpl,
|
|
484
|
+
};
|