@gearbox-protocol/sdk 15.1.0-next.22 → 15.1.0-next.23

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 (44) hide show
  1. package/dist/cjs/new-sdk/prepare/PrepareApi.js +4 -4
  2. package/dist/cjs/sdk/accounts/intents/index.js +57 -28
  3. package/dist/cjs/sdk/accounts/intents/plan.js +40 -2
  4. package/dist/cjs/sdk/accounts/intents/realize.js +22 -12
  5. package/dist/cjs/sdk/accounts/intents/tail.js +120 -0
  6. package/dist/cjs/sdk/accounts/intents/utils/index.js +1 -0
  7. package/dist/cjs/sdk/accounts/intents/utils/router-path.js +32 -0
  8. package/dist/esm/dev/AccountOpener.js +1 -1
  9. package/dist/esm/dev/withdrawalUtils.js +1 -1
  10. package/dist/esm/new-sdk/prepare/PrepareApi.js +4 -4
  11. package/dist/esm/preview/simulate/simulatePoolOperation.js +1 -1
  12. package/dist/esm/preview/trace/extractTransfers.js +1 -1
  13. package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +2 -2
  14. package/dist/esm/sdk/accounts/intents/index.js +58 -29
  15. package/dist/esm/sdk/accounts/intents/plan.js +40 -3
  16. package/dist/esm/sdk/accounts/intents/realize.js +22 -12
  17. package/dist/esm/sdk/accounts/intents/tail.js +118 -0
  18. package/dist/esm/sdk/accounts/intents/utils/index.js +2 -2
  19. package/dist/esm/sdk/accounts/intents/utils/router-path.js +32 -1
  20. package/dist/esm/sdk/accounts/liquidations/LiquidationsService.js +1 -1
  21. package/dist/esm/sdk/accounts/withdrawal-compressor/RedemptionLoggerV310Contract.js +1 -1
  22. package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV310Contract.js +1 -1
  23. package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV311Contract.js +1 -1
  24. package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV313Contract.js +1 -1
  25. package/dist/esm/sdk/base/TokensMeta.js +3 -3
  26. package/dist/esm/sdk/chain/detectNetwork.js +1 -1
  27. package/dist/esm/sdk/core/createAddressProvider.js +1 -1
  28. package/dist/esm/sdk/market/adapters/contracts/AccountMigratorAdapterContract.js +1 -1
  29. package/dist/esm/sdk/market/adapters/contracts/ERC4626AdapterContract.js +1 -1
  30. package/dist/esm/sdk/market/credit/CreditFacadeV310BaseContract.js +1 -1
  31. package/dist/esm/sdk/market/pool/PoolV310Contract.js +1 -1
  32. package/dist/esm/sdk/market/zapper/IETHZapperContract.js +1 -1
  33. package/dist/esm/sdk/market/zapper/ZapperContract.js +1 -1
  34. package/dist/esm/sdk/pools/PoolService.js +1 -1
  35. package/dist/esm/sdk/utils/viem/simulateWithPriceUpdates.js +1 -1
  36. package/dist/types/new-sdk/prepare/types.d.ts +22 -8
  37. package/dist/types/sdk/accounts/intents/index.d.ts +13 -6
  38. package/dist/types/sdk/accounts/intents/plan.d.ts +18 -2
  39. package/dist/types/sdk/accounts/intents/realize.d.ts +7 -0
  40. package/dist/types/sdk/accounts/intents/tail.d.ts +52 -0
  41. package/dist/types/sdk/accounts/intents/types.d.ts +46 -10
  42. package/dist/types/sdk/accounts/intents/utils/index.d.ts +2 -2
  43. package/dist/types/sdk/accounts/intents/utils/router-path.d.ts +15 -1
  44. package/package.json +1 -1
@@ -73,10 +73,12 @@ type StrategySimulate = {
73
73
  };
