@gearbox-protocol/sdk 17.9.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/dev/AccountOpener.js +3 -3
- package/dist/cjs/onchain/accounts/index.js +2 -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 +11 -5
- 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/market.js +5 -1
- package/dist/cjs/onchain/accounts/intents/testing/sdk-mock.js +5 -0
- package/dist/cjs/onchain/accounts/intents/utils/index.js +2 -0
- package/dist/cjs/onchain/accounts/intents/utils/quotas-for-update.js +69 -0
- package/dist/cjs/onchain/accounts/quota-utils.js +15 -0
- package/dist/cjs/onchain/chain/chains.js +12 -0
- package/dist/cjs/onchain/chain/index.js +1 -0
- package/dist/cjs/onchain/index.js +3 -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 +2 -2
- 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 +12 -6
- 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/market.js +5 -1
- package/dist/esm/onchain/accounts/intents/testing/sdk-mock.js +5 -0
- package/dist/esm/onchain/accounts/intents/utils/index.js +2 -2
- package/dist/esm/onchain/accounts/intents/utils/quotas-for-update.js +70 -3
- package/dist/esm/onchain/accounts/liquidations/LiquidationsService.js +1 -1
- package/dist/esm/onchain/accounts/quota-utils.js +14 -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/chain/chains.js +12 -1
- package/dist/esm/onchain/chain/index.js +2 -2
- package/dist/esm/onchain/core/createAddressProvider.js +1 -1
- package/dist/esm/onchain/index.js +3 -3
- 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/liquidation-errors.d.ts +4 -0
- package/dist/types/model/errors/operation-errors.d.ts +38 -14
- package/dist/types/model/errors/oracle-errors.d.ts +2 -0
- 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/testing/market.d.ts +4 -0
- package/dist/types/onchain/accounts/intents/utils/index.d.ts +2 -2
- package/dist/types/onchain/accounts/intents/utils/quotas-for-update.d.ts +21 -1
- package/dist/types/onchain/accounts/quota-utils.d.ts +16 -1
- package/dist/types/onchain/chain/chains.d.ts +15 -1
- package/dist/types/onchain/chain/index.d.ts +2 -2
- package/dist/types/onchain/index.d.ts +3 -3
- 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
|
@@ -9,6 +9,7 @@ require("../onchain/constants/addresses.js");
|
|
|
9
9
|
const require_onchain_constants_math = require("../onchain/constants/math.js");
|
|
10
10
|
const require_onchain_base_SDKConstruct = require("../onchain/base/SDKConstruct.js");
|
|
11
11
|
const require_onchain_utils_viem_sendRawTx = require("../onchain/utils/viem/sendRawTx.js");
|
|
12
|
+
const require_onchain_accounts_quota_utils = require("../onchain/accounts/quota-utils.js");
|
|
12
13
|
const require_onchain_pools_PoolService = require("../onchain/pools/PoolService.js");
|
|
13
14
|
require("../onchain/index.js");
|
|
14
15
|
const require_dev_abi = require("./abi.js");
|
|
@@ -19,7 +20,6 @@ require("./mint/index.js");
|
|
|
19
20
|
let viem = require("viem");
|
|
20
21
|
let viem_accounts = require("viem/accounts");
|
|
21
22
|
//#region src/dev/AccountOpener.ts
|
|
22
|
-
const DIRECT_TRANSFERS_QUOTA = 10000n;
|
|
23
23
|
var OpenTxRevertedError = class extends viem.BaseError {
|
|
24
24
|
txHash;
|
|
25
25
|
constructor(txHash) {
|
|
@@ -258,11 +258,11 @@ var AccountOpener = class extends require_onchain_base_SDKConstruct.SDKConstruct
|
|
|
258
258
|
for (const token of directTransfer) {
|
|
259
259
|
averageQuota.push({
|
|
260
260
|
token,
|
|
261
|
-
balance: DIRECT_TRANSFERS_QUOTA
|
|
261
|
+
balance: require_onchain_accounts_quota_utils.DIRECT_TRANSFERS_QUOTA
|
|
262
262
|
});
|
|
263
263
|
minQuota.push({
|
|
264
264
|
token,
|
|
265
|
-
balance: DIRECT_TRANSFERS_QUOTA
|
|
265
|
+
balance: require_onchain_accounts_quota_utils.DIRECT_TRANSFERS_QUOTA
|
|
266
266
|
});
|
|
267
267
|
}
|
|
268
268
|
logger?.debug({
|
|
@@ -34,6 +34,7 @@ exports.CreditAccountOperationsService = require_onchain_accounts_intents_index.
|
|
|
34
34
|
exports.CreditAccountsServiceV310 = require_onchain_accounts_CreditAccountsServiceV310.CreditAccountsServiceV310;
|
|
35
35
|
exports.DELAYED_INTENT_TYPES = require_onchain_accounts_withdrawal_compressor_intent_codec.DELAYED_INTENT_TYPES;
|
|
36
36
|
exports.DELAYED_INTENT_VERSION = require_onchain_accounts_withdrawal_compressor_intent_codec.DELAYED_INTENT_VERSION;
|
|
37
|
+
exports.DIRECT_TRANSFERS_QUOTA = require_onchain_accounts_quota_utils.DIRECT_TRANSFERS_QUOTA;
|
|
37
38
|
exports.LIQUIDATION_APPROVAL_BUFFER = require_onchain_accounts_liquidations_constants.LIQUIDATION_APPROVAL_BUFFER;
|
|
38
39
|
exports.LIQUIDATION_COMPRESSOR_V313_ADDRESS = require_onchain_accounts_liquidations_constants.LIQUIDATION_COMPRESSOR_V313_ADDRESS;
|
|
39
40
|
exports.LiquidationsService = require_onchain_accounts_liquidations_LiquidationsService.LiquidationsService;
|
|
@@ -54,6 +55,7 @@ exports.fetchCreditAccountSlice = require_onchain_accounts_intents_utils_credit_
|
|
|
54
55
|
exports.getWithdrawalCompressorAddress = require_onchain_accounts_withdrawal_compressor_addresses.getWithdrawalCompressorAddress;
|
|
55
56
|
exports.iCreditAccountAbi = require_onchain_accounts_withdrawal_compressor_AbstractWithdrawalCompressorContract.iCreditAccountAbi;
|
|
56
57
|
exports.isPhantomToken = require_onchain_accounts_intents_utils_pick_token.isPhantomToken;
|
|
58
|
+
exports.maxAmountForQuota = require_onchain_accounts_quota_utils.maxAmountForQuota;
|
|
57
59
|
exports.roundUpQuota = require_onchain_accounts_quota_utils.roundUpQuota;
|
|
58
60
|
exports.toClaimableWithdrawal = require_onchain_accounts_withdrawal_compressor_AbstractWithdrawalCompressorContract.toClaimableWithdrawal;
|
|
59
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
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_onchain_chain_chains = require("../../chain/chains.js");
|
|
2
3
|
const require_onchain_constants_math = require("../../constants/math.js");
|
|
3
4
|
const require_model_errors_operation_errors = require("../../../model/errors/operation-errors.js");
|
|
4
5
|
const require_model_errors_prepare_errors = require("../../../model/errors/prepare-errors.js");
|
|
@@ -107,6 +108,8 @@ async function realize(steps, props) {
|
|
|
107
108
|
* judge the closing collateral check at safe prices.
|
|
108
109
|
*/
|
|
109
110
|
let withdrawsCollateral = false;
|
|
111
|
+
/** Token a sold collateral later transfers to the account; needs a quota. */
|
|
112
|
+
let directTransfer;
|
|
110
113
|
const amountOf = (a) => typeof a === "bigint" ? a : min(raised, a.max ?? raised);
|
|
111
114
|
const assertHolds = (token, amount, what) => {
|
|
112
115
|
const held = ledger.balanceOf(token);
|
|
@@ -128,7 +131,7 @@ async function realize(steps, props) {
|
|
|
128
131
|
}));
|
|
129
132
|
break;
|
|
130
133
|
case "borrow":
|
|
131
|
-
require_onchain_accounts_intents_guards.assertCanBorrow(sdk, suite, step.amount);
|
|
134
|
+
require_onchain_accounts_intents_guards.assertCanBorrow(sdk, suite, step.amount, props.limits);
|
|
132
135
|
push(require_onchain_accounts_intents_operations.buildIncreaseDebtOperation({
|
|
133
136
|
amount: step.amount,
|
|
134
137
|
creditAccount,
|
|
@@ -179,6 +182,7 @@ async function realize(steps, props) {
|
|
|
179
182
|
raised = amountOut;
|
|
180
183
|
break;
|
|
181
184
|
}
|
|
185
|
+
directTransfer ??= require_onchain_chain_chains.getDirectTransferToken(step.from, sdk.networkType);
|
|
182
186
|
const held = ledger.balanceOf(step.from);
|
|
183
187
|
const leg = await paths.swap({
|
|
184
188
|
tokenIn: step.from,
|
|
@@ -259,6 +263,7 @@ async function realize(steps, props) {
|
|
|
259
263
|
const asset = await delayedConfig(sdk, creditAccount, step.token);
|
|
260
264
|
if (ledger.balanceOf(asset.withdrawalPhantomToken) > 0n) throw new require_onchain_validation_raise.IntentPreviewError(require_model_errors_prepare_errors.withdrawalInProgress(require_onchain_validation_helpers_token.toTokenAmount(sdk, asset.withdrawalPhantomToken, ledger.balanceOf(asset.withdrawalPhantomToken))), `request: ${asset.withdrawalPhantomToken} already holds a pending withdrawal`);
|
|
261
265
|
assertHolds(step.token, step.amount + step.reserve, "request");
|
|
266
|
+
if (step.record.type !== "CLOSE_ACCOUNT") directTransfer ??= require_onchain_chain_chains.getDirectTransferToken(step.token, sdk.networkType);
|
|
262
267
|
const preview = await sdk.accounts.previewDelayedWithdrawal({
|
|
263
268
|
creditAccount: creditAccount.creditAccount,
|
|
264
269
|
token: step.token,
|
|
@@ -330,7 +335,7 @@ async function realize(steps, props) {
|
|
|
330
335
|
const floor = ledger.snapshot();
|
|
331
336
|
const { assets, debt } = floor;
|
|
332
337
|
const projected = expected.snapshot();
|
|
333
|
-
const quotas = cleared ?? require_onchain_accounts_intents_utils_quotas_for_update.getQuotasForUpdate({
|
|
338
|
+
const quotas = cleared ?? require_onchain_accounts_intents_utils_quotas_for_update.withDirectTransferQuota(require_onchain_accounts_intents_utils_quotas_for_update.getQuotasForUpdate({
|
|
334
339
|
assetsBefore: creditAccount.tokens,
|
|
335
340
|
assetsAfter: assets,
|
|
336
341
|
initialQuotas: creditAccount.tokens,
|
|
@@ -340,7 +345,7 @@ async function realize(steps, props) {
|
|
|
340
345
|
quotas: market.pool.pqk.quotas,
|
|
341
346
|
maxDebt: suite.creditFacade.maxDebt,
|
|
342
347
|
convert: price
|
|
343
|
-
});
|
|
348
|
+
}), debt > 0n ? directTransfer : void 0, creditAccount.tokens, market.pool.pqk.quotas, suite.creditManager.maxEnabledTokens, suite.creditManager.collateralTokens);
|
|
344
349
|
const quotasAfter = require_onchain_accounts_intents_utils_quotas_for_update.quotasAfterUpdate(creditAccount.tokens, quotas.desiredQuota);
|
|
345
350
|
const quoted = Object.values(quotasAfter);
|
|
346
351
|
const snapshot = {
|
|
@@ -390,10 +395,11 @@ async function realize(steps, props) {
|
|
|
390
395
|
suite,
|
|
391
396
|
market,
|
|
392
397
|
before: creditAccount.tokens,
|
|
393
|
-
after: projected.assets
|
|
398
|
+
after: projected.assets,
|
|
399
|
+
limits: props.limits
|
|
394
400
|
});
|
|
395
401
|
if (!cleared && quotas.quotaIncrease.length + quotas.quotaDecrease.length > 0) {
|
|
396
|
-
require_onchain_accounts_intents_guards.assertQuotaAvailable(sdk, market, quotas.quotaIncrease);
|
|
402
|
+
require_onchain_accounts_intents_guards.assertQuotaAvailable(sdk, market, quotas.quotaIncrease, props.limits);
|
|
397
403
|
push(require_onchain_accounts_intents_operations.buildQuotaUpdateOperation({
|
|
398
404
|
update: quotas,
|
|
399
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
|
|
@@ -90,7 +90,10 @@ function buildMarketSdk(extras) {
|
|
|
90
90
|
...extras?.extraDecimals
|
|
91
91
|
},
|
|
92
92
|
quotas: extras?.quotas ?? QUOTAS,
|
|
93
|
-
liquidationThresholds:
|
|
93
|
+
liquidationThresholds: {
|
|
94
|
+
...LIQUIDATION_THRESHOLDS,
|
|
95
|
+
...extras?.extraLiquidationThresholds
|
|
96
|
+
},
|
|
94
97
|
maxDebt: MAX_DEBT,
|
|
95
98
|
minDebt: extras?.minDebt,
|
|
96
99
|
creditManager: CREDIT_MANAGER,
|
|
@@ -102,6 +105,7 @@ function buildMarketSdk(extras) {
|
|
|
102
105
|
phantoms: extras?.phantoms,
|
|
103
106
|
creditAccounts: extras?.creditAccounts,
|
|
104
107
|
delayed: extras?.delayed,
|
|
108
|
+
maxEnabledTokens: extras?.maxEnabledTokens,
|
|
105
109
|
facadePaused: extras?.facadePaused,
|
|
106
110
|
poolPaused: extras?.poolPaused,
|
|
107
111
|
expirationDate: extras?.expirationDate,
|
|
@@ -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} */
|
|
@@ -319,6 +323,7 @@ function buildMockSdk(args) {
|
|
|
319
323
|
});
|
|
320
324
|
const sdk = {
|
|
321
325
|
chainId: 1,
|
|
326
|
+
networkType: "Mainnet",
|
|
322
327
|
currentBlock: 1n,
|
|
323
328
|
timestamp: args.timestamp ?? 0,
|
|
324
329
|
withdrawalCompressor,
|