@lifi/perps-sdk 18.1.0 → 19.0.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/dist/cjs/client/PerpsClient.d.ts +13 -7
- package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
- package/dist/cjs/client/PerpsClient.js +141 -52
- package/dist/cjs/client/PerpsClient.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/api.d.ts +4 -2
- package/dist/cjs/types/api.d.ts.map +1 -1
- package/dist/cjs/types/provider.d.ts +4 -5
- package/dist/cjs/types/provider.d.ts.map +1 -1
- package/dist/cjs/utils/setupActions.d.ts +4 -2
- package/dist/cjs/utils/setupActions.d.ts.map +1 -1
- package/dist/cjs/utils/setupActions.js +9 -1
- package/dist/cjs/utils/setupActions.js.map +1 -1
- package/dist/cjs/utils/switchChain.js +1 -1
- package/dist/cjs/utils/switchChain.js.map +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/client/PerpsClient.d.ts +62 -54
- package/dist/esm/client/PerpsClient.d.ts.map +1 -1
- package/dist/esm/client/PerpsClient.js +204 -100
- package/dist/esm/client/PerpsClient.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/api.d.ts +31 -14
- package/dist/esm/types/api.d.ts.map +1 -1
- package/dist/esm/types/provider.d.ts +21 -27
- package/dist/esm/types/provider.d.ts.map +1 -1
- package/dist/esm/utils/setupActions.d.ts +17 -8
- package/dist/esm/utils/setupActions.d.ts.map +1 -1
- package/dist/esm/utils/setupActions.js +20 -7
- package/dist/esm/utils/setupActions.js.map +1 -1
- package/dist/esm/utils/switchChain.js +1 -1
- package/dist/esm/utils/switchChain.js.map +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/types/client/PerpsClient.d.ts +62 -54
- package/dist/types/client/PerpsClient.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/api.d.ts +31 -14
- package/dist/types/types/api.d.ts.map +1 -1
- package/dist/types/types/provider.d.ts +21 -27
- package/dist/types/types/provider.d.ts.map +1 -1
- package/dist/types/utils/setupActions.d.ts +17 -8
- package/dist/types/utils/setupActions.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/package.json +2 -2
- package/src/client/PerpsClient.ts +284 -129
- package/src/index.ts +4 -1
- package/src/types/api.ts +32 -14
- package/src/types/provider.ts +21 -29
- package/src/utils/setupActions.ts +27 -13
- package/src/utils/switchChain.ts +1 -1
- package/src/version.ts +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ActionType, META_PROVIDER, PerpsErrorCode, PerpsSigner, SigningMethod, } from '@lifi/perps-types';
|
|
1
|
+
import { ActionRelay, ActionType, META_PROVIDER, PerpsErrorCode, PerpsSigner, SigningMethod, } from '@lifi/perps-types';
|
|
2
2
|
import Big from 'big.js';
|
|
3
3
|
import { PerpsError } from '../errors/PerpsError.js';
|
|
4
4
|
import { getAssetRegistry, toAssetDisplay } from '../registry/assetRegistry.js';
|
|
@@ -9,10 +9,95 @@ import { getAccount as fetchAccount } from '../services/getAccount.js';
|
|
|
9
9
|
import { getOrder as fetchOrder, } from '../services/getOrder.js';
|
|
10
10
|
import { getOrders as fetchOrders, } from '../services/getOrders.js';
|
|
11
11
|
import { getProviders } from '../services/getProviders.js';
|
|
12
|
+
import { isUserFacingSetupStep } from '../utils/setupActions.js';
|
|
12
13
|
import { signTypedDataWithSigner } from '../utils/signTypedData.js';
|
|
13
14
|
import { eip712DomainChainId, switchSigningChain, userEip712TargetChainId, } from '../utils/switchChain.js';
|
|
14
15
|
import { createPerpsClient } from './createPerpsClient.js';
|
|
15
16
|
import { requireProvider as resolveProvider } from './requireProvider.js';
|
|
17
|
+
/** Absent `sequence` sorts last, so an unordered step gates nothing. */
|
|
18
|
+
function sequenceOf(descriptor) {
|
|
19
|
+
return descriptor.sequence ?? Number.MAX_SAFE_INTEGER;
|
|
20
|
+
}
|
|
21
|
+
const SIGNERS = new Set(Object.values(PerpsSigner));
|
|
22
|
+
const RELAYS = new Set(Object.values(ActionRelay));
|
|
23
|
+
/**
|
|
24
|
+
* Reject provider metadata whose steps the SDK cannot classify. A step with no
|
|
25
|
+
* known `signer`, `relay`, `options` or `revoke` would otherwise read as
|
|
26
|
+
* satisfied and report a not-onboarded account as ready.
|
|
27
|
+
*/
|
|
28
|
+
function assertSetupContract(metadata) {
|
|
29
|
+
const reject = (step, problem) => {
|
|
30
|
+
throw new PerpsError(PerpsErrorCode.SDKError, `Provider '${metadata.key}' step '${step}' ${problem}; this SDK needs a ` +
|
|
31
|
+
`backend that emits signer, relay, options and revoke.`);
|
|
32
|
+
};
|
|
33
|
+
for (const descriptor of [...metadata.setup, ...metadata.actions]) {
|
|
34
|
+
if (!SIGNERS.has(descriptor.signer)) {
|
|
35
|
+
reject(descriptor.type, 'declares no known signer');
|
|
36
|
+
}
|
|
37
|
+
if (!RELAYS.has(descriptor.relay)) {
|
|
38
|
+
reject(descriptor.type, 'declares no known relay');
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const actionTypes = new Set(metadata.actions.map((d) => d.type));
|
|
42
|
+
for (const descriptor of metadata.setup) {
|
|
43
|
+
if (descriptor.options === undefined) {
|
|
44
|
+
reject(descriptor.type, 'declares no options');
|
|
45
|
+
}
|
|
46
|
+
if (descriptor.options !== null && descriptor.options.length === 0) {
|
|
47
|
+
reject(descriptor.type, 'declares an empty options list');
|
|
48
|
+
}
|
|
49
|
+
if (descriptor.revoke === undefined) {
|
|
50
|
+
reject(descriptor.type, 'declares no revoke');
|
|
51
|
+
}
|
|
52
|
+
if (descriptor.revoke !== null && !actionTypes.has(descriptor.revoke)) {
|
|
53
|
+
reject(descriptor.type, `revokes with '${descriptor.revoke}', which Provider.actions does not declare`);
|
|
54
|
+
}
|
|
55
|
+
const defaults = (descriptor.options ?? []).filter((option) => option.default === true);
|
|
56
|
+
if (defaults.length > 1) {
|
|
57
|
+
reject(descriptor.type, 'marks more than one option default');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** Only a USER-signed non-choice step is staged for the user to sign. */
|
|
62
|
+
function isStageable(descriptor) {
|
|
63
|
+
return descriptor.options === null && descriptor.signer === PerpsSigner.USER;
|
|
64
|
+
}
|
|
65
|
+
function satisfiedTypes(settings) {
|
|
66
|
+
return new Set(settings.filter((setting) => setting.satisfied).map((s) => s.type));
|
|
67
|
+
}
|
|
68
|
+
/** The choice option whose every bound param equals the projected value. */
|
|
69
|
+
function selectedOption(descriptor, settings) {
|
|
70
|
+
if (descriptor.options === null) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
const values = settings.find((setting) => setting.type === descriptor.type)?.values ?? [];
|
|
74
|
+
return (descriptor.options.find((option) => {
|
|
75
|
+
const bound = Object.entries(option.params);
|
|
76
|
+
return (bound.length > 0 &&
|
|
77
|
+
bound.every(([name, value]) => values.some((v) => v.name === name && v.value === value)));
|
|
78
|
+
}) ?? null);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The projected value of the param a choice's options bind, as the venue
|
|
82
|
+
* names it, whether or not an option binds that value.
|
|
83
|
+
*/
|
|
84
|
+
function currentChoiceValue(descriptor, settings) {
|
|
85
|
+
if (descriptor.options === null) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
const names = new Set(descriptor.options.flatMap((option) => Object.keys(option.params)));
|
|
89
|
+
const value = settings
|
|
90
|
+
.find((setting) => setting.type === descriptor.type)
|
|
91
|
+
?.values.find((v) => names.has(v.name))?.value ?? null;
|
|
92
|
+
return value === null ? null : String(value);
|
|
93
|
+
}
|
|
94
|
+
/** Throw the first per-action failure a 200 OK `/executeAction` carried. */
|
|
95
|
+
function assertAllSucceeded(results) {
|
|
96
|
+
const failure = results.find((r) => !r.success);
|
|
97
|
+
if (failure) {
|
|
98
|
+
throw new PerpsError(failure.errorCode ?? PerpsErrorCode.ExchangeRejected, failure.error);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
16
101
|
function isNonNegativeDecimal(value) {
|
|
17
102
|
try {
|
|
18
103
|
return new Big(value).gte(0);
|
|
@@ -26,11 +111,7 @@ function isNonNegativeDecimal(value) {
|
|
|
26
111
|
* action isn't declared — defensive: better to fail loudly than to mis-sign.
|
|
27
112
|
*/
|
|
28
113
|
function findActionDescriptor(metadata, action) {
|
|
29
|
-
const descriptor = [
|
|
30
|
-
...metadata.setup,
|
|
31
|
-
...metadata.options,
|
|
32
|
-
...metadata.actions,
|
|
33
|
-
].find((d) => d.type === action);
|
|
114
|
+
const descriptor = [...metadata.setup, ...metadata.actions].find((d) => d.type === action);
|
|
34
115
|
if (!descriptor) {
|
|
35
116
|
throw new PerpsError(PerpsErrorCode.SDKError, `Provider '${metadata.key}' does not declare action '${action}'.`);
|
|
36
117
|
}
|
|
@@ -82,7 +163,7 @@ export class PerpsClient {
|
|
|
82
163
|
}
|
|
83
164
|
/**
|
|
84
165
|
* Set or update the end-user's wallet. Used whenever an action's descriptor
|
|
85
|
-
* names
|
|
166
|
+
* names `PerpsSigner.USER` as its `signer`. Pass undefined to clear.
|
|
86
167
|
*
|
|
87
168
|
* @public
|
|
88
169
|
*/
|
|
@@ -113,20 +194,20 @@ export class PerpsClient {
|
|
|
113
194
|
return this.sdkClient;
|
|
114
195
|
}
|
|
115
196
|
async getProviderMetadata(provider) {
|
|
116
|
-
|
|
117
|
-
if (
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
this.providerMetadataCache.
|
|
197
|
+
let metadata = this.providerMetadataCache.get(provider);
|
|
198
|
+
if (!metadata) {
|
|
199
|
+
const { providers } = await getProviders(this.sdkClient);
|
|
200
|
+
for (const d of providers) {
|
|
201
|
+
this.providerMetadataCache.set(d.key, d);
|
|
202
|
+
}
|
|
203
|
+
metadata = this.providerMetadataCache.get(provider);
|
|
123
204
|
}
|
|
124
|
-
const metadata = this.providerMetadataCache.get(provider);
|
|
125
205
|
if (!metadata) {
|
|
126
206
|
const error = new PerpsError(PerpsErrorCode.SDKError, `Unsupported provider: ${provider}`);
|
|
127
207
|
error.tool = '@lifi/perps-sdk';
|
|
128
208
|
throw error;
|
|
129
209
|
}
|
|
210
|
+
assertSetupContract(metadata);
|
|
130
211
|
return metadata;
|
|
131
212
|
}
|
|
132
213
|
/**
|
|
@@ -149,7 +230,7 @@ export class PerpsClient {
|
|
|
149
230
|
* Ask the provider plugin for the signer-bearing wire fields of `action` —
|
|
150
231
|
* the on-wire `signerAddress` and any signer-derived params (e.g.
|
|
151
232
|
* Hyperliquid's `agentAddress` for `APPROVE_AGENT`). Forwards the descriptor's
|
|
152
|
-
* `
|
|
233
|
+
* `signer` so the plugin can branch on signer role. Returns empty when the
|
|
153
234
|
* plugin signs as the user or with a non-EVM credential. Core constructs no
|
|
154
235
|
* `signerAddress` itself; signer identity is plugin-owned.
|
|
155
236
|
*/
|
|
@@ -158,15 +239,15 @@ export class PerpsClient {
|
|
|
158
239
|
if (typeof plugin.resolveActionRequest !== 'function') {
|
|
159
240
|
return {};
|
|
160
241
|
}
|
|
161
|
-
return plugin.resolveActionRequest(descriptor.type, address, descriptor.
|
|
242
|
+
return plugin.resolveActionRequest(descriptor.type, address, descriptor.signer);
|
|
162
243
|
}
|
|
163
244
|
/**
|
|
164
245
|
* Delegate signing of `actions` to the provider plugin, one call per
|
|
165
246
|
* consecutive run of steps sharing a descriptor. Each call carries that
|
|
166
|
-
* descriptor's `signingMethod` and `
|
|
247
|
+
* descriptor's `signingMethod` and `signer`, so a mixed batch signs every
|
|
167
248
|
* step with the signer its own descriptor declares; the signed steps come
|
|
168
249
|
* back in the batch's original order. The plugin owns every signing arm and
|
|
169
|
-
* branches on `
|
|
250
|
+
* branches on `signer` internally, reading the end-user's wallet from the
|
|
170
251
|
* {@link SignActionsContext} when an arm signs as the user.
|
|
171
252
|
*
|
|
172
253
|
* @throws {PerpsError} When a step's action is not declared by the provider,
|
|
@@ -250,9 +331,9 @@ export class PerpsClient {
|
|
|
250
331
|
}
|
|
251
332
|
/**
|
|
252
333
|
* Assemble the per-call context the provider plugin needs in order to sign:
|
|
253
|
-
* the end-user's wallet and the descriptor's declared `
|
|
254
|
-
* forwards `
|
|
255
|
-
* branch on
|
|
334
|
+
* the end-user's wallet and the descriptor's declared `signer`. Core
|
|
335
|
+
* forwards `signer` as data so the plugin can pick WHO signs; it does not
|
|
336
|
+
* branch on it. Provider-owned session credentials (the Hyperliquid agent
|
|
256
337
|
* keypair, Lighter's API key) are resolved inside the provider's
|
|
257
338
|
* `signActions`, not threaded through here.
|
|
258
339
|
*
|
|
@@ -266,7 +347,7 @@ export class PerpsClient {
|
|
|
266
347
|
* `sdkClient.userWallet`.
|
|
267
348
|
*/
|
|
268
349
|
buildSignActionsContext(descriptor, userWallet, onProgress) {
|
|
269
|
-
const ctx = {
|
|
350
|
+
const ctx = { signer: descriptor.signer };
|
|
270
351
|
if (onProgress !== undefined) {
|
|
271
352
|
ctx.onProgress = onProgress;
|
|
272
353
|
}
|
|
@@ -321,7 +402,7 @@ export class PerpsClient {
|
|
|
321
402
|
/**
|
|
322
403
|
* Fetch the user's account state from the backend and attach the
|
|
323
404
|
* SDK-projected `settings` array — one `AccountConfigSetting` per
|
|
324
|
-
* descriptor on `Provider.setup
|
|
405
|
+
* descriptor on `Provider.setup`. Callers read
|
|
325
406
|
* `result.settings` directly without re-deriving values from the typed
|
|
326
407
|
* `AccountConfig`.
|
|
327
408
|
*
|
|
@@ -335,7 +416,7 @@ export class PerpsClient {
|
|
|
335
416
|
fetchAccount(this.sdkClient, params),
|
|
336
417
|
this.getProviderMetadata(params.provider),
|
|
337
418
|
]);
|
|
338
|
-
const settings = plugin.projectConfig(response.config, metadata.setup
|
|
419
|
+
const settings = plugin.projectConfig(response.config, metadata.setup);
|
|
339
420
|
return { ...response, settings };
|
|
340
421
|
}
|
|
341
422
|
/**
|
|
@@ -525,15 +606,17 @@ export class PerpsClient {
|
|
|
525
606
|
return plugin.getPortfolioHistory({ address: params.address, range: params.range }, options);
|
|
526
607
|
}
|
|
527
608
|
/**
|
|
528
|
-
* Return the
|
|
529
|
-
*
|
|
530
|
-
* `checklist` carries the renderable onboarding list: every
|
|
531
|
-
*
|
|
532
|
-
* omitted.
|
|
609
|
+
* Return the setup steps this account must still sign as a flat,
|
|
610
|
+
* self-describing list. Trading is gated on `isReady === true`.
|
|
611
|
+
* `checklist` carries the renderable onboarding list: every choice step and
|
|
612
|
+
* every `USER`-signed step with its satisfied state, with not-required
|
|
613
|
+
* conditional steps omitted.
|
|
533
614
|
*
|
|
534
|
-
* `
|
|
535
|
-
*
|
|
536
|
-
*
|
|
615
|
+
* A non-choice `SDK`-signed step is NEVER returned here — the SDK drains it
|
|
616
|
+
* with the provider's own credentials. A choice is listed but never staged
|
|
617
|
+
* or executed, whatever its `signer`: the user picks an option through
|
|
618
|
+
* {@link executeProviderOption}, and `isReady` stays `false` until every
|
|
619
|
+
* choice on `checklist` is satisfied.
|
|
537
620
|
*
|
|
538
621
|
* @public
|
|
539
622
|
*/
|
|
@@ -554,61 +637,58 @@ export class PerpsClient {
|
|
|
554
637
|
checklist: [],
|
|
555
638
|
};
|
|
556
639
|
}
|
|
557
|
-
const
|
|
640
|
+
const settings = await this.resolveSetupSettings(provider, address);
|
|
641
|
+
const satisfiedSetup = satisfiedTypes(settings);
|
|
558
642
|
const pendingSetup = metadata.setup.filter((descriptor) => !satisfiedSetup.has(descriptor.type));
|
|
559
643
|
const plugin = this.sdkClient.getProvider(provider);
|
|
560
|
-
const
|
|
561
|
-
const isInternal = (descriptor) => internalTypes.has(descriptor.type) &&
|
|
562
|
-
!descriptor.signers.includes(PerpsSigner.USER);
|
|
563
|
-
const visibleSetup = pendingSetup.filter((descriptor) => !isInternal(descriptor));
|
|
644
|
+
const stageable = pendingSetup.filter(isStageable);
|
|
564
645
|
// The backend filters already-satisfied setup actions and returns typed
|
|
565
646
|
// data for those still outstanding; each plugin contributes its own
|
|
566
647
|
// signer-bearing request fields.
|
|
567
|
-
const actions = await this.buildProviderSetupActions(provider, address,
|
|
648
|
+
const actions = await this.buildProviderSetupActions(provider, address, stageable);
|
|
568
649
|
// A staged step is one the build produced actions for — a pending
|
|
569
650
|
// descriptor the backend staged nothing for is either satisfied
|
|
570
651
|
// (backend-gated) or not applicable to this account.
|
|
571
652
|
const stagedTypes = new Set(actions.map((step) => step.action));
|
|
572
653
|
const conditionalTypes = new Set(plugin?.conditionalSetupActions ?? []);
|
|
573
|
-
const sequenceOf = (descriptor) => descriptor.sequence ?? Number.MAX_SAFE_INTEGER;
|
|
574
654
|
const checklist = metadata.setup
|
|
575
|
-
.filter(
|
|
576
|
-
.filter((descriptor) =>
|
|
655
|
+
.filter(isUserFacingSetupStep)
|
|
656
|
+
.filter((descriptor) => descriptor.options !== null ||
|
|
657
|
+
!conditionalTypes.has(descriptor.type) ||
|
|
577
658
|
stagedTypes.has(descriptor.type))
|
|
578
659
|
.sort((a, b) => sequenceOf(a) - sequenceOf(b))
|
|
579
660
|
.map((descriptor) => ({
|
|
580
661
|
descriptor,
|
|
581
|
-
satisfied:
|
|
662
|
+
satisfied: descriptor.options !== null
|
|
663
|
+
? satisfiedSetup.has(descriptor.type)
|
|
664
|
+
: !stagedTypes.has(descriptor.type),
|
|
665
|
+
selected: selectedOption(descriptor, settings),
|
|
666
|
+
currentValue: currentChoiceValue(descriptor, settings),
|
|
582
667
|
}));
|
|
583
|
-
await this.
|
|
668
|
+
await this.drainSetup(provider, address, pendingSetup.filter((descriptor) => descriptor.options === null && descriptor.signer === PerpsSigner.SDK), stageable.filter((descriptor) => stagedTypes.has(descriptor.type)));
|
|
584
669
|
return {
|
|
585
670
|
accountExists: true,
|
|
586
671
|
setup: actions,
|
|
587
|
-
isReady: actions.length === 0,
|
|
672
|
+
isReady: actions.length === 0 && checklist.every((item) => item.satisfied),
|
|
588
673
|
checklist,
|
|
589
674
|
};
|
|
590
675
|
}
|
|
591
676
|
/**
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
*
|
|
677
|
+
* Project the provider's own typed account config onto its setup steps. This
|
|
678
|
+
* catches client-held auth state (e.g. Ondo SIWE/JWT) that the backend
|
|
679
|
+
* cannot observe, and carries the current value of each choice.
|
|
595
680
|
*/
|
|
596
|
-
async
|
|
681
|
+
async resolveSetupSettings(provider, address) {
|
|
597
682
|
const metadata = await this.getProviderMetadata(provider);
|
|
598
683
|
const plugin = this.sdkClient.getProvider(provider);
|
|
599
684
|
if (!plugin || typeof plugin.getAccount !== 'function') {
|
|
600
|
-
return
|
|
685
|
+
return [];
|
|
601
686
|
}
|
|
602
687
|
const account = await plugin.getAccount({ address });
|
|
603
|
-
|
|
604
|
-
const setupTypes = new Set(metadata.setup.map((descriptor) => descriptor.type));
|
|
605
|
-
return new Set(settings
|
|
606
|
-
.filter((setting) => setting.satisfied && setupTypes.has(setting.type))
|
|
607
|
-
.map((setting) => setting.type));
|
|
688
|
+
return plugin.projectConfig(account.config, metadata.setup);
|
|
608
689
|
}
|
|
609
690
|
/**
|
|
610
|
-
* Drain the pending
|
|
611
|
-
* `PerpsProviderPlugin.internalSetupActions`. Each step is built, signed,
|
|
691
|
+
* Drain the pending `SDK`-signed non-choice steps: each is built, signed,
|
|
612
692
|
* and executed in place with the provider's own credentials. A step is
|
|
613
693
|
* deferred while any staged user-facing step with a lower `sequence` is
|
|
614
694
|
* outstanding — it cannot succeed before its prerequisite (e.g. SET_REFERRAL
|
|
@@ -617,11 +697,10 @@ export class PerpsClient {
|
|
|
617
697
|
* blocks setup — the step stays unsatisfied and is retried on a later
|
|
618
698
|
* `checkSetup`.
|
|
619
699
|
*/
|
|
620
|
-
async
|
|
621
|
-
const
|
|
622
|
-
for (const descriptor of internalPending) {
|
|
700
|
+
async drainSetup(provider, address, drainable, stagedVisible) {
|
|
701
|
+
for (const descriptor of drainable) {
|
|
623
702
|
// A tie (equal sequences, or both absent) declares no order, so the
|
|
624
|
-
//
|
|
703
|
+
// drained step defers to the next checkSetup rather than racing.
|
|
625
704
|
const blocked = stagedVisible.some((staged) => sequenceOf(staged) <= sequenceOf(descriptor));
|
|
626
705
|
if (blocked) {
|
|
627
706
|
continue;
|
|
@@ -643,7 +722,7 @@ export class PerpsClient {
|
|
|
643
722
|
}
|
|
644
723
|
}
|
|
645
724
|
catch (error) {
|
|
646
|
-
console.debug(`[perps-sdk]
|
|
725
|
+
console.debug(`[perps-sdk] setup step '${descriptor.type}' for '${provider}' did not drain; will retry on the next checkSetup.`, error);
|
|
647
726
|
}
|
|
648
727
|
}
|
|
649
728
|
}
|
|
@@ -680,16 +759,18 @@ export class PerpsClient {
|
|
|
680
759
|
}
|
|
681
760
|
/**
|
|
682
761
|
* Build the unsigned setup `ActionStep`s still outstanding for an account,
|
|
683
|
-
* ordered by descriptor `sequence`.
|
|
684
|
-
*
|
|
685
|
-
*
|
|
762
|
+
* ordered by descriptor `sequence`. Only a `USER`-signed non-choice step is
|
|
763
|
+
* staged: an `SDK`-signed non-choice step drains inside {@link checkSetup}
|
|
764
|
+
* and a choice executes the option the user picks. The backend filters
|
|
765
|
+
* already-satisfied setup; each plugin contributes its own signer-bearing
|
|
766
|
+
* request fields and any local-state params (e.g. Lighter's known pubkey).
|
|
686
767
|
*
|
|
687
768
|
* @public
|
|
688
769
|
*/
|
|
689
770
|
async buildProviderSetup(params) {
|
|
690
771
|
const { provider, address } = params;
|
|
691
772
|
const metadata = await this.getProviderMetadata(provider);
|
|
692
|
-
const actions = await this.buildProviderSetupActions(provider, address, metadata.setup);
|
|
773
|
+
const actions = await this.buildProviderSetupActions(provider, address, metadata.setup.filter(isStageable));
|
|
693
774
|
return { actions };
|
|
694
775
|
}
|
|
695
776
|
/**
|
|
@@ -718,35 +799,33 @@ export class PerpsClient {
|
|
|
718
799
|
});
|
|
719
800
|
const results = this.resolveExplorerLinks(provider, response.results);
|
|
720
801
|
await this.notifyExecuteResults(provider, address, results);
|
|
721
|
-
|
|
722
|
-
if (failure) {
|
|
723
|
-
throw new PerpsError(failure.errorCode ?? PerpsErrorCode.ExchangeRejected, failure.error);
|
|
724
|
-
}
|
|
802
|
+
assertAllSucceeded(results);
|
|
725
803
|
return { results: { results } };
|
|
726
804
|
}
|
|
727
805
|
/**
|
|
728
806
|
* Sign and submit one pre-staged setup `ActionStep` end-to-end.
|
|
729
807
|
*
|
|
730
|
-
*
|
|
731
|
-
*
|
|
732
|
-
*
|
|
733
|
-
*
|
|
734
|
-
* through submit. If the cached step has gone stale (Lighter's `/nextNonce`
|
|
735
|
-
* advanced underneath us), `executeProviderSetup` will surface a nonce
|
|
736
|
-
* conflict that the caller invalidates on, refetches `checkSetup`, and
|
|
737
|
-
* retries with a fresh step.
|
|
808
|
+
* `step` and `checklist` must come from the same {@link checkSetup} result.
|
|
809
|
+
* This preserves the nonce allocated while staging and validates sequence
|
|
810
|
+
* prerequisites against that same snapshot, without another `createAction`
|
|
811
|
+
* round-trip.
|
|
738
812
|
*
|
|
739
813
|
* @throws {PerpsError} When the step's action is not in the provider's
|
|
740
|
-
* `setup` descriptors
|
|
814
|
+
* `setup` descriptors, or when a lower-`sequence` step on the same
|
|
815
|
+
* checklist, a choice included, is still unsatisfied.
|
|
741
816
|
* @public
|
|
742
817
|
*/
|
|
743
818
|
async executeProviderSetupAction(params) {
|
|
744
|
-
const { provider, address, step } = params;
|
|
819
|
+
const { provider, address, step, checklist } = params;
|
|
745
820
|
const metadata = await this.getProviderMetadata(provider);
|
|
746
821
|
const descriptor = metadata.setup.find((d) => d.type === step.action);
|
|
747
822
|
if (!descriptor) {
|
|
748
823
|
throw new PerpsError(PerpsErrorCode.SDKError, `Action '${step.action}' is not in '${provider}'.setup`);
|
|
749
824
|
}
|
|
825
|
+
const blockingStep = checklist.find((item) => !item.satisfied && sequenceOf(item.descriptor) < sequenceOf(descriptor));
|
|
826
|
+
if (blockingStep) {
|
|
827
|
+
throw new PerpsError(PerpsErrorCode.SDKError, `Setup step '${step.action}' for '${provider}' is blocked: '${blockingStep.descriptor.type}' runs first and is not satisfied.`);
|
|
828
|
+
}
|
|
750
829
|
const signed = await this.signProviderSetupAction(provider, address, step);
|
|
751
830
|
// A client-executed setup action (Lighter SET_REFERRAL) produces no
|
|
752
831
|
// backend-bound step — it already ran during signing, so there is nothing
|
|
@@ -762,22 +841,13 @@ export class PerpsClient {
|
|
|
762
841
|
});
|
|
763
842
|
}
|
|
764
843
|
/**
|
|
765
|
-
*
|
|
766
|
-
*
|
|
767
|
-
*
|
|
844
|
+
* Execute one option of a choice setup step (Lighter `ACCOUNT_TYPE`,
|
|
845
|
+
* Hyperliquid `ACCOUNT_MODE`) end-to-end with its bound `{ type, params }`,
|
|
846
|
+
* throwing on a venue rejection.
|
|
768
847
|
*
|
|
769
|
-
*
|
|
770
|
-
* trades, but unlike a trade an option change is a single mandatory action:
|
|
848
|
+
* The option runs through the same {@link execute} pipeline as a trade, but
|
|
771
849
|
* a per-action `success: false` (returned as a 200 OK) means the user's
|
|
772
|
-
* selection was rejected and must surface, not be silently dropped.
|
|
773
|
-
* wrapper inspects the result and throws a {@link PerpsError} carrying the
|
|
774
|
-
* venue `error`, giving options the same throw contract that setup has via
|
|
775
|
-
* {@link executeProviderSetupAction}. The only structural difference is that
|
|
776
|
-
* an option carries `params` (the selected value) rather than a pre-staged
|
|
777
|
-
* step.
|
|
778
|
-
*
|
|
779
|
-
* `execute` itself is unchanged — it still returns results without throwing,
|
|
780
|
-
* which the trade hooks rely on for partial-fill handling.
|
|
850
|
+
* selection was rejected and must surface, not be silently dropped.
|
|
781
851
|
*
|
|
782
852
|
* @throws {PerpsError} Carrying the venue error when any returned result has
|
|
783
853
|
* `success: false`, under that result's `errorCode` when the backend
|
|
@@ -787,11 +857,40 @@ export class PerpsClient {
|
|
|
787
857
|
* @public
|
|
788
858
|
*/
|
|
789
859
|
async executeProviderOption(params) {
|
|
790
|
-
const {
|
|
791
|
-
const
|
|
792
|
-
|
|
793
|
-
|
|
860
|
+
const { provider, address, option } = params;
|
|
861
|
+
const { results } = await this.execute({
|
|
862
|
+
provider,
|
|
863
|
+
address,
|
|
864
|
+
action: option.type,
|
|
865
|
+
params: option.params,
|
|
866
|
+
});
|
|
867
|
+
assertAllSucceeded(results);
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* Undo a satisfied setup step by executing its `revoke` action through
|
|
871
|
+
* create, sign and execute, signed with that action's `Provider.actions`
|
|
872
|
+
* descriptor. The step stages again on the next {@link checkSetup}.
|
|
873
|
+
*
|
|
874
|
+
* @throws {PerpsError} `SDKError` when `step.revoke` is `null` or when
|
|
875
|
+
* `createAction` stages nothing to revoke; otherwise the errors
|
|
876
|
+
* {@link executeProviderOption} throws.
|
|
877
|
+
* @public
|
|
878
|
+
*/
|
|
879
|
+
async executeProviderRevoke(params) {
|
|
880
|
+
const { provider, address, step } = params;
|
|
881
|
+
if (step.revoke === null) {
|
|
882
|
+
throw new PerpsError(PerpsErrorCode.SDKError, `Setup step '${step.type}' for '${provider}' declares no revoke.`);
|
|
883
|
+
}
|
|
884
|
+
const { staged, results } = await this.runAction({
|
|
885
|
+
provider,
|
|
886
|
+
address,
|
|
887
|
+
action: step.revoke,
|
|
888
|
+
params: {},
|
|
889
|
+
});
|
|
890
|
+
if (staged.length === 0) {
|
|
891
|
+
throw new PerpsError(PerpsErrorCode.SDKError, `Nothing to revoke for setup step '${step.type}' on '${provider}'.`);
|
|
794
892
|
}
|
|
893
|
+
assertAllSucceeded(results);
|
|
795
894
|
}
|
|
796
895
|
/**
|
|
797
896
|
* Place a market or limit order. Convenience wrapper over {@link execute}
|
|
@@ -936,6 +1035,11 @@ export class PerpsClient {
|
|
|
936
1035
|
* @public
|
|
937
1036
|
*/
|
|
938
1037
|
async execute(params) {
|
|
1038
|
+
const { results } = await this.runAction(params);
|
|
1039
|
+
return { results };
|
|
1040
|
+
}
|
|
1041
|
+
/** {@link execute}, also returning the steps `createAction` staged. */
|
|
1042
|
+
async runAction(params) {
|
|
939
1043
|
const { provider, address, action, onProgress } = params;
|
|
940
1044
|
const metadata = await this.getProviderMetadata(provider);
|
|
941
1045
|
const descriptor = findActionDescriptor(metadata, action);
|
|
@@ -955,7 +1059,7 @@ export class PerpsClient {
|
|
|
955
1059
|
// token-authenticated venue mutations), leaving no backend-bound step. With
|
|
956
1060
|
// nothing to submit, skip the `/executeAction` hop.
|
|
957
1061
|
if (signedActions.length === 0) {
|
|
958
|
-
return { results: [] };
|
|
1062
|
+
return { staged: actions, results: [] };
|
|
959
1063
|
}
|
|
960
1064
|
const response = await executeAction(this.sdkClient, {
|
|
961
1065
|
provider,
|
|
@@ -968,7 +1072,7 @@ export class PerpsClient {
|
|
|
968
1072
|
});
|
|
969
1073
|
const results = this.resolveExplorerLinks(provider, response.results);
|
|
970
1074
|
await this.notifyExecuteResults(provider, address, results);
|
|
971
|
-
return { results };
|
|
1075
|
+
return { staged: actions, results };
|
|
972
1076
|
}
|
|
973
1077
|
/**
|
|
974
1078
|
* Accept the current terms and, when a code is supplied and the backend
|