@gearbox-protocol/sdk 14.12.0-next.29 → 14.12.0-next.30
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/preview/preview/CreditAccountState.js +154 -0
- package/dist/cjs/preview/preview/buildDelayedPreview.js +111 -0
- package/dist/cjs/preview/preview/detectDelayedOperation.js +71 -0
- package/dist/cjs/preview/preview/errors.js +17 -0
- package/dist/cjs/preview/preview/index.js +10 -4
- package/dist/cjs/preview/preview/previewAdjustCreditAccount.js +16 -39
- package/dist/cjs/preview/preview/previewCloseOrRepayCreditAccount.js +11 -37
- package/dist/cjs/preview/preview/previewOpenCreditAccount.js +13 -8
- package/dist/cjs/preview/preview/previewOperation.js +46 -5
- package/dist/cjs/preview/preview/{applyInnerOperations.js → replayInnerOperations.js} +24 -35
- package/dist/cjs/preview/preview/{applyQuotaChanges.js → replayMulticall.js} +14 -20
- package/dist/cjs/preview/preview/types.js +5 -2
- package/dist/esm/preview/preview/CreditAccountState.js +133 -0
- package/dist/esm/preview/preview/buildDelayedPreview.js +90 -0
- package/dist/esm/preview/preview/detectDelayedOperation.js +51 -0
- package/dist/esm/preview/preview/errors.js +16 -0
- package/dist/esm/preview/preview/index.js +5 -2
- package/dist/esm/preview/preview/previewAdjustCreditAccount.js +19 -48
- package/dist/esm/preview/preview/previewCloseOrRepayCreditAccount.js +14 -44
- package/dist/esm/preview/preview/previewOpenCreditAccount.js +15 -10
- package/dist/esm/preview/preview/previewOperation.js +52 -6
- package/dist/esm/preview/preview/{applyInnerOperations.js → replayInnerOperations.js} +20 -34
- package/dist/esm/preview/preview/replayMulticall.js +16 -0
- package/dist/esm/preview/preview/types.js +3 -1
- package/dist/types/preview/preview/CreditAccountState.d.ts +83 -0
- package/dist/types/preview/preview/buildDelayedPreview.d.ts +41 -0
- package/dist/types/preview/preview/detectDelayedOperation.d.ts +29 -0
- package/dist/types/preview/preview/errors.d.ts +15 -0
- package/dist/types/preview/preview/index.d.ts +5 -2
- package/dist/types/preview/preview/previewAdjustCreditAccount.d.ts +3 -3
- package/dist/types/preview/preview/previewCloseOrRepayCreditAccount.d.ts +2 -2
- package/dist/types/preview/preview/replayInnerOperations.d.ts +44 -0
- package/dist/types/preview/preview/replayMulticall.d.ts +38 -0
- package/dist/types/preview/preview/types.d.ts +58 -3
- package/dist/types/preview/types.d.ts +17 -6
- package/package.json +1 -1
- package/dist/esm/preview/preview/applyQuotaChanges.js +0 -19
- package/dist/types/preview/preview/applyInnerOperations.d.ts +0 -59
- package/dist/types/preview/preview/applyQuotaChanges.d.ts +0 -24
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Address } from "viem";
|
|
2
|
+
import type { SdkWithAdapters } from "../../plugins/adapters/index.js";
|
|
3
|
+
import type { PluginsMap } from "../../sdk/index.js";
|
|
4
|
+
import type { InnerOperation } from "../parse/index.js";
|
|
5
|
+
import type { PreviewOperationOptions } from "../types.js";
|
|
6
|
+
import { CreditAccountState } from "./CreditAccountState.js";
|
|
7
|
+
import { type ReplayState } from "./replayInnerOperations.js";
|
|
8
|
+
import type { OperationPreviewError } from "./types.js";
|
|
9
|
+
/**
|
|
10
|
+
* Parsed operation on an existing credit account whose multicall can be
|
|
11
|
+
* replayed: the facade `closeCreditAccount` entry point and
|
|
12
|
+
* plain/bot/RWA multicalls all fit structurally.
|
|
13
|
+
*/
|
|
14
|
+
export interface ReplayableOperation {
|
|
15
|
+
creditAccount: Address;
|
|
16
|
+
multicall: InnerOperation[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Result of {@link replayMulticall}: the account's pre-state and the replayed
|
|
20
|
+
* (minimal guaranteed) post-state.
|
|
21
|
+
*/
|
|
22
|
+
export interface ReplayMulticallResult {
|
|
23
|
+
/**
|
|
24
|
+
* Account state before the operation, dust-filtered
|
|
25
|
+
*/
|
|
26
|
+
before: CreditAccountState;
|
|
27
|
+
/**
|
|
28
|
+
* Post-operation state and per-multicall bookkeeping, mutated by the
|
|
29
|
+
* replay in facade execution order
|
|
30
|
+
*/
|
|
31
|
+
after: ReplayState;
|
|
32
|
+
error?: OperationPreviewError;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Replays the operation's multicall over the account's pre-resolved
|
|
36
|
+
* pre-state (`options.creditAccount`) via {@link replayInnerOperations}.
|
|
37
|
+
*/
|
|
38
|
+
export declare function replayMulticall<P extends PluginsMap>(sdk: SdkWithAdapters<P>, operation: ReplayableOperation, options: PreviewOperationOptions<true>): Promise<ReplayMulticallResult>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Address } from "viem";
|
|
2
|
-
import type { Asset } from "../../sdk/index.js";
|
|
2
|
+
import type { Asset, DelayedIntent } from "../../sdk/index.js";
|
|
3
3
|
import type { PoolOperationType } from "../parse/index.js";
|
|
4
4
|
/**
|
|
5
5
|
* Broken `storeExpectedBalances`/`compareBalances` bracket structure:
|
|
@@ -33,6 +33,21 @@ export declare const ERROR_UNPREVIEWABLE_RWA_WRAP_UNWRAP = 1005;
|
|
|
33
33
|
export declare const ERROR_INVALID_TRANSACTION_VALUE = 1006;
|
|
34
34
|
/** A token in the preview could not be priced by the oracle */
|
|
35
35
|
export declare const ERROR_UNPRICEABLE_TOKEN = 2001;
|
|
36
|
+
/**
|
|
37
|
+
* Dust threshold for replayed preview balances and balance changes.
|
|
38
|
+
*
|
|
39
|
+
* Multicalls assembled by `CreditAccountsServiceV310` subtract a 10-unit
|
|
40
|
+
* safety buffer from every expected output token in their
|
|
41
|
+
* `storeExpectedBalances` deltas (`amount - 10n` in
|
|
42
|
+
* `assembleStartDelayedWithdrawalCalls`/`assembleClaimDelayedCalls`), while
|
|
43
|
+
* subsequent calls in the same multicall spend exact on-chain amounts. A
|
|
44
|
+
* replay that credits only the min-guarantee can therefore be off by up to
|
|
45
|
+
* 10 units per token, including small negative residues that are impossible
|
|
46
|
+
* on-chain. Amounts within this threshold (in absolute value) are filtered
|
|
47
|
+
* from preview outputs; anything beyond it is a genuine discrepancy and is
|
|
48
|
+
* reported.
|
|
49
|
+
*/
|
|
50
|
+
export declare const PREVIEW_DUST = 10n;
|
|
36
51
|
/**
|
|
37
52
|
* Non-throwing preview failure. When set on a preview, all fields are still
|
|
38
53
|
* computed best-effort, but some of them may be unreliable.
|
|
@@ -255,8 +270,48 @@ export interface RepayCreditAccountPreview {
|
|
|
255
270
|
*/
|
|
256
271
|
error?: OperationPreviewError;
|
|
257
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Preview of the "instant" part of an operation on an existing credit
|
|
275
|
+
* account: what the transaction does in the same block, before any delayed
|
|
276
|
+
* withdrawal is claimed.
|
|
277
|
+
*/
|
|
278
|
+
export type InstantOperationPreview = AdjustCreditAccountPreview | CloseCreditAccountPreview | RepayCreditAccountPreview;
|
|
279
|
+
/**
|
|
280
|
+
* Preview of a multicall that requests a delayed withdrawal (e.g. Securitize
|
|
281
|
+
* redemption): the source token is spent now and a withdrawal phantom token is received;
|
|
282
|
+
* the actual claim token materializes later, when the withdrawal is claimed and
|
|
283
|
+
* the recorded (if any) is resumed
|
|
284
|
+
*/
|
|
285
|
+
export interface DelayedCreditAccountOperationPreview {
|
|
286
|
+
operation: "DelayedCreditAccountOperation";
|
|
287
|
+
/**
|
|
288
|
+
* Credit account the operation is performed on
|
|
289
|
+
*/
|
|
290
|
+
creditAccount: Address;
|
|
291
|
+
/**
|
|
292
|
+
* Credit manager the account belongs to
|
|
293
|
+
*/
|
|
294
|
+
creditManager: Address;
|
|
295
|
+
/**
|
|
296
|
+
* Decoded from the withdrawal request's extraData; undefined when the
|
|
297
|
+
* request carries no intent (e.g. Mellow)
|
|
298
|
+
*/
|
|
299
|
+
intent?: DelayedIntent;
|
|
300
|
+
/**
|
|
301
|
+
* What this transaction does right now: the delayed withdrawal is
|
|
302
|
+
* represented by the phantom token among the account's assets
|
|
303
|
+
*/
|
|
304
|
+
instantPreview: InstantOperationPreview;
|
|
305
|
+
/**
|
|
306
|
+
* Best-effort state after the withdrawal is claimed and the intent is
|
|
307
|
+
* resumed; claim-only (phantom burned, claim token received) when
|
|
308
|
+
* `intent` is undefined
|
|
309
|
+
*/
|
|
310
|
+
delayedPreview: InstantOperationPreview;
|
|
311
|
+
}
|
|
258
312
|
/**
|
|
259
313
|
* Result of previewing a raw operation calldata: currently pool operations and
|
|
260
|
-
* credit account opening, adjustment, closure
|
|
314
|
+
* credit account opening, adjustment, closure, repayment and delayed
|
|
315
|
+
* withdrawal operations are supported.
|
|
261
316
|
*/
|
|
262
|
-
export type OperationPreview = PoolOperationPreview | OpenCreditAccountPreview | AdjustCreditAccountPreview | CloseCreditAccountPreview | RepayCreditAccountPreview;
|
|
317
|
+
export type OperationPreview = PoolOperationPreview | OpenCreditAccountPreview | AdjustCreditAccountPreview | CloseCreditAccountPreview | RepayCreditAccountPreview | DelayedCreditAccountOperationPreview;
|
|
@@ -30,11 +30,7 @@ export interface PreviewOperationInput<P extends PluginsMap = PluginsMap> {
|
|
|
30
30
|
**/
|
|
31
31
|
value?: bigint;
|
|
32
32
|
}
|
|
33
|
-
|
|
34
|
-
* Options shared by the preview, simulation and prerequisite-check flows.
|
|
35
|
-
* Each flow uses the subset it needs and ignores the rest.
|
|
36
|
-
*/
|
|
37
|
-
export interface PreviewOperationOptions {
|
|
33
|
+
interface PreviewOperationOptionsBase {
|
|
38
34
|
/**
|
|
39
35
|
* Block to read/simulate at; defaults to latest. Only set for testnet
|
|
40
36
|
* forks.
|
|
@@ -44,9 +40,24 @@ export interface PreviewOperationOptions {
|
|
|
44
40
|
* Optional logger.
|
|
45
41
|
**/
|
|
46
42
|
logger?: ILogger;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Options shared by the preview, simulation and prerequisite-check flows.
|
|
46
|
+
* Each flow uses the subset it needs and ignores the rest.
|
|
47
|
+
*
|
|
48
|
+
* `WithAccount = true` marks options whose credit account state has already
|
|
49
|
+
* been resolved (internal preview paths on an existing credit account).
|
|
50
|
+
*/
|
|
51
|
+
export type PreviewOperationOptions<WithAccount extends boolean = false> = WithAccount extends true ? PreviewOperationOptionsBase & {
|
|
52
|
+
/**
|
|
53
|
+
* Pre-resolved state of the credit account the operation targets
|
|
54
|
+
*/
|
|
55
|
+
creditAccount: CreditAccountData;
|
|
56
|
+
} : PreviewOperationOptionsBase & {
|
|
47
57
|
/**
|
|
48
58
|
* @internal
|
|
49
59
|
* Credit account state for testing purposes
|
|
50
60
|
*/
|
|
51
61
|
creditAccount?: CreditAccountData;
|
|
52
|
-
}
|
|
62
|
+
};
|
|
63
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { MIN_INT96 } from "../../sdk/index.js";
|
|
2
|
-
function applyQuotaChanges(initialQuotas, changes) {
|
|
3
|
-
const final = initialQuotas.clone();
|
|
4
|
-
for (const { token, balance: change } of changes) {
|
|
5
|
-
if (change === MIN_INT96) {
|
|
6
|
-
final.upsert(token, 0n);
|
|
7
|
-
} else {
|
|
8
|
-
const next = final.getOrZero(token) + change;
|
|
9
|
-
final.upsert(token, next > 0n ? next : 0n);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
return {
|
|
13
|
-
quotas: final.toAssets(0n),
|
|
14
|
-
quotasChange: final.difference(initialQuotas).toAssets()
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
export {
|
|
18
|
-
applyQuotaChanges
|
|
19
|
-
};
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { type SdkWithAdapters } from "../../plugins/adapters/index.js";
|
|
2
|
-
import { type Asset, AssetsMap, type PluginsMap } from "../../sdk/index.js";
|
|
3
|
-
import type { InnerOperation } from "../parse/index.js";
|
|
4
|
-
import { type OperationPreviewError } from "./types.js";
|
|
5
|
-
/**
|
|
6
|
-
* Running state threaded through a credit-facade multicall by
|
|
7
|
-
* {@link applyInnerOperations}. Seeded by the caller:
|
|
8
|
-
* - account opening: everything zero/empty (the account doesn't exist yet);
|
|
9
|
-
* - account adjustment: `balances` from the account's current token balances,
|
|
10
|
-
* `debt` from the principal, `totalDebt` from principal + accrued interest +
|
|
11
|
-
* accrued fees.
|
|
12
|
-
*/
|
|
13
|
-
export interface InnerOperationsState {
|
|
14
|
-
/**
|
|
15
|
-
* Cumulative credit-account balances, mutated in facade execution order.
|
|
16
|
-
*/
|
|
17
|
-
balances: AssetsMap;
|
|
18
|
-
/**
|
|
19
|
-
* Debt principal
|
|
20
|
-
*/
|
|
21
|
-
debt: bigint;
|
|
22
|
-
/**
|
|
23
|
-
* Total debt: principal + accrued interest + accrued fees.
|
|
24
|
-
* `decreaseDebt` amounts operate on total debt (interest and fees are repaid
|
|
25
|
-
* before principal), so principal alone is not enough to resolve either the
|
|
26
|
-
* underlying balance decrement or the final principal.
|
|
27
|
-
*/
|
|
28
|
-
totalDebt: bigint;
|
|
29
|
-
/**
|
|
30
|
-
* Cumulative amounts added via `addCollateral`.
|
|
31
|
-
*/
|
|
32
|
-
collateralAdded: AssetsMap;
|
|
33
|
-
/**
|
|
34
|
-
* Cumulative amounts withdrawn via `withdrawCollateral`, with `MAX_UINT256`
|
|
35
|
-
* resolved against the running balance at the point of the call.
|
|
36
|
-
*/
|
|
37
|
-
collateralWithdrawn: AssetsMap;
|
|
38
|
-
/**
|
|
39
|
-
* Raw `updateQuota` changes, one entry per op. Entries are relative signed
|
|
40
|
-
* changes (with the `MIN_INT96` "disable" sentinel kept as-is), not final
|
|
41
|
-
* quotas: folding them requires pre-state quotas, see `applyQuotaChanges`.
|
|
42
|
-
*/
|
|
43
|
-
quotaChanges: Asset[];
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Creates an all-zero {@link InnerOperationsState} (the account-opening seed).
|
|
47
|
-
*/
|
|
48
|
-
export declare function makeInnerOperationsState(): InnerOperationsState;
|
|
49
|
-
/**
|
|
50
|
-
* Replays a credit-facade multicall over `state`, mutating it in place in
|
|
51
|
-
* facade execution order. The result is the *minimal guaranteed* post-state.
|
|
52
|
-
*
|
|
53
|
-
* The function assumes that the call was generated by our frontend using
|
|
54
|
-
* router/withdrawal compressor, otherwise it returns an error, but still
|
|
55
|
-
* proceeds best-effort.
|
|
56
|
-
*
|
|
57
|
-
* @returns `undefined` on success, the error on a malformed multicall.
|
|
58
|
-
*/
|
|
59
|
-
export declare function applyInnerOperations<P extends PluginsMap>(sdk: SdkWithAdapters<P>, multicall: InnerOperation[], state: InnerOperationsState): Promise<OperationPreviewError | undefined>;
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { type Asset, type AssetsMap } from "../../sdk/index.js";
|
|
2
|
-
export interface ApplyQuotaChangesResult {
|
|
3
|
-
/**
|
|
4
|
-
* Final non-zero per-token quotas.
|
|
5
|
-
*/
|
|
6
|
-
quotas: Asset[];
|
|
7
|
-
/**
|
|
8
|
-
* Applied (post-clamp) per-token deltas, non-zero entries only.
|
|
9
|
-
*/
|
|
10
|
-
quotasChange: Asset[];
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Applies raw `updateQuota` changes to the account's initial quotas.
|
|
14
|
-
*
|
|
15
|
-
* Each change is applied sequentially: `MIN_INT96` disables the quota, other
|
|
16
|
-
* changes are added to the running quota with the result clamped at zero
|
|
17
|
-
* (quotas cannot go negative on-chain).
|
|
18
|
-
*
|
|
19
|
-
* @param initialQuotas - Per-token quotas before the operation.
|
|
20
|
-
* @param changes - Raw changes recorded by `applyInnerOperations`.
|
|
21
|
-
* @returns `quotas` - final non-zero per-token quotas; `quotasChange` -
|
|
22
|
-
* applied (post-clamp) per-token deltas, non-zero entries only.
|
|
23
|
-
*/
|
|
24
|
-
export declare function applyQuotaChanges(initialQuotas: AssetsMap, changes: Asset[]): ApplyQuotaChangesResult;
|