@gearbox-protocol/sdk 17.1.0-next.1 → 17.1.0-next.2

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.
@@ -24,17 +24,12 @@ function proportionalDebt(position, collateralDelta) {
24
24
  if (position.collateral <= 0n) throw new require_onchain_validation_raise.IntentPreviewError(require_model_errors_operation_errors.insufficientBalance(), "cannot preserve leverage on an account with no collateral");
25
25
  return position.debt * collateralDelta / position.collateral;
26
26
  }
27
- /**
28
- * Headroom on top of the debt read, in `PERCENTAGE_FACTOR`: enough interest for
29
- * the transaction to sit in the mempool for hours at any sane borrow rate,
30
- * small enough not to matter to the wallet that fronts it.
31
- */
32
- const SETTLE_MARGIN = 10n;
27
+ /** Share of the debt kept above `minDebt` for interest accrued before sending. */
28
+ const WITHDRAW_DEBT_MARGIN = 1n;
33
29
  /**
34
30
  * Largest withdrawal (in underlying) that {@link proportionalDebt} can still
35
31
  * pay for: the repayment it implies leaves debt at or above `minDebt` plus
36
- * {@link SETTLE_MARGIN} of the debt, so the figure survives the interest of the
37
- * blocks before it is sent, and
32
+ * {@link WITHDRAW_DEBT_MARGIN} of the debt, and
38
33
  * strictly less than the collateral goes — the last unit closes the account
39
34
  * rather than shrinks it. `0n` when the debt already sits below `minDebt`.
40
35
  *
@@ -45,7 +40,7 @@ function maxProportionalWithdrawal(position, debtLimits) {
45
40
  if (collateral <= 0n) return 0n;
46
41
  const allButLast = collateral - 1n;
47
42
  if (debt === 0n) return allButLast;
48
- const repayable = debt - debtLimits.minDebt - debt * SETTLE_MARGIN / require_onchain_constants_math.PERCENTAGE_FACTOR;
43
+ const repayable = debt - debtLimits.minDebt - debt * WITHDRAW_DEBT_MARGIN / require_onchain_constants_math.PERCENTAGE_FACTOR;
49
44
  if (repayable < 0n) return 0n;
50
45
  const bound = require_onchain_utils_bigint_math.BigIntMath.ceilDiv(collateral * (repayable + 1n), debt) - 1n;
51
46
  return bound < allButLast ? bound : allButLast;
@@ -71,7 +66,6 @@ function assertDebtLimits(sdk, debt, debtLimits, underlying) {
71
66
  }), debt > debtLimits.maxDebt ? `debt ${debt} exceeds maxDebt ${debtLimits.maxDebt}` : `debt ${debt} is below minDebt ${debtLimits.minDebt}`);
72
67
  }
73
68
  //#endregion
74
- exports.SETTLE_MARGIN = SETTLE_MARGIN;
75
69
  exports.assertDebtLimits = assertDebtLimits;
76
70
  exports.assertLeverageAtLeastOne = assertLeverageAtLeastOne;
77
71
  exports.debtForLeverage = debtForLeverage;
@@ -361,7 +361,14 @@ const claim = (claimable) => ({
361
361
  claimable
362
362
  });
363
363
  const clearQuotas = () => ({ kind: "clearQuotas" });
364
- const withMargin = (debt) => debt + debt * require_onchain_accounts_intents_math.SETTLE_MARGIN / require_onchain_constants_math.PERCENTAGE_FACTOR;
364
+ /**
365
+ * Headroom a settlement raises on top of the debt it read, in
366
+ * `PERCENTAGE_FACTOR`: enough interest for the transaction to sit in the
367
+ * mempool for hours at any sane borrow rate, small enough not to matter to the
368
+ * wallet that fronts it.
369
+ */
370
+ const SETTLE_MARGIN = 10n;
371
+ const withMargin = (debt) => debt + debt * SETTLE_MARGIN / require_onchain_constants_math.PERCENTAGE_FACTOR;
365
372
  /** The amount that asks for all of it, whatever "all" turns out to be. */
366
373
  const everything = (amount) => amount >= require_onchain_constants_math.MAX_UINT256;
367
374
  const min = (a, b) => a < b ? a : b;
@@ -23,17 +23,12 @@ function proportionalDebt(position, collateralDelta) {
23
23
  if (position.collateral <= 0n) throw new IntentPreviewError(insufficientBalance(), "cannot preserve leverage on an account with no collateral");
24
24
  return position.debt * collateralDelta / position.collateral;
25
25
  }
