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