@reyaxyz/common 0.362.1 → 0.363.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 +1 -1
- package/dist/commands/exposure/command.js +8 -22
- package/dist/commands/exposure/command.js.map +1 -1
- package/dist/incentives-api-types.js.map +1 -1
- package/dist/services/getNativeToken.js +1 -0
- package/dist/services/getNativeToken.js.map +1 -1
- package/dist/services/getSocketAddresses.js +1 -0
- package/dist/services/getSocketAddresses.js.map +1 -1
- package/dist/trading-api-types.js.map +1 -1
- package/dist/transactions/abis/Errors.json +1638 -34
- package/dist/transactions/abis/OrdersGateway.json +729 -123
- package/dist/transactions/abis/PassivePerpProxy.json +542 -0
- package/dist/transactions/buildMulticallTx.js.map +1 -1
- package/dist/transactions/consts.js +1 -2
- package/dist/transactions/consts.js.map +1 -1
- package/dist/transactions/contractAddresses.js +45 -5
- package/dist/transactions/contractAddresses.js.map +1 -1
- package/dist/transactions/sign.js +237 -111
- package/dist/transactions/sign.js.map +1 -1
- package/dist/types/commands/exposure/command.d.ts +8 -7
- package/dist/types/commands/exposure/command.d.ts.map +1 -1
- package/dist/types/incentives-api-types.d.ts +61 -5
- package/dist/types/incentives-api-types.d.ts.map +1 -1
- package/dist/types/services/getNativeToken.d.ts.map +1 -1
- package/dist/types/services/getSocketAddresses.d.ts.map +1 -1
- package/dist/types/trading-api-types.d.ts +200 -32
- package/dist/types/trading-api-types.d.ts.map +1 -1
- package/dist/types/transactions/buildMulticallTx.d.ts +1 -0
- package/dist/types/transactions/buildMulticallTx.d.ts.map +1 -1
- package/dist/types/transactions/consts.d.ts +0 -1
- package/dist/types/transactions/consts.d.ts.map +1 -1
- package/dist/types/transactions/contractAddresses.d.ts +0 -1
- package/dist/types/transactions/contractAddresses.d.ts.map +1 -1
- package/dist/types/transactions/sign.d.ts +113 -9
- package/dist/types/transactions/sign.d.ts.map +1 -1
- package/dist/types/types.d.ts +61 -65
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utils/calculate.d.ts.map +1 -1
- package/dist/types/utils/consts.d.ts +2 -2
- package/dist/types/utils/consts.d.ts.map +1 -1
- package/dist/types/utils/error-sanitizer.d.ts +15 -0
- package/dist/types/utils/error-sanitizer.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +3 -0
- package/dist/types/utils/index.d.ts.map +1 -1
- package/dist/types/utils/network.d.ts.map +1 -1
- package/dist/types/utils/number.d.ts +5 -0
- package/dist/types/utils/number.d.ts.map +1 -1
- package/dist/types/utils/realized-balance.d.ts +54 -0
- package/dist/types/utils/realized-balance.d.ts.map +1 -0
- package/dist/types/utils/throttled-error.d.ts +42 -0
- package/dist/types/utils/throttled-error.d.ts.map +1 -0
- package/dist/types/utils/token/normalize.d.ts +2 -1
- package/dist/types/utils/token/normalize.d.ts.map +1 -1
- package/dist/types/utils/token/token-info-getters.d.ts.map +1 -1
- package/dist/types/utils/token/token-info.d.ts.map +1 -1
- package/dist/types.js +25 -11
- package/dist/types.js.map +1 -1
- package/dist/utils/calculate.js +18 -9
- package/dist/utils/calculate.js.map +1 -1
- package/dist/utils/consts.js +2 -7
- package/dist/utils/consts.js.map +1 -1
- package/dist/utils/error-sanitizer.js +135 -0
- package/dist/utils/error-sanitizer.js.map +1 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/network.js +4 -0
- package/dist/utils/network.js.map +1 -1
- package/dist/utils/number.js +96 -1
- package/dist/utils/number.js.map +1 -1
- package/dist/utils/realized-balance.js +126 -0
- package/dist/utils/realized-balance.js.map +1 -0
- package/dist/utils/throttled-error.js +60 -0
- package/dist/utils/throttled-error.js.map +1 -0
- package/dist/utils/token/normalize.js +2 -2
- package/dist/utils/token/normalize.js.map +1 -1
- package/dist/utils/token/token-info-getters.js +80 -5
- package/dist/utils/token/token-info-getters.js.map +1 -1
- package/dist/utils/token/token-info.js +7 -0
- package/dist/utils/token/token-info.js.map +1 -1
- package/package.json +3 -3
- package/src/commands/exposure/command.ts +8 -38
- package/src/incentives-api-types.ts +60 -5
- package/src/services/getNativeToken.ts +1 -0
- package/src/services/getSocketAddresses.ts +1 -0
- package/src/trading-api-types.ts +258 -39
- package/src/transactions/abis/Errors.json +5478 -3874
- package/src/transactions/abis/OrdersGateway.json +730 -124
- package/src/transactions/abis/PassivePerpProxy.json +542 -0
- package/src/transactions/buildMulticallTx.ts +4 -0
- package/src/transactions/consts.ts +0 -1
- package/src/transactions/contractAddresses.ts +42 -7
- package/src/transactions/sign.ts +306 -150
- package/src/types.ts +98 -83
- package/src/utils/calculate.ts +21 -8
- package/src/utils/consts.ts +4 -16
- package/src/utils/error-sanitizer.ts +137 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/network.ts +4 -0
- package/src/utils/number.ts +110 -0
- package/src/utils/realized-balance.ts +163 -0
- package/src/utils/throttled-error.ts +85 -0
- package/src/utils/token/normalize.ts +6 -1
- package/src/utils/token/token-info-getters.ts +104 -6
- package/src/utils/token/token-info.ts +42 -0
package/src/transactions/sign.ts
CHANGED
|
@@ -7,13 +7,7 @@ import {
|
|
|
7
7
|
} from 'ethers';
|
|
8
8
|
import { ContractType, getAddress } from './contractAddresses';
|
|
9
9
|
import { Command } from './action';
|
|
10
|
-
import {
|
|
11
|
-
Address,
|
|
12
|
-
ConditionalOrderStatus,
|
|
13
|
-
ConditionalOrderType,
|
|
14
|
-
OrdersGatewayOrderType,
|
|
15
|
-
ReyaChainId,
|
|
16
|
-
} from '../types';
|
|
10
|
+
import { Address, OrderType, ReyaChainId } from '../types';
|
|
17
11
|
|
|
18
12
|
export type EIP712Signature = {
|
|
19
13
|
v: number;
|
|
@@ -177,140 +171,28 @@ export function getReyaDomain(verifyingContract: string) {
|
|
|
177
171
|
};
|
|
178
172
|
}
|
|
179
173
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
): bigint {
|
|
203
|
-
// Validate the input ranges
|
|
204
|
-
if (marketId < 0 || marketId >= 2 ** 32)
|
|
205
|
-
throw new Error('marketId is out of range');
|
|
206
|
-
if (accountId < 0 || accountId >= 2 ** 128)
|
|
207
|
-
throw new Error('accountId is out of range');
|
|
208
|
-
if (timestampMs < 0 || timestampMs >= 2 ** 64)
|
|
209
|
-
throw new Error('timestamp is out of range');
|
|
210
|
-
|
|
211
|
-
const hashUint256 =
|
|
212
|
-
(BigInt(accountId) << BigInt(98)) |
|
|
213
|
-
(BigInt(timestampMs) << BigInt(32)) |
|
|
214
|
-
BigInt(marketId);
|
|
215
|
-
|
|
216
|
-
return hashUint256;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
type SignOrdersGatewayOrderOutput = {
|
|
220
|
-
serializedSignature: string;
|
|
221
|
-
nonce: bigint;
|
|
222
|
-
signature: EIP712Signature;
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
export async function signOrdersGatewayOrder(
|
|
226
|
-
signer: Signer | JsonRpcSigner,
|
|
227
|
-
reyaChainId: ReyaChainId,
|
|
228
|
-
accountId: number,
|
|
229
|
-
marketId: number,
|
|
230
|
-
exchangeId: number,
|
|
231
|
-
counterpartyAccountIds: number[],
|
|
232
|
-
orderType: ConditionalOrderType | OrdersGatewayOrderType,
|
|
233
|
-
inputs: string,
|
|
234
|
-
deadline: number,
|
|
235
|
-
creationTimestampMs: number,
|
|
236
|
-
customNonce?: bigint,
|
|
237
|
-
): Promise<SignOrdersGatewayOrderOutput> {
|
|
238
|
-
const nonce =
|
|
239
|
-
customNonce ??
|
|
240
|
-
createOrdersGatewayOrderNonce(accountId, marketId, creationTimestampMs);
|
|
241
|
-
|
|
242
|
-
const signature = await signReyaTypedData(
|
|
243
|
-
signer,
|
|
244
|
-
getAddress(reyaChainId, ContractType.ORDERS_GATEWAY_PROXY),
|
|
245
|
-
conditionalOrderTypes,
|
|
246
|
-
{
|
|
247
|
-
verifyingChainId: reyaChainId,
|
|
248
|
-
deadline: BigInt(deadline),
|
|
249
|
-
order: {
|
|
250
|
-
accountId: accountId,
|
|
251
|
-
marketId: marketId,
|
|
252
|
-
exchangeId: exchangeId,
|
|
253
|
-
counterpartyAccountIds: counterpartyAccountIds,
|
|
254
|
-
orderType: orderType,
|
|
255
|
-
inputs: inputs,
|
|
256
|
-
signer: await signer.getAddress(),
|
|
257
|
-
nonce: nonce,
|
|
258
|
-
},
|
|
259
|
-
},
|
|
260
|
-
);
|
|
261
|
-
|
|
262
|
-
return {
|
|
263
|
-
serializedSignature: signature.serialized,
|
|
264
|
-
nonce,
|
|
265
|
-
signature: convertEthersSignatureToEIP712Signature(signature, deadline),
|
|
266
|
-
};
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
export function isConditionalOrderSignatureValid(
|
|
270
|
-
signerAddress: string,
|
|
271
|
-
signature: string,
|
|
272
|
-
reyaChainId: ReyaChainId,
|
|
273
|
-
accountId: number,
|
|
274
|
-
marketId: number,
|
|
275
|
-
exchangeId: number,
|
|
276
|
-
counterpartyAccountIds: number[],
|
|
277
|
-
orderType: number,
|
|
278
|
-
inputs: string,
|
|
279
|
-
nonce: bigint,
|
|
280
|
-
deadline: number,
|
|
281
|
-
): boolean {
|
|
282
|
-
// Encode the data for recovering the address
|
|
283
|
-
const domain = getReyaDomain(
|
|
284
|
-
getAddress(reyaChainId, ContractType.CONDITIONAL_ORDERS_PROXY),
|
|
285
|
-
);
|
|
286
|
-
|
|
287
|
-
const value = {
|
|
288
|
-
verifyingChainId: reyaChainId,
|
|
289
|
-
deadline: BigInt(deadline),
|
|
290
|
-
order: {
|
|
291
|
-
accountId: accountId,
|
|
292
|
-
marketId: marketId,
|
|
293
|
-
exchangeId: exchangeId,
|
|
294
|
-
counterpartyAccountIds: counterpartyAccountIds,
|
|
295
|
-
orderType: orderType,
|
|
296
|
-
inputs: inputs,
|
|
297
|
-
signer: signerAddress,
|
|
298
|
-
nonce: nonce,
|
|
299
|
-
},
|
|
300
|
-
};
|
|
301
|
-
|
|
302
|
-
// Recover the address
|
|
303
|
-
const recoveredAddress = verifyTypedData(
|
|
304
|
-
domain,
|
|
305
|
-
conditionalOrderTypes,
|
|
306
|
-
value,
|
|
307
|
-
signature,
|
|
308
|
-
);
|
|
309
|
-
|
|
310
|
-
return recoveredAddress.toLowerCase() === signerAddress.toLowerCase();
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
const orderCancelTypes = {
|
|
174
|
+
// PRO-126 Bug 5 retired the legacy `ConditionalOrderDetails` typehash +
|
|
175
|
+
// `signOrdersGatewayOrder` / `isConditionalOrderSignatureValid` /
|
|
176
|
+
// `createOrdersGatewayOrderNonce` helpers. The on-chain
|
|
177
|
+
// `OrdersGateway.execute` selector that consumed those signatures is
|
|
178
|
+
// gone; every order class now signs the new flat `OrderDetails` schema
|
|
179
|
+
// via `signOrder` further down this file.
|
|
180
|
+
|
|
181
|
+
// The three cancel-side typed-data schemas below are EXPORTED as the single
|
|
182
|
+
// source of truth for signer AND verifier — common-backend's
|
|
183
|
+
// `recover{OrderCancel,MassCancel,CancelAllAfter}Signer` import them from
|
|
184
|
+
// here (like `orderTypes` for the Order struct). They used to be duplicated
|
|
185
|
+
// module-private consts on each side; any drift (field rename / width /
|
|
186
|
+
// reorder) silently breaks 100% of signature recovery for that request
|
|
187
|
+
// class, so exactly one copy may exist.
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* EIP-712 typed-data structures for the OrderCancel signature scheme used by
|
|
191
|
+
* the matching engine for order cancellations. The signer signs over a value
|
|
192
|
+
* of shape
|
|
193
|
+
* `{ verifyingChainId, deadline, cancel: { accountId, marketId, orderId, clOrdId, nonce } }`.
|
|
194
|
+
*/
|
|
195
|
+
export const orderCancelTypes = {
|
|
314
196
|
OrderCancel: [
|
|
315
197
|
{ name: 'verifyingChainId', type: 'uint64' },
|
|
316
198
|
{ name: 'deadline', type: 'uint64' },
|
|
@@ -325,7 +207,12 @@ const orderCancelTypes = {
|
|
|
325
207
|
],
|
|
326
208
|
};
|
|
327
209
|
|
|
328
|
-
|
|
210
|
+
/**
|
|
211
|
+
* EIP-712 typed-data structures for the MassCancel signature scheme used by
|
|
212
|
+
* the matching engine for mass cancellations. The signer signs over a value
|
|
213
|
+
* of shape `{ verifyingChainId, deadline, massCancel: { accountId, marketId, nonce } }`.
|
|
214
|
+
*/
|
|
215
|
+
export const massCancelTypes = {
|
|
329
216
|
MassCancel: [
|
|
330
217
|
{ name: 'verifyingChainId', type: 'uint64' },
|
|
331
218
|
{ name: 'deadline', type: 'uint64' },
|
|
@@ -338,6 +225,26 @@ const massCancelTypes = {
|
|
|
338
225
|
],
|
|
339
226
|
};
|
|
340
227
|
|
|
228
|
+
/**
|
|
229
|
+
* EIP-712 typed-data structures for the CancelAllAfter signature scheme used
|
|
230
|
+
* by the matching engine for the cancel-on-disconnect dead-man's-switch. The
|
|
231
|
+
* signer signs over a value of shape
|
|
232
|
+
* `{ verifyingChainId, deadline, cancelAllAfter: { accountId, timeoutMs, nonce } }`.
|
|
233
|
+
* uint64 throughout, matching the OrderCancel / MassCancel family.
|
|
234
|
+
*/
|
|
235
|
+
export const cancelAllAfterTypes = {
|
|
236
|
+
CancelAllAfter: [
|
|
237
|
+
{ name: 'verifyingChainId', type: 'uint64' },
|
|
238
|
+
{ name: 'deadline', type: 'uint64' },
|
|
239
|
+
{ name: 'cancelAllAfter', type: 'CancelAllAfterDetails' },
|
|
240
|
+
],
|
|
241
|
+
CancelAllAfterDetails: [
|
|
242
|
+
{ name: 'accountId', type: 'uint64' },
|
|
243
|
+
{ name: 'timeoutMs', type: 'uint64' },
|
|
244
|
+
{ name: 'nonce', type: 'uint64' },
|
|
245
|
+
],
|
|
246
|
+
};
|
|
247
|
+
|
|
341
248
|
type SignCancelOutput = {
|
|
342
249
|
serializedSignature: string;
|
|
343
250
|
nonce: bigint;
|
|
@@ -408,16 +315,183 @@ export async function signMassCancel(
|
|
|
408
315
|
};
|
|
409
316
|
}
|
|
410
317
|
|
|
411
|
-
|
|
318
|
+
// -----------------------------------------------------------------------------
|
|
319
|
+
// perpOB: EIP-712 typed data for the flat `Order` / `OrderDetails` struct
|
|
320
|
+
// -----------------------------------------------------------------------------
|
|
321
|
+
// Matches the post-audit on-chain redesign in
|
|
322
|
+
// reya-network/feat/perpOB (orders-gateway/src/libraries/DataTypes.sol +
|
|
323
|
+
// ConditionalOrderHashing.sol). PRO-126 Bug 5 retired the legacy
|
|
324
|
+
// `conditionalOrderTypes` / `signOrdersGatewayOrder` / `isConditionalOrderSignatureValid`
|
|
325
|
+
// surface that this struct replaced — every order class (spot + perp,
|
|
326
|
+
// IOC + GTC + TP + SL) now signs against `OrderDetails` here.
|
|
327
|
+
//
|
|
328
|
+
// Contract: Order envelope + flat OrderDetails inner struct. Two distinct time
|
|
329
|
+
// values are signed:
|
|
330
|
+
// - envelope `deadline` -> signature validity, enforced by API at entry
|
|
331
|
+
// - inner `expiresAfter` -> order lifetime, enforced on-chain at execution
|
|
332
|
+
// Convention: callers should set `deadline <= expiresAfter` when `expiresAfter > 0`.
|
|
333
|
+
// See reya-api-specs/docs/eip712.md for the canonical description.
|
|
334
|
+
|
|
335
|
+
export const orderTypes = {
|
|
336
|
+
Order: [
|
|
337
|
+
{ name: 'verifyingChainId', type: 'uint256' },
|
|
338
|
+
{ name: 'deadline', type: 'uint256' },
|
|
339
|
+
{ name: 'order', type: 'OrderDetails' },
|
|
340
|
+
],
|
|
341
|
+
OrderDetails: [
|
|
342
|
+
{ name: 'accountId', type: 'uint128' },
|
|
343
|
+
{ name: 'marketId', type: 'uint128' },
|
|
344
|
+
{ name: 'exchangeId', type: 'uint128' },
|
|
345
|
+
{ name: 'orderType', type: 'uint8' },
|
|
346
|
+
{ name: 'quantity', type: 'int256' },
|
|
347
|
+
{ name: 'limitPrice', type: 'uint256' },
|
|
348
|
+
{ name: 'triggerPrice', type: 'uint256' },
|
|
349
|
+
{ name: 'timeInForce', type: 'uint8' },
|
|
350
|
+
{ name: 'clientOrderId', type: 'uint64' },
|
|
351
|
+
{ name: 'reduceOnly', type: 'bool' },
|
|
352
|
+
{ name: 'postOnly', type: 'bool' },
|
|
353
|
+
{ name: 'expiresAfter', type: 'uint256' },
|
|
354
|
+
{ name: 'signer', type: 'address' },
|
|
355
|
+
{ name: 'nonce', type: 'uint256' },
|
|
356
|
+
],
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Full-position stop sentinel — `type(int256).max` (2^255 − 1), mirroring
|
|
361
|
+
* `FULL_POSITION_STOP_SENTINEL` in the OrdersGateway (reya-network #738). A
|
|
362
|
+
* STOP_LOSS / TAKE_PROFIT signs quantity `±sentinel` (the sign carries the
|
|
363
|
+
* close side: −sentinel sells out a long, +sentinel buys back a short); the
|
|
364
|
+
* chain enforces the iff — a stop with any other quantity (including the old
|
|
365
|
+
* quantity-0 encoding) reverts, and the sentinel magnitude is reserved (a
|
|
366
|
+
* non-stop signing it reverts). Raw int256 value, NOT E18-scaled. Signer and
|
|
367
|
+
* verifier must derive it from THIS constant so they can never drift.
|
|
368
|
+
*/
|
|
369
|
+
export const FULL_POSITION_STOP_SENTINEL =
|
|
370
|
+
(BigInt(1) << BigInt(255)) - BigInt(1);
|
|
371
|
+
|
|
372
|
+
/** The signed quantity for a full-position stop: sign carries the close side. */
|
|
373
|
+
export function fullPositionStopQuantity(isBuy: boolean): bigint {
|
|
374
|
+
return isBuy ? FULL_POSITION_STOP_SENTINEL : -FULL_POSITION_STOP_SENTINEL;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export type SignOrderParams = {
|
|
378
|
+
/** Unified market id: core_id for perp, core_id + 1e10 for spot. */
|
|
379
|
+
accountId: number;
|
|
380
|
+
marketId: number;
|
|
381
|
+
exchangeId: number;
|
|
382
|
+
orderType: OrderType;
|
|
383
|
+
/**
|
|
384
|
+
* Signed: positive = buy/long, negative = sell/short. For STOP_LOSS /
|
|
385
|
+
* TAKE_PROFIT this must be `fullPositionStopQuantity(isBuy)` — the
|
|
386
|
+
* ±int256.max full-position sentinel, never a real size and never 0.
|
|
387
|
+
*/
|
|
388
|
+
quantity: bigint;
|
|
389
|
+
limitPrice: bigint;
|
|
390
|
+
/** Must be 0n for Limit; must be non-zero for StopLoss / TakeProfit. */
|
|
391
|
+
triggerPrice: bigint;
|
|
392
|
+
/** 0 = GTC, 1 = IOC, 2 = GTT. Signed but not on-chain-enforced. */
|
|
393
|
+
timeInForce: number;
|
|
394
|
+
/** Signed but not on-chain-enforced. Off-chain correlation id. */
|
|
395
|
+
clientOrderId: bigint;
|
|
396
|
+
reduceOnly: boolean;
|
|
397
|
+
/**
|
|
398
|
+
* Maker-only intent, signed into OrderDetails (api-specs 3.0.1 / on-chain
|
|
399
|
+
* Phase 0). Optional for back-compat: omitted ⇒ signed as `false`, so
|
|
400
|
+
* existing signers keep producing valid signatures. Must equal the
|
|
401
|
+
* client's intent for recovery to match.
|
|
402
|
+
*/
|
|
403
|
+
postOnly?: boolean;
|
|
404
|
+
/** 0n = good-until-cancelled. On-chain-enforced at execution. */
|
|
405
|
+
expiresAfter: bigint;
|
|
406
|
+
nonce: bigint;
|
|
407
|
+
/** EIP-712 envelope signature deadline (unix seconds). API-enforced at entry. */
|
|
408
|
+
deadline: number;
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
export type SignOrderOutput = {
|
|
412
|
+
serializedSignature: string;
|
|
413
|
+
signature: EIP712Signature;
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
export async function signOrder(
|
|
412
417
|
signer: Signer | JsonRpcSigner,
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
418
|
+
reyaChainId: ReyaChainId,
|
|
419
|
+
params: SignOrderParams,
|
|
420
|
+
): Promise<SignOrderOutput> {
|
|
421
|
+
const signature = await signReyaTypedData(
|
|
422
|
+
signer,
|
|
423
|
+
getAddress(reyaChainId, ContractType.ORDERS_GATEWAY_PROXY),
|
|
424
|
+
orderTypes,
|
|
425
|
+
{
|
|
426
|
+
verifyingChainId: reyaChainId,
|
|
427
|
+
deadline: BigInt(params.deadline),
|
|
428
|
+
order: {
|
|
429
|
+
accountId: params.accountId,
|
|
430
|
+
marketId: params.marketId,
|
|
431
|
+
exchangeId: params.exchangeId,
|
|
432
|
+
orderType: params.orderType,
|
|
433
|
+
quantity: params.quantity,
|
|
434
|
+
limitPrice: params.limitPrice,
|
|
435
|
+
triggerPrice: params.triggerPrice,
|
|
436
|
+
timeInForce: params.timeInForce,
|
|
437
|
+
clientOrderId: params.clientOrderId,
|
|
438
|
+
reduceOnly: params.reduceOnly,
|
|
439
|
+
postOnly: params.postOnly ?? false,
|
|
440
|
+
expiresAfter: params.expiresAfter,
|
|
441
|
+
signer: await signer.getAddress(),
|
|
442
|
+
nonce: params.nonce,
|
|
443
|
+
},
|
|
444
|
+
},
|
|
445
|
+
);
|
|
446
|
+
|
|
447
|
+
return {
|
|
448
|
+
serializedSignature: signature.serialized,
|
|
449
|
+
signature: convertEthersSignatureToEIP712Signature(
|
|
450
|
+
signature,
|
|
451
|
+
params.deadline,
|
|
452
|
+
),
|
|
419
453
|
};
|
|
420
|
-
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
export function isOrderSignatureValid(
|
|
457
|
+
signerAddress: string,
|
|
458
|
+
signature: string,
|
|
459
|
+
reyaChainId: ReyaChainId,
|
|
460
|
+
params: SignOrderParams,
|
|
461
|
+
): boolean {
|
|
462
|
+
const domain = getReyaDomain(
|
|
463
|
+
getAddress(reyaChainId, ContractType.ORDERS_GATEWAY_PROXY),
|
|
464
|
+
);
|
|
465
|
+
|
|
466
|
+
const value = {
|
|
467
|
+
verifyingChainId: reyaChainId,
|
|
468
|
+
deadline: BigInt(params.deadline),
|
|
469
|
+
order: {
|
|
470
|
+
accountId: params.accountId,
|
|
471
|
+
marketId: params.marketId,
|
|
472
|
+
exchangeId: params.exchangeId,
|
|
473
|
+
orderType: params.orderType,
|
|
474
|
+
quantity: params.quantity,
|
|
475
|
+
limitPrice: params.limitPrice,
|
|
476
|
+
triggerPrice: params.triggerPrice,
|
|
477
|
+
timeInForce: params.timeInForce,
|
|
478
|
+
clientOrderId: params.clientOrderId,
|
|
479
|
+
reduceOnly: params.reduceOnly,
|
|
480
|
+
postOnly: params.postOnly ?? false,
|
|
481
|
+
expiresAfter: params.expiresAfter,
|
|
482
|
+
signer: signerAddress,
|
|
483
|
+
nonce: params.nonce,
|
|
484
|
+
},
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
const recoveredAddress = verifyTypedData(
|
|
488
|
+
domain,
|
|
489
|
+
orderTypes,
|
|
490
|
+
value,
|
|
491
|
+
signature,
|
|
492
|
+
);
|
|
493
|
+
|
|
494
|
+
return recoveredAddress.toLowerCase() === signerAddress.toLowerCase();
|
|
421
495
|
}
|
|
422
496
|
|
|
423
497
|
export async function signGrantPermission(
|
|
@@ -522,6 +596,88 @@ export async function signRevokePermission(
|
|
|
522
596
|
return convertEthersSignatureToEIP712Signature(signature, deadline);
|
|
523
597
|
}
|
|
524
598
|
|
|
599
|
+
export async function signMEOrderCancel(
|
|
600
|
+
signer: Signer | JsonRpcSigner,
|
|
601
|
+
reyaChainId: ReyaChainId,
|
|
602
|
+
accountId: number,
|
|
603
|
+
marketId: number,
|
|
604
|
+
orderId: bigint,
|
|
605
|
+
clOrdId: bigint,
|
|
606
|
+
nonce: bigint,
|
|
607
|
+
deadline: number,
|
|
608
|
+
): Promise<string> {
|
|
609
|
+
const signature = await signReyaTypedData(
|
|
610
|
+
signer,
|
|
611
|
+
getAddress(reyaChainId, ContractType.ORDERS_GATEWAY_PROXY),
|
|
612
|
+
orderCancelTypes,
|
|
613
|
+
{
|
|
614
|
+
verifyingChainId: reyaChainId,
|
|
615
|
+
deadline: BigInt(deadline),
|
|
616
|
+
cancel: {
|
|
617
|
+
accountId: accountId,
|
|
618
|
+
marketId: marketId,
|
|
619
|
+
orderId: orderId,
|
|
620
|
+
clOrdId: clOrdId,
|
|
621
|
+
nonce: nonce,
|
|
622
|
+
},
|
|
623
|
+
},
|
|
624
|
+
);
|
|
625
|
+
|
|
626
|
+
return signature.serialized;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
export async function signMEMassCancel(
|
|
630
|
+
signer: Signer | JsonRpcSigner,
|
|
631
|
+
reyaChainId: ReyaChainId,
|
|
632
|
+
accountId: number,
|
|
633
|
+
marketId: number,
|
|
634
|
+
nonce: bigint,
|
|
635
|
+
deadline: number,
|
|
636
|
+
): Promise<string> {
|
|
637
|
+
const signature = await signReyaTypedData(
|
|
638
|
+
signer,
|
|
639
|
+
getAddress(reyaChainId, ContractType.ORDERS_GATEWAY_PROXY),
|
|
640
|
+
massCancelTypes,
|
|
641
|
+
{
|
|
642
|
+
verifyingChainId: reyaChainId,
|
|
643
|
+
deadline: BigInt(deadline),
|
|
644
|
+
massCancel: {
|
|
645
|
+
accountId: accountId,
|
|
646
|
+
marketId: marketId,
|
|
647
|
+
nonce: nonce,
|
|
648
|
+
},
|
|
649
|
+
},
|
|
650
|
+
);
|
|
651
|
+
|
|
652
|
+
return signature.serialized;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
export async function signMECancelAllAfter(
|
|
656
|
+
signer: Signer | JsonRpcSigner,
|
|
657
|
+
reyaChainId: ReyaChainId,
|
|
658
|
+
accountId: number,
|
|
659
|
+
timeoutMs: number,
|
|
660
|
+
nonce: bigint,
|
|
661
|
+
deadline: number,
|
|
662
|
+
): Promise<string> {
|
|
663
|
+
const signature = await signReyaTypedData(
|
|
664
|
+
signer,
|
|
665
|
+
getAddress(reyaChainId, ContractType.ORDERS_GATEWAY_PROXY),
|
|
666
|
+
cancelAllAfterTypes,
|
|
667
|
+
{
|
|
668
|
+
verifyingChainId: reyaChainId,
|
|
669
|
+
deadline: BigInt(deadline),
|
|
670
|
+
cancelAllAfter: {
|
|
671
|
+
accountId: accountId,
|
|
672
|
+
timeoutMs: timeoutMs,
|
|
673
|
+
nonce: nonce,
|
|
674
|
+
},
|
|
675
|
+
},
|
|
676
|
+
);
|
|
677
|
+
|
|
678
|
+
return signature.serialized;
|
|
679
|
+
}
|
|
680
|
+
|
|
525
681
|
export const signManagePermissionBySig = async (
|
|
526
682
|
signer: Signer | JsonRpcSigner,
|
|
527
683
|
signerAddress: string,
|