@gearbox-protocol/sdk 16.0.0-next.40 → 16.0.0-next.42

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.
Files changed (49) hide show
  1. package/dist/cjs/model/errors.js +1 -0
  2. package/dist/cjs/model/index.js +1 -0
  3. package/dist/cjs/onchain/accounts/intents/open-strategy.js +1 -1
  4. package/dist/cjs/onchain/positions/PositionsService.js +1 -11
  5. package/dist/cjs/sdk/execute/ExecuteApi.js +4 -4
  6. package/dist/cjs/sdk/index.js +5 -0
  7. package/dist/cjs/sdk/prepare/PrepareApi.js +326 -93
  8. package/dist/cjs/sdk/prepare/errors.js +90 -0
  9. package/dist/cjs/sdk/prepare/index.js +5 -0
  10. package/dist/esm/dev/AccountOpener.js +1 -1
  11. package/dist/esm/dev/withdrawalUtils.js +1 -1
  12. package/dist/esm/model/errors.js +1 -0
  13. package/dist/esm/model/index.js +1 -0
  14. package/dist/esm/onchain/accounts/CreditAccountsServiceV310.js +2 -2
  15. package/dist/esm/onchain/accounts/intents/open-strategy.js +1 -1
  16. package/dist/esm/onchain/accounts/liquidations/LiquidationsService.js +1 -1
  17. package/dist/esm/onchain/accounts/withdrawal-compressor/RedemptionLoggerV310Contract.js +1 -1
  18. package/dist/esm/onchain/accounts/withdrawal-compressor/WithdrawalCompressorV310Contract.js +1 -1
  19. package/dist/esm/onchain/accounts/withdrawal-compressor/WithdrawalCompressorV311Contract.js +1 -1
  20. package/dist/esm/onchain/accounts/withdrawal-compressor/WithdrawalCompressorV313Contract.js +1 -1
  21. package/dist/esm/onchain/base/TokensMeta.js +3 -3
  22. package/dist/esm/onchain/core/createAddressProvider.js +1 -1
  23. package/dist/esm/onchain/market/adapters/contracts/AccountMigratorAdapterContract.js +1 -1
  24. package/dist/esm/onchain/market/adapters/contracts/ERC4626AdapterContract.js +1 -1
  25. package/dist/esm/onchain/market/credit/CreditFacadeV310BaseContract.js +1 -1
  26. package/dist/esm/onchain/market/pool/PoolV310Contract.js +1 -1
  27. package/dist/esm/onchain/market/zapper/IETHZapperContract.js +1 -1
  28. package/dist/esm/onchain/market/zapper/ZapperContract.js +1 -1
  29. package/dist/esm/onchain/positions/PositionsService.js +1 -11
  30. package/dist/esm/onchain/utils/viem/simulateWithPriceUpdates.js +1 -1
  31. package/dist/esm/preview/simulate/simulatePoolOperation.js +1 -1
  32. package/dist/esm/preview/trace/extractTransfers.js +1 -1
  33. package/dist/esm/sdk/execute/ExecuteApi.js +4 -4
  34. package/dist/esm/sdk/index.js +2 -1
  35. package/dist/esm/sdk/prepare/PrepareApi.js +327 -94
  36. package/dist/esm/sdk/prepare/errors.js +86 -0
  37. package/dist/esm/sdk/prepare/index.js +2 -1
  38. package/dist/types/model/errors.d.ts +74 -0
  39. package/dist/types/model/index.d.ts +2 -1
  40. package/dist/types/onchain/accounts/intents/open-strategy.d.ts +3 -3
  41. package/dist/types/onchain/accounts/intents/types.d.ts +13 -14
  42. package/dist/types/onchain/positions/PositionsService.d.ts +0 -8
  43. package/dist/types/sdk/execute/types.d.ts +6 -6
  44. package/dist/types/sdk/index.d.ts +3 -2
  45. package/dist/types/sdk/prepare/PrepareApi.d.ts +11 -6
  46. package/dist/types/sdk/prepare/errors.d.ts +280 -0
  47. package/dist/types/sdk/prepare/index.d.ts +3 -2
  48. package/dist/types/sdk/prepare/types.d.ts +73 -46
  49. package/package.json +1 -1
@@ -1,4 +1,5 @@
1
1
  import { Bps } from "../../model/primitives.js";
2
+ import { WithError } from "../../model/errors.js";
2
3
  import { PoolOpportunityKey, StrategyOpportunityKey } from "../../model/opportunities.js";
