@gearbox-protocol/sdk 17.10.0 → 17.10.1-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/index.js +1 -0
- package/dist/cjs/onchain/accounts/intents/guards.js +4 -2
- package/dist/cjs/onchain/accounts/intents/index.js +44 -11
- package/dist/cjs/onchain/accounts/intents/leverage-band.js +49 -8
- 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 +6 -2
- 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/intents/withdraw-limits.js +1 -1
- 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/cjs/sdk/prepare/PrepareApi.js +3 -2
- 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 +44 -11
- package/dist/esm/onchain/accounts/intents/leverage-band.js +50 -9
- 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 +6 -2
- 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/intents/withdraw-limits.js +1 -1
- 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/esm/sdk/prepare/PrepareApi.js +3 -2
- 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/index.d.ts +2 -3
- package/dist/types/onchain/accounts/intents/leverage-band.d.ts +8 -6
- 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/dist/types/sdk/prepare/PrepareApi.d.ts +1 -1
- package/dist/types/sdk/prepare/types.d.ts +3 -4
- 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");
|
|
@@ -10,12 +11,13 @@ const require_onchain_accounts_intents_utils_pick_token = require("./utils/pick-
|
|
|
10
11
|
const require_onchain_accounts_intents_guards = require("./guards.js");
|
|
11
12
|
const require_onchain_accounts_intents_utils_credit_account_slice = require("./utils/credit-account-slice.js");
|
|
12
13
|
const require_onchain_accounts_intents_borrow = require("./borrow.js");
|
|
14
|
+
const require_onchain_accounts_intents_strategyLimits = require("./strategyLimits.js");
|
|
15
|
+
const require_onchain_accounts_intents_view = require("./view.js");
|
|
13
16
|
const require_onchain_accounts_intents_leverage_band = require("./leverage-band.js");
|
|
14
17
|
const require_onchain_accounts_intents_maxBorrow = require("./maxBorrow.js");
|
|
15
18
|
const require_onchain_accounts_intents_maxWithdrawCollateral = require("./maxWithdrawCollateral.js");
|
|
16
19
|
const require_onchain_accounts_intents_open_strategy = require("./open-strategy.js");
|
|
17
20
|
const require_onchain_accounts_intents_plan = require("./plan.js");
|
|
18
|
-
const require_onchain_accounts_intents_view = require("./view.js");
|
|
19
21
|
const require_onchain_accounts_intents_withdraw_limits = require("./withdraw-limits.js");
|
|
20
22
|
const require_onchain_accounts_intents_realize = require("./realize.js");
|
|
21
23
|
const require_onchain_accounts_intents_tail = require("./tail.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
|
|
@@ -106,9 +124,8 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
|
|
|
106
124
|
* given deposit reaches is decided by the debt it implies and by the
|
|
107
125
|
* `debtLimits` the market puts that debt in — the range a leverage slider should offer.
|
|
108
126
|
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* holds. Nothing is fetched, so a form can ask on every keystroke.
|
|
127
|
+
* Nothing is fetched: opening uses the supplied collateral, while adjust
|
|
128
|
+
* uses a supplied account slice, so a form can ask on every keystroke.
|
|
112
129
|
*
|
|
113
130
|
* @param props - The manager, the SDK holding its market, what stands
|
|
114
131
|
* behind the position, and optionally the health factor the ceiling should
|
|
@@ -191,14 +208,28 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
|
|
|
191
208
|
*/
|
|
192
209
|
async startDelayedIntent(props) {
|
|
193
210
|
const { intent } = props;
|
|
211
|
+
const current = require_onchain_accounts_intents_view.accountView(props.creditAccount, props.sdk);
|
|
212
|
+
const repayFromSource = current.debt - current.balanceOf(current.underlying);
|
|
213
|
+
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;
|
|
214
|
+
const limits = intent.type === "ADJUST_LEVERAGE" && maximum !== void 0 ? require_onchain_accounts_intents_strategyLimits.strategyLimits({
|
|
215
|
+
...intent,
|
|
216
|
+
suite: props.sdk.marketRegister.findCreditManager(props.creditAccount.creditManager),
|
|
217
|
+
view: current,
|
|
218
|
+
initialQuotas: props.creditAccount.tokens,
|
|
219
|
+
quotaReserve: props.quotaReserve,
|
|
220
|
+
maxLeverage: maximum
|
|
221
|
+
}) : {};
|
|
194
222
|
const result = await this.#preview(props, () => {
|
|
195
|
-
const view =
|
|
223
|
+
const view = {
|
|
224
|
+
...current,
|
|
225
|
+
limits
|
|
226
|
+
};
|
|
196
227
|
switch (intent.type) {
|
|
197
228
|
case "ADJUST_LEVERAGE": return require_onchain_accounts_intents_plan.planAdjustLeverageDelayed(intent, view);
|
|
198
229
|
case "WITHDRAW": return require_onchain_accounts_intents_plan.planWithdrawDelayed(intent, view);
|
|
199
230
|
default: throw new Error(`${intent.type} - cannot be delayed`);
|
|
200
231
|
}
|
|
201
|
-
});
|
|
232
|
+
}, limits);
|
|
202
233
|
if (!result.ok) return result;
|
|
203
234
|
const { delayed } = result;
|
|
204
235
|
if (!delayed) throw new Error("startDelayedIntent: plan started no withdrawal");
|
|
@@ -214,7 +245,8 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
|
|
|
214
245
|
delayed,
|
|
215
246
|
creditAccount: props.creditAccount,
|
|
216
247
|
sdk: props.sdk,
|
|
217
|
-
quotaReserve: props.quotaReserve
|
|
248
|
+
quotaReserve: props.quotaReserve,
|
|
249
|
+
limits
|
|
218
250
|
});
|
|
219
251
|
return {
|
|
220
252
|
...result,
|
|
@@ -400,7 +432,7 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
|
|
|
400
432
|
}
|
|
401
433
|
}
|
|
402
434
|
/** Plan → realise → wrap. Unviable requests become `{ ok: false }`. */
|
|
403
|
-
async #preview(props, plan) {
|
|
435
|
+
async #preview(props, plan, limits = {}) {
|
|
404
436
|
const draft = props.draft ?? {};
|
|
405
437
|
try {
|
|
406
438
|
const suite = props.sdk.marketRegister.findCreditManager(props.creditAccount.creditManager);
|
|
@@ -411,7 +443,8 @@ var CreditAccountOperationsService = class extends require_onchain_base_SDKConst
|
|
|
411
443
|
sdk: props.sdk,
|
|
412
444
|
slippage: props.slippage ?? 0,
|
|
413
445
|
quotaReserve: props.quotaReserve,
|
|
414
|
-
draft
|
|
446
|
+
draft,
|
|
447
|
+
limits
|
|
415
448
|
});
|
|
416
449
|
return {
|
|
417
450
|
ok: true,
|
|
@@ -2,6 +2,8 @@ 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
4
|
const require_onchain_accounts_intents_utils_common = require("./utils/common.js");
|
|
5
|
+
const require_onchain_accounts_intents_strategyLimits = require("./strategyLimits.js");
|
|
6
|
+
const require_onchain_accounts_intents_view = require("./view.js");
|
|
5
7
|
//#region src/onchain/accounts/intents/leverage-band.ts
|
|
6
8
|
/**
|
|
7
9
|
* The leverages this market will actually fund for a position of this size.
|
|
@@ -16,8 +18,8 @@ const require_onchain_accounts_intents_utils_common = require("./utils/common.js
|
|
|
16
18
|
* rounds **up** so the leverage it names really does clear `minDebt`, and the
|
|
17
19
|
* ceiling rounds **down** so it really does stay under the borrow limit.
|
|
18
20
|
*
|
|
19
|
-
* Nothing here is fetched or simulated —
|
|
20
|
-
* so a form can call this on each keystroke.
|
|
21
|
+
* Nothing here is fetched or simulated — adjust state comes from the supplied
|
|
22
|
+
* position and the attached market, so a form can call this on each keystroke.
|
|
21
23
|
*
|
|
22
24
|
* @param props - {@link LeverageBandProps}
|
|
23
25
|
* @returns The band, or `undefined` when there is nothing to offer: an
|
|
@@ -30,13 +32,53 @@ const require_onchain_accounts_intents_utils_common = require("./utils/common.js
|
|
|
30
32
|
* calcLeverageBand({ sdk, creditManager, collateral }) // { min: 1.1, max: 9 }
|
|
31
33
|
* ```
|
|
32
34
|
**/
|
|
33
|
-
function calcLeverageBand({ sdk, creditManager, collateral, targetHF }) {
|
|
35
|
+
function calcLeverageBand({ sdk, creditManager, collateral, targetHF, position }) {
|
|
34
36
|
const found = require_onchain_accounts_intents_utils_common.resolveCreditManager(sdk, creditManager);
|
|
35
37
|
if (!found) return;
|
|
36
38
|
const { suite, market } = found;
|
|
37
|
-
const
|
|
38
|
-
if (!
|
|
39
|
+
const strategy = suite.strategy;
|
|
40
|
+
if (!strategy) return;
|
|
41
|
+
const target = strategy.targetCollateral;
|
|
39
42
|
const ceiling = suite.creditManager.maxLeverage(target, targetHF);
|
|
43
|
+
if (position) {
|
|
44
|
+
if (!require_onchain_accounts_intents_utils_common.eq(position.creditManager, creditManager)) throw new Error("credit account belongs to a different credit manager");
|
|
45
|
+
const creditAccount = {
|
|
46
|
+
creditAccount: position.creditAccount,
|
|
47
|
+
creditManager: position.creditManager,
|
|
48
|
+
creditFacade: suite.creditFacade.address,
|
|
49
|
+
underlying: position.underlyingToken.address,
|
|
50
|
+
enabledTokensMask: 0n,
|
|
51
|
+
totalDebtUSD: 0n,
|
|
52
|
+
totalDebt: position.totalDebt.value,
|
|
53
|
+
tokens: position.collaterals.map(({ collateral, quota }) => ({
|
|
54
|
+
token: collateral.token.address,
|
|
55
|
+
mask: 0n,
|
|
56
|
+
balance: collateral.value,
|
|
57
|
+
quota: quota.value,
|
|
58
|
+
success: true
|
|
59
|
+
}))
|
|
60
|
+
};
|
|
61
|
+
const view = {
|
|
62
|
+
...require_onchain_accounts_intents_view.accountView(creditAccount, sdk),
|
|
63
|
+
collateral: position.totalValue.value - position.totalDebt.value
|
|
64
|
+
};
|
|
65
|
+
const { leverageLimits, quotaLimits } = require_onchain_accounts_intents_strategyLimits.strategyLimits({
|
|
66
|
+
type: "ADJUST_LEVERAGE",
|
|
67
|
+
suite,
|
|
68
|
+
view,
|
|
69
|
+
initialQuotas: creditAccount.tokens,
|
|
70
|
+
token: target,
|
|
71
|
+
targetLeverage: require_onchain_constants_math.LEVERAGE_DECIMALS,
|
|
72
|
+
quotaReserve: void 0
|
|
73
|
+
});
|
|
74
|
+
if (!leverageLimits) return void 0;
|
|
75
|
+
const min = Number(leverageLimits.min) / Number(require_onchain_constants_math.LEVERAGE_DECIMALS);
|
|
76
|
+
const max = Math.min(ceiling, Number(require_onchain_utils_bigint_math.BigIntMath.min(leverageLimits.max, quotaLimits?.leverageMax ?? leverageLimits.max)) / Number(require_onchain_constants_math.LEVERAGE_DECIMALS));
|
|
77
|
+
return min > max ? void 0 : {
|
|
78
|
+
min,
|
|
79
|
+
max
|
|
80
|
+
};
|
|
81
|
+
}
|
|
40
82
|
const underlying = market.pool.underlying;
|
|
41
83
|
const convert = (from, to, amount) => market.priceOracle.safeConvert(from, to, amount).value;
|
|
42
84
|
const netValue = collateral.reduce((acc, a) => acc + convert(a.token, underlying, a.balance), 0n);
|
|
@@ -44,9 +86,8 @@ function calcLeverageBand({ sdk, creditManager, collateral, targetHF }) {
|
|
|
44
86
|
min: 1,
|
|
45
87
|
max: ceiling
|
|
46
88
|
};
|
|
47
|
-
const { minDebt
|
|
48
|
-
const
|
|
49
|
-
const borrowLimit = available === void 0 ? maxDebt : require_onchain_utils_bigint_math.BigIntMath.min(maxDebt, available);
|
|
89
|
+
const { minDebt } = suite.creditFacade;
|
|
90
|
+
const borrowLimit = strategy.maxBorrowAmount().amount.value;
|
|
50
91
|
const floor = require_onchain_utils_bigint_math.BigIntMath.ceilDiv(require_onchain_constants_math.LEVERAGE_DECIMALS * minDebt, netValue);
|
|
51
92
|
const roof = require_onchain_utils_bigint_math.BigIntMath.ceilDiv(require_onchain_constants_math.LEVERAGE_DECIMALS * (borrowLimit + 1n), netValue) - 1n;
|
|
52
93
|
const min = 1 + Number(floor) / Number(require_onchain_constants_math.LEVERAGE_DECIMALS);
|
|
@@ -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;
|
|
@@ -6,9 +6,9 @@ const require_onchain_validation_helpers_token = require("../../validation/helpe
|
|
|
6
6
|
const require_onchain_validation_raise = require("../../validation/raise.js");
|
|
7
7
|
const require_onchain_accounts_intents_utils_common = require("./utils/common.js");
|
|
8
8
|
const require_onchain_accounts_intents_utils_router_path = require("./utils/router-path.js");
|
|
9
|
+
const require_onchain_accounts_intents_view = require("./view.js");
|
|
9
10
|
const require_onchain_accounts_intents_plan = require("./plan.js");
|
|
10
11
|
const require_onchain_accounts_intents_operations = require("./operations.js");
|
|
11
|
-
const require_onchain_accounts_intents_view = require("./view.js");
|
|
12
12
|
const require_onchain_accounts_intents_realize = require("./realize.js");
|
|
13
13
|
//#region src/onchain/accounts/intents/tail.ts
|
|
14
14
|
/**
|
|
@@ -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} */
|