@gearbox-protocol/sdk 16.4.0-next.4 → 16.4.0-next.6
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/dev/midasUtils.js +46 -11
- package/dist/cjs/onchain/accounts/intents/borrow.js +139 -0
- package/dist/cjs/onchain/accounts/intents/{collateral-money.js → collateral-valuation.js} +7 -7
- package/dist/cjs/onchain/accounts/intents/index.js +79 -1
- package/dist/cjs/onchain/accounts/intents/leverage-band.js +2 -12
- package/dist/cjs/onchain/accounts/intents/maxBorrow.js +85 -0
- package/dist/cjs/onchain/accounts/intents/maxWithdrawCollateral.js +14 -14
- package/dist/cjs/onchain/accounts/intents/open-strategy.js +6 -45
- package/dist/cjs/onchain/accounts/intents/testing/sdk-mock.js +1 -0
- package/dist/cjs/onchain/accounts/intents/utils/common.js +19 -0
- package/dist/cjs/onchain/accounts/intents/utils/credit-account-slice.js +21 -0
- package/dist/cjs/onchain/accounts/intents/utils/index.js +2 -0
- package/dist/cjs/onchain/accounts/intents/withdraw-limits.js +8 -8
- package/dist/cjs/onchain/positions/calcHealthFactor.js +3 -3
- package/dist/cjs/onchain/preview/preview/previewOpenStrategyPosition.js +2 -1
- package/dist/cjs/sdk/execute/ExecuteApi.js +65 -5
- package/dist/cjs/sdk/prepare/PrepareApi.js +86 -12
- package/dist/esm/dev/midasUtils.js +47 -12
- package/dist/esm/onchain/accounts/intents/borrow.js +137 -0
- package/dist/esm/onchain/accounts/intents/{collateral-money.js → collateral-valuation.js} +7 -7
- package/dist/esm/onchain/accounts/intents/index.js +79 -1
- package/dist/esm/onchain/accounts/intents/leverage-band.js +2 -12
- package/dist/esm/onchain/accounts/intents/maxBorrow.js +83 -0
- package/dist/esm/onchain/accounts/intents/maxWithdrawCollateral.js +14 -14
- package/dist/esm/onchain/accounts/intents/open-strategy.js +6 -45
- package/dist/esm/onchain/accounts/intents/testing/sdk-mock.js +1 -0
- package/dist/esm/onchain/accounts/intents/utils/common.js +19 -1
- package/dist/esm/onchain/accounts/intents/utils/credit-account-slice.js +21 -1
- package/dist/esm/onchain/accounts/intents/utils/index.js +3 -3
- package/dist/esm/onchain/accounts/intents/withdraw-limits.js +8 -8
- package/dist/esm/onchain/positions/calcHealthFactor.js +3 -3
- package/dist/esm/onchain/preview/preview/previewOpenStrategyPosition.js +2 -1
- package/dist/esm/sdk/execute/ExecuteApi.js +65 -5
- package/dist/esm/sdk/prepare/PrepareApi.js +86 -12
- package/dist/types/dev/midasUtils.d.ts +4 -3
- package/dist/types/model/previews.d.ts +11 -2
- package/dist/types/onchain/accounts/index.d.ts +4 -3
- package/dist/types/onchain/accounts/intents/borrow.d.ts +141 -0
- package/dist/types/onchain/accounts/intents/{collateral-money.d.ts → collateral-valuation.d.ts} +8 -8
- package/dist/types/onchain/accounts/intents/index.d.ts +78 -2
- package/dist/types/onchain/accounts/intents/maxBorrow.d.ts +47 -0
- package/dist/types/onchain/accounts/intents/maxWithdrawCollateral.d.ts +3 -3
- package/dist/types/onchain/accounts/intents/open-strategy.d.ts +3 -17
- package/dist/types/onchain/accounts/intents/testing/sdk-mock.d.ts +2 -0
- package/dist/types/onchain/accounts/intents/tests/open-strategy.fixtures.d.ts +2 -2
- package/dist/types/onchain/accounts/intents/types.d.ts +6 -1
- package/dist/types/onchain/accounts/intents/utils/common.d.ts +15 -1
- package/dist/types/onchain/accounts/intents/utils/credit-account-slice.d.ts +14 -1
- package/dist/types/onchain/accounts/intents/utils/index.d.ts +3 -3
- package/dist/types/onchain/accounts/intents/withdraw-limits.d.ts +1 -1
- package/dist/types/onchain/index.d.ts +4 -3
- package/dist/types/sdk/execute/index.d.ts +2 -2
- package/dist/types/sdk/execute/types.d.ts +48 -6
- package/dist/types/sdk/index.d.ts +5 -4
- package/dist/types/sdk/prepare/PrepareApi.d.ts +15 -3
- package/dist/types/sdk/prepare/index.d.ts +4 -3
- package/dist/types/sdk/prepare/types.d.ts +169 -49
- package/package.json +1 -1
|
@@ -10,9 +10,10 @@ import "../../model/index.js";
|
|
|
10
10
|
import { Asset } from "../../onchain/base/types.js";
|
|
11
11
|
import { MultiCall } from "../../onchain/types/transactions.js";
|
|
12
12
|
import { PoolSimulation } from "../../onchain/pools/types.js";
|
|
13
|
-
import { LeverageBand } from "../../onchain/accounts/intents/leverage-band.js";
|
|
14
13
|
import { AccountCalculatorOperation } from "../../onchain/accounts/intents/operations.js";
|
|
15
14
|
import { ClaimRemainder, DelayedStart, OperationState, PathLossRate, ResumableIntent, RouteErrors, WithdrawCeilings } from "../../onchain/accounts/intents/types.js";
|
|
15
|
+
import { BorrowState } from "../../onchain/accounts/intents/borrow.js";
|
|
16
|
+
import { LeverageBand } from "../../onchain/accounts/intents/leverage-band.js";
|
|
16
17
|
import { OpenStrategyState } from "../../onchain/accounts/intents/open-strategy.js";
|
|
17
18
|
import { PoolOperationError } from "../../onchain/validation/bundles/checkPoolOperation.js";
|
|
18
19
|
import "../../onchain/index.js";
|
|
@@ -227,6 +228,41 @@ interface OpenStrategyResult {
|
|
|
227
228
|
/** Unix seconds of {@link blockNumber}. */
|
|
228
229
|
timestamp: Timestamp;
|
|
229
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* What taking a loan against collateral comes to.
|
|
233
|
+
*
|
|
234
|
+
* Shaped like {@link OpenStrategyResult} — a borrow opens its account too, so
|
|
235
|
+
* there is no chain of steps to report — with the whole projection on the
|
|
236
|
+
* state rather than two branches of balances, see {@link BorrowState}.
|
|
237
|
+
**/
|
|
238
|
+
interface BorrowResult {
|
|
239
|
+
/**
|
|
240
|
+
* Everything the loan arrives at: the collateral and the debt it backs, the
|
|
241
|
+
* payout expected and its floor, the position metrics a screen shows beside
|
|
242
|
+
* them, and the router path `openCA` is handed.
|
|
243
|
+
**/
|
|
244
|
+
state: BorrowState;
|
|
245
|
+
/** Block of the chain state this result was computed from. */
|
|
246
|
+
blockNumber: number;
|
|
247
|
+
/** Unix seconds of {@link blockNumber}. */
|
|
248
|
+
timestamp: Timestamp;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* What opening an account that holds nothing comes to: the block it was
|
|
252
|
+
* cleared at, and nothing else.
|
|
253
|
+
*
|
|
254
|
+
* The one prepared operation with no state to report. Every other result
|
|
255
|
+
* describes where the account lands, but this one lands it holding nothing and
|
|
256
|
+
* owing nothing — there is no collateral to value, no debt to weigh and no
|
|
257
|
+
* health factor to read. What the preparation is for is the refusal it can
|
|
258
|
+
* answer with instead: a market that takes no multicall.
|
|
259
|
+
**/
|
|
260
|
+
interface EmptyCreditAccountResult {
|
|
261
|
+
/** Block of the chain state this result was computed from. */
|
|
262
|
+
blockNumber: number;
|
|
263
|
+
/** Unix seconds of {@link blockNumber}. */
|
|
264
|
+
timestamp: Timestamp;
|
|
265
|
+
}
|
|
230
266
|
/**
|
|
231
267
|
* Shared knobs. Both default to the SDK's own defaults when omitted.
|
|
232
268
|
**/
|
|
@@ -338,15 +374,7 @@ interface WithdrawCollateralParams extends PrepareOptions {
|
|
|
338
374
|
/** Wallet receiving the tokens. */
|
|
339
375
|
to: Address;
|
|
340
376
|
}
|
|
341
|
-
|
|
342
|
-
* Opening a position, in one of the two shapes an opening comes in.
|
|
343
|
-
*
|
|
344
|
-
* The union is the check: an empty opening names nothing to open with, so
|
|
345
|
-
* collateral it meant to spend or an account it meant to reuse cannot be
|
|
346
|
-
* silently dropped — those arguments do not typecheck against `empty: true`.
|
|
347
|
-
**/
|
|
348
|
-
type OpenStrategyParams = OpenStrategyFundedParams | OpenStrategyEmptyParams;
|
|
349
|
-
interface OpenStrategyFundedParams extends PrepareOptions {
|
|
377
|
+
interface OpenStrategyParams extends PrepareOptions {
|
|
350
378
|
/** Collateral coming from the wallet, in their own tokens. */
|
|
351
379
|
collateral: Asset[];
|
|
352
380
|
/**
|
|
@@ -363,43 +391,80 @@ interface OpenStrategyFundedParams extends PrepareOptions {
|
|
|
363
391
|
/**
|
|
364
392
|
* Existing credit account to open the position on, instead of creating one.
|
|
365
393
|
*
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
|
|
394
|
+
* Any account of `strategy.creditManager` that carries no debt and no
|
|
395
|
+
* quotas qualifies. The projection is identical either way; only the
|
|
396
|
+
* transaction differs, and `execute.buildTx` reads which one to build off
|
|
397
|
+
* the result's own `state.creditAccount`.
|
|
398
|
+
**/
|
|
399
|
+
creditAccount?: Address;
|
|
400
|
+
}
|
|
401
|
+
interface BorrowParams extends PrepareOptions {
|
|
402
|
+
/**
|
|
403
|
+
* Token the wallet puts up as collateral. Must be a collateral token of the
|
|
404
|
+
* market, and cannot be {@link borrowToken} — the payout is swept off the
|
|
405
|
+
* account, and a sweep takes the whole balance of the token it names.
|
|
406
|
+
**/
|
|
407
|
+
collateralToken: Address;
|
|
408
|
+
/** Amount of {@link collateralToken} that leaves the wallet. */
|
|
409
|
+
collateralAmount: bigint;
|
|
410
|
+
/**
|
|
411
|
+
* Token the wallet is paid the loan in. The market underlying is handed over
|
|
412
|
+
* as it is borrowed; anything else the router has a path to is bought with
|
|
413
|
+
* the borrowed underlying first, and then {@link BorrowResult} reports both
|
|
414
|
+
* what that trade is expected to return and its floor.
|
|
415
|
+
*
|
|
416
|
+
* An RWA market is paid in the asset its underlying wraps — `USDC`, not the
|
|
417
|
+
* `dcUSDC` the pool lends — because the wrapper cannot leave the account.
|
|
418
|
+
* The two convert one for one through the market's vault, so that payout is
|
|
419
|
+
* exact like the underlying's. Naming the wrapper is refused with
|
|
420
|
+
* `unsupportedCollateralToken`.
|
|
421
|
+
**/
|
|
422
|
+
borrowToken: Address;
|
|
423
|
+
/**
|
|
424
|
+
* Amount of {@link borrowToken} the wallet asks for. It is the debt exactly
|
|
425
|
+
* when `borrowToken` is the market underlying, and the same amount in the
|
|
426
|
+
* underlying's own decimals when it is the asset an RWA market unwraps into;
|
|
427
|
+
* otherwise the debt is what the oracle prices that much of it at, and what
|
|
428
|
+
* actually arrives is the router's answer.
|
|
429
|
+
**/
|
|
430
|
+
borrowAmount: bigint;
|
|
431
|
+
/**
|
|
432
|
+
* Existing credit account to draw the loan on, instead of opening one.
|
|
433
|
+
*
|
|
434
|
+
* Any account of `strategy.creditManager` that carries no debt and no
|
|
435
|
+
* quotas qualifies. Only the transaction differs; `execute.buildTx` reads
|
|
436
|
+
* which one to build off the result's own `state.creditAccount`.
|
|
437
|
+
*
|
|
438
|
+
* Balances already sitting on it are left where they are and are **not**
|
|
439
|
+
* counted towards the health factor, so the loan this allows is the one the
|
|
440
|
+
* named collateral alone carries. The exception is a balance in
|
|
441
|
+
* {@link borrowToken}: the payout sweep takes the whole balance of the token
|
|
442
|
+
* it names, so that one leaves with the loan.
|
|
371
443
|
**/
|
|
372
444
|
creditAccount?: Address;
|
|
373
|
-
empty?: false;
|
|
374
445
|
}
|
|
375
446
|
/**
|
|
376
|
-
*
|
|
377
|
-
*
|
|
378
|
-
* an opening that names it as
|
|
379
|
-
* {@link OpenStrategyFundedParams.creditAccount}.
|
|
380
|
-
*
|
|
381
|
-
* The market is the whole request. There is nothing else to say: with no
|
|
382
|
-
* collateral the debt is zero at any leverage, and there is nothing to route
|
|
383
|
-
* anywhere — so leverage and a target token are not merely ignored here, they
|
|
384
|
-
* cannot be named.
|
|
447
|
+
* What a borrow form knows before it knows the amount: everything
|
|
448
|
+
* {@link BorrowParams} carries except the loan itself, which is the answer.
|
|
385
449
|
**/
|
|
386
|
-
interface
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
*
|
|
395
|
-
*
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
450
|
+
interface MaxBorrowParams {
|
|
451
|
+
/** Token the wallet would put up, as in {@link BorrowParams}. */
|
|
452
|
+
collateralToken: Address;
|
|
453
|
+
/** Amount of {@link collateralToken} that would leave the wallet. */
|
|
454
|
+
collateralAmount: bigint;
|
|
455
|
+
/** Token the loan would be paid in; the ceiling comes back in its units. */
|
|
456
|
+
borrowToken: Address;
|
|
457
|
+
/**
|
|
458
|
+
* Health factor the loan should leave the account at, in basis points.
|
|
459
|
+
* Omitted, the SDK holds it to the threshold a form would.
|
|
460
|
+
**/
|
|
461
|
+
targetHF?: bigint;
|
|
462
|
+
/**
|
|
463
|
+
* Extra quota headroom in PERCENTAGE_FORMAT. Pass what the borrow itself
|
|
464
|
+
* will pass: a quota short of the collateral's weighted value is what caps
|
|
465
|
+
* the loan, so a different reserve here answers about a different loan.
|
|
466
|
+
**/
|
|
467
|
+
quotaReserve?: number;
|
|
403
468
|
}
|
|
404
469
|
interface LpParams {
|
|
405
470
|
/**
|
|
@@ -472,11 +537,11 @@ interface FinalizeParams extends PrepareOptions {
|
|
|
472
537
|
* throws: a chain that cannot be reached or a crash on the way arrives as
|
|
473
538
|
* `unexpectedFailure` with the cause attached.
|
|
474
539
|
*
|
|
475
|
-
* The bare readers stay outside the envelope
|
|
476
|
-
* number and throw on an account or chain the SDK does not hold
|
|
477
|
-
* synchronous
|
|
478
|
-
* weigh state already loaded and say "nothing available"
|
|
479
|
-
* an empty list
|
|
540
|
+
* The bare readers stay outside the envelope. The ones that read an account
|
|
541
|
+
* answer their number and throw on an account or chain the SDK does not hold;
|
|
542
|
+
* the synchronous ones ({@link leverageBand}, {@link withdrawableCollaterals},
|
|
543
|
+
* {@link maxBorrow}) weigh state already loaded and say "nothing available"
|
|
544
|
+
* with `undefined`, an empty list or `0n`.
|
|
480
545
|
**/
|
|
481
546
|
interface IOpportunitiesPrepare {
|
|
482
547
|
/**
|
|
@@ -503,10 +568,41 @@ interface IOpportunitiesPrepare {
|
|
|
503
568
|
/**
|
|
504
569
|
* Opening a leveraged position from wallet collateral.
|
|
505
570
|
*
|
|
506
|
-
*
|
|
571
|
+
* There is no account to walk yet, so the result carries no operation list —
|
|
507
572
|
* it feeds `sdk.accounts.openCA` instead.
|
|
573
|
+
*
|
|
574
|
+
* `creditAccount` puts the position on an account the wallet already holds
|
|
575
|
+
* rather than creating one.
|
|
508
576
|
**/
|
|
509
577
|
openNewStrategy(strategy: StrategyInput, params: OpenStrategyParams): Promise<SDKReturn<OpenStrategyResult, OpenFlowError | DebtOutOfRangeError | LeverageOutOfRangeError | UnsupportedTokenPairError | InsufficientPoolLiquidityError | NoStrategyTargetCollateralError | CreditAccountNotFoundError | CreditAccountNotEmptyError>>;
|
|
578
|
+
/**
|
|
579
|
+
* Handing a wallet an account that holds nothing: no collateral, no debt, no
|
|
580
|
+
* quotas and no route quoted. It exists so a wallet can hold an account
|
|
581
|
+
* ahead of having a use for one, and so the two flows that put something on
|
|
582
|
+
* an account have one to name as their `creditAccount`.
|
|
583
|
+
*
|
|
584
|
+
* The market is the whole request, and the only thing that can refuse it is
|
|
585
|
+
* the market itself: a facade that is paused or past its expiration takes no
|
|
586
|
+
* multicall, so an account cannot be opened there either.
|
|
587
|
+
**/
|
|
588
|
+
openEmptyCreditAccount(strategy: StrategyInput): Promise<SDKReturn<EmptyCreditAccountResult, CreditManagerPausedError | MarketExpiredError | UnexpectedFailureError>>;
|
|
589
|
+
/**
|
|
590
|
+
* Borrowing against collateral: one transaction opens an account, puts the
|
|
591
|
+
* collateral on it, draws the loan and pays it out to the wallet.
|
|
592
|
+
*
|
|
593
|
+
* The account is left holding the collateral and owing the debt, and nothing
|
|
594
|
+
* else — which is what tells this apart from {@link openNewStrategy}, where
|
|
595
|
+
* the borrowed funds stay on the account as part of the position. There is
|
|
596
|
+
* no leverage to name for the same reason: the loan is the amount asked for.
|
|
597
|
+
*
|
|
598
|
+
* The payout leaves the account, so the market weighs what is left at safe
|
|
599
|
+
* prices, and a loan the remaining collateral cannot carry there comes back
|
|
600
|
+
* as `insufficientCollateral` rather than reverting on arrival.
|
|
601
|
+
*
|
|
602
|
+
* `creditAccount` draws the loan on an account the wallet already holds
|
|
603
|
+
* instead of opening another, as {@link openNewStrategy} does.
|
|
604
|
+
**/
|
|
605
|
+
borrow(strategy: StrategyInput, params: BorrowParams): Promise<SDKReturn<BorrowResult, OpenFlowError | DebtOutOfRangeError | UnsupportedCollateralTokenError | UnsupportedTokenPairError | InsufficientPoolLiquidityError | CreditAccountNotFoundError | CreditAccountNotEmptyError>>;
|
|
510
606
|
/**
|
|
511
607
|
* Growing a position: collateral in, debt drawn on top, both converted into
|
|
512
608
|
* the position token.
|
|
@@ -670,6 +766,30 @@ interface IOpportunitiesPrepare {
|
|
|
670
766
|
* the SDK does not hold.
|
|
671
767
|
**/
|
|
672
768
|
maxWithdrawCollateral(position: PositionInput, token: Address, targetHF?: bigint): Promise<bigint>;
|
|
769
|
+
/**
|
|
770
|
+
* Largest loan {@link borrow} can draw against a given collateral while the
|
|
771
|
+
* account stays safely collateralised, in the payout token's units — the
|
|
772
|
+
* ceiling a borrow form should offer.
|
|
773
|
+
*
|
|
774
|
+
* The loan leaves the account, so the collateral alone backs the debt and
|
|
775
|
+
* the market weighs it at safe prices, under its liquidation threshold and
|
|
776
|
+
* capped by the quota the borrow buys for it. The answer is then held to
|
|
777
|
+
* what the market will actually lend: the pool's free liquidity, the
|
|
778
|
+
* manager's own allowance and the facade's `maxDebt`.
|
|
779
|
+
*
|
|
780
|
+
* `targetHF` names the health factor to land at, in basis points; omitted,
|
|
781
|
+
* the SDK holds it to the threshold a form would.
|
|
782
|
+
*
|
|
783
|
+
* Synchronous, like {@link leverageBand} and for the same reason: the
|
|
784
|
+
* account does not exist yet, so there is nothing to read and a form can ask
|
|
785
|
+
* on every keystroke.
|
|
786
|
+
*
|
|
787
|
+
* `0n` where this market funds no loan of this shape — a loan that would
|
|
788
|
+
* land under `minDebt`, a collateral worth nothing at safe prices, a payout
|
|
789
|
+
* token equal to the collateral, or a manager the SDK does not hold. Not the
|
|
790
|
+
* same as "any amount works", and a caller must not offer a Max for it.
|
|
791
|
+
**/
|
|
792
|
+
maxBorrow(strategy: StrategyInput, params: MaxBorrowParams): bigint;
|
|
673
793
|
/**
|
|
674
794
|
* The tail of a delayed route: claim the matured withdrawal, then whatever the
|
|
675
795
|
* operation that requested it still owes — repaying debt and paying the wallet
|
|
@@ -695,4 +815,4 @@ interface IOpportunitiesPrepare {
|
|
|
695
815
|
finalize(position: PositionInput, params: FinalizeParams): Promise<SDKReturn<FinalizeResult, AccountFlowError | NoRecordedIntentError | NoDelayedRouteError | WithdrawalInProgressError | UnsupportedTokenPairError>>;
|
|
696
816
|
}
|
|
697
817
|
//#endregion
|
|
698
|
-
export { AccountFlowError, AddCollateralParams, AdjustLeverageParams, type CreditAccountNotEmptyError, type CreditAccountNotFoundError, DelayedStrategyResult, DepositStrategyParams, FinalizeParams, FinalizeResult, IOpportunitiesPrepare, type LeverageBand, LpParams, LpRedeemParams, LpResult, LpState, type MultipleDelayedWithdrawalsError, type NoDelayedRouteError, type NoRecordedIntentError, type NoStrategyTargetCollateralError, OpenFlowError,
|
|
818
|
+
export { AccountFlowError, AddCollateralParams, AdjustLeverageParams, BorrowParams, BorrowResult, type BorrowState, type CreditAccountNotEmptyError, type CreditAccountNotFoundError, DelayedStrategyResult, DepositStrategyParams, EmptyCreditAccountResult, FinalizeParams, FinalizeResult, IOpportunitiesPrepare, type LeverageBand, LpParams, LpRedeemParams, LpResult, LpState, MaxBorrowParams, type MultipleDelayedWithdrawalsError, type NoDelayedRouteError, type NoRecordedIntentError, type NoStrategyTargetCollateralError, OpenFlowError, OpenStrategyParams, OpenStrategyResult, type OperationState, type PathLossRate, PoolInput, type PoolOperationError, PositionInput, PrepareOptions, RepayStrategyParams, type RouteErrors, StrategyInput, StrategyResult, StrategyRoutesResult, type UnexpectedFailureError, type UnsupportedCollateralTokenError, type UnsupportedTokenPairError, WithRouteErrors, type WithdrawCeilings, WithdrawCollateralParams, WithdrawStrategyParams, type WithdrawalInProgressError };
|