3
4
  import { PositionCollateral, StrategyPosition, StrategyPositionKey } from "../../model/positions.js";
4
5
  import { DataResponse } from "../../model/response.js";
@@ -13,18 +14,18 @@ import { AccountCalculatorOperation } from "../../onchain/accounts/intents/opera
13
14
  import { DelayedStart, OperationState, PathLossRate, ResumableIntent, RouteRefusals } from "../../onchain/accounts/intents/types.js";
14
15
  import { OpenStrategyState } from "../../onchain/accounts/intents/open-strategy.js";
15
16
  import "../../onchain/index.js";
17
+ import { PrepareError, RoutesPrepareError } from "./errors.js";
16
18
  import { Address } from "viem";
17
19
  //#region src/sdk/prepare/types.d.ts
18
20
  /**
19
- * What a pool deposit or withdrawal would yield.
21
+ * What a pool deposit or withdrawal comes to.
20
22
  *
21
- * Shaped like {@link StrategyPrepare} so both kinds of result are consumed the
23
+ * Shaped like {@link StrategyPlan} so both kinds of result are consumed the
22
24
  * same way, with the pool's own numbers as the state: the ERC-4626
23
25
  * conversion applied to the amount, at the rate of the block the market was
24
26
  * loaded at.
25
27
  **/
26
- type LpPrepare = {
27
- ok: true;
28
+ interface LpPlan {
28
29
  /**
29
30
  * Always empty: a pool operation is a single transaction, so there is no
30
31
  * chain of steps to show. Present so callers can treat both kinds of
@@ -41,16 +42,15 @@ type LpPrepare = {
41
42
  * operation is a single call on the pool or on its zapper.
42
43
  **/
43
44
  calls: MultiCall[];
44
- } | PreviewRefusal;
45
+ }
45
46
  /**
46
- * What an operation on an existing credit account would yield.
47
- *
48
- * `ok: false` means the request itself is not viable — not that a call failed —
49
- * so the reason is a value rather than an exception: too much leverage, too
50
- * little of the source token, a token the flow does not accept.
47
+ * {@link LpPlan}, or why the pool cannot serve the request.
48
+ **/
49
+ type LpPrepare = WithError<LpPlan, PrepareError>;
50
+ /**
51
+ * What an operation on an existing credit account comes to.
51
52
  **/
52
- type StrategyPrepare = {
53
- ok: true;
53
+ interface StrategyPlan {
54
54
  /**
55
55
  * The logical steps, each carrying the amounts it was computed from.
56
56
  * Useful for showing the user what will happen, and for pinning behaviour
@@ -67,20 +67,29 @@ type StrategyPrepare = {
67
67
  * through `sdk.accounts`.
68
68
  **/
69
69
  calls: MultiCall[];
70
- } | PreviewRefusal;
70
+ }
71
+ /**
72
+ * {@link StrategyPlan}, or why the request cannot be served.
73
+ *
74
+ * A failure here means the request itself is not viable — not that a call
75
+ * failed — so it is a value rather than an exception: too much leverage, too
76
+ * little of the source token, a token the flow does not accept. What could not
77
+ * be done is `error.code`, and the limit that was missed is on the error beside
78
+ * it, see {@link PrepareError}.
79
+ **/
80
+ type StrategyPrepare = WithError<StrategyPlan, PrepareError>;
71
81
  /**
72
- * What the leading half of a delayed operation would yield: the request
82
+ * What the leading half of a delayed operation comes to: the request
73
83
  * transaction, plus what it recorded for the tail and where that tail leads.
74
84
  *
75
- * Shaped like {@link StrategyPrepare} with one field more, so the instant and
85
+ * Shaped like {@link StrategyPlan} with one field more, so the instant and
76
86
  * the delayed route of the same request are compared side by side — and they
77
87
  * are meant to be compared on the same footing, so `state` is the end of the
78
88
  * operation in both, not the end of the transaction.
79
89
  **/
