@gmx-io/sdk 1.6.2 → 1.6.3
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/build/cjs/src/clients/v2/__tests__/subaccount.spec.js +270 -0
- package/build/cjs/src/clients/v2/__tests__/testUtil.js +92 -1
- package/build/cjs/src/clients/v2/index.js +34 -57
- package/build/cjs/src/utils/subaccount/index.js +11 -1
- package/build/cjs/src/utils/subaccount/sdkClient.js +367 -0
- package/build/cjs/src/utils/subaccount/state.js +76 -0
- package/build/esm/src/clients/v2/__tests__/subaccount.spec.js +271 -1
- package/build/esm/src/clients/v2/__tests__/testUtil.js +87 -0
- package/build/esm/src/clients/v2/index.js +33 -56
- package/build/esm/src/utils/subaccount/index.js +1 -0
- package/build/esm/src/utils/subaccount/sdkClient.js +352 -0
- package/build/esm/src/utils/subaccount/state.js +64 -0
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/types/src/clients/v2/__tests__/testUtil.d.ts +37 -1
- package/build/types/src/clients/v2/index.d.ts +8 -2
- package/build/types/src/utils/subaccount/index.d.ts +1 -0
- package/build/types/src/utils/subaccount/sdkClient.d.ts +36 -0
- package/build/types/src/utils/subaccount/state.d.ts +11 -0
- package/build/types/src/utils/subaccount/types.d.ts +18 -0
- package/package.json +1 -1
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.clearSdkSubaccount = exports.executeExpressOrderWithSubaccount = exports.submitOrderWithSubaccount = exports.signOrderWithSubaccount = exports.prepareWithSubaccount = exports.getSdkSubaccountApprovalForOrder = exports.activateSdkSubaccount = exports.refreshSdkSubaccountState = exports.reconcileSdkSubaccountApproval = exports.generateSdkSubaccount = exports.getSdkSubaccountApprovalMessage = exports.hasActiveSdkSubaccount = void 0;
|
|
4
|
+
const express_1 = require("../../configs/express");
|
|
5
|
+
const api_1 = require("../orderTransactions/api");
|
|
6
|
+
const time_1 = require("../time");
|
|
7
|
+
const api_2 = require("./api");
|
|
8
|
+
const generateSubaccount_1 = require("./generateSubaccount");
|
|
9
|
+
const getEmptySubaccountApproval_1 = require("./getEmptySubaccountApproval");
|
|
10
|
+
const state_1 = require("./state");
|
|
11
|
+
const sdkSubaccountSigners = new WeakMap();
|
|
12
|
+
const MAX_PREPARED_SUBACCOUNT_APPROVALS = 100;
|
|
13
|
+
const SUBACCOUNT_STATUS_CACHE_TTL_MS = 15000;
|
|
14
|
+
const LOW_SUBACCOUNT_ACTIONS_REFRESH_THRESHOLD = 1n;
|
|
15
|
+
const SUBMITTED_ORDER_STATUSES = new Set([
|
|
16
|
+
"relay_accepted",
|
|
17
|
+
"relay_pending",
|
|
18
|
+
"relay_submitted",
|
|
19
|
+
"created",
|
|
20
|
+
"executed",
|
|
21
|
+
]);
|
|
22
|
+
const FINAL_ORDER_STATUSES = new Set(["created", "executed"]);
|
|
23
|
+
function hasSdkSubaccountSigner(owner) {
|
|
24
|
+
return sdkSubaccountSigners.has(owner);
|
|
25
|
+
}
|
|
26
|
+
function getSdkSubaccountSigner(owner) {
|
|
27
|
+
return sdkSubaccountSigners.get(owner);
|
|
28
|
+
}
|
|
29
|
+
function isSameAddress(a, b) {
|
|
30
|
+
return a !== undefined && b !== undefined && a.toLowerCase() === b.toLowerCase();
|
|
31
|
+
}
|
|
32
|
+
function hasDifferentSubaccountAccount(subaccount, account) {
|
|
33
|
+
return subaccount.account !== undefined && !isSameAddress(subaccount.account, account);
|
|
34
|
+
}
|
|
35
|
+
function throwSubaccountAccountMismatch(subaccount, account) {
|
|
36
|
+
throw new Error(`Subaccount ${subaccount.address} belongs to account ${subaccount.account}; requested account is ${account}.`);
|
|
37
|
+
}
|
|
38
|
+
function throwSubaccountAddressMismatch(source, provided, expected) {
|
|
39
|
+
throw new Error(`${source} ${provided} does not match generated subaccount ${expected}.`);
|
|
40
|
+
}
|
|
41
|
+
function resetSdkSubaccount(client) {
|
|
42
|
+
sdkSubaccountSigners.delete(client.owner);
|
|
43
|
+
client.preparedSubaccountApprovals.clear();
|
|
44
|
+
client.setSubaccount(undefined);
|
|
45
|
+
}
|
|
46
|
+
function getUsableSubaccountApproval(client) {
|
|
47
|
+
const subaccount = client.getSubaccount();
|
|
48
|
+
if (!subaccount || !(0, state_1.isSubaccountStatusUsable)(subaccount.onchainData)) {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
return (0, getEmptySubaccountApproval_1.getEmptySubaccountApproval)(client.ctx.chainId, subaccount.address);
|
|
52
|
+
}
|
|
53
|
+
function isSubaccountStatusCacheFresh(subaccount, account) {
|
|
54
|
+
return (isSameAddress(subaccount.account, account) &&
|
|
55
|
+
subaccount.onchainData !== undefined &&
|
|
56
|
+
subaccount.lastStatusFetchedAt !== undefined &&
|
|
57
|
+
Date.now() - subaccount.lastStatusFetchedAt < SUBACCOUNT_STATUS_CACHE_TTL_MS);
|
|
58
|
+
}
|
|
59
|
+
function assertSdkSubaccountRequest(request, subaccount, approval) {
|
|
60
|
+
if (request.subaccountAddress && !isSameAddress(request.subaccountAddress, subaccount.address)) {
|
|
61
|
+
throwSubaccountAddressMismatch("subaccountAddress", request.subaccountAddress, subaccount.address);
|
|
62
|
+
}
|
|
63
|
+
if (approval?.subaccount && !isSameAddress(approval.subaccount, subaccount.address)) {
|
|
64
|
+
throwSubaccountAddressMismatch("subaccountApproval.subaccount", approval.subaccount, subaccount.address);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function shouldRefreshSubaccountApprovalForPrepare(approval, subaccount, account) {
|
|
68
|
+
if ((0, state_1.isEmptySubaccountApproval)(approval)) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
return (!isSubaccountStatusCacheFresh(subaccount, account) ||
|
|
72
|
+
(0, state_1.shouldRefreshExplicitSubaccountApproval)(approval, subaccount.onchainData, subaccount.approval));
|
|
73
|
+
}
|
|
74
|
+
function shouldRefreshLowActionSubaccountStatus(status, alreadyRefreshed) {
|
|
75
|
+
return (!alreadyRefreshed && status !== undefined && status.remainingActions <= LOW_SUBACCOUNT_ACTIONS_REFRESH_THRESHOLD);
|
|
76
|
+
}
|
|
77
|
+
function prunePreparedSubaccountApprovals(client) {
|
|
78
|
+
while (client.preparedSubaccountApprovals.size > MAX_PREPARED_SUBACCOUNT_APPROVALS) {
|
|
79
|
+
const oldestRequestId = client.preparedSubaccountApprovals.keys().next().value;
|
|
80
|
+
if (!oldestRequestId) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
client.preparedSubaccountApprovals.delete(oldestRequestId);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function isOrderSubmitAccepted(response) {
|
|
87
|
+
return response.error === undefined && SUBMITTED_ORDER_STATUSES.has(response.status);
|
|
88
|
+
}
|
|
89
|
+
function markSubaccountApprovalSubmitted(subaccount, approval, requestId) {
|
|
90
|
+
if (!approval || !requestId || (0, state_1.isEmptySubaccountApproval)(approval)) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const pendingApproval = subaccount?.approval;
|
|
94
|
+
if (pendingApproval && (0, state_1.isSameSubaccountApproval)(pendingApproval.message, approval)) {
|
|
95
|
+
pendingApproval.submittedRequestId = requestId;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function markSubaccountActionSubmitted(subaccount, approval) {
|
|
99
|
+
if (!subaccount?.onchainData || !approval || !(0, state_1.isEmptySubaccountApproval)(approval)) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
subaccount.onchainData.currentActionsCount += 1n;
|
|
103
|
+
subaccount.onchainData.remainingActions =
|
|
104
|
+
subaccount.onchainData.remainingActions > 0n ? subaccount.onchainData.remainingActions - 1n : 0n;
|
|
105
|
+
}
|
|
106
|
+
function hasActiveSdkSubaccount(client) {
|
|
107
|
+
const subaccount = client.getSubaccount();
|
|
108
|
+
return (hasSdkSubaccountSigner(client.owner) &&
|
|
109
|
+
(subaccount?.approval !== undefined || (0, state_1.isSubaccountStatusUsable)(subaccount?.onchainData)));
|
|
110
|
+
}
|
|
111
|
+
exports.hasActiveSdkSubaccount = hasActiveSdkSubaccount;
|
|
112
|
+
function getSdkSubaccountApprovalMessage(client) {
|
|
113
|
+
const subaccount = client.getSubaccount();
|
|
114
|
+
if (!subaccount) {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
if (subaccount.approval) {
|
|
118
|
+
return subaccount.approval.message;
|
|
119
|
+
}
|
|
120
|
+
return getUsableSubaccountApproval(client);
|
|
121
|
+
}
|
|
122
|
+
exports.getSdkSubaccountApprovalMessage = getSdkSubaccountApprovalMessage;
|
|
123
|
+
async function generateSdkSubaccount(client, mainSigner) {
|
|
124
|
+
client.preparedSubaccountApprovals.clear();
|
|
125
|
+
const result = await (0, generateSubaccount_1.generateSubaccount)(mainSigner);
|
|
126
|
+
sdkSubaccountSigners.set(client.owner, result.signer);
|
|
127
|
+
client.setSubaccount({
|
|
128
|
+
account: mainSigner.address,
|
|
129
|
+
address: result.address,
|
|
130
|
+
});
|
|
131
|
+
return result.address;
|
|
132
|
+
}
|
|
133
|
+
exports.generateSdkSubaccount = generateSdkSubaccount;
|
|
134
|
+
function reconcileSdkSubaccountApproval(subaccount) {
|
|
135
|
+
const approval = subaccount?.approval?.message;
|
|
136
|
+
const status = subaccount?.onchainData;
|
|
137
|
+
if (!subaccount || !approval || !status) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if ((0, state_1.isSubaccountApprovalSynced)(approval, status) ||
|
|
141
|
+
(0, state_1.isSubaccountApprovalNonceExpired)(approval, status) ||
|
|
142
|
+
(0, state_1.isSubaccountApprovalExpired)(approval)) {
|
|
143
|
+
subaccount.approval = undefined;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
exports.reconcileSdkSubaccountApproval = reconcileSdkSubaccountApproval;
|
|
147
|
+
async function refreshSdkSubaccountState(client, account) {
|
|
148
|
+
const subaccount = client.getSubaccount();
|
|
149
|
+
if (!subaccount) {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
if (hasDifferentSubaccountAccount(subaccount, account)) {
|
|
153
|
+
throwSubaccountAccountMismatch(subaccount, account);
|
|
154
|
+
}
|
|
155
|
+
const status = (0, state_1.parseSubaccountStatus)(await (0, api_2.fetchSubaccountStatus)(client.ctx, {
|
|
156
|
+
account,
|
|
157
|
+
subaccountAddress: subaccount.address,
|
|
158
|
+
}));
|
|
159
|
+
subaccount.account = account;
|
|
160
|
+
subaccount.onchainData = status;
|
|
161
|
+
subaccount.lastStatusFetchedAt = Date.now();
|
|
162
|
+
reconcileSdkSubaccountApproval(subaccount);
|
|
163
|
+
return status;
|
|
164
|
+
}
|
|
165
|
+
exports.refreshSdkSubaccountState = refreshSdkSubaccountState;
|
|
166
|
+
async function getSdkSubaccountStatus(client, account, options) {
|
|
167
|
+
const subaccount = client.getSubaccount();
|
|
168
|
+
if (!subaccount) {
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
171
|
+
if (hasDifferentSubaccountAccount(subaccount, account)) {
|
|
172
|
+
throwSubaccountAccountMismatch(subaccount, account);
|
|
173
|
+
}
|
|
174
|
+
if (!options?.force && isSubaccountStatusCacheFresh(subaccount, account)) {
|
|
175
|
+
reconcileSdkSubaccountApproval(subaccount);
|
|
176
|
+
return subaccount.onchainData;
|
|
177
|
+
}
|
|
178
|
+
return refreshSdkSubaccountState(client, account);
|
|
179
|
+
}
|
|
180
|
+
async function activateSdkSubaccount(client, mainSigner, options, knownStatus) {
|
|
181
|
+
const existingSubaccount = client.getSubaccount();
|
|
182
|
+
if (existingSubaccount && hasDifferentSubaccountAccount(existingSubaccount, mainSigner.address)) {
|
|
183
|
+
resetSdkSubaccount(client);
|
|
184
|
+
}
|
|
185
|
+
if (!client.getSubaccount()) {
|
|
186
|
+
await generateSdkSubaccount(client, mainSigner);
|
|
187
|
+
}
|
|
188
|
+
const status = knownStatus ?? (await getSdkSubaccountStatus(client, mainSigner.address, { force: true }));
|
|
189
|
+
const subaccount = client.getSubaccount();
|
|
190
|
+
const requestedExpiresAt = BigInt((0, time_1.nowInSeconds)() + (options?.expiresInSeconds ?? express_1.DEFAULT_SUBACCOUNT_EXPIRY_DURATION));
|
|
191
|
+
const expiresAt = status && status.expiresAt > requestedExpiresAt ? status.expiresAt : requestedExpiresAt;
|
|
192
|
+
const requestedMaxAllowedCount = options?.maxAllowedCount !== undefined
|
|
193
|
+
? BigInt(options.maxAllowedCount)
|
|
194
|
+
: (status?.currentActionsCount ?? 0n) + BigInt(express_1.DEFAULT_SUBACCOUNT_MAX_ALLOWED_COUNT);
|
|
195
|
+
if (status &&
|
|
196
|
+
(0, state_1.isSubaccountStatusUsable)(status) &&
|
|
197
|
+
status.maxAllowedCount >= requestedMaxAllowedCount &&
|
|
198
|
+
status.expiresAt >= requestedExpiresAt) {
|
|
199
|
+
subaccount.approval = undefined;
|
|
200
|
+
return subaccount.address;
|
|
201
|
+
}
|
|
202
|
+
const prepared = await (0, api_2.prepareSubaccountApproval)(client.ctx, {
|
|
203
|
+
account: mainSigner.address,
|
|
204
|
+
subaccountAddress: subaccount.address,
|
|
205
|
+
shouldAdd: status?.active !== true,
|
|
206
|
+
expiresAt: String(expiresAt),
|
|
207
|
+
maxAllowedCount: String(requestedMaxAllowedCount),
|
|
208
|
+
});
|
|
209
|
+
const signature = await (0, api_2.signSubaccountApproval)(prepared, mainSigner, {
|
|
210
|
+
chainId: client.ctx.chainId,
|
|
211
|
+
expectedSubaccountAddress: subaccount.address,
|
|
212
|
+
});
|
|
213
|
+
const message = (0, state_1.toSignedSubaccountApproval)(prepared, signature);
|
|
214
|
+
subaccount.approval = {
|
|
215
|
+
message,
|
|
216
|
+
};
|
|
217
|
+
return subaccount.address;
|
|
218
|
+
}
|
|
219
|
+
exports.activateSdkSubaccount = activateSdkSubaccount;
|
|
220
|
+
async function getSdkSubaccountApprovalForOrder(client, account, mainSigner, options) {
|
|
221
|
+
const subaccount = client.getSubaccount();
|
|
222
|
+
if (!subaccount || !hasSdkSubaccountSigner(client.owner)) {
|
|
223
|
+
return undefined;
|
|
224
|
+
}
|
|
225
|
+
if (hasDifferentSubaccountAccount(subaccount, account)) {
|
|
226
|
+
if (mainSigner && isSameAddress(mainSigner.address, account)) {
|
|
227
|
+
await activateSdkSubaccount(client, mainSigner);
|
|
228
|
+
return client.getSubaccount()?.approval?.message ?? getUsableSubaccountApproval(client);
|
|
229
|
+
}
|
|
230
|
+
throwSubaccountAccountMismatch(subaccount, account);
|
|
231
|
+
}
|
|
232
|
+
const forceStatusRefresh = Boolean(options?.forceRefresh || subaccount.approval?.submittedRequestId !== undefined);
|
|
233
|
+
let status = await getSdkSubaccountStatus(client, account, {
|
|
234
|
+
force: forceStatusRefresh,
|
|
235
|
+
});
|
|
236
|
+
// After forced refresh due to submittedRequestId, clear the flag if approval still exists (relay failed but approval is still valid)
|
|
237
|
+
if (subaccount.approval?.submittedRequestId !== undefined) {
|
|
238
|
+
subaccount.approval.submittedRequestId = undefined;
|
|
239
|
+
}
|
|
240
|
+
const pendingApproval = subaccount.approval;
|
|
241
|
+
if (pendingApproval) {
|
|
242
|
+
if (pendingApproval.submittedRequestId) {
|
|
243
|
+
throw new Error(`Subaccount approval is pending in request ${pendingApproval.submittedRequestId}. ` +
|
|
244
|
+
"Refresh subaccount state after it is submitted on-chain before preparing another subaccount order.");
|
|
245
|
+
}
|
|
246
|
+
if (!(0, state_1.isSubaccountApprovalExpired)(pendingApproval.message)) {
|
|
247
|
+
return pendingApproval.message;
|
|
248
|
+
}
|
|
249
|
+
subaccount.approval = undefined;
|
|
250
|
+
}
|
|
251
|
+
if ((0, state_1.isSubaccountStatusUsable)(status)) {
|
|
252
|
+
if (shouldRefreshLowActionSubaccountStatus(status, forceStatusRefresh)) {
|
|
253
|
+
status = await getSdkSubaccountStatus(client, account, { force: true });
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if ((0, state_1.isSubaccountStatusUsable)(status)) {
|
|
257
|
+
return getUsableSubaccountApproval(client);
|
|
258
|
+
}
|
|
259
|
+
if (mainSigner) {
|
|
260
|
+
await activateSdkSubaccount(client, mainSigner, undefined, status);
|
|
261
|
+
return client.getSubaccount()?.approval?.message ?? getUsableSubaccountApproval(client);
|
|
262
|
+
}
|
|
263
|
+
throw new Error("Subaccount is not active. Call activateSubaccount(mainSigner) before preparing express orders.");
|
|
264
|
+
}
|
|
265
|
+
exports.getSdkSubaccountApprovalForOrder = getSdkSubaccountApprovalForOrder;
|
|
266
|
+
async function prepareWithSubaccount(client, request, prepareFn, mainSigner) {
|
|
267
|
+
let enrichedRequest = request;
|
|
268
|
+
let subaccountApproval;
|
|
269
|
+
const subaccount = client.getSubaccount();
|
|
270
|
+
if (request.mode === "express" && subaccount && hasSdkSubaccountSigner(client.owner)) {
|
|
271
|
+
if (hasDifferentSubaccountAccount(subaccount, request.from)) {
|
|
272
|
+
throwSubaccountAccountMismatch(subaccount, request.from);
|
|
273
|
+
}
|
|
274
|
+
subaccountApproval = request.subaccountApproval;
|
|
275
|
+
assertSdkSubaccountRequest(request, subaccount, subaccountApproval);
|
|
276
|
+
if (subaccountApproval && (0, state_1.isEmptySubaccountApproval)(subaccountApproval)) {
|
|
277
|
+
subaccountApproval = await getSdkSubaccountApprovalForOrder(client, request.from, mainSigner);
|
|
278
|
+
}
|
|
279
|
+
else if (subaccountApproval &&
|
|
280
|
+
shouldRefreshSubaccountApprovalForPrepare(subaccountApproval, subaccount, request.from)) {
|
|
281
|
+
subaccountApproval = await getSdkSubaccountApprovalForOrder(client, request.from, mainSigner, {
|
|
282
|
+
forceRefresh: true,
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
else if (!subaccountApproval) {
|
|
286
|
+
subaccountApproval = await getSdkSubaccountApprovalForOrder(client, request.from, mainSigner);
|
|
287
|
+
}
|
|
288
|
+
else if ((0, state_1.isEmptySubaccountApproval)(subaccountApproval)) {
|
|
289
|
+
let status = await getSdkSubaccountStatus(client, request.from);
|
|
290
|
+
if (shouldRefreshLowActionSubaccountStatus(status, false)) {
|
|
291
|
+
status = await getSdkSubaccountStatus(client, request.from, { force: true });
|
|
292
|
+
}
|
|
293
|
+
if (!(0, state_1.isSubaccountStatusUsable)(status)) {
|
|
294
|
+
throw new Error("Subaccount is not active. Call activateSubaccount(mainSigner) before preparing express orders.");
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (subaccountApproval) {
|
|
298
|
+
assertSdkSubaccountRequest(request, client.getSubaccount(), subaccountApproval);
|
|
299
|
+
enrichedRequest = {
|
|
300
|
+
...request,
|
|
301
|
+
subaccountAddress: request.subaccountAddress ?? client.getSubaccount().address,
|
|
302
|
+
subaccountApproval,
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
const prepared = await prepareFn(client.ctx, enrichedRequest);
|
|
307
|
+
if (prepared.requestId && subaccountApproval) {
|
|
308
|
+
client.preparedSubaccountApprovals.set(prepared.requestId, subaccountApproval);
|
|
309
|
+
prunePreparedSubaccountApprovals(client);
|
|
310
|
+
}
|
|
311
|
+
return prepared;
|
|
312
|
+
}
|
|
313
|
+
exports.prepareWithSubaccount = prepareWithSubaccount;
|
|
314
|
+
function signOrderWithSubaccount(client, prepared, signer) {
|
|
315
|
+
const subSigner = client.preparedSubaccountApprovals.has(prepared.requestId)
|
|
316
|
+
? getSdkSubaccountSigner(client.owner)
|
|
317
|
+
: undefined;
|
|
318
|
+
const effectiveSigner = subSigner ?? signer;
|
|
319
|
+
const accountAddress = subSigner ? signer.address : undefined;
|
|
320
|
+
return (0, api_1.signPreparedOrder)(prepared, effectiveSigner, client.ctx.chainId, accountAddress);
|
|
321
|
+
}
|
|
322
|
+
exports.signOrderWithSubaccount = signOrderWithSubaccount;
|
|
323
|
+
async function submitOrderWithSubaccount(client, request) {
|
|
324
|
+
const subaccountApproval = request.requestId ? client.preparedSubaccountApprovals.get(request.requestId) : undefined;
|
|
325
|
+
const enrichedRequest = subaccountApproval && request.eip712Data
|
|
326
|
+
? {
|
|
327
|
+
...request,
|
|
328
|
+
eip712Data: {
|
|
329
|
+
...request.eip712Data,
|
|
330
|
+
subaccountApproval,
|
|
331
|
+
},
|
|
332
|
+
}
|
|
333
|
+
: request;
|
|
334
|
+
const response = await (0, api_1.submitOrder)(client.ctx, enrichedRequest);
|
|
335
|
+
if (isOrderSubmitAccepted(response)) {
|
|
336
|
+
markSubaccountApprovalSubmitted(client.getSubaccount(), subaccountApproval, request.requestId ?? response.requestId);
|
|
337
|
+
if (FINAL_ORDER_STATUSES.has(response.status)) {
|
|
338
|
+
markSubaccountActionSubmitted(client.getSubaccount(), subaccountApproval);
|
|
339
|
+
}
|
|
340
|
+
if (request.requestId) {
|
|
341
|
+
client.preparedSubaccountApprovals.delete(request.requestId);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return response;
|
|
345
|
+
}
|
|
346
|
+
exports.submitOrderWithSubaccount = submitOrderWithSubaccount;
|
|
347
|
+
async function executeExpressOrderWithSubaccount(client, request, signer) {
|
|
348
|
+
const prepared = await prepareWithSubaccount(client, request, api_1.prepareOrder, signer);
|
|
349
|
+
const signature = await signOrderWithSubaccount(client, prepared, signer);
|
|
350
|
+
const isSubaccountOrder = client.preparedSubaccountApprovals.has(prepared.requestId);
|
|
351
|
+
return submitOrderWithSubaccount(client, {
|
|
352
|
+
mode: prepared.mode,
|
|
353
|
+
requestId: prepared.requestId,
|
|
354
|
+
signature,
|
|
355
|
+
from: isSubaccountOrder ? request.from : signer.address,
|
|
356
|
+
idempotencyKey: prepared.idempotencyKey,
|
|
357
|
+
eip712Data: {
|
|
358
|
+
batchParams: prepared.payload.batchParams,
|
|
359
|
+
relayParams: prepared.payload.relayParams,
|
|
360
|
+
},
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
exports.executeExpressOrderWithSubaccount = executeExpressOrderWithSubaccount;
|
|
364
|
+
function clearSdkSubaccount(client) {
|
|
365
|
+
resetSdkSubaccount(client);
|
|
366
|
+
}
|
|
367
|
+
exports.clearSdkSubaccount = clearSdkSubaccount;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toSignedSubaccountApproval = exports.isSubaccountApprovalExpired = exports.isSubaccountApprovalSynced = exports.isSubaccountApprovalNonceExpired = exports.shouldRefreshExplicitSubaccountApproval = exports.isSameSubaccountApproval = exports.isEmptySubaccountApproval = exports.isSubaccountStatusUsable = exports.parseSubaccountStatus = void 0;
|
|
4
|
+
const numbers_1 = require("../numbers");
|
|
5
|
+
const time_1 = require("../time");
|
|
6
|
+
function parseSubaccountStatus(status) {
|
|
7
|
+
return {
|
|
8
|
+
active: status.active,
|
|
9
|
+
maxAllowedCount: (0, numbers_1.toBigInt)(status.maxAllowedCount) ?? 0n,
|
|
10
|
+
currentActionsCount: (0, numbers_1.toBigInt)(status.currentActionsCount) ?? 0n,
|
|
11
|
+
remainingActions: (0, numbers_1.toBigInt)(status.remainingActions) ?? 0n,
|
|
12
|
+
expiresAt: (0, numbers_1.toBigInt)(status.expiresAt) ?? 0n,
|
|
13
|
+
approvalNonce: (0, numbers_1.toBigInt)(status.approvalNonce) ?? 0n,
|
|
14
|
+
multichainApprovalNonce: (0, numbers_1.toBigInt)(status.multichainApprovalNonce) ?? 0n,
|
|
15
|
+
integrationId: status.integrationId ?? undefined,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
exports.parseSubaccountStatus = parseSubaccountStatus;
|
|
19
|
+
function isSubaccountStatusUsable(status) {
|
|
20
|
+
return (status !== undefined && status.active && status.remainingActions > 0n && status.expiresAt > BigInt((0, time_1.nowInSeconds)()));
|
|
21
|
+
}
|
|
22
|
+
exports.isSubaccountStatusUsable = isSubaccountStatusUsable;
|
|
23
|
+
function isEmptySubaccountApproval(approval) {
|
|
24
|
+
return approval.signature === "0x";
|
|
25
|
+
}
|
|
26
|
+
exports.isEmptySubaccountApproval = isEmptySubaccountApproval;
|
|
27
|
+
function isSameSubaccountApproval(a, b) {
|
|
28
|
+
return a.signature === b.signature && (0, numbers_1.toBigInt)(a.nonce) === (0, numbers_1.toBigInt)(b.nonce);
|
|
29
|
+
}
|
|
30
|
+
exports.isSameSubaccountApproval = isSameSubaccountApproval;
|
|
31
|
+
function shouldRefreshExplicitSubaccountApproval(approval, status, pendingApproval) {
|
|
32
|
+
if (isEmptySubaccountApproval(approval)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
if (pendingApproval?.submittedRequestId && isSameSubaccountApproval(approval, pendingApproval.message)) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
return (status !== undefined &&
|
|
39
|
+
(isSubaccountApprovalSynced(approval, status) ||
|
|
40
|
+
isSubaccountApprovalNonceExpired(approval, status) ||
|
|
41
|
+
isSubaccountApprovalExpired(approval)));
|
|
42
|
+
}
|
|
43
|
+
exports.shouldRefreshExplicitSubaccountApproval = shouldRefreshExplicitSubaccountApproval;
|
|
44
|
+
function isSubaccountApprovalNonceExpired(approval, status) {
|
|
45
|
+
return (0, numbers_1.toBigInt)(approval.nonce) !== status.approvalNonce;
|
|
46
|
+
}
|
|
47
|
+
exports.isSubaccountApprovalNonceExpired = isSubaccountApprovalNonceExpired;
|
|
48
|
+
function isSubaccountApprovalSynced(approval, status) {
|
|
49
|
+
return (status.active &&
|
|
50
|
+
status.maxAllowedCount === (0, numbers_1.toBigInt)(approval.maxAllowedCount) &&
|
|
51
|
+
status.expiresAt === (0, numbers_1.toBigInt)(approval.expiresAt));
|
|
52
|
+
}
|
|
53
|
+
exports.isSubaccountApprovalSynced = isSubaccountApprovalSynced;
|
|
54
|
+
function isSubaccountApprovalExpired(approval) {
|
|
55
|
+
const now = BigInt((0, time_1.nowInSeconds)());
|
|
56
|
+
return ((0, numbers_1.toBigInt)(approval.expiresAt) ?? 0n) <= now || ((0, numbers_1.toBigInt)(approval.deadline) ?? 0n) <= now;
|
|
57
|
+
}
|
|
58
|
+
exports.isSubaccountApprovalExpired = isSubaccountApprovalExpired;
|
|
59
|
+
function toSignedSubaccountApproval(prepared, signature) {
|
|
60
|
+
return {
|
|
61
|
+
subaccount: prepared.message.subaccount,
|
|
62
|
+
shouldAdd: Boolean(prepared.message.shouldAdd),
|
|
63
|
+
expiresAt: (0, numbers_1.toBigInt)(prepared.message.expiresAt) ?? 0n,
|
|
64
|
+
maxAllowedCount: (0, numbers_1.toBigInt)(prepared.message.maxAllowedCount) ?? 0n,
|
|
65
|
+
actionType: prepared.message.actionType,
|
|
66
|
+
nonce: (0, numbers_1.toBigInt)(prepared.message.nonce ?? prepared.nonce) ?? 0n,
|
|
67
|
+
desChainId: (0, numbers_1.toBigInt)(prepared.message.desChainId) ?? 0n,
|
|
68
|
+
deadline: (0, numbers_1.toBigInt)(prepared.message.deadline) ?? 0n,
|
|
69
|
+
integrationId: prepared.message.integrationId,
|
|
70
|
+
signature,
|
|
71
|
+
signedAt: Date.now(),
|
|
72
|
+
signatureChainId: prepared.domain.chainId,
|
|
73
|
+
subaccountRouterAddress: prepared.relayRouterAddress,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
exports.toSignedSubaccountApproval = toSignedSubaccountApproval;
|