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

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 (128) hide show
  1. package/dist/cjs/model/index.js +4 -0
  2. package/dist/cjs/model/result.js +27 -0
  3. package/dist/cjs/onchain/accounts/index.js +0 -2
  4. package/dist/cjs/onchain/accounts/intents/tail.js +1 -1
  5. package/dist/cjs/onchain/accounts/withdrawal-compressor/RedemptionLoggerV310Contract.js +13 -7
  6. package/dist/cjs/onchain/accounts/withdrawal-compressor/errors.js +0 -19
  7. package/dist/cjs/onchain/accounts/withdrawal-compressor/index.js +1 -2
  8. package/dist/cjs/onchain/index.js +0 -5
  9. package/dist/cjs/onchain/market/index.js +0 -2
  10. package/dist/cjs/onchain/market/zapper/ZapperContract.js +15 -8
  11. package/dist/cjs/onchain/market/zapper/errors.js +0 -17
  12. package/dist/cjs/onchain/market/zapper/index.js +1 -2
  13. package/dist/cjs/onchain/validation/checks.js +1 -1
  14. package/dist/cjs/onchain/validation/index.js +0 -1
  15. package/dist/cjs/preview/index.js +2 -8
  16. package/dist/cjs/preview/parse/errors.js +0 -32
  17. package/dist/cjs/preview/parse/index.js +1 -5
  18. package/dist/cjs/preview/parse/parseOperationCalldata.js +28 -17
  19. package/dist/cjs/preview/parse/parsePoolOperationCalldata.js +17 -11
  20. package/dist/cjs/preview/prerequisites/checkPrerequisites.js +5 -2
  21. package/dist/cjs/preview/preview/detectDelayedClaim.js +9 -7
  22. package/dist/cjs/preview/preview/detectDelayedOperation.js +11 -4
  23. package/dist/cjs/preview/preview/errors.js +0 -16
  24. package/dist/cjs/preview/preview/index.js +1 -2
  25. package/dist/cjs/preview/preview/previewOperation.js +30 -11
  26. package/dist/cjs/preview/preview/previewPoolPositionOperation.js +7 -3
  27. package/dist/cjs/preview/simulate/errors.js +15 -24
  28. package/dist/cjs/preview/simulate/index.js +1 -1
  29. package/dist/cjs/preview/simulate/simulateOperation.js +6 -4
  30. package/dist/cjs/preview/simulate/simulatePoolOperation.js +6 -3
  31. package/dist/cjs/sdk/execute/ExecuteApi.js +1 -1
  32. package/dist/cjs/sdk/index.js +1 -1
  33. package/dist/cjs/sdk/prepare/PrepareApi.js +198 -278
  34. package/dist/cjs/sdk/prepare/errors.js +5 -2
  35. package/dist/cjs/sdk/prepare/index.js +1 -1
  36. package/dist/esm/dev/AccountOpener.js +1 -1
  37. package/dist/esm/dev/withdrawalUtils.js +1 -1
  38. package/dist/esm/model/index.js +2 -1
  39. package/dist/esm/model/result.js +24 -0
  40. package/dist/esm/onchain/accounts/CreditAccountsServiceV310.js +2 -2
  41. package/dist/esm/onchain/accounts/index.js +1 -2
  42. package/dist/esm/onchain/accounts/intents/tail.js +1 -1
  43. package/dist/esm/onchain/accounts/liquidations/LiquidationsService.js +1 -1
  44. package/dist/esm/onchain/accounts/withdrawal-compressor/RedemptionLoggerV310Contract.js +14 -8
  45. package/dist/esm/onchain/accounts/withdrawal-compressor/WithdrawalCompressorV310Contract.js +1 -1
  46. package/dist/esm/onchain/accounts/withdrawal-compressor/WithdrawalCompressorV311Contract.js +1 -1
  47. package/dist/esm/onchain/accounts/withdrawal-compressor/WithdrawalCompressorV313Contract.js +1 -1
  48. package/dist/esm/onchain/accounts/withdrawal-compressor/errors.js +1 -19
  49. package/dist/esm/onchain/accounts/withdrawal-compressor/index.js +2 -2
  50. package/dist/esm/onchain/base/TokensMeta.js +3 -3
  51. package/dist/esm/onchain/core/createAddressProvider.js +1 -1
  52. package/dist/esm/onchain/index.js +2 -4
  53. package/dist/esm/onchain/market/adapters/contracts/AccountMigratorAdapterContract.js +1 -1
  54. package/dist/esm/onchain/market/adapters/contracts/ERC4626AdapterContract.js +1 -1
  55. package/dist/esm/onchain/market/credit/CreditFacadeV310BaseContract.js +1 -1
  56. package/dist/esm/onchain/market/index.js +1 -2
  57. package/dist/esm/onchain/market/pool/PoolV310Contract.js +1 -1
  58. package/dist/esm/onchain/market/zapper/IETHZapperContract.js +1 -1
  59. package/dist/esm/onchain/market/zapper/ZapperContract.js +16 -9
  60. package/dist/esm/onchain/market/zapper/errors.js +1 -17
  61. package/dist/esm/onchain/market/zapper/index.js +2 -2
  62. package/dist/esm/onchain/utils/viem/simulateWithPriceUpdates.js +1 -1
  63. package/dist/esm/onchain/validation/checks.js +1 -1
  64. package/dist/esm/onchain/validation/index.js +2 -2
  65. package/dist/esm/preview/index.js +3 -5
  66. package/dist/esm/preview/parse/errors.js +1 -30
  67. package/dist/esm/preview/parse/index.js +2 -3
  68. package/dist/esm/preview/parse/parseOperationCalldata.js +28 -17
  69. package/dist/esm/preview/parse/parsePoolOperationCalldata.js +17 -11
  70. package/dist/esm/preview/prerequisites/checkPrerequisites.js +5 -2
  71. package/dist/esm/preview/preview/detectDelayedClaim.js +9 -7
  72. package/dist/esm/preview/preview/detectDelayedOperation.js +11 -4
  73. package/dist/esm/preview/preview/errors.js +1 -16
  74. package/dist/esm/preview/preview/index.js +2 -2
  75. package/dist/esm/preview/preview/previewOperation.js +30 -11
  76. package/dist/esm/preview/preview/previewPoolPositionOperation.js +7 -3
  77. package/dist/esm/preview/simulate/errors.js +16 -24
  78. package/dist/esm/preview/simulate/index.js +2 -2
  79. package/dist/esm/preview/simulate/simulateOperation.js +6 -4
  80. package/dist/esm/preview/simulate/simulatePoolOperation.js +7 -4
  81. package/dist/esm/preview/trace/extractTransfers.js +1 -1
  82. package/dist/esm/sdk/execute/ExecuteApi.js +1 -1
  83. package/dist/esm/sdk/index.js +2 -2
  84. package/dist/esm/sdk/prepare/PrepareApi.js +199 -279
  85. package/dist/esm/sdk/prepare/errors.js +5 -2
  86. package/dist/esm/sdk/prepare/index.js +2 -2
  87. package/dist/types/model/errors.d.ts +4 -32
  88. package/dist/types/model/index.d.ts +3 -2
  89. package/dist/types/model/result.d.ts +48 -0
  90. package/dist/types/onchain/accounts/index.d.ts +1 -1
  91. package/dist/types/onchain/accounts/withdrawal-compressor/RedemptionLoggerV310Contract.d.ts +6 -4
  92. package/dist/types/onchain/accounts/withdrawal-compressor/errors.d.ts +9 -5
  93. package/dist/types/onchain/accounts/withdrawal-compressor/index.d.ts +1 -1
  94. package/dist/types/onchain/accounts/withdrawal-compressor/types.d.ts +6 -4
  95. package/dist/types/onchain/index.d.ts +4 -4
  96. package/dist/types/onchain/market/index.d.ts +1 -1
  97. package/dist/types/onchain/market/zapper/ZapperContract.d.ts +7 -3
  98. package/dist/types/onchain/market/zapper/errors.d.ts +11 -6
  99. package/dist/types/onchain/market/zapper/index.d.ts +1 -1
  100. package/dist/types/onchain/market/zapper/types.d.ts +4 -1
  101. package/dist/types/onchain/validation/checks.d.ts +1 -1
  102. package/dist/types/onchain/validation/index.d.ts +2 -2
  103. package/dist/types/preview/index.d.ts +5 -3
  104. package/dist/types/preview/parse/errors.d.ts +19 -12
  105. package/dist/types/preview/parse/index.d.ts +1 -1
  106. package/dist/types/preview/parse/parseOperationCalldata.d.ts +5 -1
  107. package/dist/types/preview/parse/parsePoolOperationCalldata.d.ts +5 -2
  108. package/dist/types/preview/prerequisites/checkPrerequisites.d.ts +6 -1
  109. package/dist/types/preview/preview/detectDelayedClaim.d.ts +7 -6
  110. package/dist/types/preview/preview/detectDelayedOperation.d.ts +3 -1
  111. package/dist/types/preview/preview/errors.d.ts +8 -5
  112. package/dist/types/preview/preview/index.d.ts +2 -2
  113. package/dist/types/preview/preview/previewOperation.d.ts +21 -2
  114. package/dist/types/preview/preview/previewPoolPositionOperation.d.ts +3 -1
  115. package/dist/types/preview/simulate/errors.d.ts +12 -8
  116. package/dist/types/preview/simulate/index.d.ts +2 -2
  117. package/dist/types/preview/simulate/simulateOperation.d.ts +4 -1
  118. package/dist/types/preview/simulate/simulatePoolOperation.d.ts +4 -1
  119. package/dist/types/sdk/execute/types.d.ts +6 -11
  120. package/dist/types/sdk/index.d.ts +3 -3
  121. package/dist/types/sdk/prepare/PrepareApi.d.ts +29 -26
  122. package/dist/types/sdk/prepare/errors.d.ts +57 -20
  123. package/dist/types/sdk/prepare/index.d.ts +3 -3
  124. package/dist/types/sdk/prepare/types.d.ts +73 -74
  125. package/dist/types/sdk/preview/PreviewNamespace.d.ts +4 -1
  126. package/dist/types/sdk/preview/types.d.ts +7 -3
  127. package/dist/types/sdk/types.d.ts +1 -3
  128. package/package.json +11 -2
