@gearbox-protocol/sdk 17.10.0 → 17.10.1-next.1
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/index.js +1 -0
- package/dist/cjs/onchain/accounts/intents/guards.js +4 -2
- package/dist/cjs/onchain/accounts/intents/index.js +41 -7
- package/dist/cjs/onchain/accounts/intents/math.js +3 -44
- package/dist/cjs/onchain/accounts/intents/maxBorrow.js +7 -1
- package/dist/cjs/onchain/accounts/intents/open-strategy.js +12 -7
- package/dist/cjs/onchain/accounts/intents/plan.js +4 -4
- package/dist/cjs/onchain/accounts/intents/realize.js +4 -3
- package/dist/cjs/onchain/accounts/intents/strategyLimits.js +159 -0
- package/dist/cjs/onchain/accounts/intents/tail.js +5 -1
- package/dist/cjs/onchain/accounts/intents/testing/sdk-mock.js +4 -0
- package/dist/cjs/onchain/accounts/intents/utils/index.js +1 -0
- package/dist/cjs/onchain/accounts/intents/utils/quotas-for-update.js +39 -0
- package/dist/cjs/onchain/accounts/quota-utils.js +9 -0
- package/dist/cjs/onchain/index.js +1 -0
- package/dist/cjs/onchain/market/oracle/PriceOracleBaseContract.js +8 -6
- package/dist/cjs/onchain/validation/checks/checkBorrowLimit.js +2 -3
- package/dist/cjs/onchain/validation/checks/checkDebtLimits.js +3 -4
- package/dist/cjs/onchain/validation/checks/checkQuotaLimit.js +2 -1
- package/dist/esm/dev/AccountOpener.js +1 -1
- package/dist/esm/dev/withdrawalUtils.js +1 -1
- package/dist/esm/history/trace/extractTransfers.js +1 -1
- package/dist/esm/onchain/accounts/CreditAccountsServiceV310.js +2 -2
- package/dist/esm/onchain/accounts/index.js +2 -2
- package/dist/esm/onchain/accounts/intents/guards.js +4 -2
- package/dist/esm/onchain/accounts/intents/index.js +41 -7
- package/dist/esm/onchain/accounts/intents/math.js +5 -45
- package/dist/esm/onchain/accounts/intents/maxBorrow.js +7 -1
- package/dist/esm/onchain/accounts/intents/open-strategy.js +13 -8
- package/dist/esm/onchain/accounts/intents/plan.js +4 -4
- package/dist/esm/onchain/accounts/intents/realize.js +4 -3
- package/dist/esm/onchain/accounts/intents/strategyLimits.js +158 -0
- package/dist/esm/onchain/accounts/intents/tail.js +5 -1
- package/dist/esm/onchain/accounts/intents/testing/sdk-mock.js +4 -0
- package/dist/esm/onchain/accounts/intents/utils/index.js +2 -2
- package/dist/esm/onchain/accounts/intents/utils/quotas-for-update.js +41 -3
- package/dist/esm/onchain/accounts/liquidations/LiquidationsService.js +1 -1
- package/dist/esm/onchain/accounts/quota-utils.js +9 -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 +2 -2
- 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/oracle/PriceOracleBaseContract.js +8 -6
- 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/checkBorrowLimit.js +2 -3
- package/dist/esm/onchain/validation/checks/checkDebtLimits.js +3 -4
- package/dist/esm/onchain/validation/checks/checkQuotaLimit.js +2 -1
- package/dist/types/model/errors/index.d.ts +2 -2
- package/dist/types/model/errors/operation-errors.d.ts +28 -14
- package/dist/types/model/index.d.ts +2 -2
- package/dist/types/onchain/accounts/index.d.ts +2 -2
- package/dist/types/onchain/accounts/intents/guards.d.ts +12 -12
- package/dist/types/onchain/accounts/intents/math.d.ts +7 -16
- package/dist/types/onchain/accounts/intents/plan.d.ts +2 -0
- package/dist/types/onchain/accounts/intents/realize.d.ts +3 -0
- package/dist/types/onchain/accounts/intents/strategyLimits.d.ts +33 -0
- package/dist/types/onchain/accounts/intents/tail.d.ts +13 -9
- package/dist/types/onchain/accounts/intents/utils/index.d.ts +2 -2
- package/dist/types/onchain/accounts/intents/utils/quotas-for-update.d.ts +15 -1
- package/dist/types/onchain/accounts/quota-utils.d.ts +11 -1
- package/dist/types/onchain/index.d.ts +2 -2
- package/dist/types/onchain/market/oracle/PriceOracleBaseContract.d.ts +0 -3
- package/dist/types/onchain/market/oracle/types.d.ts +4 -3
- package/dist/types/onchain/validation/checks/checkBorrowLimit.d.ts +2 -3
- package/dist/types/onchain/validation/checks/checkDebtLimits.d.ts +2 -3
- package/dist/types/onchain/validation/checks/checkQuotaLimit.d.ts +2 -2
- package/package.json +1 -1
|
@@ -55,6 +55,7 @@ exports.fetchCreditAccountSlice = require_onchain_accounts_intents_utils_credit_
|
|
|
55
55
|
exports.getWithdrawalCompressorAddress = require_onchain_accounts_withdrawal_compressor_addresses.getWithdrawalCompressorAddress;
|
|
56
56
|
exports.iCreditAccountAbi = require_onchain_accounts_withdrawal_compressor_AbstractWithdrawalCompressorContract.iCreditAccountAbi;
|
|
57
57
|
exports.isPhantomToken = require_onchain_accounts_intents_utils_pick_token.isPhantomToken;
|
|
58
|
+
exports.maxAmountForQuota = require_onchain_accounts_quota_utils.maxAmountForQuota;
|
|
58
59
|
exports.roundUpQuota = require_onchain_accounts_quota_utils.roundUpQuota;
|
|
59
60
|
exports.toClaimableWithdrawal = require_onchain_accounts_withdrawal_compressor_AbstractWithdrawalCompressorContract.toClaimableWithdrawal;
|
|
60
61
|
exports.toCreditAccountSlice = require_onchain_accounts_intents_utils_credit_account_slice.toCreditAccountSlice;
|
|
@@ -53,7 +53,7 @@ function assertCanBorrow(sdk, suite, amount, options = {}) {
|
|
|
53
53
|
available: maxBorrowAmount.amount.value,
|
|
54
54
|
limit: maxBorrowAmount.limit,
|
|
55
55
|
underlying: require_onchain_validation_helpers_token.toToken(sdk, suite.market.pool.underlying),
|
|
56
|
-
|
|
56
|
+
...options
|
|
57
57
|
}), `borrow: ${amount} exceeds what the pool can lend now (${maxBorrowAmount.amount.value})`);
|
|
58
58
|
}
|
|
59
59
|
/**
|
|
@@ -82,6 +82,7 @@ function assertGrowthAllowed(args) {
|
|
|
82
82
|
token: require_onchain_validation_helpers_token.toToken(sdk, token),
|
|
83
83
|
requested: void 0,
|
|
84
84
|
available: 0n,
|
|
85
|
+
...args.limits,
|
|
85
86
|
underlying: require_onchain_validation_helpers_token.toToken(sdk, underlying)
|
|
86
87
|
}), `${token} takes no quota in this market, so it counts as no collateral`);
|
|
87
88
|
}
|
|
@@ -139,7 +140,7 @@ function assertCollateralised(healthFactorBps, safePrices, atSafePrices) {
|
|
|
139
140
|
* A quota can only be raised as far as the market still has room for: past the
|
|
140
141
|
* token's limit the keeper takes nothing more, whoever is asking.
|
|
141
142
|
*/
|
|
142
|
-
function assertQuotaAvailable(sdk, market, increases) {
|
|
143
|
+
function assertQuotaAvailable(sdk, market, increases, options = {}) {
|
|
143
144
|
const underlying = market.pool.underlying;
|
|
144
145
|
const { pqk } = market.pool;
|
|
145
146
|
for (const { token, balance } of increases) {
|
|
@@ -149,6 +150,7 @@ function assertQuotaAvailable(sdk, market, increases) {
|
|
|
149
150
|
token: require_onchain_validation_helpers_token.toToken(sdk, token),
|
|
150
151
|
requested: balance,
|
|
151
152
|
available: left,
|
|
153
|
+
...options,
|
|
152
154
|
underlying: require_onchain_validation_helpers_token.toToken(sdk, underlying)
|
|
153
155
|
}), `${token} has ${left} of quota left, the plan needs ${balance}`);
|
|
154
156
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_onchain_constants_math = require("../../constants/math.js");
|
|
2
3
|
const require_onchain_base_SDKConstruct = require("../../base/SDKConstruct.js");
|
|
3
4
|
const require_model_errors_prepare_errors = require("../../../model/errors/prepare-errors.js");
|
|
4
5
|
const require_model_result = require("../../../model/result.js");
|
|
@@ -13,6 +14,7 @@ const require_onchain_accounts_intents_borrow = require("./borrow.js");
|
|
|
13
14
|
const require_onchain_accounts_intents_leverage_band = require("./leverage-band.js");
|
|
14
15
|
const require_onchain_accounts_intents_maxBorrow = require("./maxBorrow.js");
|
|
15
16
|
const require_onchain_accounts_intents_maxWithdrawCollateral = require("./maxWithdrawCollateral.js");
|
|
17
|
+
const require_onchain_accounts_intents_strategyLimits = require("./strategyLimits.js");
|
|
16
18
|
const require_onchain_accounts_intents_open_strategy = require("./open-strategy.js");
|
|
17
19
|
const require_onchain_accounts_intents_plan = require("./plan.js");
|
|
18
20
|
const require_onchain_accounts_intents_view = require("./view.js");
|
|
@@ -37,9 +39,25 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
|
|
|
37
39
|
* balance)
|
|
38
40
|
*/
|
|
39
41
|
async startIntent(props) {
|
|
42
|
+
const { intent, sdk, creditAccount, quotaReserve } = props;
|
|
43
|
+
let current;
|
|
44
|
+
let limits = {};
|
|
45
|
+
if (intent.type === "DEPOSIT" || intent.type === "ADJUST_LEVERAGE") {
|
|
46
|
+
current = require_onchain_accounts_intents_view.accountView(creditAccount, sdk);
|
|
47
|
+
limits = require_onchain_accounts_intents_strategyLimits.strategyLimits({
|
|
48
|
+
...intent,
|
|
49
|
+
suite: sdk.marketRegister.findCreditManager(creditAccount.creditManager),
|
|
50
|
+
view: current,
|
|
51
|
+
initialQuotas: creditAccount.tokens,
|
|
52
|
+
quotaReserve
|
|
53
|
+
});
|
|
54
|
+
}
|
|
40
55
|
return plain(await this.#preview(props, () => {
|
|
41
56
|
const { intent } = props;
|
|
42
|
-
const view =
|
|
57
|
+
const view = {
|
|
58
|
+
...current ?? require_onchain_accounts_intents_view.accountView(props.creditAccount, props.sdk),
|
|
59
|
+
limits
|
|
60
|
+
};
|
|
43
61
|
switch (intent.type) {
|
|
44
62
|
case "ADD_COLLATERAL": return require_onchain_accounts_intents_plan.planAddCollateral(intent);
|
|
45
63
|
case "WITHDRAW_ASSET": return require_onchain_accounts_intents_plan.planWithdrawAsset(intent, view);
|
|
@@ -49,7 +67,7 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
|
|
|
49
67
|
case "WITHDRAW": return require_onchain_accounts_intents_plan.planWithdraw(intent, view);
|
|
50
68
|
default: throw new Error(`${intent.type} - not implemented`);
|
|
51
69
|
}
|
|
52
|
-
}));
|
|
70
|
+
}, limits));
|
|
53
71
|
}
|
|
54
72
|
/**
|
|
55
73
|
* Both ends of what a `WITHDRAW` can take out, in underlying: the largest
|
|
@@ -191,14 +209,28 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
|
|
|
191
209
|
*/
|
|
192
210
|
async startDelayedIntent(props) {
|
|
193
211
|
const { intent } = props;
|
|
212
|
+
const current = require_onchain_accounts_intents_view.accountView(props.creditAccount, props.sdk);
|
|
213
|
+
const repayFromSource = current.debt - current.balanceOf(current.underlying);
|
|
214
|
+
const maximum = intent.type === "ADJUST_LEVERAGE" && current.collateral > 0n && repayFromSource > 0n ? require_onchain_constants_math.LEVERAGE_DECIMALS + (repayFromSource * require_onchain_constants_math.LEVERAGE_DECIMALS - 1n) / current.collateral : void 0;
|
|
215
|
+
const limits = intent.type === "ADJUST_LEVERAGE" && maximum !== void 0 ? require_onchain_accounts_intents_strategyLimits.strategyLimits({
|
|
216
|
+
...intent,
|
|
217
|
+
suite: props.sdk.marketRegister.findCreditManager(props.creditAccount.creditManager),
|
|
218
|
+
view: current,
|
|
219
|
+
initialQuotas: props.creditAccount.tokens,
|
|
220
|
+
quotaReserve: props.quotaReserve,
|
|
221
|
+
maxLeverage: maximum
|
|
222
|
+
}) : {};
|
|
194
223
|
const result = await this.#preview(props, () => {
|
|
195
|
-
const view =
|
|
224
|
+
const view = {
|
|
225
|
+
...current,
|
|
226
|
+
limits
|
|
227
|
+
};
|
|
196
228
|
switch (intent.type) {
|
|
197
229
|
case "ADJUST_LEVERAGE": return require_onchain_accounts_intents_plan.planAdjustLeverageDelayed(intent, view);
|
|
198
230
|
case "WITHDRAW": return require_onchain_accounts_intents_plan.planWithdrawDelayed(intent, view);
|
|
199
231
|
default: throw new Error(`${intent.type} - cannot be delayed`);
|
|
200
232
|
}
|
|
201
|
-
});
|
|
233
|
+
}, limits);
|
|
202
234
|
if (!result.ok) return result;
|
|
203
235
|
const { delayed } = result;
|
|
204
236
|
if (!delayed) throw new Error("startDelayedIntent: plan started no withdrawal");
|
|
@@ -214,7 +246,8 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
|
|
|
214
246
|
delayed,
|
|
215
247
|
creditAccount: props.creditAccount,
|
|
216
248
|
sdk: props.sdk,
|
|
217
|
-
quotaReserve: props.quotaReserve
|
|
249
|
+
quotaReserve: props.quotaReserve,
|
|
250
|
+
limits
|
|
218
251
|
});
|
|
219
252
|
return {
|
|
220
253
|
...result,
|
|
@@ -400,7 +433,7 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
|
|
|
400
433
|
}
|
|
401
434
|
}
|
|
402
435
|
/** Plan → realise → wrap. Unviable requests become `{ ok: false }`. */
|
|
403
|
-
async #preview(props, plan) {
|
|
436
|
+
async #preview(props, plan, limits = {}) {
|
|
404
437
|
const draft = props.draft ?? {};
|
|
405
438
|
try {
|
|
406
439
|
const suite = props.sdk.marketRegister.findCreditManager(props.creditAccount.creditManager);
|
|
@@ -411,7 +444,8 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
|
|
|
411
444
|
sdk: props.sdk,
|
|
412
445
|
slippage: props.slippage ?? 0,
|
|
413
446
|
quotaReserve: props.quotaReserve,
|
|
414
|
-
draft
|
|
447
|
+
draft,
|
|
448
|
+
limits
|
|
415
449
|
});
|
|
416
450
|
return {
|
|
417
451
|
ok: true,
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_onchain_utils_bigint_math = require("../../utils/bigint-math.js");
|
|
3
3
|
const require_onchain_constants_math = require("../../constants/math.js");
|
|
4
|
-
require("../../constants/index.js");
|
|
5
4
|
const require_model_errors_operation_errors = require("../../../model/errors/operation-errors.js");
|
|
6
5
|
require("../../../model/index.js");
|
|
7
6
|
const require_onchain_validation_helpers_token = require("../../validation/helpers/token.js");
|
|
@@ -59,58 +58,18 @@ function assertLeverageAtLeastOne(leverage) {
|
|
|
59
58
|
* may end owing nothing, an opening may not.
|
|
60
59
|
*/
|
|
61
60
|
function assertDebtLimits(sdk, debt, debtLimits, underlying, options = {}) {
|
|
62
|
-
const { allowZero = true,
|
|
61
|
+
const { allowZero = true, ...rest } = options;
|
|
63
62
|
require_onchain_validation_raise.raise(require_onchain_validation_checks_checkDebtLimits.checkDebtLimits({
|
|
64
63
|
debt,
|
|
65
|
-
|
|
66
|
-
maxDebt: debtLimits.maxDebt,
|
|
64
|
+
...debtLimits,
|
|
67
65
|
underlying: require_onchain_validation_helpers_token.toToken(sdk, underlying),
|
|
68
66
|
allowZero,
|
|
69
|
-
|
|
70
|
-
maxBorrowAmount
|
|
67
|
+
...rest
|
|
71
68
|
}), debt > debtLimits.maxDebt ? `debt ${debt} exceeds maxDebt ${debtLimits.maxDebt}` : `debt ${debt} is below minDebt ${debtLimits.minDebt}`);
|
|
72
69
|
}
|
|
73
|
-
/** Finds single-token Open amounts whose projected debt fits the market limits. */
|
|
74
|
-
function openCollateralForDebtLimits({ suite, collateral, leverage }) {
|
|
75
|
-
if (collateral.length !== 1 || leverage <= 100n) return void 0;
|
|
76
|
-
const [{ token }] = collateral;
|
|
77
|
-
const { market } = suite;
|
|
78
|
-
const limits = {
|
|
79
|
-
minDebt: suite.creditFacade.minDebt,
|
|
80
|
-
maxDebt: suite.maxBorrowAmount().amount.value
|
|
81
|
-
};
|
|
82
|
-
if (limits.maxDebt < limits.minDebt) return void 0;
|
|
83
|
-
const debtAt = (amount) => {
|
|
84
|
-
const converted = market.priceOracle.safeConvert(token, market.pool.underlying, amount);
|
|
85
|
-
return converted.error ? void 0 : debtForLeverage(converted.value, leverage);
|
|
86
|
-
};
|
|
87
|
-
const lastDebt = debtAt(require_onchain_constants_math.MAX_UINT256);
|
|
88
|
-
if (lastDebt === void 0 || lastDebt < limits.minDebt) return void 0;
|
|
89
|
-
const firstAtLeast = (threshold) => {
|
|
90
|
-
let low = 0n;
|
|
91
|
-
let high = require_onchain_constants_math.MAX_UINT256;
|
|
92
|
-
while (low < high) {
|
|
93
|
-
const middle = (low + high) / 2n;
|
|
94
|
-
const debt = debtAt(middle);
|
|
95
|
-
if (debt === void 0) return void 0;
|
|
96
|
-
if (debt < threshold) low = middle + 1n;
|
|
97
|
-
else high = middle;
|
|
98
|
-
}
|
|
99
|
-
return low;
|
|
100
|
-
};
|
|
101
|
-
const min = firstAtLeast(limits.minDebt > 0n ? limits.minDebt : 1n);
|
|
102
|
-
const above = lastDebt > limits.maxDebt ? firstAtLeast(limits.maxDebt + 1n) : void 0;
|
|
103
|
-
if (min === void 0 || lastDebt > limits.maxDebt && above === void 0) return void 0;
|
|
104
|
-
const max = above === void 0 ? require_onchain_constants_math.MAX_UINT256 : above - 1n;
|
|
105
|
-
return min <= max ? {
|
|
106
|
-
min: market.priceOracle.toTokenAmount(token, min),
|
|
107
|
-
max: market.priceOracle.toTokenAmount(token, max)
|
|
108
|
-
} : void 0;
|
|
109
|
-
}
|
|
110
70
|
//#endregion
|
|
111
71
|
exports.assertDebtLimits = assertDebtLimits;
|
|
112
72
|
exports.assertLeverageAtLeastOne = assertLeverageAtLeastOne;
|
|
113
73
|
exports.debtForLeverage = debtForLeverage;
|
|
114
74
|
exports.maxProportionalWithdrawal = maxProportionalWithdrawal;
|
|
115
|
-
exports.openCollateralForDebtLimits = openCollateralForDebtLimits;
|
|
116
75
|
exports.proportionalDebt = proportionalDebt;
|
|
@@ -87,7 +87,13 @@ function maxBorrow(props) {
|
|
|
87
87
|
if (lends < suite.creditFacade.minDebt) return 0n;
|
|
88
88
|
const ceiling = require_onchain_utils_bigint_math.BigIntMath.min(priceOracle.safeConvertFromUSD(underlying, backed / targetHF).value, lends);
|
|
89
89
|
const unwrapsPayout = !!rwaAsset && require_onchain_accounts_intents_utils_common.eq(borrowToken, rwaAsset);
|
|
90
|
-
|
|
90
|
+
if (require_onchain_accounts_intents_utils_common.eq(borrowToken, underlying)) return ceiling;
|
|
91
|
+
if (unwrapsPayout) return require_onchain_accounts_intents_utils_common.toTargetDecimals(ceiling, underlying, borrowToken, sdk);
|
|
92
|
+
const payout = priceOracle.safeConvert(underlying, borrowToken, ceiling);
|
|
93
|
+
if (payout.error) return 0n;
|
|
94
|
+
const debt = priceOracle.safeConvert(borrowToken, underlying, payout.value);
|
|
95
|
+
if (debt.error) return 0n;
|
|
96
|
+
return require_onchain_utils_bigint_math.BigIntMath.max(payout.value - (debt.value > ceiling ? 1n : 0n), 0n);
|
|
91
97
|
}
|
|
92
98
|
//#endregion
|
|
93
99
|
exports.maxBorrow = maxBorrow;
|
|
@@ -9,6 +9,7 @@ const require_onchain_accounts_intents_utils_price_impact = require("./utils/pri
|
|
|
9
9
|
const require_onchain_accounts_intents_utils_quotas_for_update = require("./utils/quotas-for-update.js");
|
|
10
10
|
const require_onchain_accounts_intents_utils_router_path = require("./utils/router-path.js");
|
|
11
11
|
require("./utils/index.js");
|
|
12
|
+
const require_onchain_accounts_intents_strategyLimits = require("./strategyLimits.js");
|
|
12
13
|
//#region src/onchain/accounts/intents/open-strategy.ts
|
|
13
14
|
/**
|
|
14
15
|
* Builds the state opening a leveraged position out of wallet collateral would
|
|
@@ -45,17 +46,20 @@ async function buildOpenStrategyState(props) {
|
|
|
45
46
|
creditFacade: suite.creditFacade.address,
|
|
46
47
|
underlying
|
|
47
48
|
});
|
|
48
|
-
const
|
|
49
|
+
const options = leftoverBalances.length || existing?.tokens.some((t) => t.balance > 0n) ? void 0 : require_onchain_accounts_intents_strategyLimits.strategyLimits({
|
|
50
|
+
type: "OPEN",
|
|
49
51
|
suite,
|
|
50
52
|
collateral,
|
|
51
|
-
leverage
|
|
53
|
+
leverage,
|
|
54
|
+
targetToken,
|
|
55
|
+
quotaReserve
|
|
52
56
|
});
|
|
53
57
|
require_onchain_accounts_intents_math.assertDebtLimits(sdk, debt, suite.creditFacade, underlying, {
|
|
58
|
+
...options,
|
|
54
59
|
allowZero: false,
|
|
55
|
-
maxBorrowAmount: suite.maxBorrowAmount()
|
|
56
|
-
getCollateralLimits
|
|
60
|
+
maxBorrowAmount: suite.maxBorrowAmount()
|
|
57
61
|
});
|
|
58
|
-
require_onchain_accounts_intents_guards.assertCanBorrow(sdk, suite, debt,
|
|
62
|
+
require_onchain_accounts_intents_guards.assertCanBorrow(sdk, suite, debt, options);
|
|
59
63
|
const paths = require_onchain_accounts_intents_utils_router_path.createRouterPaths({
|
|
60
64
|
sdk,
|
|
61
65
|
creditAccount: account,
|
|
@@ -124,9 +128,10 @@ async function buildOpenStrategyState(props) {
|
|
|
124
128
|
suite,
|
|
125
129
|
market,
|
|
126
130
|
before: [],
|
|
127
|
-
after: averageAssets
|
|
131
|
+
after: averageAssets,
|
|
132
|
+
limits: options
|
|
128
133
|
});
|
|
129
|
-
require_onchain_accounts_intents_guards.assertQuotaAvailable(sdk, market, averageQuota);
|
|
134
|
+
require_onchain_accounts_intents_guards.assertQuotaAvailable(sdk, market, averageQuota, options);
|
|
130
135
|
const projection = reached ?? settle();
|
|
131
136
|
require_onchain_accounts_intents_guards.assertCollateralised(projection.healthFactor, false);
|
|
132
137
|
const priceImpact = await require_onchain_accounts_intents_utils_price_impact.collectPriceImpact(leg.probe ? [leg.probe] : [], {
|
|
@@ -43,7 +43,7 @@ function planDeposit(intent, view) {
|
|
|
43
43
|
const aU = view.price(intent.token, U, intent.amount);
|
|
44
44
|
const debtDelta = intent.targetLeverage === void 0 ? require_onchain_accounts_intents_math.proportionalDebt(view, aU) : require_onchain_accounts_intents_math.debtForLeverage(view.collateral + aU, intent.targetLeverage) - view.debt;
|
|
45
45
|
if (debtDelta < 0n) throw new require_onchain_validation_raise.IntentPreviewError(require_model_errors_operation_errors.leverageOutOfRange(), `deposit: target leverage ${intent.targetLeverage} would require repaying debt`);
|
|
46
|
-
require_onchain_accounts_intents_math.assertDebtLimits(view.sdk, view.debt + debtDelta, view.debtLimits, U);
|
|
46
|
+
require_onchain_accounts_intents_math.assertDebtLimits(view.sdk, view.debt + debtDelta, view.debtLimits, U, view.limits);
|
|
47
47
|
const T = intent.positionToken ?? positionToken(view, "deposit");
|
|
48
48
|
const depositStays = require_onchain_accounts_intents_utils_common.eq(intent.token, T);
|
|
49
49
|
return [
|
|
@@ -78,7 +78,7 @@ function planRepay(intent, view) {
|
|
|
78
78
|
}), "repay: the account owes nothing");
|
|
79
79
|
const funding = everything(intent.amount) ? view.price(U, intent.token, withMargin(view.debt)) : intent.amount;
|
|
80
80
|
const repaid = min(view.price(intent.token, U, funding), view.debt);
|
|
81
|
-
require_onchain_accounts_intents_math.assertDebtLimits(view.sdk, view.debt - repaid, view.debtLimits, view.underlying);
|
|
81
|
+
require_onchain_accounts_intents_math.assertDebtLimits(view.sdk, view.debt - repaid, view.debtLimits, view.underlying, view.limits);
|
|
82
82
|
return [
|
|
83
83
|
add(intent.token, funding, intent.value),
|
|
84
84
|
...fundsInU ? [] : [convert(intent.token, U, funding)],
|
|
@@ -307,7 +307,7 @@ function withdrawShape(intent, view) {
|
|
|
307
307
|
};
|
|
308
308
|
}
|
|
309
309
|
const dD = require_onchain_accounts_intents_math.proportionalDebt(view, WU);
|
|
310
|
-
require_onchain_accounts_intents_math.assertDebtLimits(view.sdk, view.debt - dD, view.debtLimits, view.underlying);
|
|
310
|
+
require_onchain_accounts_intents_math.assertDebtLimits(view.sdk, view.debt - dD, view.debtLimits, view.underlying, view.limits);
|
|
311
311
|
return {
|
|
312
312
|
U,
|
|
313
313
|
T,
|
|
@@ -326,7 +326,7 @@ function leverageShape(intent, view) {
|
|
|
326
326
|
require_onchain_accounts_intents_math.assertLeverageAtLeastOne(intent.targetLeverage);
|
|
327
327
|
if (view.collateral <= 0n) throw new require_onchain_validation_raise.IntentPreviewError(require_model_errors_operation_errors.insufficientBalance(), "adjustLeverage: account has no collateral to lever");
|
|
328
328
|
const target = require_onchain_accounts_intents_math.debtForLeverage(view.collateral, intent.targetLeverage);
|
|
329
|
-
require_onchain_accounts_intents_math.assertDebtLimits(view.sdk, target, view.debtLimits, view.underlying);
|
|
329
|
+
require_onchain_accounts_intents_math.assertDebtLimits(view.sdk, target, view.debtLimits, view.underlying, view.limits);
|
|
330
330
|
return {
|
|
331
331
|
U: view.underlying,
|
|
332
332
|
delta: target - view.debt
|
|
@@ -131,7 +131,7 @@ async function realize(steps, props) {
|
|
|
131
131
|
}));
|
|
132
132
|
break;
|
|
133
133
|
case "borrow":
|
|
134
|
-
require_onchain_accounts_intents_guards.assertCanBorrow(sdk, suite, step.amount);
|
|
134
|
+
require_onchain_accounts_intents_guards.assertCanBorrow(sdk, suite, step.amount, props.limits);
|
|
135
135
|
push(require_onchain_accounts_intents_operations.buildIncreaseDebtOperation({
|
|
136
136
|
amount: step.amount,
|
|
137
137
|
creditAccount,
|
|
@@ -395,10 +395,11 @@ async function realize(steps, props) {
|
|
|
395
395
|
suite,
|
|
396
396
|
market,
|
|
397
397
|
before: creditAccount.tokens,
|
|
398
|
-
after: projected.assets
|
|
398
|
+
after: projected.assets,
|
|
399
|
+
limits: props.limits
|
|
399
400
|
});
|
|
400
401
|
if (!cleared && quotas.quotaIncrease.length + quotas.quotaDecrease.length > 0) {
|
|
401
|
-
require_onchain_accounts_intents_guards.assertQuotaAvailable(sdk, market, quotas.quotaIncrease);
|
|
402
|
+
require_onchain_accounts_intents_guards.assertQuotaAvailable(sdk, market, quotas.quotaIncrease, props.limits);
|
|
402
403
|
push(require_onchain_accounts_intents_operations.buildQuotaUpdateOperation({
|
|
403
404
|
update: quotas,
|
|
404
405
|
creditAccount,
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_onchain_utils_bigint_math = require("../../utils/bigint-math.js");
|
|
3
|
+
const require_onchain_constants_math = require("../../constants/math.js");
|
|
4
|
+
require("../../constants/index.js");
|
|
5
|
+
const require_onchain_accounts_intents_utils_quotas_for_update = require("./utils/quotas-for-update.js");
|
|
6
|
+
//#region src/onchain/accounts/intents/strategyLimits.ts
|
|
7
|
+
/** Independent input bounds supplied to debt, liquidity and quota errors. */
|
|
8
|
+
function strategyLimits(props) {
|
|
9
|
+
const { suite, quotaReserve } = props;
|
|
10
|
+
const { market } = suite;
|
|
11
|
+
const { priceOracle: oracle } = market;
|
|
12
|
+
switch (props.type) {
|
|
13
|
+
case "OPEN": {
|
|
14
|
+
const { collateral, leverage, targetToken } = props;
|
|
15
|
+
if (collateral.length !== 1 || leverage <= 100n) return {};
|
|
16
|
+
const [{ token }] = collateral;
|
|
17
|
+
const minDebt = suite.creditFacade.minDebt;
|
|
18
|
+
const maxDebt = suite.maxBorrowAmount().amount.value;
|
|
19
|
+
if (maxDebt < minDebt) return {};
|
|
20
|
+
const minCollateralForDebt = (debt) => {
|
|
21
|
+
const collateralUnderlying = require_onchain_utils_bigint_math.BigIntMath.ceilDiv(debt * require_onchain_constants_math.LEVERAGE_DECIMALS, leverage - require_onchain_constants_math.LEVERAGE_DECIMALS);
|
|
22
|
+
const result = oracle.safeConvert(market.pool.underlying, token, collateralUnderlying - 1n);
|
|
23
|
+
if (result.error) return void 0;
|
|
24
|
+
const valuation = oracle.safeConvert(token, market.pool.underlying, result.value);
|
|
25
|
+
if (valuation.error) return void 0;
|
|
26
|
+
const collateral = result.value + (valuation.value < collateralUnderlying ? 1n : 0n);
|
|
27
|
+
const checked = oracle.safeConvert(token, market.pool.underlying, collateral);
|
|
28
|
+
return checked.error || checked.value < collateralUnderlying ? void 0 : collateral;
|
|
29
|
+
};
|
|
30
|
+
if (oracle.safeConvert(token, market.pool.underlying, 1n).error) return {};
|
|
31
|
+
const minCollateral = minCollateralForDebt(require_onchain_utils_bigint_math.BigIntMath.max(minDebt, 1n));
|
|
32
|
+
const excessCollateral = minCollateralForDebt(maxDebt + 1n);
|
|
33
|
+
if (minCollateral === void 0 || excessCollateral === void 0 || minCollateral > 115792089237316195423570985008687907853269984665640564039457584007913129639935n) return {};
|
|
34
|
+
const maxCollateral = require_onchain_utils_bigint_math.BigIntMath.min(require_onchain_constants_math.MAX_UINT256, excessCollateral - 1n);
|
|
35
|
+
let maxCollateralByQuota;
|
|
36
|
+
if (!market.isUnderlyingLike(targetToken) && !market.pool.pqk.hasActiveQuota(targetToken)) maxCollateralByQuota = 0n;
|
|
37
|
+
else {
|
|
38
|
+
const quotaResult = require_onchain_accounts_intents_utils_quotas_for_update.quotaIncreaseLimit({
|
|
39
|
+
suite,
|
|
40
|
+
token: targetToken,
|
|
41
|
+
balance: 0n,
|
|
42
|
+
initialQuotas: [],
|
|
43
|
+
quotaReserve
|
|
44
|
+
});
|
|
45
|
+
if (!quotaResult.error && quotaResult.value !== void 0) {
|
|
46
|
+
const excessUnderlying = require_onchain_utils_bigint_math.BigIntMath.ceilDiv((require_onchain_utils_bigint_math.BigIntMath.max(quotaResult.value, 0n) + 1n) * require_onchain_constants_math.LEVERAGE_DECIMALS, leverage);
|
|
47
|
+
const result = oracle.safeConvert(market.pool.underlying, token, excessUnderlying - 1n);
|
|
48
|
+
if (!result.error) {
|
|
49
|
+
const valuation = oracle.safeConvert(token, market.pool.underlying, result.value);
|
|
50
|
+
if (!valuation.error) {
|
|
51
|
+
const collateral = result.value - (valuation.value >= excessUnderlying ? 1n : 0n);
|
|
52
|
+
maxCollateralByQuota = require_onchain_utils_bigint_math.BigIntMath.min(require_onchain_constants_math.MAX_UINT256, require_onchain_utils_bigint_math.BigIntMath.max(collateral, 0n));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
collateralLimits: minCollateral <= maxCollateral ? {
|
|
59
|
+
min: oracle.toTokenAmount(token, minCollateral),
|
|
60
|
+
max: oracle.toTokenAmount(token, maxCollateral)
|
|
61
|
+
} : void 0,
|
|
62
|
+
quotaLimits: maxCollateralByQuota === void 0 ? void 0 : { collateralMax: oracle.toTokenAmount(token, maxCollateralByQuota) }
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
case "DEPOSIT": {
|
|
66
|
+
const { view, token, positionToken, initialQuotas, targetLeverage } = props;
|
|
67
|
+
if (targetLeverage !== void 0 || view.collateral <= 0n) return {};
|
|
68
|
+
const minDebt = require_onchain_utils_bigint_math.BigIntMath.max(view.debt, suite.creditFacade.minDebt);
|
|
69
|
+
const maxDebt = require_onchain_utils_bigint_math.BigIntMath.min(suite.creditFacade.maxDebt, view.debt + suite.maxBorrowAmount().amount.value);
|
|
70
|
+
const minCollateralForDebt = (debt) => {
|
|
71
|
+
if (debt <= view.debt) return 0n;
|
|
72
|
+
if (view.debt === 0n) return void 0;
|
|
73
|
+
const collateralUnderlying = require_onchain_utils_bigint_math.BigIntMath.ceilDiv((debt - view.debt) * view.collateral, view.debt);
|
|
74
|
+
const result = oracle.safeConvert(view.underlying, token, collateralUnderlying - 1n);
|
|
75
|
+
if (result.error) return void 0;
|
|
76
|
+
const valuation = oracle.safeConvert(token, view.underlying, result.value);
|
|
77
|
+
if (valuation.error) return void 0;
|
|
78
|
+
const collateral = result.value + (valuation.value < collateralUnderlying ? 1n : 0n);
|
|
79
|
+
const checked = oracle.safeConvert(token, view.underlying, collateral);
|
|
80
|
+
return checked.error || checked.value < collateralUnderlying ? void 0 : collateral;
|
|
81
|
+
};
|
|
82
|
+
const debtFloorCollateral = minCollateralForDebt(minDebt);
|
|
83
|
+
const minCollateral = debtFloorCollateral === void 0 ? void 0 : require_onchain_utils_bigint_math.BigIntMath.max(1n, debtFloorCollateral);
|
|
84
|
+
const excessCollateral = debtFloorCollateral === void 0 ? void 0 : minCollateralForDebt(maxDebt + 1n);
|
|
85
|
+
const maxCollateral = excessCollateral === void 0 ? require_onchain_constants_math.MAX_UINT256 : require_onchain_utils_bigint_math.BigIntMath.min(require_onchain_constants_math.MAX_UINT256, excessCollateral - 1n);
|
|
86
|
+
const position = positionToken ?? view.fattest([view.underlying]);
|
|
87
|
+
let maxCollateralByQuota;
|
|
88
|
+
if (position) {
|
|
89
|
+
if (!market.isUnderlyingLike(position) && !market.pool.pqk.hasActiveQuota(position)) maxCollateralByQuota = 0n;
|
|
90
|
+
else {
|
|
91
|
+
const quotaResult = require_onchain_accounts_intents_utils_quotas_for_update.quotaIncreaseLimit({
|
|
92
|
+
suite,
|
|
93
|
+
token: position,
|
|
94
|
+
balance: view.balanceOf(position),
|
|
95
|
+
initialQuotas,
|
|
96
|
+
quotaReserve,
|
|
97
|
+
rwaAsset: view.rwaAsset
|
|
98
|
+
});
|
|
99
|
+
maxCollateralByQuota = require_onchain_constants_math.MAX_UINT256;
|
|
100
|
+
if (!quotaResult.error && quotaResult.value !== void 0) {
|
|
101
|
+
const excessUnderlying = require_onchain_utils_bigint_math.BigIntMath.ceilDiv((quotaResult.value + 1n) * view.collateral, view.collateral + view.debt);
|
|
102
|
+
const result = oracle.safeConvert(view.underlying, token, require_onchain_utils_bigint_math.BigIntMath.max(excessUnderlying - 1n, 0n));
|
|
103
|
+
maxCollateralByQuota = void 0;
|
|
104
|
+
if (!result.error) {
|
|
105
|
+
const valuation = oracle.safeConvert(token, view.underlying, result.value);
|
|
106
|
+
if (!valuation.error) {
|
|
107
|
+
const collateral = result.value - (valuation.value >= excessUnderlying ? 1n : 0n);
|
|
108
|
+
maxCollateralByQuota = require_onchain_utils_bigint_math.BigIntMath.min(require_onchain_constants_math.MAX_UINT256, require_onchain_utils_bigint_math.BigIntMath.max(collateral, 0n));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
collateralLimits: minCollateral !== void 0 && minCollateral <= maxCollateral ? {
|
|
116
|
+
min: oracle.toTokenAmount(token, minCollateral),
|
|
117
|
+
max: oracle.toTokenAmount(token, maxCollateral)
|
|
118
|
+
} : void 0,
|
|
119
|
+
quotaLimits: maxCollateralByQuota === void 0 ? void 0 : { collateralMax: oracle.toTokenAmount(token, maxCollateralByQuota) }
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
case "ADJUST_LEVERAGE": {
|
|
123
|
+
const { view, token, initialQuotas, targetLeverage, maxLeverage = require_onchain_constants_math.MAX_UINT256 } = props;
|
|
124
|
+
if (view.collateral <= 0n || targetLeverage < 100n) return {};
|
|
125
|
+
const maxDebt = require_onchain_utils_bigint_math.BigIntMath.min(suite.creditFacade.maxDebt, view.debt + suite.maxBorrowAmount().amount.value);
|
|
126
|
+
const minLeverage = require_onchain_utils_bigint_math.BigIntMath.max(require_onchain_constants_math.LEVERAGE_DECIMALS + 1n, require_onchain_constants_math.LEVERAGE_DECIMALS + require_onchain_utils_bigint_math.BigIntMath.ceilDiv(suite.creditFacade.minDebt * require_onchain_constants_math.LEVERAGE_DECIMALS, view.collateral));
|
|
127
|
+
const debtMaxLeverage = require_onchain_utils_bigint_math.BigIntMath.min(maxLeverage, require_onchain_constants_math.LEVERAGE_DECIMALS + require_onchain_utils_bigint_math.BigIntMath.ceilDiv((maxDebt + 1n) * require_onchain_constants_math.LEVERAGE_DECIMALS, view.collateral) - 1n);
|
|
128
|
+
const position = token ?? view.fattest([view.underlying]);
|
|
129
|
+
let maxLeverageByQuota;
|
|
130
|
+
if (position) {
|
|
131
|
+
if (!market.isUnderlyingLike(position) && !market.pool.pqk.hasActiveQuota(position)) maxLeverageByQuota = require_onchain_constants_math.LEVERAGE_DECIMALS;
|
|
132
|
+
else {
|
|
133
|
+
const quotaResult = require_onchain_accounts_intents_utils_quotas_for_update.quotaIncreaseLimit({
|
|
134
|
+
suite,
|
|
135
|
+
token: position,
|
|
136
|
+
balance: view.balanceOf(position),
|
|
137
|
+
initialQuotas,
|
|
138
|
+
quotaReserve,
|
|
139
|
+
rwaAsset: view.rwaAsset
|
|
140
|
+
});
|
|
141
|
+
maxLeverageByQuota = maxLeverage;
|
|
142
|
+
if (!quotaResult.error && quotaResult.value !== void 0) {
|
|
143
|
+
const quotaMaxDebt = view.debt + quotaResult.value;
|
|
144
|
+
maxLeverageByQuota = require_onchain_utils_bigint_math.BigIntMath.min(maxLeverage, require_onchain_constants_math.LEVERAGE_DECIMALS + require_onchain_utils_bigint_math.BigIntMath.ceilDiv((quotaMaxDebt + 1n) * require_onchain_constants_math.LEVERAGE_DECIMALS, view.collateral) - 1n);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
leverageLimits: minLeverage <= debtMaxLeverage ? {
|
|
150
|
+
min: minLeverage,
|
|
151
|
+
max: debtMaxLeverage
|
|
152
|
+
} : void 0,
|
|
153
|
+
quotaLimits: maxLeverageByQuota === void 0 ? void 0 : { leverageMax: maxLeverageByQuota }
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
//#endregion
|
|
159
|
+
exports.strategyLimits = strategyLimits;
|
|
@@ -169,13 +169,17 @@ async function projectTail(args) {
|
|
|
169
169
|
const { steps } = planTail({
|
|
170
170
|
intent: delayed.record,
|
|
171
171
|
claimable: projectedClaimable(request, queued.token, queued.amount, claim),
|
|
172
|
-
view:
|
|
172
|
+
view: {
|
|
173
|
+
...require_onchain_accounts_intents_view.accountView(next, sdk),
|
|
174
|
+
limits: args.limits
|
|
175
|
+
}
|
|
173
176
|
});
|
|
174
177
|
const { state, operations } = await require_onchain_accounts_intents_realize.realize(steps, {
|
|
175
178
|
creditAccount: next,
|
|
176
179
|
sdk,
|
|
177
180
|
slippage: 0,
|
|
178
181
|
quotaReserve,
|
|
182
|
+
limits: args.limits,
|
|
179
183
|
paths: require_onchain_accounts_intents_utils_router_path.createOraclePaths({
|
|
180
184
|
sdk,
|
|
181
185
|
creditAccount: next
|
|
@@ -148,6 +148,10 @@ function buildMockSdk(args) {
|
|
|
148
148
|
});
|
|
149
149
|
const market = {
|
|
150
150
|
toUnderlyingAmount,
|
|
151
|
+
isUnderlyingLike: (token) => {
|
|
152
|
+
const address = token.toLowerCase();
|
|
153
|
+
return address === args.underlying.toLowerCase() || address === args.rwaAssets?.[args.underlying.toLowerCase()]?.toLowerCase();
|
|
154
|
+
},
|
|
151
155
|
/** {@inheritDoc MarketSuite.curator} */
|
|
152
156
|
curator: MOCK_CURATOR,
|
|
153
157
|
/** {@inheritDoc MarketSuite.underlying} */
|
|
@@ -24,6 +24,7 @@ exports.isPhantomToken = require_onchain_accounts_intents_utils_pick_token.isPha
|
|
|
24
24
|
exports.isRedemptionPhantomToken = require_onchain_accounts_intents_utils_pick_token.isRedemptionPhantomToken;
|
|
25
25
|
exports.lossRate = require_onchain_accounts_intents_utils_price_impact.lossRate;
|
|
26
26
|
exports.pickFattestNonPhantomToken = require_onchain_accounts_intents_utils_pick_token.pickFattestNonPhantomToken;
|
|
27
|
+
exports.quotaIncreaseLimit = require_onchain_accounts_intents_utils_quotas_for_update.quotaIncreaseLimit;
|
|
27
28
|
exports.quotasAfterUpdate = require_onchain_accounts_intents_utils_quotas_for_update.quotasAfterUpdate;
|
|
28
29
|
exports.rankAccountTokens = require_onchain_accounts_intents_utils_pick_token.rankAccountTokens;
|
|
29
30
|
exports.resolveCreditManager = require_onchain_accounts_intents_utils_common.resolveCreditManager;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_onchain_utils_bigint_math = require("../../../utils/bigint-math.js");
|
|
2
3
|
const require_onchain_constants_math = require("../../../constants/math.js");
|
|
3
4
|
const require_onchain_utils_mappers = require("../../../utils/mappers.js");
|
|
5
|
+
require("../../../constants/index.js");
|
|
4
6
|
const require_onchain_accounts_quota_utils = require("../../quota-utils.js");
|
|
5
7
|
//#region src/onchain/accounts/intents/utils/quotas-for-update.ts
|
|
6
8
|
function getQuotasForUpdate({ assetsBefore, assetsAfter, initialQuotas, quotaReserve = 0, liquidationThresholds: liquidationThresholdsMap, quotas: quotasMap, maxDebt, underlyingToken, convert }) {
|
|
@@ -195,8 +197,45 @@ function constructAssetRecord(a) {
|
|
|
195
197
|
return acc;
|
|
196
198
|
}, {});
|
|
197
199
|
}
|
|
200
|
+
/** Largest additional underlying amount whose purchase fits this token's quota. */
|
|
201
|
+
function quotaIncreaseLimit({ suite, token, balance, initialQuotas, quotaReserve = 0, rwaAsset }) {
|
|
202
|
+
const { market } = suite;
|
|
203
|
+
if (market.isUnderlyingLike(token)) return { value: void 0 };
|
|
204
|
+
const oracle = market.priceOracle;
|
|
205
|
+
const purchaseResult = oracle.safeConvert(market.pool.underlying, token, require_onchain_constants_math.MAX_UINT256);
|
|
206
|
+
if (purchaseResult.error) return purchaseResult;
|
|
207
|
+
const valuationResult = oracle.safeConvert(token, market.pool.underlying, require_onchain_constants_math.MAX_UINT256);
|
|
208
|
+
if (valuationResult.error) return valuationResult;
|
|
209
|
+
if (purchaseResult.value === 0n || valuationResult.value === 0n) return { value: void 0 };
|
|
210
|
+
const maxQuotaValue = require_onchain_accounts_quota_utils.maxAmountForQuota({
|
|
211
|
+
available: market.pool.pqk.quotaAvailable(token),
|
|
212
|
+
initialQuota: initialQuotas.find((q) => q.token.toLowerCase() === token.toLowerCase())?.quota ?? 0n,
|
|
213
|
+
totalInitialQuota: initialQuotas.reduce((sum, q) => sum + require_onchain_accounts_quota_utils.roundUpQuota(q.quota), 0n),
|
|
214
|
+
maxDebt: suite.creditFacade.maxDebt,
|
|
215
|
+
lt: BigInt(suite.creditManager.liquidationThresholds.get(token) ?? 0),
|
|
216
|
+
quotaReserve: BigInt(quotaReserve)
|
|
217
|
+
});
|
|
218
|
+
if (maxQuotaValue === void 0) return { value: void 0 };
|
|
219
|
+
const quotaBalanceResult = oracle.safeConvert(market.pool.underlying, token, maxQuotaValue);
|
|
220
|
+
if (quotaBalanceResult.error) return quotaBalanceResult;
|
|
221
|
+
const quotaValueResult = oracle.safeConvert(token, market.pool.underlying, quotaBalanceResult.value);
|
|
222
|
+
if (quotaValueResult.error) return quotaValueResult;
|
|
223
|
+
const availableTokenIncrease = quotaBalanceResult.value - (quotaValueResult.value > maxQuotaValue ? 1n : 0n) - balance;
|
|
224
|
+
const excessTokenIncrease = require_onchain_utils_bigint_math.BigIntMath.max(availableTokenIncrease, 0n) + 1n;
|
|
225
|
+
if (rwaAsset?.toLowerCase() === token.toLowerCase()) {
|
|
226
|
+
const underlyingScale = 10n ** BigInt(suite.sdk.tokensMeta.decimals(market.pool.underlying));
|
|
227
|
+
const tokenScale = 10n ** BigInt(suite.sdk.tokensMeta.decimals(token));
|
|
228
|
+
return { value: require_onchain_utils_bigint_math.BigIntMath.ceilDiv(excessTokenIncrease * underlyingScale, tokenScale) - 1n };
|
|
229
|
+
}
|
|
230
|
+
const increaseResult = oracle.safeConvert(token, market.pool.underlying, excessTokenIncrease - 1n);
|
|
231
|
+
if (increaseResult.error) return increaseResult;
|
|
232
|
+
const purchaseValue = oracle.safeConvert(market.pool.underlying, token, increaseResult.value);
|
|
233
|
+
if (purchaseValue.error) return purchaseValue;
|
|
234
|
+
return { value: increaseResult.value - (purchaseValue.value >= excessTokenIncrease ? 1n : 0n) };
|
|
235
|
+
}
|
|
198
236
|
//#endregion
|
|
199
237
|
exports.clearedQuotas = clearedQuotas;
|
|
200
238
|
exports.getQuotasForUpdate = getQuotasForUpdate;
|
|
239
|
+
exports.quotaIncreaseLimit = quotaIncreaseLimit;
|
|
201
240
|
exports.quotasAfterUpdate = quotasAfterUpdate;
|
|
202
241
|
exports.withDirectTransferQuota = withDirectTransferQuota;
|
|
@@ -43,6 +43,14 @@ function calcRecommendedQuota({ amount, debt, lt, quotaReserve }) {
|
|
|
43
43
|
function calcDefaultQuota({ amount, lt, quotaReserve }) {
|
|
44
44
|
return roundUpQuota(amount * lt / require_onchain_constants_math.PERCENTAGE_FACTOR * (require_onchain_constants_math.PERCENTAGE_FACTOR + quotaReserve) / require_onchain_constants_math.PERCENTAGE_FACTOR);
|
|
45
45
|
}
|
|
46
|
+
/** Inverse of default quota rounding, including the account-wide increase cap. */
|
|
47
|
+
function maxAmountForQuota({ available, initialQuota, totalInitialQuota, maxDebt, lt, quotaReserve }) {
|
|
48
|
+
const increaseCap = roundUpQuota(require_onchain_utils_bigint_math.BigIntMath.max(roundUpQuota(maxDebt * 2n) - totalInitialQuota, 0n));
|
|
49
|
+
if (lt === 0n || increaseCap <= available) return void 0;
|
|
50
|
+
const desired = roundUpQuota(initialQuota) + roundUpQuota(available) + require_onchain_constants_math.PERCENTAGE_FACTOR - 1n;
|
|
51
|
+
const base = require_onchain_utils_bigint_math.BigIntMath.ceilDiv((desired + 1n) * require_onchain_constants_math.PERCENTAGE_FACTOR, require_onchain_constants_math.PERCENTAGE_FACTOR + quotaReserve) - 1n;
|
|
52
|
+
return require_onchain_utils_bigint_math.BigIntMath.ceilDiv((base + 1n) * require_onchain_constants_math.PERCENTAGE_FACTOR, lt) - 1n;
|
|
53
|
+
}
|
|
46
54
|
/**
|
|
47
55
|
* Produces desired quota state and explicit increase/decrease actions.
|
|
48
56
|
*
|
|
@@ -147,4 +155,5 @@ exports.DIRECT_TRANSFERS_QUOTA = DIRECT_TRANSFERS_QUOTA;
|
|
|
147
155
|
exports.calcDefaultQuota = calcDefaultQuota;
|
|
148
156
|
exports.calcQuotaUpdate = calcQuotaUpdate;
|
|
149
157
|
exports.calcRecommendedQuota = calcRecommendedQuota;
|
|
158
|
+
exports.maxAmountForQuota = maxAmountForQuota;
|
|
150
159
|
exports.roundUpQuota = roundUpQuota;
|
|
@@ -766,6 +766,7 @@ exports.json_parse = require_onchain_utils_json.json_parse;
|
|
|
766
766
|
exports.json_stringify = require_onchain_utils_json.json_stringify;
|
|
767
767
|
exports.lidoV1_WETHGatewayAbi = require_onchain_market_adapters_abi_targetContractAbi.lidoV1_WETHGatewayAbi;
|
|
768
768
|
exports.makeReplayState = require_onchain_preview_preview_replayInnerOperations.makeReplayState;
|
|
769
|
+
exports.maxAmountForQuota = require_onchain_accounts_quota_utils.maxAmountForQuota;
|
|
769
770
|
exports.mellowDvvAdapterAbi = require_onchain_market_adapters_abi_adapters_mellowDvvAdapter.mellowDvvAdapterAbi;
|
|
770
771
|
exports.midasGreenlistsAccount = require_onchain_preview_preview_midasGreenlistsAccount.midasGreenlistsAccount;
|
|
771
772
|
exports.minSeizedAmount = require_onchain_market_math.minSeizedAmount;
|