@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.
- package/dist/cjs/onchain/accounts/intents/math.js +4 -10
- package/dist/cjs/onchain/accounts/intents/plan.js +8 -1
- package/dist/esm/onchain/accounts/intents/math.js +5 -10
- package/dist/esm/onchain/accounts/intents/plan.js +8 -1
- package/dist/types/onchain/accounts/intents/math.d.ts +2 -9
- package/package.json +1 -1
|
@@ -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
|
-
|
|
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
|
|
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 *
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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 *
|
|
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 {
|
|
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 {
|
|
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
|
|
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,
|
|
49
|
+
export { DebtLimits, Position, assertDebtLimits, assertLeverageAtLeastOne, debtForLeverage, maxProportionalWithdrawal, proportionalDebt };
|