@@ -1,6 +1,8 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_onchain_utils_hex = require("../../onchain/utils/hex.js");
3
3
  const require_onchain_base_MultichainConstruct = require("../../onchain/base/MultichainConstruct.js");
4
+ const require_model_result = require("../../model/result.js");
5
+ require("../../model/index.js");
4
6
  const require_onchain_validation_token = require("../../onchain/validation/token.js");
5
7
  const require_onchain_accounts_intents_utils_credit_account_slice = require("../../onchain/accounts/intents/utils/credit-account-slice.js");
6
8
  const require_onchain_accounts_intents_index = require("../../onchain/accounts/intents/index.js");
@@ -16,15 +18,18 @@ const require_sdk_prepare_withdrawable_collaterals = require("./withdrawable-col
16
18
  * protocol knowledge stays in `CreditAccountOperationsService` and
17
19
  * `PoolService`.
18
20
  *
19
- * A prepared operation names one chain, so it reads through
20
- * {@link MultichainConstruct.queryChain}: there is no second source to fall back
21
- * to, hence a chain the SDK does not cover, or one that fails the read, is a
22
- * failure of the whole request rather than a thinner answer.
21
+ * A prepared operation names one chain and reads it directly: there is no
22
+ * second source to fall back to, so a chain the SDK does not cover, or one
23
+ * that fails the read, is a failure of the whole request rather than a thinner
24
+ * answer.
23
25
  *
24
- * No method here throws. Every way a preparation can fail — the market's own
25
- * refusals, the two the namespace decides itself, and anything the chain or the
26
- * engine raises — comes back described in the envelope, see {@link PrepareError}.
27
- * A caller writes one branch, not a branch and a `try`.
26
+ * No refusable async method here throws. Every way such a preparation can
27
+ * fail — the market's own refusals, the ones the namespace decides itself, and
28
+ * anything the chain or the engine raises — comes back in the failure half of
29
+ * its `SDKReturn`, under the codes that method's signature names. A caller
30
+ * writes one branch, not a branch and a `try`. The synchronous LP methods and
31
+ * the bare `max*` reads throw on bugs and lifecycle errors instead, see
32
+ * {@link IOpportunitiesPrepare}.
28
33
  **/
29
34
  var PrepareApi = class extends require_onchain_base_MultichainConstruct.MultichainConstruct {
30
35
  #ensureFresh;
@@ -32,54 +37,28 @@ var PrepareApi = class extends require_onchain_base_MultichainConstruct.Multicha
32
37
  super(sdk);
33
38
  this.#ensureFresh = ensureFresh;
34
39
  }
35
- async queryChain(props) {
36
- await this.#ensureFresh?.([this.sdk.chain(props.network).chainId]);
37
- return super.queryChain(props);
38
- }
39
40
  /**
40
- * Runs a one-chain request whose answer is an envelope, describing whatever
41
- * the chain, the read or the engine throws on the way rather than letting it
42
- * escape.
43
- *
44
- * So every method below answers instead of rejecting, and a caller has one
45
- * thing to branch on. "This cannot be done" and "we could not find out" are
46
- * still told apart, by `error.code`: only the second is
47
- * `unexpectedFailure`, and only it marks the chain failed in `meta`.
41
+ * The chain an async method reads, revalidated first so the state it is
42
+ * about to weigh is no older than the SDK's freshness bar.
48
43
  **/
49
- async #answer(chainId, run) {
50
- try {
51
- return await this.queryChain({
52
- network: chainId,
53
- run
54
- });
55
- } catch (e) {
56
- return {
57
- data: {
58
- success: false,
59
- error: require_sdk_prepare_errors.unexpectedFailure(e)
60
- },
61
- meta: { chains: [failedChain(chainId, e)] }
62
- };
63
- }
44
+ async #chain(chainId) {
45
+ await this.#ensureFresh?.([this.sdk.chain(chainId).chainId]);
46
+ return this.sdk.chain(chainId);
64
47
  }
