@gearbox-protocol/sdk 16.3.2-next.1 → 16.3.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/dev/midasUtils.js +46 -11
- package/dist/cjs/model/errors/index.js +0 -1
- package/dist/cjs/model/errors/operation-errors.js +0 -9
- package/dist/cjs/model/index.js +0 -1
- package/dist/cjs/onchain/accounts/intents/guards.js +3 -15
- package/dist/cjs/onchain/accounts/intents/index.js +18 -21
- package/dist/cjs/onchain/accounts/intents/maxWithdrawCollateral.js +38 -12
- package/dist/cjs/onchain/accounts/intents/realize.js +2 -10
- package/dist/cjs/onchain/accounts/intents/tail.js +1 -1
- package/dist/cjs/onchain/index.js +0 -2
- package/dist/cjs/onchain/market/adapters/abi/conctructorAbi.js +1 -1
- package/dist/cjs/onchain/validation/checks/index.js +0 -2
- package/dist/cjs/onchain/validation/index.js +0 -2
- package/dist/cjs/sdk/prepare/PrepareApi.js +2 -3
- package/dist/esm/dev/AccountOpener.js +1 -1
- package/dist/esm/dev/midasUtils.js +47 -12
- package/dist/esm/dev/withdrawalUtils.js +1 -1
- package/dist/esm/history/trace/extractTransfers.js +1 -1
- package/dist/esm/model/errors/index.js +2 -2
- package/dist/esm/model/errors/operation-errors.js +1 -9
- package/dist/esm/model/index.js +2 -2
- package/dist/esm/onchain/accounts/CreditAccountsServiceV310.js +2 -2
- package/dist/esm/onchain/accounts/intents/guards.js +3 -15
- package/dist/esm/onchain/accounts/intents/index.js +18 -21
- package/dist/esm/onchain/accounts/intents/maxWithdrawCollateral.js +38 -12
- package/dist/esm/onchain/accounts/intents/realize.js +2 -10
- package/dist/esm/onchain/accounts/intents/tail.js +1 -1
- package/dist/esm/onchain/accounts/liquidations/LiquidationsService.js +1 -1
- package/dist/esm/onchain/accounts/withdrawal-compressor/RedemptionLoggerV310Contract.js +1 -1
- package/dist/esm/onchain/accounts/withdrawal-compressor/WithdrawalCompressorV310Contract.js +1 -1
- package/dist/esm/onchain/accounts/withdrawal-compressor/WithdrawalCompressorV311Contract.js +1 -1
- package/dist/esm/onchain/accounts/withdrawal-compressor/WithdrawalCompressorV313Contract.js +1 -1
- package/dist/esm/onchain/base/TokensMeta.js +3 -3
- package/dist/esm/onchain/core/createAddressProvider.js +1 -1
- package/dist/esm/onchain/index.js +1 -2
- package/dist/esm/onchain/market/adapters/abi/conctructorAbi.js +1 -1
- package/dist/esm/onchain/market/adapters/contracts/AccountMigratorAdapterContract.js +1 -1
- package/dist/esm/onchain/market/adapters/contracts/ERC4626AdapterContract.js +1 -1
- package/dist/esm/onchain/market/credit/CreditFacadeV310BaseContract.js +1 -1
- package/dist/esm/onchain/market/pool/PoolV310Contract.js +1 -1
- package/dist/esm/onchain/market/rwa/createDegenNFT.js +1 -1
- package/dist/esm/onchain/market/zapper/IETHZapperContract.js +1 -1
- package/dist/esm/onchain/market/zapper/ZapperContract.js +1 -1
- package/dist/esm/onchain/preview/preview/previewPoolPositionOperation.js +2 -2
- package/dist/esm/onchain/utils/viem/simulateWithPriceUpdates.js +1 -1
- package/dist/esm/onchain/validation/checks/index.js +1 -2
- package/dist/esm/onchain/validation/index.js +1 -2
- package/dist/esm/sdk/prepare/PrepareApi.js +2 -3
- package/dist/types/dev/midasUtils.d.ts +4 -3
- package/dist/types/model/errors/index.d.ts +2 -2
- package/dist/types/model/errors/operation-errors.d.ts +1 -41
- package/dist/types/model/index.d.ts +2 -2
- package/dist/types/onchain/accounts/intents/guards.d.ts +2 -18
- package/dist/types/onchain/accounts/intents/index.d.ts +14 -23
- package/dist/types/onchain/accounts/intents/maxWithdrawCollateral.d.ts +8 -3
- package/dist/types/onchain/accounts/intents/types.d.ts +4 -26
- package/dist/types/onchain/index.d.ts +1 -2
- package/dist/types/onchain/validation/checks/index.d.ts +1 -2
- package/dist/types/onchain/validation/index.d.ts +1 -2
- package/dist/types/onchain/validation/raise.d.ts +2 -2
- package/dist/types/sdk/prepare/PrepareApi.d.ts +1 -1
- package/dist/types/sdk/prepare/types.d.ts +16 -23
- package/package.json +1 -1
- package/dist/cjs/onchain/accounts/intents/collateral-money.js +0 -41
- package/dist/cjs/onchain/accounts/intents/withdraw-limits.js +0 -97
- package/dist/cjs/onchain/validation/checks/checkReservePriceLimited.js +0 -30
- package/dist/esm/onchain/accounts/intents/collateral-money.js +0 -40
- package/dist/esm/onchain/accounts/intents/withdraw-limits.js +0 -95
- package/dist/esm/onchain/validation/checks/checkReservePriceLimited.js +0 -29
- package/dist/types/onchain/accounts/intents/collateral-money.d.ts +0 -42
- package/dist/types/onchain/accounts/intents/withdraw-limits.d.ts +0 -80
- package/dist/types/onchain/validation/checks/checkReservePriceLimited.d.ts +0 -31
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_onchain_utils_bigint_math = require("../../utils/bigint-math.js");
|
|
3
|
-
require("../../utils/index.js");
|
|
4
|
-
const require_onchain_validation_helpers_health_factor_limits = require("../../validation/helpers/health-factor-limits.js");
|
|
5
|
-
require("../../validation/index.js");
|
|
6
|
-
const require_onchain_accounts_intents_utils_common = require("./utils/common.js");
|
|
7
|
-
const require_onchain_accounts_intents_collateral_money = require("./collateral-money.js");
|
|
8
|
-
const require_onchain_accounts_intents_math = require("./math.js");
|
|
9
|
-
const require_onchain_accounts_intents_view = require("./view.js");
|
|
10
|
-
//#region src/onchain/accounts/intents/withdraw-limits.ts
|
|
11
|
-
/**
|
|
12
|
-
* Every limit a `WITHDRAW` answers to, in underlying units.
|
|
13
|
-
*
|
|
14
|
-
* The one place they are assembled, so the figure a form is offered and the
|
|
15
|
-
* figure the collateral guard names when it turns a withdrawal down cannot
|
|
16
|
-
* drift apart: `CreditAccountOperationsService` reports this, and the guard
|
|
17
|
-
* quotes it back.
|
|
18
|
-
*
|
|
19
|
-
* @param props - Account slice, the SDK holding its market, and optionally the
|
|
20
|
-
* collateral the withdrawal would be funded from
|
|
21
|
-
* @returns The three limits, see {@link WithdrawCeilings}
|
|
22
|
-
**/
|
|
23
|
-
function withdrawLimits(props) {
|
|
24
|
-
const { creditAccount, sdk } = props;
|
|
25
|
-
const view = require_onchain_accounts_intents_view.accountView(creditAccount, sdk);
|
|
26
|
-
const partial = require_onchain_accounts_intents_math.maxProportionalWithdrawal(view, view.debtLimits);
|
|
27
|
-
const safe = maxSafeWithdrawal({
|
|
28
|
-
...props,
|
|
29
|
-
targetHF: BigInt(require_onchain_validation_helpers_health_factor_limits.MIN_HEALTH_FACTOR_FACADE)
|
|
30
|
-
});
|
|
31
|
-
return {
|
|
32
|
-
partial,
|
|
33
|
-
safePartial: require_onchain_utils_bigint_math.BigIntMath.min(partial, safe),
|
|
34
|
-
exit: view.collateral > 0n ? view.collateral : 0n
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Largest proportional withdrawal the safe-price collateral check still clears,
|
|
39
|
-
* in underlying units.
|
|
40
|
-
*
|
|
41
|
-
* A withdrawal hands funds over, so the facade weighs the account it leaves
|
|
42
|
-
* behind at safe prices rather than main ones — see {@link collateralMoney}.
|
|
43
|
-
* That is a second limit on top of the facade's `debtLimits`, and the two are
|
|
44
|
-
* independent: a caller wanting the amount a form may actually offer takes the
|
|
45
|
-
* lesser of this and `maxProportionalWithdrawal`.
|
|
46
|
-
*
|
|
47
|
-
* The arithmetic is the check solved for the amount. Taking `W` out at fixed
|
|
48
|
-
* leverage repays `dD = D·W/C`, so `W·TVL/C` of value is sold out of the source
|
|
49
|
-
* token; each dollar of that sale costs the check the source's threshold times
|
|
50
|
-
* its safe-to-main price ratio, while the repayment relieves `targetHF` per
|
|
51
|
-
* dollar of debt. Both terms are linear in `W`, which is why one division
|
|
52
|
-
* answers instead of a search — and why the answer is exact rather than a
|
|
53
|
-
* bound, as long as the plan really does fund itself from `sourceToken`.
|
|
54
|
-
*
|
|
55
|
-
* Two consequences worth stating, because they surprise:
|
|
56
|
-
*
|
|
57
|
-
* - An account whose collateral is entirely a token the reserve feed marks
|
|
58
|
-
* down cannot withdraw at all once it is under the threshold. A proportional
|
|
59
|
-
* withdrawal scales collateral and debt together, so it leaves the safe-price
|
|
60
|
-
* factor exactly where it found it — no amount climbs back over.
|
|
61
|
-
* - Leaving entirely is never refused for this reason: the exit settles the
|
|
62
|
-
* debt instead of shrinking it, and a check with no debt to divide by has
|
|
63
|
-
* nothing to refuse.
|
|
64
|
-
*
|
|
65
|
-
* @returns Amount in underlying units. The account's net value when safe prices
|
|
66
|
-
* do not limit the withdrawal at all, so the caller's `min` is a no-op; `0n`
|
|
67
|
-
* when the account already sits below `targetHF` at safe prices, and only the
|
|
68
|
-
* exit is left
|
|
69
|
-
**/
|
|
70
|
-
function maxSafeWithdrawal(props) {
|
|
71
|
-
const { creditAccount, sdk, targetHF } = props;
|
|
72
|
-
const view = require_onchain_accounts_intents_view.accountView(creditAccount, sdk);
|
|
73
|
-
if (view.collateral <= 0n) return 0n;
|
|
74
|
-
if (view.debt === 0n) return view.collateral;
|
|
75
|
-
const money = require_onchain_accounts_intents_collateral_money.collateralMoney(creditAccount, sdk);
|
|
76
|
-
const source = props.sourceToken ?? view.fattest();
|
|
77
|
-
const holding = source === void 0 ? void 0 : creditAccount.tokens.find((t) => require_onchain_accounts_intents_utils_common.eq(t.token, source));
|
|
78
|
-
if (!holding) return view.collateral;
|
|
79
|
-
let total = 0n;
|
|
80
|
-
for (const t of creditAccount.tokens) if (money.counts(t)) total += money.weigh(t);
|
|
81
|
-
const debtUsd = money.mainUsd(money.underlying, view.debt);
|
|
82
|
-
if (debtUsd === void 0 || debtUsd <= 0n) return view.collateral;
|
|
83
|
-
const slack = total - debtUsd * targetHF;
|
|
84
|
-
if (slack < 0n) return 0n;
|
|
85
|
-
const sourceMainUsd = money.mainUsd(holding.token, holding.balance);
|
|
86
|
-
if (sourceMainUsd === void 0 || sourceMainUsd <= 0n) return view.collateral;
|
|
87
|
-
const sourceRate = money.lt(holding.token) * money.checkedUsd(holding);
|
|
88
|
-
const tvlUsd = money.mainUsd(money.underlying, view.collateral + view.debt);
|
|
89
|
-
if (tvlUsd === void 0) return view.collateral;
|
|
90
|
-
const drain = sourceRate * tvlUsd - targetHF * debtUsd * sourceMainUsd;
|
|
91
|
-
if (drain <= 0n) return view.collateral;
|
|
92
|
-
const limit = view.collateral * slack * sourceMainUsd / drain;
|
|
93
|
-
return limit < view.collateral ? limit : view.collateral;
|
|
94
|
-
}
|
|
95
|
-
//#endregion
|
|
96
|
-
exports.maxSafeWithdrawal = maxSafeWithdrawal;
|
|
97
|
-
exports.withdrawLimits = withdrawLimits;
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_model_errors_operation_errors = require("../../../model/errors/operation-errors.js");
|
|
3
|
-
require("../../../model/index.js");
|
|
4
|
-
//#region src/onchain/validation/checks/checkReservePriceLimited.ts
|
|
5
|
-
/**
|
|
6
|
-
* Whether a failed collateral check is the reserve price feed's doing.
|
|
7
|
-
*
|
|
8
|
-
* A call that hands funds over is weighed at safe prices — `min` of a token's
|
|
9
|
-
* two feeds, and nothing at all where governance registered no reserve feed —
|
|
10
|
-
* so an account that covers its debt at the main feed can still be refused.
|
|
11
|
-
* The two are worth telling apart: a position that is genuinely too small is
|
|
12
|
-
* fixed by adding collateral or requesting less, while this one is a valuation
|
|
13
|
-
* the account does not control, and requesting less only helps as far as
|
|
14
|
-
* `withdrawable` says it does.
|
|
15
|
-
*
|
|
16
|
-
* Runs after `checkCollateralised` and answers only when that one refused, so
|
|
17
|
-
* the caller keeps its own threshold rather than restating it here.
|
|
18
|
-
*/
|
|
19
|
-
function checkReservePriceLimited(args) {
|
|
20
|
-
const { healthFactor, atMainPrices, healthFactorThreshold, withdrawable } = args;
|
|
21
|
-
if (healthFactor >= healthFactorThreshold || atMainPrices < healthFactorThreshold) return [];
|
|
22
|
-
return [require_model_errors_operation_errors.reservePriceLimited({
|
|
23
|
-
healthFactor,
|
|
24
|
-
atMainPrices,
|
|
25
|
-
healthFactorThreshold,
|
|
26
|
-
withdrawable
|
|
27
|
-
})];
|
|
28
|
-
}
|
|
29
|
-
//#endregion
|
|
30
|
-
exports.checkReservePriceLimited = checkReservePriceLimited;
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { BigIntMath } from "../../utils/bigint-math.js";
|
|
2
|
-
import { PERCENTAGE_FACTOR } from "../../constants/math.js";
|
|
3
|
-
import "../../utils/index.js";
|
|
4
|
-
import { eq } from "./utils/common.js";
|
|
5
|
-
//#region src/onchain/accounts/intents/collateral-money.ts
|
|
6
|
-
/** {@inheritDoc CollateralMoney} */
|
|
7
|
-
function collateralMoney(creditAccount, sdk) {
|
|
8
|
-
const { market, creditManager } = sdk.marketRegister.findCreditManager(creditAccount.creditManager);
|
|
9
|
-
const { priceOracle } = market;
|
|
10
|
-
const { pqk } = market.pool;
|
|
11
|
-
const underlying = market.pool.underlying;
|
|
12
|
-
const masked = creditAccount.enabledTokensMask !== 0n;
|
|
13
|
-
const mainUsd = (token, amount) => {
|
|
14
|
-
try {
|
|
15
|
-
return priceOracle.convertToUSD(token, amount);
|
|
16
|
-
} catch {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
const lt = (token) => BigInt(creditManager.liquidationThresholds.get(token) ?? 0);
|
|
21
|
-
const checkedUsd = (holding) => eq(holding.token, underlying) ? mainUsd(holding.token, holding.balance) ?? 0n : priceOracle.safeConvertMinUSD(holding.token, holding.balance).value;
|
|
22
|
-
/** A quota is underlying-denominated, and a closed market backs nothing. */
|
|
23
|
-
const quotaMoney = (holding) => pqk.hasActiveQuota(holding.token) ? (mainUsd(underlying, holding.quota) ?? 0n) * PERCENTAGE_FACTOR : 0n;
|
|
24
|
-
const weigh = (holding) => {
|
|
25
|
-
const weighted = checkedUsd(holding) * lt(holding.token);
|
|
26
|
-
if (holding.quota === 0n) return weighted;
|
|
27
|
-
return BigIntMath.min(quotaMoney(holding), weighted);
|
|
28
|
-
};
|
|
29
|
-
return {
|
|
30
|
-
underlying,
|
|
31
|
-
counts: (holding) => holding.balance > 10n && (!masked || (holding.mask & creditAccount.enabledTokensMask) !== 0n),
|
|
32
|
-
weigh,
|
|
33
|
-
quotaMoney,
|
|
34
|
-
mainUsd,
|
|
35
|
-
checkedUsd,
|
|
36
|
-
lt
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
//#endregion
|
|
40
|
-
export { collateralMoney };
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { BigIntMath } from "../../utils/bigint-math.js";
|
|
2
|
-
import "../../utils/index.js";
|
|
3
|
-
import { MIN_HEALTH_FACTOR_FACADE } from "../../validation/helpers/health-factor-limits.js";
|
|
4
|
-
import "../../validation/index.js";
|
|
5
|
-
import { eq } from "./utils/common.js";
|
|
6
|
-
import { collateralMoney } from "./collateral-money.js";
|
|
7
|
-
import { maxProportionalWithdrawal } from "./math.js";
|
|
8
|
-
import { accountView } from "./view.js";
|
|
9
|
-
//#region src/onchain/accounts/intents/withdraw-limits.ts
|
|
10
|
-
/**
|
|
11
|
-
* Every limit a `WITHDRAW` answers to, in underlying units.
|
|
12
|
-
*
|
|
13
|
-
* The one place they are assembled, so the figure a form is offered and the
|
|
14
|
-
* figure the collateral guard names when it turns a withdrawal down cannot
|
|
15
|
-
* drift apart: `CreditAccountOperationsService` reports this, and the guard
|
|
16
|
-
* quotes it back.
|
|
17
|
-
*
|
|
18
|
-
* @param props - Account slice, the SDK holding its market, and optionally the
|
|
19
|
-
* collateral the withdrawal would be funded from
|
|
20
|
-
* @returns The three limits, see {@link WithdrawCeilings}
|
|
21
|
-
**/
|
|
22
|
-
function withdrawLimits(props) {
|
|
23
|
-
const { creditAccount, sdk } = props;
|
|
24
|
-
const view = accountView(creditAccount, sdk);
|
|
25
|
-
const partial = maxProportionalWithdrawal(view, view.debtLimits);
|
|
26
|
-
const safe = maxSafeWithdrawal({
|
|
27
|
-
...props,
|
|
28
|
-
targetHF: BigInt(MIN_HEALTH_FACTOR_FACADE)
|
|
29
|
-
});
|
|
30
|
-
return {
|
|
31
|
-
partial,
|
|
32
|
-
safePartial: BigIntMath.min(partial, safe),
|
|
33
|
-
exit: view.collateral > 0n ? view.collateral : 0n
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Largest proportional withdrawal the safe-price collateral check still clears,
|
|
38
|
-
* in underlying units.
|
|
39
|
-
*
|
|
40
|
-
* A withdrawal hands funds over, so the facade weighs the account it leaves
|
|
41
|
-
* behind at safe prices rather than main ones — see {@link collateralMoney}.
|
|
42
|
-
* That is a second limit on top of the facade's `debtLimits`, and the two are
|
|
43
|
-
* independent: a caller wanting the amount a form may actually offer takes the
|
|
44
|
-
* lesser of this and `maxProportionalWithdrawal`.
|
|
45
|
-
*
|
|
46
|
-
* The arithmetic is the check solved for the amount. Taking `W` out at fixed
|
|
47
|
-
* leverage repays `dD = D·W/C`, so `W·TVL/C` of value is sold out of the source
|
|
48
|
-
* token; each dollar of that sale costs the check the source's threshold times
|
|
49
|
-
* its safe-to-main price ratio, while the repayment relieves `targetHF` per
|
|
50
|
-
* dollar of debt. Both terms are linear in `W`, which is why one division
|
|
51
|
-
* answers instead of a search — and why the answer is exact rather than a
|
|
52
|
-
* bound, as long as the plan really does fund itself from `sourceToken`.
|
|
53
|
-
*
|
|
54
|
-
* Two consequences worth stating, because they surprise:
|
|
55
|
-
*
|
|
56
|
-
* - An account whose collateral is entirely a token the reserve feed marks
|
|
57
|
-
* down cannot withdraw at all once it is under the threshold. A proportional
|
|
58
|
-
* withdrawal scales collateral and debt together, so it leaves the safe-price
|
|
59
|
-
* factor exactly where it found it — no amount climbs back over.
|
|
60
|
-
* - Leaving entirely is never refused for this reason: the exit settles the
|
|
61
|
-
* debt instead of shrinking it, and a check with no debt to divide by has
|
|
62
|
-
* nothing to refuse.
|
|
63
|
-
*
|
|
64
|
-
* @returns Amount in underlying units. The account's net value when safe prices
|
|
65
|
-
* do not limit the withdrawal at all, so the caller's `min` is a no-op; `0n`
|
|
66
|
-
* when the account already sits below `targetHF` at safe prices, and only the
|
|
67
|
-
* exit is left
|
|
68
|
-
**/
|
|
69
|
-
function maxSafeWithdrawal(props) {
|
|
70
|
-
const { creditAccount, sdk, targetHF } = props;
|
|
71
|
-
const view = accountView(creditAccount, sdk);
|
|
72
|
-
if (view.collateral <= 0n) return 0n;
|
|
73
|
-
if (view.debt === 0n) return view.collateral;
|
|
74
|
-
const money = collateralMoney(creditAccount, sdk);
|
|
75
|
-
const source = props.sourceToken ?? view.fattest();
|
|
76
|
-
const holding = source === void 0 ? void 0 : creditAccount.tokens.find((t) => eq(t.token, source));
|
|
77
|
-
if (!holding) return view.collateral;
|
|
78
|
-
let total = 0n;
|
|
79
|
-
for (const t of creditAccount.tokens) if (money.counts(t)) total += money.weigh(t);
|
|
80
|
-
const debtUsd = money.mainUsd(money.underlying, view.debt);
|
|
81
|
-
if (debtUsd === void 0 || debtUsd <= 0n) return view.collateral;
|
|
82
|
-
const slack = total - debtUsd * targetHF;
|
|
83
|
-
if (slack < 0n) return 0n;
|
|
84
|
-
const sourceMainUsd = money.mainUsd(holding.token, holding.balance);
|
|
85
|
-
if (sourceMainUsd === void 0 || sourceMainUsd <= 0n) return view.collateral;
|
|
86
|
-
const sourceRate = money.lt(holding.token) * money.checkedUsd(holding);
|
|
87
|
-
const tvlUsd = money.mainUsd(money.underlying, view.collateral + view.debt);
|
|
88
|
-
if (tvlUsd === void 0) return view.collateral;
|
|
89
|
-
const drain = sourceRate * tvlUsd - targetHF * debtUsd * sourceMainUsd;
|
|
90
|
-
if (drain <= 0n) return view.collateral;
|
|
91
|
-
const limit = view.collateral * slack * sourceMainUsd / drain;
|
|
92
|
-
return limit < view.collateral ? limit : view.collateral;
|
|
93
|
-
}
|
|
94
|
-
//#endregion
|
|
95
|
-
export { maxSafeWithdrawal, withdrawLimits };
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { reservePriceLimited } from "../../../model/errors/operation-errors.js";
|
|
2
|
-
import "../../../model/index.js";
|
|
3
|
-
//#region src/onchain/validation/checks/checkReservePriceLimited.ts
|
|
4
|
-
/**
|
|
5
|
-
* Whether a failed collateral check is the reserve price feed's doing.
|
|
6
|
-
*
|
|
7
|
-
* A call that hands funds over is weighed at safe prices — `min` of a token's
|
|
8
|
-
* two feeds, and nothing at all where governance registered no reserve feed —
|
|
9
|
-
* so an account that covers its debt at the main feed can still be refused.
|
|
10
|
-
* The two are worth telling apart: a position that is genuinely too small is
|
|
11
|
-
* fixed by adding collateral or requesting less, while this one is a valuation
|
|
12
|
-
* the account does not control, and requesting less only helps as far as
|
|
13
|
-
* `withdrawable` says it does.
|
|
14
|
-
*
|
|
15
|
-
* Runs after `checkCollateralised` and answers only when that one refused, so
|
|
16
|
-
* the caller keeps its own threshold rather than restating it here.
|
|
17
|
-
*/
|
|
18
|
-
function checkReservePriceLimited(args) {
|
|
19
|
-
const { healthFactor, atMainPrices, healthFactorThreshold, withdrawable } = args;
|
|
20
|
-
if (healthFactor >= healthFactorThreshold || atMainPrices < healthFactorThreshold) return [];
|
|
21
|
-
return [reservePriceLimited({
|
|
22
|
-
healthFactor,
|
|
23
|
-
atMainPrices,
|
|
24
|
-
healthFactorThreshold,
|
|
25
|
-
withdrawable
|
|
26
|
-
})];
|
|
27
|
-
}
|
|
28
|
-
//#endregion
|
|
29
|
-
export { checkReservePriceLimited };
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { OnchainSDK } from "../../OnchainSDK.js";
|
|
2
|
-
import { CreditAccountSlice } from "./types.js";
|
|
3
|
-
import "../../index.js";
|
|
4
|
-
import { Address } from "viem";
|
|
5
|
-
//#region src/onchain/accounts/intents/collateral-money.d.ts
|
|
6
|
-
/** One balance on the account, as the slice carries it. */
|
|
7
|
-
type Holding = CreditAccountSlice["tokens"][number];
|
|
8
|
-
/**
|
|
9
|
-
* The collateral check's own valuation of an account, as a handful of lookups.
|
|
10
|
-
*
|
|
11
|
-
* Shared by everything that solves that check for an amount, so the rules it
|
|
12
|
-
* encodes are written once: a holding backed by a quota counts the lesser of the
|
|
13
|
-
* quota and its threshold-weighted value, an unquoted one its weighted value
|
|
14
|
-
* alone, and dust or a disabled balance nothing at all. Collateral is valued at
|
|
15
|
-
* the protocol safe price — `min` of the two feeds, 0 where there is no reserve
|
|
16
|
-
* — the way the facade values a call that hands funds over; the underlying is
|
|
17
|
-
* exempt and stays on the main feed, as `CreditManagerV3._safeConvertToUSD`
|
|
18
|
-
* does.
|
|
19
|
-
*
|
|
20
|
-
* Money is carried in USD × `PERCENTAGE_FACTOR`, the units the check compares
|
|
21
|
-
* in, so a threshold never has to be divided back out.
|
|
22
|
-
*/
|
|
23
|
-
interface CollateralMoney {
|
|
24
|
-
/** Market underlying, the one token safe pricing does not touch. */
|
|
25
|
-
underlying: Address;
|
|
26
|
-
/** Whether the holding is weighed at all. */
|
|
27
|
-
counts(holding: Holding): boolean;
|
|
28
|
-
/** What the holding backs, in USD × `PERCENTAGE_FACTOR`. */
|
|
29
|
-
weigh(holding: Holding): bigint;
|
|
30
|
-
/** What the holding's quota backs, in the same units; 0 on a closed market. */
|
|
31
|
-
quotaMoney(holding: Holding): bigint;
|
|
32
|
-
/** USD at the main feed; `undefined` when the token has no price at all. */
|
|
33
|
-
mainUsd(token: Address, amount: bigint): bigint | undefined;
|
|
34
|
-
/** USD the check counts the holding at, before its threshold. */
|
|
35
|
-
checkedUsd(holding: Holding): bigint;
|
|
36
|
-
/** Liquidation threshold in basis points; 0 for a token the manager refuses. */
|
|
37
|
-
lt(token: Address): bigint;
|
|
38
|
-
}
|
|
39
|
-
/** {@inheritDoc CollateralMoney} */
|
|
40
|
-
declare function collateralMoney(creditAccount: CreditAccountSlice, sdk: OnchainSDK): CollateralMoney;
|
|
41
|
-
//#endregion
|
|
42
|
-
export { CollateralMoney, Holding, collateralMoney };
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { OnchainSDK } from "../../OnchainSDK.js";
|
|
2
|
-
import { CreditAccountSlice, WithdrawCeilings } from "./types.js";
|
|
3
|
-
import "../../index.js";
|
|
4
|
-
import { Address } from "viem";
|
|
5
|
-
//#region src/onchain/accounts/intents/withdraw-limits.d.ts
|
|
6
|
-
interface WithdrawLimitsProps {
|
|
7
|
-
creditAccount: CreditAccountSlice;
|
|
8
|
-
sdk: OnchainSDK;
|
|
9
|
-
/**
|
|
10
|
-
* Token the withdrawal liquidates. Defaults to the account's largest
|
|
11
|
-
* non-phantom balance, which is what the planner reaches for when the intent
|
|
12
|
-
* names none.
|
|
13
|
-
*/
|
|
14
|
-
sourceToken?: Address;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Every limit a `WITHDRAW` answers to, in underlying units.
|
|
18
|
-
*
|
|
19
|
-
* The one place they are assembled, so the figure a form is offered and the
|
|
20
|
-
* figure the collateral guard names when it turns a withdrawal down cannot
|
|
21
|
-
* drift apart: `CreditAccountOperationsService` reports this, and the guard
|
|
22
|
-
* quotes it back.
|
|
23
|
-
*
|
|
24
|
-
* @param props - Account slice, the SDK holding its market, and optionally the
|
|
25
|
-
* collateral the withdrawal would be funded from
|
|
26
|
-
* @returns The three limits, see {@link WithdrawCeilings}
|
|
27
|
-
**/
|
|
28
|
-
declare function withdrawLimits(props: WithdrawLimitsProps): WithdrawCeilings;
|
|
29
|
-
interface MaxSafeWithdrawalProps {
|
|
30
|
-
creditAccount: CreditAccountSlice;
|
|
31
|
-
sdk: OnchainSDK;
|
|
32
|
-
/**
|
|
33
|
-
* Token the withdrawal liquidates. Defaults to the account's largest
|
|
34
|
-
* non-phantom balance, which is what the planner reaches for when the intent
|
|
35
|
-
* names none.
|
|
36
|
-
*/
|
|
37
|
-
sourceToken?: Address;
|
|
38
|
-
/**
|
|
39
|
-
* Health factor the withdrawal has to leave behind, in basis points. The
|
|
40
|
-
* facade's own threshold answers "would this land"; a form holding the
|
|
41
|
-
* account to something stricter passes its own.
|
|
42
|
-
*/
|
|
43
|
-
targetHF: bigint;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Largest proportional withdrawal the safe-price collateral check still clears,
|
|
47
|
-
* in underlying units.
|
|
48
|
-
*
|
|
49
|
-
* A withdrawal hands funds over, so the facade weighs the account it leaves
|
|
50
|
-
* behind at safe prices rather than main ones — see {@link collateralMoney}.
|
|
51
|
-
* That is a second limit on top of the facade's `debtLimits`, and the two are
|
|
52
|
-
* independent: a caller wanting the amount a form may actually offer takes the
|
|
53
|
-
* lesser of this and `maxProportionalWithdrawal`.
|
|
54
|
-
*
|
|
55
|
-
* The arithmetic is the check solved for the amount. Taking `W` out at fixed
|
|
56
|
-
* leverage repays `dD = D·W/C`, so `W·TVL/C` of value is sold out of the source
|
|
57
|
-
* token; each dollar of that sale costs the check the source's threshold times
|
|
58
|
-
* its safe-to-main price ratio, while the repayment relieves `targetHF` per
|
|
59
|
-
* dollar of debt. Both terms are linear in `W`, which is why one division
|
|
60
|
-
* answers instead of a search — and why the answer is exact rather than a
|
|
61
|
-
* bound, as long as the plan really does fund itself from `sourceToken`.
|
|
62
|
-
*
|
|
63
|
-
* Two consequences worth stating, because they surprise:
|
|
64
|
-
*
|
|
65
|
-
* - An account whose collateral is entirely a token the reserve feed marks
|
|
66
|
-
* down cannot withdraw at all once it is under the threshold. A proportional
|
|
67
|
-
* withdrawal scales collateral and debt together, so it leaves the safe-price
|
|
68
|
-
* factor exactly where it found it — no amount climbs back over.
|
|
69
|
-
* - Leaving entirely is never refused for this reason: the exit settles the
|
|
70
|
-
* debt instead of shrinking it, and a check with no debt to divide by has
|
|
71
|
-
* nothing to refuse.
|
|
72
|
-
*
|
|
73
|
-
* @returns Amount in underlying units. The account's net value when safe prices
|
|
74
|
-
* do not limit the withdrawal at all, so the caller's `min` is a no-op; `0n`
|
|
75
|
-
* when the account already sits below `targetHF` at safe prices, and only the
|
|
76
|
-
* exit is left
|
|
77
|
-
**/
|
|
78
|
-
declare function maxSafeWithdrawal(props: MaxSafeWithdrawalProps): bigint;
|
|
79
|
-
//#endregion
|
|
80
|
-
export { MaxSafeWithdrawalProps, WithdrawLimitsProps, maxSafeWithdrawal, withdrawLimits };
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { Bps, TokenAmount } from "../../../model/primitives.js";
|
|
2
|
-
import { ReservePriceLimitedError } from "../../../model/errors/operation-errors.js";
|
|
3
|
-
import "../../../model/index.js";
|
|
4
|
-
//#region src/onchain/validation/checks/checkReservePriceLimited.d.ts
|
|
5
|
-
interface ReservePriceLimitedArgs {
|
|
6
|
-
/** The safe-price factor, the one the collateral check compared. */
|
|
7
|
-
healthFactor: Bps;
|
|
8
|
-
/** The same account at the main feed. */
|
|
9
|
-
atMainPrices: Bps;
|
|
10
|
-
/** The lowest acceptable factor — a factor equal to it passes. */
|
|
11
|
-
healthFactorThreshold: Bps;
|
|
12
|
-
/** What the account can still take out, in the market's underlying. */
|
|
13
|
-
withdrawable: TokenAmount;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Whether a failed collateral check is the reserve price feed's doing.
|
|
17
|
-
*
|
|
18
|
-
* A call that hands funds over is weighed at safe prices — `min` of a token's
|
|
19
|
-
* two feeds, and nothing at all where governance registered no reserve feed —
|
|
20
|
-
* so an account that covers its debt at the main feed can still be refused.
|
|
21
|
-
* The two are worth telling apart: a position that is genuinely too small is
|
|
22
|
-
* fixed by adding collateral or requesting less, while this one is a valuation
|
|
23
|
-
* the account does not control, and requesting less only helps as far as
|
|
24
|
-
* `withdrawable` says it does.
|
|
25
|
-
*
|
|
26
|
-
* Runs after `checkCollateralised` and answers only when that one refused, so
|
|
27
|
-
* the caller keeps its own threshold rather than restating it here.
|
|
28
|
-
*/
|
|
29
|
-
declare function checkReservePriceLimited(args: ReservePriceLimitedArgs): ReservePriceLimitedError[];
|
|
30
|
-
//#endregion
|
|
31
|
-
export { ReservePriceLimitedArgs, checkReservePriceLimited };
|