80
- type DelayedStrategyPrepare = {
81
- ok: true;
90
+ interface DelayedStrategyPlan {
82
91
  /**
83
- * {@inheritDoc StrategyPrepare.operations}
92
+ * {@inheritDoc StrategyPlan.operations}
84
93
  **/
85
94
  operations: AccountCalculatorOperation[];
86
95
  /**
@@ -95,7 +104,7 @@ type DelayedStrategyPrepare = {
95
104
  **/
96
105
  state: OperationState;
97
106
  /**
98
- * {@inheritDoc StrategyPrepare.calls}
107
+ * {@inheritDoc StrategyPlan.calls}
99
108
  **/
100
109
  calls: MultiCall[];
101
110
  /**
@@ -103,7 +112,11 @@ type DelayedStrategyPrepare = {
103
112
  * {@link DelayedStart}.
104
113
  **/
105
114
  delayed: DelayedStart;
106
- } | PreviewRefusal;
115
+ }
116
+ /**
117
+ * {@link DelayedStrategyPlan}, or why the redemption route cannot be taken.
118
+ **/
119
+ type DelayedStrategyPrepare = WithError<DelayedStrategyPlan, PrepareError>;
107
120
  /**
108
121
  * What one of the two flows that sell a position asset —
109
122
  * {@link IOpportunitiesPrepare.withdrawStrategy} and
@@ -114,51 +127,56 @@ type DelayedStrategyPrepare = {
114
127
  * issuer, or both, is not something the caller can know up front, so both routes
115
128
  * are quoted from one request. A route the account cannot take is `undefined`
116
129
  * with its refusal in `refused`, which is what lets a form offer exactly the
117
- * routes that exist; `ok: false` means neither does.
130
+ * routes that exist; a failure means neither does, and the error still carries
131
+ * `refused`, see {@link RoutesPrepareError}.
118
132
  **/
119
- type StrategyRoutesPrepare = {
120
- ok: true;
133
+ interface StrategyRoutes {
121
134
  /**
122
135
  * The router route: one transaction, settled on the spot. `undefined`
123
136
  * when the asset cannot be sold, see `refused.instant`.
124
137
  **/
125
- instant: Extract<StrategyPrepare, {
126
- ok: true;
127
- }> | undefined;
138
+ instant: StrategyPlan | undefined;
128
139
  /**
129
140
  * The request half of the redemption route, which
130
141
  * {@link IOpportunitiesPrepare.finalize} completes once it matures.
131
142
  * `undefined` when the route does not exist — no redemption venue for the
132
143
  * asset, or a request that settles at once anyway — see `refused.delayed`.
133
144
  **/
134
- delayed: Extract<DelayedStrategyPrepare, {
135
- ok: true;
136
- }> | undefined;
145
+ delayed: DelayedStrategyPlan | undefined;
137
146
  /**
138
147
  * Why a missing route was refused, see {@link RouteRefusals}.
139
148
  **/
140
149
  refused: RouteRefusals;
141
- } |
150
+ }
142
151
  /**
143
- * The instant route's refusal, which is the one a caller can usually act on;
144
- * the delayed route's when the instant one did not even get that far.
152
+ * {@link StrategyRoutes}, or the refusal of both routes at once.
145
153
  **/
146
- (PreviewRefusal & {
147
- /**
148
- * {@inheritDoc StrategyRoutesPrepare.refused}
149
- **/
150
- refused: RouteRefusals;
151
- });
154
+ type StrategyRoutesPrepare = WithError<StrategyRoutes, RoutesPrepareError>;
152
155
  /**
153
- * What opening a new leveraged position would yield.
156
+ * What opening a new leveraged position comes to.
154
157
  *
155
158
  * The only result that reports both an expected and a floor branch: opening
156
159
  * takes both from a single pathfinder call, and `openCA` consumes both.
157
160
  **/
158
- type OpenStrategyPrepare = {
159
- ok: true;
161
+ interface OpenStrategyPlan {
162
+ /**
163
+ * Everything the opening arrives at: the projection, both branches of the
164
+ * post-open balances and quotas, and the router path `openCA` is handed.
165
+ **/
160
166
  state: OpenStrategyState;
161
- } | PreviewRefusal;
167
+ }
168
+ /**
169
+ * {@link OpenStrategyPlan}, or why the position cannot be opened.
170
+ **/
171
+ type OpenStrategyPrepare = WithError<OpenStrategyPlan, PrepareError>;
172
+ /**
173
+ * A ceiling one of the `max*` reads answers with, in the units that read names,
174
+ * or why the account it was asked about could not be weighed.
175
+ *
176
+ * The same envelope as a prepared operation, for the same reason: a form that
177
+ * cannot show a limit needs to say why as much as one that cannot prepare.
178
+ **/
179
+ type AmountPrepare = WithError<bigint, PrepareError>;
162
180
  /**
163
181
  * Shared knobs. Both default to the SDK's own defaults when omitted.
164
182
  **/