65
48
  /**
66
49
  * {@inheritDoc IOpportunitiesPrepare.finalize}
67
50
  **/
68
51
  async finalize(position, params) {
69
- return this.#answer(position.chainId, async (sdk) => {
52
+ try {
53
+ const sdk = await this.#chain(position.chainId);
54
+ const at = stateBlock(sdk);
70
55
  const intent = resumable(params.intent ?? params.claimable.intent);
71
- if (!intent) return {
72
- success: false,
73
- error: require_sdk_prepare_errors.toPrepareError({
74
- reason: "noRecordedIntent",
75
- detail: void 0
76
- })
77
- };
56
+ if (!intent) return require_model_result.sdkErr(require_sdk_prepare_errors.toRefusalError({
57
+ reason: "noRecordedIntent",
58
+ detail: void 0
59
+ }));
78
60
  const creditAccount = await slice(sdk, position.creditAccount);
79
- if (!creditAccount) return {
80
- success: false,
81
- error: require_sdk_prepare_errors.creditAccountNotFound(position.creditAccount)
82
- };
61
+ if (!creditAccount) return require_model_result.sdkErr(require_sdk_prepare_errors.creditAccountNotFound(position.creditAccount));
83
62
  return planned(await service(sdk).finishIntent({
84
63
  intent,
85
64
  claimable: toClaimableWithdrawal(params.claimable),
@@ -87,27 +66,15 @@ var PrepareApi = class extends require_onchain_base_MultichainConstruct.Multicha
87
66
  sdk,
88
67
  slippage: params.slippage,
89
68
  quotaReserve: params.quotaReserve
90
- }));
91
- });
92
- }
93
- /**
94
- * {@inheritDoc IOpportunitiesPrepare.deposit}
95
- **/
96
- deposit(pool, params) {
97
- try {
98
- return this.#depositPlan(pool, params);
69
+ }), at);
99
70
  } catch (e) {
100
- return {
101
- success: false,
102
- error: require_sdk_prepare_errors.unexpectedFailure(e)
103
- };
71
+ return require_model_result.sdkErr(require_sdk_prepare_errors.unexpectedFailure(e));
104
72
  }