74
74
  /**
75
75
  * What the leading half of a delayed operation would yield: the request
76
- * transaction, plus what it recorded for the tail.
76
+ * transaction, plus what it recorded for the tail and where that tail leads.
77
77
  *
78
78
  * Shaped like {@link StrategySimulate} with one field more, so the instant and
79
- * the delayed route of the same request are compared side by side.
79
+ * the delayed route of the same request are compared side by side — and they
80
+ * are meant to be compared on the same footing, so `preview` is the end of the
81
+ * operation in both, not the end of the transaction.
80
82
  **/
81
83
  type DelayedStrategySimulate = {
82
84
  ok: true;
@@ -85,9 +87,14 @@ type DelayedStrategySimulate = {
85
87
  **/
86
88
  operations: AccountCalculatorOperation[];
87
89
  /**
88
- * State once the request executes: the source token is gone and the
89
- * withdrawal position stands in its place, with debt untouched — the
90
- * repayment belongs to the tail.
90
+ * Where the operation ends: the account once the redemption has matured,
91
+ * been claimed and the tail has run — the same place the instant route
92
+ * reaches in one transaction, which is what makes the two comparable.
93
+ *
94
+ * The tail's half of it is an estimate priced by the oracle: the funds it
95
+ * trades do not exist yet, so no route can be quoted for them. The state
96
+ * the request alone lands in — source spent, withdrawal position in its
97
+ * place, debt untouched — is `delayed.afterRequest`.
91
98
  **/
92
99
  preview: OperationState;
93
100
  /**
@@ -398,7 +405,13 @@ interface OpportunitiesPrepare {
398
405
  * many-to-one route, the debt is settled in full and every balance left goes
399
406
  * to `to`. `tokenOut` is ignored, since the proceeds are already the
400
407
  * underlying (unwrapped on an RWA market). The account stays open with
401
- * nothing on it. An exit has no delayed route.
408
+ * nothing on it.
409
+ *
410
+ * An exit has both routes as well, which is what lets an account whose
411
+ * position only redeems through its issuer leave at all: the delayed route
412
+ * redeems `sourceToken` whole and records the exit, and {@link finalize}
413
+ * rebuilds it against the account the claim finds — the debt as it stands by
414
+ * then, whatever else is on the account, everything sold in one route.
402
415
  *
403
416
  * @see withdrawCollateral to move an asset out without touching debt, which
404
417
  * raises leverage instead.
@@ -463,8 +476,9 @@ interface OpportunitiesPrepare {
463
476
  /**
464
477
  * The tail of a delayed route: claim the matured withdrawal, then whatever the
465
478
  * operation that requested it still owes — repaying debt and paying the wallet
466
- * out for a withdrawal, repaying alone for a deleveraging, nothing beyond the
467
- * claim for the rest.
479
+ * out for a withdrawal, repaying alone for a deleveraging, selling the rest of
480
+ * the account and settling the loan for an exit, nothing beyond the claim for
481
+ * the rest.
468
482
  *
469
483
  * The route is requested by {@link withdrawStrategy} or
470
484
  * {@link adjustLeverage}, whose `delayed` branch is the transaction that
@@ -75,10 +75,16 @@ declare class CreditAccountOperationsService extends SDKConstruct {
75
75
  * {@link intentRoutes}; this is the one to call when the delayed route is the
76
76
  * only one of interest.
77
77
  *
78
+ * `preview` is where the intent ends — the account once the redemption has
79
+ * matured, been claimed and the tail has run — because that is what the
80
+ * caller asked for; the half-way state the request itself lands in is
81
+ * `delayed.afterRequest`. Both are validated, so a request whose tail could
82
+ * not be completed is refused instead of started.
83
+ *
78
84
  * @param props - Intent plus account slice, quota reserve and slippage
79
- * @returns The request transaction and what it recorded for the tail, or
80
- * `{ ok: false, reason }` — `noDelayedRoute` when this route does not exist
81
- * for the account at all
85
+ * @returns The request transaction, the state it ends in and what it recorded
86
+ * for the tail, or `{ ok: false, reason }` — `noDelayedRoute` when this route
87
+ * does not exist for the account at all
82
88
  */
83
89
  startDelayedIntent(props: StartIntentProps & {
84
90
  intent: DelayableIntent;
@@ -109,9 +115,10 @@ declare class CreditAccountOperationsService extends SDKConstruct {
109
115
  * Previews the tail of a delayed intent, once the withdrawal it started has
110
116
  * matured: the claim, then whatever the intent still owes.
111
117
  *
112
- * Serves the two operations that can genuinely be interrupted by a delay — a
113
- * withdrawal and a deleveraging. For the rest, the claim is the whole tail:
114
- * the tokens land on the account and only their quota has to catch up.
118
+ * Serves the three operations that can genuinely be interrupted by a delay —
119
+ * a withdrawal, a deleveraging and an exit. For the rest, the claim is the
120
+ * whole tail: the tokens land on the account and only their quota has to
121
+ * catch up.
115
122
  *
116
123
  * @param props - The recorded intent, the account slice as it stands now, and
117
124
  * the matured claimable
@@ -153,7 +153,8 @@ declare function planWithdraw(intent: Omit<WithdrawStrategyIntent, "type">, view
153
153
  *
154
154
  * The tail is planned at claim time by {@link planFinishWithdraw}, from the
155
155
  * intent this request records — only then is the claimed amount, and the token
156
- * it arrived in, known.
156
+ * it arrived in, known. An exit records {@link planFinishCloseAccount}'s intent
157
+ * instead, and rebuilds itself from the account rather than from the request.
157
158
  */
158
159
  declare function planWithdrawDelayed(intent: Omit<WithdrawStrategyIntent, "type">, view: AccountView): Step[];
159
160
  /**
@@ -185,7 +186,22 @@ declare function planFinishWithdraw(intent: {
185
186
  token: Address;
186
187
  amount: bigint;
187
188
  }, view: AccountView): Step[];
189
+ /**
190
+ * The tail of an exit: the claim lands, everything the account holds is sold
191
+ * into the underlying, the loan is settled out of the proceeds and the rest
192
+ * goes to the wallet. The account survives it, empty and owing nothing.
193
+ *
194
+ * Nothing is quoted from the request — the same shape {@link planWithdraw}
195
+ * builds for an instant exit is rebuilt here against the account as it stands
196
+ * now, which is the only state that can name these amounts.
197
+ */
198
+ declare function planFinishCloseAccount(intent: {
199
+ to: Address;
200
+ }, claimable: ClaimableWithdrawal, claimed: {
201
+ token: Address;
202
+ amount: bigint;
203
+ }, view: AccountView): Step[];
188
204
  /** Nothing is owed beyond the claim: the tokens land and quotas catch up. */
189
205
  declare function planFinishClaimOnly(claimable: ClaimableWithdrawal): Step[];
190
206
  //#endregion
191
- export { AccountView, Amount, RAISED, Step, planAddCollateral, planAdjustLeverage, planAdjustLeverageDelayed, planDeposit, planFinishClaimOnly, planFinishDecreaseLeverage, planFinishWithdraw, planRepay, planWithdraw, planWithdrawAsset, planWithdrawDelayed };
207
+ export { AccountView, Amount, RAISED, Step, planAddCollateral, planAdjustLeverage, planAdjustLeverageDelayed, planDeposit, planFinishClaimOnly, planFinishCloseAccount, planFinishDecreaseLeverage, planFinishWithdraw, planRepay, planWithdraw, planWithdrawAsset, planWithdrawDelayed };
@@ -4,6 +4,7 @@ import { CreditAccountSlice, DelayedStart, OperationState } from "./types.js";
4
4
  import { AccountCalculatorOperation } from "./operations.js";
5
5
  import "../../index.js";
6
6
  import { Step } from "./plan.js";
7
+ import { RouterPaths } from "./utils/router-path.js";
7
8
  //#region src/sdk/accounts/intents/realize.d.ts
8
9
  interface RealizeProps {
9
10
  creditAccount: CreditAccountSlice;
@@ -12,6 +13,12 @@ interface RealizeProps {
12
13
  slippage: number;
13
14
  /** Extra quota headroom in PERCENTAGE_FORMAT. */
14
15
  quotaReserve: number | undefined;
16
+ /**
17
+ * Where routed legs are quoted. Defaults to the pathfinder, which is what
18
+ * anything that will be sent needs; a walk that only projects a state passes
19
+ * `createOraclePaths` instead.
20
+ */
21
+ paths?: RouterPaths;
15
22
  }
16
23
  interface Realized {
17
24
  operations: AccountCalculatorOperation[];
@@ -0,0 +1,52 @@
1
+ import { ClaimableWithdrawal } from "../withdrawal-compressor/types.js";
2
+ import { OnchainSDK } from "../../OnchainSDK.js";
3
+ import { CreditAccountSlice, DelayedStart, OperationState, ResumableIntent } from "./types.js";
4
+ import { AccountCalculatorOperation, StartDelayedWithdrawalOperation } from "./operations.js";
5
+ import "../../index.js";
6
+ import { AccountView, Step } from "./plan.js";
7
+ //#region src/sdk/accounts/intents/tail.d.ts
8
+ /**
9
+ * The second half of a delayed intent: the claim, then whatever the intent
10
+ * still owes.
11
+ *
12
+ * Shared by the two callers that need it and must not disagree — the tail as
13
+ * it is previewed days later against the account that really exists, and the
14
+ * tail as it is projected the moment the request is made.
15
+ */
16
+ declare function planTail(args: {
17
+ intent: ResumableIntent;
18
+ claimable: ClaimableWithdrawal;
19
+ view: AccountView;
20
+ }): Step[];
21
+ /**
22
+ * Where a delayed intent ends up, worked out at the moment it is started.
23
+ *
24
+ * A request is only half a withdrawal, so the state it lands in is not the
25
+ * answer to "what does this do to my position": the debt is still there, the
26
+ * payout has not been made, and the position sits in a phantom token. What the
27
+ * caller means is the far side — and that side can be walked now, because the
28
+ * request already fixes the claim it will be finished from.
29
+ *
30
+ * So the same tail {@link planTail} builds at claim time is built here against
31
+ * the account as the request leaves it, with the claim it is expected to bring,
32
+ * and walked by the same realiser — with one substitution: routed legs are
33
+ * priced by the oracle rather than the pathfinder, since the funds they trade
34
+ * do not exist yet and no calldata is being produced. The result is an
35
+ * estimate that the engine's guards are nevertheless applied to, so a request
36
+ * that would strand the account is refused before it is sent rather than
37
+ * discovered days later.
38
+ */
39
+ declare function projectTail(args: {
40
+ /** The request as realised: the source spent, the phantom it produced. */
41
+ request: StartDelayedWithdrawalOperation;
42
+ delayed: DelayedStart;
43
+ /** The account the request was previewed against, for masks and market. */
44
+ creditAccount: CreditAccountSlice;
45
+ sdk: OnchainSDK;
46
+ quotaReserve: number | undefined;
47
+ }): Promise<{
48
+ state: OperationState;
49
+ operations: AccountCalculatorOperation[];
50
+ }>;
51
+ //#endregion
52
+ export { planTail, projectTail };
@@ -81,7 +81,11 @@ interface OperationState {
81
81
  type PreviewErrorReason = "debtOutOfRange" | "leverageOutOfRange" | "insufficientSourceBalance" |
82
82
  /** Input token is not accepted by the flow (e.g. deposit of a non-underlying). */
83
83
  "unsupportedCollateralToken" |
84
- /** No pool route between the requested pair, or several and none was picked. */
84
+ /**
85
+ * No route for the trade the plan needs: no pool pair between the tokens
86
+ * requested, several and none was picked, or the pathfinder itself found no
87
+ * path for the amounts involved.
88
+ */
85
89
  "unsupportedTokenPair" |
86
90
  /**
87
91
  * The intent cannot settle with a delay: the source has no redemption config,
@@ -93,9 +97,8 @@ type PreviewErrorReason = "debtOutOfRange" | "leverageOutOfRange" | "insufficien
93
97
  /** A redemption of the same asset is already in flight. */
94
98
  "withdrawalInProgress" |
95
99
  /**
96
- * The claim names no operation to resume: requested without an intent, read
97
- * through a compressor too old to report one, or a full close, which the
98
- * engine no longer previews.
100
+ * The claim names no operation to resume: requested without an intent, or
101
+ * read through a compressor too old to report one.
99
102
  */
100
103
  "noRecordedIntent" |
101
104
  /** The facade or the pool behind it is paused: nothing can be done at all. */
@@ -146,10 +149,44 @@ interface DelayedStart {
146
149
  * instead, which settles all of it in one transaction.
147
150
  */
148
151
  settlement: "instant" | "delayed";
152
+ /**
153
+ * What the claim is expected to credit the account with once the redemption
154
+ * matures: the venue's payout token and the amount the request queued.
155
+ * `undefined` when the request settled on the spot and nothing is coming.
156
+ *
157
+ * An estimate, not a quote — the issuer prices the redemption when it pays
158
+ * out, and the state the intent is previewed against was read now.
159
+ */
160
+ claim: {
161
+ token: Address;
162
+ amount: bigint;
163
+ } | undefined;
164
+ /**
165
+ * The account as the request transaction alone leaves it: the source spent,
166
+ * the phantom of the in-flight redemption in its place, the debt untouched.
167
+ *
168
+ * This is the state the facade judges when the transaction lands, so it is
169
+ * the one the engine's guards are applied to — while the `preview` beside it
170
+ * is where the intent ends up, tail included, which is what a caller asking
171
+ * "what does this do to my position" means.
172
+ */
173
+ afterRequest: OperationState;
149
174
  }
150
175
  /**
151
176
  * What the leading half of a delayed intent yields: the request transaction,
152
177
  * plus what it recorded for the tail.
178
+ *
179
+ * `operations` and `calls` are the request and nothing else — that is the only
180
+ * transaction there is to send now. `preview`, though, is where the intent
181
+ * ends: the state the account reaches once the redemption matures, is claimed
182
+ * and the tail runs, since that is what the caller asked for when they asked
183
+ * to withdraw. The half-way state the request itself lands in is
184
+ * {@link DelayedStart.afterRequest}, and both are validated before either is
185
+ * reported.
186
+ *
187
+ * The tail is projected from oracle prices rather than from a route — the funds
188
+ * it trades do not exist yet — so its half of the numbers is an estimate. What
189
+ * the transaction on offer does is not.
153
190
  */
154
191
  type DelayedStartResult = {
155
192
  ok: true;
@@ -406,14 +443,13 @@ type StartIntent = AddCollateralIntent | WithdrawAssetIntent | AdjustLeverageInt
406
443
  */
407
444
  type DelayableIntent = AdjustLeverageIntent | WithdrawStrategyIntent;
408
445
  /**
409
- * A delayed intent this engine knows how to finish.
446
+ * A delayed intent this engine knows how to finish — every one of them.
410
447
  *
411
- * `CLOSE_ACCOUNT` is absent on purpose: closing goes through the facade's own
412
- * entry point, which this engine no longer builds.
448
+ * `CLOSE_ACCOUNT` included: an exit is a plain multicall like any other
449
+ * operation here (sell everything, settle the loan, hand the rest over), not
450
+ * the facade's own close entry point, so the engine can build its tail too.
413
451
  */
414
- type ResumableIntent = Exclude<DelayedIntent, {
415
- type: "CLOSE_ACCOUNT";
416
- }>;
452
+ type ResumableIntent = DelayedIntent;
417
453
  /** Shared inputs plus the matured withdrawal the tail is built around. */
418
454
  type FinishIntentProps = StartIntentProps & {
419
455
  intent: ResumableIntent;
@@ -1,4 +1,5 @@
1
1
  import { fetchCreditAccountSlice, toCreditAccountSlice } from "./credit-account-slice.js";
2
+ import { OpenStrategyLeg, RouterPaths, SwapLeg, createOraclePaths, createRouterPaths } from "./router-path.js";
2
3
  import { adjustStateToSnapshot } from "./adjust-state-to-snapshot.js";
3
4
  import { assembleOperationCalls } from "./assemble-operation-calls.js";
4
5
  import { calcBorrowedAmountPlusInterestAndFees } from "./borrowed-amount-plus-interest-and-fees.js";
@@ -7,5 +8,4 @@ import { convertAmount } from "./convert-amount.js";
7
8
  import { ConvertFn, LedgerSnapshot, OperationLedger } from "./ledger.js";
8
9
  import { CandidateToken, isPhantomToken, isRedemptionPhantomToken, pickFattestNonPhantomToken, rankAccountTokens } from "./pick-token.js";
9
10
  import { clearedQuotas, getQuotasForUpdate, quotasAfterUpdate } from "./quotas-for-update.js";
10
- import { OpenStrategyLeg, RouterPaths, SwapLeg, createRouterPaths } from "./router-path.js";
11
- export { CandidateToken, ConvertFn, LedgerSnapshot, OpenStrategyLeg, OperationLedger, RouterPaths, SwapLeg, adjustStateToSnapshot, assembleOperationCalls, calcBorrowedAmountPlusInterestAndFees, clearedQuotas, convertAmount, createRouterPaths, eq, fetchCreditAccountSlice, getQuotasForUpdate, isPhantomToken, isRedemptionPhantomToken, pickFattestNonPhantomToken, quotasAfterUpdate, rankAccountTokens, toCreditAccountSlice, toRouterCaSlice, toTargetDecimals };
11
+ export { CandidateToken, ConvertFn, LedgerSnapshot, OpenStrategyLeg, OperationLedger, RouterPaths, SwapLeg, adjustStateToSnapshot, assembleOperationCalls, calcBorrowedAmountPlusInterestAndFees, clearedQuotas, convertAmount, createOraclePaths, createRouterPaths, eq, fetchCreditAccountSlice, getQuotasForUpdate, isPhantomToken, isRedemptionPhantomToken, pickFattestNonPhantomToken, quotasAfterUpdate, rankAccountTokens, toCreditAccountSlice, toRouterCaSlice, toTargetDecimals };
@@ -66,5 +66,19 @@ declare function createRouterPaths(args: {
66
66
  creditAccount: CreditAccountSlice;
67
67
  slippage: number;
68
68
  }): RouterPaths;
69
+ /**
70
+ * The same door, priced by the oracle and opening onto no calldata.
71
+ *
72
+ * For a leg that cannot be quoted yet: the tail of a redemption trades funds
73
+ * that do not exist, along a route the pathfinder will only be able to build
74
+ * once they do. Asking it now would price a swap of nothing, so the amounts
75
+ * come from the oracle instead — an estimate with no slippage floor, which is
76
+ * all a projection days out can honestly be — and the walk yields a state
77
+ * rather than a transaction.
78
+ */
79
+ declare function createOraclePaths(args: {
80
+ sdk: OnchainSDK;
81
+ creditAccount: CreditAccountSlice;
82
+ }): RouterPaths;
69
83
  //#endregion
70
- export { OpenStrategyLeg, RouterPaths, SwapLeg, createRouterPaths };
84
+ export { OpenStrategyLeg, RouterPaths, SwapLeg, createOraclePaths, createRouterPaths };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "15.1.0-next.22",
3
+ "version": "15.1.0-next.23",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "repository": {