@@ -346,6 +364,15 @@ interface FinalizeParams extends PrepareOptions {
346
364
  *
347
365
  * Not to be confused with `src/preview`, which goes the other way: it takes
348
366
  * calldata that already exists and reports what it would do.
367
+ *
368
+ * Every method that can fail answers in the error envelope and none of them
369
+ * throws: a market that refuses, an account that is not there, a chain that
370
+ * cannot be reached — all of it arrives as `{ success: false, error }` with a
371
+ * code, see {@link PrepareError}. The two synchronous readers
372
+ * ({@link leverageBand}, {@link withdrawableCollaterals}) stay outside the
373
+ * envelope: they weigh state already loaded and say "nothing available" with
374
+ * `undefined` or an empty list, so their only failure is being handed a chain
375
+ * this SDK was never connected to — an argument error, which throws.
349
376
  **/
350
377
  interface IOpportunitiesPrepare {
351
378
  /**
@@ -418,7 +445,7 @@ interface IOpportunitiesPrepare {
418
445
  * Taking everything out needs none of this arithmetic: send `MAX_UINT256` to
419
446
  * {@link withdrawStrategy} and the exit is what runs.
420
447
  **/
421
- maxWithdraw(position: PositionInput): Promise<DataResponse<bigint>>;
448
+ maxWithdraw(position: PositionInput): Promise<DataResponse<AmountPrepare>>;
422
449
  /**
423
450
  * Paying debt down with funds from the wallet: collateral stays where it is,
424
451
  * so net value grows by what was repaid, leverage falls and the health factor
@@ -441,7 +468,7 @@ interface IOpportunitiesPrepare {
441
468
  * underlying units: principal, interest and fees as of this read. Interest
442
469
  * keeps accruing, so a wallet meaning to settle sends this with a buffer.
443
470
  **/
444
- maxRepay(position: PositionInput): Promise<DataResponse<bigint>>;
471
+ maxRepay(position: PositionInput): Promise<DataResponse<AmountPrepare>>;
445
472
  /**
446
473
  * Retargeting leverage at fixed collateral: debt moves, own funds do not.
447
474
  *
@@ -514,7 +541,7 @@ interface IOpportunitiesPrepare {
514
541
  * `targetHF` names the health factor to leave the account at, in basis
515
542
  * points; omitted, the SDK holds it to the bar a form would.
516
543
  **/
517
- maxWithdrawCollateral(position: PositionInput, token: Address, targetHF?: bigint): Promise<DataResponse<bigint>>;
544
+ maxWithdrawCollateral(position: PositionInput, token: Address, targetHF?: bigint): Promise<DataResponse<AmountPrepare>>;
518
545
  /**
519
546
  * The tail of a delayed route: claim the matured withdrawal, then whatever the
520
547
  * operation that requested it still owes — repaying debt and paying the wallet
@@ -535,4 +562,4 @@ interface IOpportunitiesPrepare {
535
562
  finalize(position: PositionInput, params: FinalizeParams): Promise<DataResponse<StrategyPrepare>>;
536
563
  }
537
564
  //#endregion
538
- export { AddCollateralParams, AdjustLeverageParams, BorrowLimitBinding, DelayedStrategyPrepare, DepositStrategyParams, FinalizeParams, IOpportunitiesPrepare, IntentPreviewError, type LeverageBand, LpParams, LpPrepare, LpRedeemParams, OpenStrategyParams, OpenStrategyPrepare, type OperationState, type PathLossRate, PoolInput, PositionInput, PrepareOptions, PreviewErrorDetails, PreviewErrorReason, PreviewIssue, PreviewRefusal, RepayStrategyParams, StrategyInput, StrategyPrepare, StrategyRoutesPrepare, WithdrawCollateralParams, WithdrawStrategyParams, raise, refuse };
565
+ export { AddCollateralParams, AdjustLeverageParams, AmountPrepare, BorrowLimitBinding, DelayedStrategyPlan, DelayedStrategyPrepare, DepositStrategyParams, FinalizeParams, IOpportunitiesPrepare, IntentPreviewError, type LeverageBand, LpParams, LpPlan, LpPrepare, LpRedeemParams, OpenStrategyParams, OpenStrategyPlan, OpenStrategyPrepare, type OperationState, type PathLossRate, PoolInput, PositionInput, PrepareOptions, PreviewErrorDetails, PreviewErrorReason, PreviewIssue, PreviewRefusal, RepayStrategyParams, StrategyInput, StrategyPlan, StrategyPrepare, StrategyRoutes, StrategyRoutesPrepare, WithdrawCollateralParams, WithdrawStrategyParams, raise, refuse };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "16.0.0-next.40",
3
+ "version": "16.0.0-next.42",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "repository": {