105
73
  }
106
74
  /**
107
- * The arithmetic behind {@link deposit}, which throws where the SDK holds
108
- * nothing for the pool or the chain it was handed.
75
+ * {@inheritDoc IOpportunitiesPrepare.deposit}
109
76
  **/
110
- #depositPlan(pool, params) {
77
+ deposit(pool, params) {
111
78
  const chain = this.sdk.chain(pool.chainId);
112
79
  const { marketRegister, pools } = chain;
113
80
  const tokenIn = params.tokenIn ?? marketRegister.findByPool(pool.pool).pool.underlying;
@@ -129,32 +96,17 @@ var PrepareApi = class extends require_onchain_base_MultichainConstruct.Multicha
129
96
  meta: pools.getDepositMetadata(pool.pool, tokenIn, tokenOut)
130
97
  });
131
98
  if (!call) return unroutable(chain, tokenIn, tokenOut);
132
- return {
133
- success: true,
134
- data: {
135
- operations: [],
136
- state,
137
- calls: call.calls
138
- }
139
- };
99
+ return require_model_result.sdkOk({
100
+ operations: [],
101
+ state,
102
+ calls: call.calls,
103
+ ...stateBlock(chain)
104
+ });
140
105
  }
141
106
  /**
142
107
  * {@inheritDoc IOpportunitiesPrepare.withdraw}
143
108
  **/
144
109
  withdraw(pool, params) {
145
- try {
146
- return this.#withdrawPlan(pool, params);
147
- } catch (e) {
148
- return {
149
- success: false,
150
- error: require_sdk_prepare_errors.unexpectedFailure(e)
151
- };
152
- }
153
- }
154
- /**
155
- * {@inheritDoc PrepareApi.depositPlan}
156
- **/
157
- #withdrawPlan(pool, params) {
158
110
  const chain = this.sdk.chain(pool.chainId);
159
111
  const { pools } = chain;
160
112
  const tokenIn = params.tokenIn ?? pool.pool;
@@ -174,32 +126,17 @@ var PrepareApi = class extends require_onchain_base_MultichainConstruct.Multicha
174
126
  meta: pools.getWithdrawalMetadata(pool.pool, tokenIn, tokenOut),
175
127
  mode: "withdraw"
176
128
  });
177
- return {
178
- success: true,
179
- data: {
180
- operations: [],
181
- state,
182
- calls
183
- }
184
- };
129
+ return require_model_result.sdkOk({
130
+ operations: [],
131
+ state,
132
+ calls,
133
+ ...stateBlock(chain)
134
+ });
185
135
  }
186
136
  /**
187
137
  * {@inheritDoc IOpportunitiesPrepare.redeem}
188
138
  **/
189
139
  redeem(pool, params) {
190
- try {
191
- return this.#redeemPlan(pool, params);
192
- } catch (e) {
193
- return {
194
- success: false,
195
- error: require_sdk_prepare_errors.unexpectedFailure(e)
196
- };
197
- }
198
- }
199
- /**
200
- * {@inheritDoc PrepareApi.depositPlan}
201
- **/
202
- #redeemPlan(pool, params) {
203
140
  const chain = this.sdk.chain(pool.chainId);