26
- /**
27
- * Headroom on top of the debt read, in `PERCENTAGE_FACTOR`: enough interest for
28
- * the transaction to sit in the mempool for hours at any sane borrow rate,
29
- * small enough not to matter to the wallet that fronts it.
30
- */
31
- const SETTLE_MARGIN = 10n;
26
+ /** Share of the debt kept above `minDebt` for interest accrued before sending. */
27
+ const WITHDRAW_DEBT_MARGIN = 1n;
32
28
  /**
33
29
  * Largest withdrawal (in underlying) that {@link proportionalDebt} can still
34
30
  * pay for: the repayment it implies leaves debt at or above `minDebt` plus
35
- * {@link SETTLE_MARGIN} of the debt, so the figure survives the interest of the
36
- * blocks before it is sent, and
31
+ * {@link WITHDRAW_DEBT_MARGIN} of the debt, and
37
32
  * strictly less than the collateral goes — the last unit closes the account
38
33
  * rather than shrinks it. `0n` when the debt already sits below `minDebt`.
39
34
  *
@@ -44,7 +39,7 @@ function maxProportionalWithdrawal(position, debtLimits) {
44
39
  if (collateral <= 0n) return 0n;
45
40
  const allButLast = collateral - 1n;
46
41
  if (debt === 0n) return allButLast;
47
- const repayable = debt - debtLimits.minDebt - debt * SETTLE_MARGIN / PERCENTAGE_FACTOR;
42
+ const repayable = debt - debtLimits.minDebt - debt * WITHDRAW_DEBT_MARGIN / PERCENTAGE_FACTOR;
48
43
  if (repayable < 0n) return 0n;
49
44
  const bound = BigIntMath.ceilDiv(collateral * (repayable + 1n), debt) - 1n;
50
45
  return bound < allButLast ? bound : allButLast;
@@ -70,4 +65,4 @@ function assertDebtLimits(sdk, debt, debtLimits, underlying) {
70
65
  }), debt > debtLimits.maxDebt ? `debt ${debt} exceeds maxDebt ${debtLimits.maxDebt}` : `debt ${debt} is below minDebt ${debtLimits.minDebt}`);
71
66
  }
72
67
  //#endregion
73
- export { SETTLE_MARGIN, assertDebtLimits, assertLeverageAtLeastOne, debtForLeverage, maxProportionalWithdrawal, proportionalDebt };
68
+ export { assertDebtLimits, assertLeverageAtLeastOne, debtForLeverage, maxProportionalWithdrawal, proportionalDebt };
@@ -6,7 +6,7 @@ import "../../../model/index.js";
6
6
  import { toToken, toTokenAmount } from "../../validation/helpers/token.js";
7
7
  import { IntentPreviewError } from "../../validation/raise.js";
8
8
  import { eq } from "./utils/common.js";
9
- import { SETTLE_MARGIN, assertDebtLimits, assertLeverageAtLeastOne, debtForLeverage, proportionalDebt } from "./math.js";
9
+ import { assertDebtLimits, assertLeverageAtLeastOne, debtForLeverage, proportionalDebt } from "./math.js";
10
10
  //#region src/onchain/accounts/intents/plan.ts
11
11
  /** Whatever the previous convert produced. */
12
12
  const RAISED = { raised: true };
@@ -360,6 +360,13 @@ const claim = (claimable) => ({
360
360
  claimable
361
361
  });
362
362
  const clearQuotas = () => ({ kind: "clearQuotas" });
363
+ /**
364
+ * Headroom a settlement raises on top of the debt it read, in
365
+ * `PERCENTAGE_FACTOR`: enough interest for the transaction to sit in the
366
+ * mempool for hours at any sane borrow rate, small enough not to matter to the
367
+ * wallet that fronts it.
368
+ */
369
+ const SETTLE_MARGIN = 10n;
363
370
  const withMargin = (debt) => debt + debt * SETTLE_MARGIN / PERCENTAGE_FACTOR;
364
371
  /** The amount that asks for all of it, whatever "all" turns out to be. */
365
372
  const everything = (amount) => amount >= MAX_UINT256;
@@ -28,17 +28,10 @@ declare function debtForLeverage(collateral: bigint, leverage: bigint): bigint;
28
28
  * it, so no rounding is introduced on the way.
29
29
  */
30
30
  declare function proportionalDebt(position: Position, collateralDelta: bigint): bigint;
31
- /**
32
- * Headroom on top of the debt read, in `PERCENTAGE_FACTOR`: enough interest for
33
- * the transaction to sit in the mempool for hours at any sane borrow rate,
34
- * small enough not to matter to the wallet that fronts it.
35
- */
36
- declare const SETTLE_MARGIN = 10n;
37
31
  /**
38
32
  * Largest withdrawal (in underlying) that {@link proportionalDebt} can still
39
33
  * pay for: the repayment it implies leaves debt at or above `minDebt` plus
40
- * {@link SETTLE_MARGIN} of the debt, so the figure survives the interest of the
41
- * blocks before it is sent, and
34
+ * {@link WITHDRAW_DEBT_MARGIN} of the debt, and
42
35
  * strictly less than the collateral goes — the last unit closes the account
43
36
  * rather than shrinks it. `0n` when the debt already sits below `minDebt`.
44
37
  *
@@ -53,4 +46,4 @@ declare function assertLeverageAtLeastOne(leverage: bigint): void;
53
46
  */
54
47
  declare function assertDebtLimits(sdk: OnchainSDK, debt: bigint, debtLimits: DebtLimits, underlying: Address): void;
55
48
  //#endregion
56
- export { DebtLimits, Position, SETTLE_MARGIN, assertDebtLimits, assertLeverageAtLeastOne, debtForLeverage, maxProportionalWithdrawal, proportionalDebt };
49
+ export { DebtLimits, Position, assertDebtLimits, assertLeverageAtLeastOne, debtForLeverage, maxProportionalWithdrawal, proportionalDebt };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "17.1.0-next.1",
3
+ "version": "17.1.0-next.2",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "repository": {