204
141
  const { pools } = chain;
205
142
  const tokenIn = params.tokenIn ?? pool.pool;
@@ -219,25 +156,22 @@ var PrepareApi = class extends require_onchain_base_MultichainConstruct.Multicha
219
156
  meta: pools.getWithdrawalMetadata(pool.pool, tokenIn, tokenOut),
220
157
  mode: "redeem"
221
158
  });
222
- return {
223
- success: true,
224
- data: {
225
- operations: [],
226
- state,
227
- calls
228
- }
229
- };
159
+ return require_model_result.sdkOk({
160
+ operations: [],
161
+ state,
162
+ calls,
163
+ ...stateBlock(chain)
164
+ });
230
165
  }
231
166
  /**
232
167
  * {@inheritDoc IOpportunitiesPrepare.openNewStrategy}
233
168
  **/
234
169
  async openNewStrategy(strategy, params) {
235
- return this.#answer(strategy.chainId, async (sdk) => {
170
+ try {
171
+ const sdk = await this.#chain(strategy.chainId);
172
+ const at = stateBlock(sdk);
236
173
  const targetToken = params.targetToken ?? sdk.marketRegister.findCreditManager(strategy.creditManager).strategyTargetCollateral;
237
- if (!targetToken) return {
238
- success: false,
239
- error: require_sdk_prepare_errors.noStrategyTargetCollateral(strategy.creditManager)
240
- };
174
+ if (!targetToken) return require_model_result.sdkErr(require_sdk_prepare_errors.noStrategyTargetCollateral(strategy.creditManager));
241
175
  return opened(await service(sdk).openStrategyIntent({
242
176
  sdk,
243
177
  creditManager: strategy.creditManager,
@@ -247,8 +181,10 @@ var PrepareApi = class extends require_onchain_base_MultichainConstruct.Multicha
247
181
  leftoverBalances: params.leftoverBalances,
248
182
  slippage: params.slippage,
249
183
  quotaReserve: params.quotaReserve
250
- }));
251
- });
184
+ }), at);
185
+ } catch (e) {
186
+ return require_model_result.sdkErr(require_sdk_prepare_errors.unexpectedFailure(e));
187
+ }
252
188
  }
253
189
  /**
254
190
  * {@inheritDoc IOpportunitiesPrepare.depositStrategy}
@@ -279,19 +215,11 @@ var PrepareApi = class extends require_onchain_base_MultichainConstruct.Multicha
279
215
  * {@inheritDoc IOpportunitiesPrepare.maxWithdraw}
280
216
  **/
281
217
  async maxWithdraw(position) {
282
- return this.#answer(position.chainId, async (sdk) => {
283
- const creditAccount = await slice(sdk, position.creditAccount);
284
- if (!creditAccount) return {
285
- success: false,
286
- error: require_sdk_prepare_errors.creditAccountNotFound(position.creditAccount)
287
- };
288
- return {
289
- success: true,
290
- data: await service(sdk).maxWithdraw({
291
- creditAccount,
292
- sdk
293
- })
294
- };
218
+ const sdk = await this.#chain(position.chainId);
219
+ const creditAccount = await this.#account(sdk, position);
220
+ return service(sdk).maxWithdraw({
221
+ creditAccount,
222
+ sdk
295
223
  });
296
224
  }
297
225
  /**
@@ -309,19 +237,11 @@ var PrepareApi = class extends require_onchain_base_MultichainConstruct.Multicha
309
237
  * {@inheritDoc IOpportunitiesPrepare.maxRepay}
310
238
  **/
311
239
  async maxRepay(position) {
312
- return this.#answer(position.chainId, async (sdk) => {
313
- const creditAccount = await slice(sdk, position.creditAccount);
314
- if (!creditAccount) return {
315
- success: false,
316
- error: require_sdk_prepare_errors.creditAccountNotFound(position.creditAccount)
317
- };
318
- return {
319
- success: true,
320
- data: await service(sdk).maxRepay({
321
- creditAccount,
322
- sdk
323
- })
324
- };
240
+ const sdk = await this.#chain(position.chainId);
241
+ const creditAccount = await this.#account(sdk, position);
242
+ return service(sdk).maxRepay({
243
+ creditAccount,
244
+ sdk
325
245
  });
326
246
  }
327
247
  /**
@@ -378,75 +298,92 @@ var PrepareApi = class extends require_onchain_base_MultichainConstruct.Multicha
378
298
  * {@inheritDoc IOpportunitiesPrepare.maxWithdrawCollateral}
379
299
  **/
380
300
  async maxWithdrawCollateral(position, token, targetHF) {
381
- return this.#answer(position.chainId, async (sdk) => {
382
- const creditAccount = await slice(sdk, position.creditAccount);
383
- if (!creditAccount) return {
384
- success: false,
385
- error: require_sdk_prepare_errors.creditAccountNotFound(position.creditAccount)
386
- };
387
- return {
388
- success: true,
389
- data: await service(sdk).maxWithdrawCollateral({
390
- creditAccount,
391
- sdk,
392
- token,
393
- targetHF
394
- })
395
- };
301
+ const sdk = await this.#chain(position.chainId);
302
+ const creditAccount = await this.#account(sdk, position);
303
+ return service(sdk).maxWithdrawCollateral({
304
+ creditAccount,
305
+ sdk,
306
+ token,
307
+ targetHF
396
308
  });
397
309
  }
398
310
  /**
399
- * Shared path of the two flows that sell a position asset, and therefore have
400
- * two routes to offer: one account read, one intent, both routes quoted.
311
+ * The account a bare `max*` read weighs. These reads answer a number, not
312
+ * an envelope, so an account the markets do not hold is thrown rather than
313
+ * described, see {@link IOpportunitiesPrepare.maxWithdraw}.
314
+ **/
315
+ async #account(sdk, position) {
316
+ const creditAccount = await slice(sdk, position.creditAccount);
317
+ if (!creditAccount) throw new Error(require_sdk_prepare_errors.creditAccountNotFound(position.creditAccount).message);
318
+ return creditAccount;
319
+ }
320
+ /**
321
+ * Shared path of the two flows that sell a position asset, and therefore
322
+ * have two routes to offer: one account read, one intent, both routes
323
+ * quoted. Every refusal, the crash wrap included, carries `refused` so a
324
+ * form can say which routes were ruled out and why.
325
+ *
326
+ * @typeParam X - The codes the calling flow can raise beyond the shared
327
+ * ones, per the engine trace its signature spells out.
401
328
  **/
402
329
  async #startRoutes(position, options, intent) {
403
330
  try {
404
- return await this.queryChain({
405
- network: position.chainId,
406
- run: async (sdk) => {
407
- const creditAccount = await slice(sdk, position.creditAccount);
408
- if (!creditAccount) return neitherRoute(require_sdk_prepare_errors.creditAccountNotFound(position.creditAccount));
409
- return routed(await service(sdk).intentRoutes({
410
- intent,
411
- creditAccount,
412
- sdk,
413
- slippage: options.slippage,
414
- quotaReserve: options.quotaReserve
415
- }));
416
- }
417
- });
331
+ const sdk = await this.#chain(position.chainId);
332
+ const at = stateBlock(sdk);
333
+ const creditAccount = await slice(sdk, position.creditAccount);
334
+ if (!creditAccount) return neitherRoute(require_sdk_prepare_errors.creditAccountNotFound(position.creditAccount));
335
+ return routed(await service(sdk).intentRoutes({
336
+ intent,
337
+ creditAccount,
338
+ sdk,
339
+ slippage: options.slippage,
340
+ quotaReserve: options.quotaReserve
341
+ }), at);
418
342
  } catch (e) {
419
- return {
420
- data: neitherRoute(require_sdk_prepare_errors.unexpectedFailure(e)),
421
- meta: { chains: [failedChain(position.chainId, e)] }
422
- };
343
+ return neitherRoute(require_sdk_prepare_errors.unexpectedFailure(e));
423
344
  }
424
345
  }
425
346
  /**
426
347
  * Shared path of the five flows that act on an existing account: read the
427
- * account, then run the intent through the engine.
348
+ * account, then run the intent through the engine. Whatever the chain or
349
+ * the engine throws on the way is described as `unexpectedFailure` instead
350
+ * of escaping, so the flow always answers.
351
+ *
352
+ * @typeParam X - The codes the calling flow can raise beyond the shared
353
+ * ones, per the engine trace its signature spells out.
428
354
  **/
429
355
  async #startIntent(position, options, intent) {
430
- return this.#answer(position.chainId, async (sdk) => {
356
+ try {
357
+ const sdk = await this.#chain(position.chainId);
358
+ const at = stateBlock(sdk);
431
359
  const creditAccount = await slice(sdk, position.creditAccount);
432
- if (!creditAccount) return {
433
- success: false,
434
- error: require_sdk_prepare_errors.creditAccountNotFound(position.creditAccount)
435
- };
360
+ if (!creditAccount) return require_model_result.sdkErr(require_sdk_prepare_errors.creditAccountNotFound(position.creditAccount));
436
361
  return planned(await service(sdk).startIntent({
437
362
  intent,
438
363
  creditAccount,
439
364
  sdk,
440
365
  slippage: options.slippage,
441
366
  quotaReserve: options.quotaReserve
442
- }));
443
- });
367
+ }), at);
368
+ } catch (e) {
369
+ return require_model_result.sdkErr(require_sdk_prepare_errors.unexpectedFailure(e));
370
+ }
444
371
  }
445
372
  };
446
373
  function service(sdk) {
447
374
  return new require_onchain_accounts_intents_index.CreditAccountOperationsService(sdk);
448
375
  }
449
376
  /**
377
+ * Reads the block the SDK's loaded state stands at — the same state every
378
+ * preparation below is computed from, so it is the block the result reports.
379
+ **/
380
+ function stateBlock(sdk) {
381
+ return {
382
+ blockNumber: Number(sdk.currentBlock),
383
+ timestamp: Number(sdk.timestamp)
384
+ };
385
+ }
386
+ /**
450
387
  * The account the request names, or nothing where the markets this SDK is
451
388
  * connected to hold no such account — closed since it was listed, or named on
452
389
  * the wrong chain. Read rather than thrown, so the caller gets a code for it.
@@ -486,89 +423,59 @@ function toClaimableWithdrawal(claimable) {
486
423
  };
487
424
  }
488
425
  /**
426
+ * The engine's refusal as one flow's failure half.
427
+ *
428
+ * The engine answers with the open union of every reason it can raise
429
+ * anywhere; which of them a given flow actually reaches is the engine trace
430
+ * written into the public signatures. This cast is the one place the open
431
+ * union is narrowed onto them — a code added to or removed from a flow has to
432
+ * move its signature, which `types.test-d.ts` pins.
433
+ **/
434
+ function refusal(issue) {
435
+ return require_model_result.sdkErr(require_sdk_prepare_errors.toRefusalError(issue));
436
+ }
437
+ /**
489
438
  * A flow with two routes, refused before either could be quoted: the error is
490
439
  * the same one any other flow would report, with nothing to say about the
491
440
  * routes because neither was reached.
492
441
  **/
493
442
  function neitherRoute(error) {
494
- return {
495
- success: false,
496
- error: {
497
- ...error,
498
- refused: {
499
- instant: void 0,
500
- delayed: void 0
501
- }
443
+ return require_model_result.sdkErr({
444
+ ...error,
445
+ refused: {
446
+ instant: void 0,
447
+ delayed: void 0
502
448
  }
503
- };
504
- }
505
- /**
506
- * The chain entry for a request that was answered with
507
- * {@link UnexpectedFailureError}: the read did not happen, so the metadata says
508
- * so rather than reporting a block it never got.
509
- **/
510
- function failedChain(chainId, error) {
511
- return {
512
- chainId,
513
- status: "error",
514
- source: "onchain",
515
- error
516
- };
517
- }
518
- /**
519
- * A pool route the market does not offer, as the refusal a caller reads.
520
- *
521
- * `to` is absent where {@link lpRoute} found no output to name at all, which
522
- * is the usual way of it; both are present where a pair exists but nothing of
523
- * ours implements it.
524
- **/
525
- function unroutable(sdk, from, to) {
526
- return {
527
- success: false,
528
- error: require_sdk_prepare_errors.toPrepareError({
529
- reason: "unsupportedTokenPair",
530
- detail: {
531
- from: require_onchain_validation_token.toToken(sdk, from),
532
- to: to === void 0 ? void 0 : require_onchain_validation_token.toToken(sdk, to)
533
- }
534
- })
535
- };
449
+ });
536
450
  }
537
451
  /**
538
452
  * The engine's answer, as the envelope the namespace speaks in: what the
539
- * operation comes to under `data`, or the refusal as an error carrying its own
540
- * numbers, see {@link toPrepareError}.
453
+ * operation comes to under `data`, stamped with the block it was computed
454
+ * from, or the refusal as an error carrying its own numbers, see
455
+ * {@link refusal}.
541
456
  *
542
457
  * The engine keeps its `ok` union — it is the shape the planners, the guards
543
458
  * and their tests are written against — and the boundary is the one place the
544
459
  * two vocabularies meet.
545
460
  **/
546
- function planned(result) {
547
- if (!result.ok) return {
548
- success: false,
549
- error: require_sdk_prepare_errors.toPrepareError(result)
550
- };
461
+ function planned(result, at) {
462
+ if (!result.ok) return refusal(result);
551
463
  const { operations, state, calls } = result;
552
- return {
553
- success: true,
554
- data: {
555
- operations,
556
- state,
557
- calls
558
- }
559
- };
464
+ return require_model_result.sdkOk({
465
+ operations,
466
+ state,
467
+ calls,
468
+ ...at
469
+ });
560
470
  }
561
471
  /**
562
472
  * {@inheritDoc planned}
563
473
  **/
564
- function opened(result) {
565
- return result.ok ? {
566
- success: true,
567
- data: { state: result.state }
568
- } : {
569
- success: false,
570
- error: require_sdk_prepare_errors.toPrepareError(result)
571
- };
474
+ function opened(result, at) {
475
+ return result.ok ? require_model_result.sdkOk({
476
+ state: result.state,
477
+ ...at
478
+ }) : refusal(result);
572
479
  }
573
480
  /**
574
481
  * {@inheritDoc planned}
@@ -577,40 +484,53 @@ function opened(result) {
577
484
  * missing, and it stays on the error when neither route answered, since that is
578
485
  * the same question asked of a request that has no viable half at all.
579
486
  **/
580
- function routed(result) {
487
+ function routed(result, at) {
581
488
  if (!result.ok) {
582
489
  const { refused, ...issue } = result;
583
- return {
584
- success: false,
585
- error: {
586
- ...require_sdk_prepare_errors.toPrepareError(issue),
587
- refused
588
- }
589
- };
490
+ return require_model_result.sdkErr({
491
+ ...require_sdk_prepare_errors.toRefusalError(issue),
492
+ refused
493
+ });
590
494
  }
591
495
  const { instant, delayed, refused } = result;
592
- return {
593
- success: true,
594
- data: {
595
- instant: instant && {
596
- operations: instant.operations,
597
- state: instant.state,
598
- calls: instant.calls
599
- },
600
- delayed: delayed && {
601
- operations: delayed.operations,
602
- state: delayed.state,
603
- calls: delayed.calls,
604
- delayed: delayed.delayed
605
- },
606
- refused
496
+ return require_model_result.sdkOk({
497
+ instant: instant && {
498
+ operations: instant.operations,
499
+ state: instant.state,
500
+ calls: instant.calls,
501
+ ...at
502
+ },
503
+ delayed: delayed && {
504
+ operations: delayed.operations,
505
+ state: delayed.state,
506
+ calls: delayed.calls,
507
+ delayed: delayed.delayed,
508
+ ...at
509
+ },
510
+ refused,
511
+ ...at
512
+ });
513
+ }
514
+ /**
515
+ * A pool route the market does not offer, as the refusal a caller reads.
516
+ *
517
+ * `to` is absent where {@link lpRoute} found no output to name at all, which
518
+ * is the usual way of it; both are present where a pair exists but nothing of
519
+ * ours implements it.
520
+ **/
521
+ function unroutable(sdk, from, to) {
522
+ return require_model_result.sdkErr(require_sdk_prepare_errors.toRefusalError({
523
+ reason: "unsupportedTokenPair",
524
+ detail: {
525
+ from: require_onchain_validation_token.toToken(sdk, from),
526
+ to: to === void 0 ? void 0 : require_onchain_validation_token.toToken(sdk, to)
607
527
  }
608
- };
528
+ }));
609
529
  }
610
530
  /**
611
531
  * Picks the route the operation takes out of `tokenIn`, as a value rather than
612
532
  * an exception: an unroutable or ambiguous pair is a request the caller can
613
- * fix, so it belongs in the `ok: false` half alongside the strategy refusals.
533
+ * fix, so it belongs in the failure half alongside the strategy refusals.
614
534
  *
615
535
  * A pool with no route out of the input reports it by throwing, hence the
616
536
  * catch; a requested output is checked against the list rather than trusted,
@@ -35,8 +35,11 @@ const MESSAGES = {
35
35
  * {@link MESSAGES}. A malformed transaction is spelled out rather than spread,
36
36
  * because its detail names a `code` and a `message` of its own and they are not
37
37
  * the envelope's.
38
+ *
39
+ * Generic over the issue it is handed, so a call site that already knows the
40
+ * reason gets that reason's error back rather than a union to narrow again.
38
41
  **/
39
- function toPrepareError(issue) {
42
+ function toRefusalError(issue) {
40
43
  if (issue.reason === "malformedTransaction") return {
41
44
  code: "malformedTransaction",
42
45
  message: MESSAGES.malformedTransaction,
@@ -86,5 +89,5 @@ function unexpectedFailure(thrown) {
86
89
  //#endregion
87
90
  exports.creditAccountNotFound = creditAccountNotFound;
88
91
  exports.noStrategyTargetCollateral = noStrategyTargetCollateral;
89
- exports.toPrepareError = toPrepareError;
92
+ exports.toRefusalError = toRefusalError;
90
93
  exports.unexpectedFailure = unexpectedFailure;
@@ -9,5 +9,5 @@ exports.creditAccountNotFound = require_sdk_prepare_errors.creditAccountNotFound
9
9
  exports.noStrategyTargetCollateral = require_sdk_prepare_errors.noStrategyTargetCollateral;
10
10
  exports.raise = require_onchain_validation_refusal.raise;
11
11
  exports.refuse = require_onchain_validation_refusal.refuse;
12
- exports.toPrepareError = require_sdk_prepare_errors.toPrepareError;
12
+ exports.toRefusalError = require_sdk_prepare_errors.toRefusalError;
13
13
  exports.unexpectedFailure = require_sdk_prepare_errors.unexpectedFailure;
@@ -1,5 +1,5 @@
1
- import { ierc20Abi } from "../abi/iERC20.js";
2
1
  import { iCreditFacadeV310Abi } from "../abi/310/generated.js";
2
+ import { ierc20Abi } from "../abi/iERC20.js";
3
3
  import { AddressMap } from "../onchain/utils/AddressMap.js";
4
4
  import { AddressSet } from "../onchain/utils/AddressSet.js";
5
5
  import { AssetsMap } from "../onchain/utils/AssetsMap.js";