@gearbox-protocol/sdk 14.12.0-next.69 → 14.12.0-next.70
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/common-utils/charts/credit-session.js +0 -1
- package/dist/cjs/common-utils/index.js +1 -1
- package/dist/cjs/common-utils/utils/creditAccount/index.js +1 -1
- package/dist/cjs/common-utils/utils/creditAccount/quota-utils.js +0 -1
- package/dist/cjs/common-utils/utils/index.js +1 -1
- package/dist/cjs/common-utils/utils/strategies/strategy-info/get-cm-you-can-earn.js +1 -1
- package/dist/cjs/sdk/accounts/CreditAccountsServiceV310.js +81 -66
- package/dist/cjs/sdk/accounts/index.js +2 -0
- package/dist/cjs/sdk/accounts/intents/index.js +388 -0
- package/dist/cjs/sdk/accounts/intents/intents/index.js +14 -0
- package/dist/cjs/sdk/accounts/intents/intents/resume/add-collateral/add-collateral.fixtures.js +121 -0
- package/dist/cjs/sdk/accounts/intents/intents/resume/add-collateral/add-collateral.js +15 -0
- package/dist/cjs/sdk/accounts/intents/intents/resume/close/close.fixtures.js +108 -0
- package/dist/cjs/sdk/accounts/intents/intents/resume/close/close.js +51 -0
- package/dist/cjs/sdk/accounts/intents/intents/resume/decrease-leverage/decrease-leverage.fixtures.js +130 -0
- package/dist/cjs/sdk/accounts/intents/intents/resume/decrease-leverage/decrease-leverage.js +83 -0
- package/dist/cjs/sdk/accounts/intents/intents/resume/deposit/deposit.fixtures.js +117 -0
- package/dist/cjs/sdk/accounts/intents/intents/resume/deposit/deposit.js +15 -0
- package/dist/cjs/sdk/accounts/intents/intents/resume/increase-leverage/increase-leverage.fixtures.js +95 -0
- package/dist/cjs/sdk/accounts/intents/intents/resume/increase-leverage/increase-leverage.js +15 -0
- package/dist/cjs/sdk/accounts/intents/intents/resume/index.js +13 -0
- package/dist/cjs/sdk/accounts/intents/intents/resume/withdraw/withdraw.fixtures.js +138 -0
- package/dist/cjs/sdk/accounts/intents/intents/resume/withdraw/withdraw.js +161 -0
- package/dist/cjs/sdk/accounts/intents/operations/add-collateral/index.js +17 -0
- package/dist/cjs/sdk/accounts/intents/operations/claim-delayed/index.js +54 -0
- package/dist/cjs/sdk/accounts/intents/operations/close-credit-account/index.js +26 -0
- package/dist/cjs/sdk/accounts/intents/operations/decrease-debt/index.js +12 -0
- package/dist/cjs/sdk/accounts/intents/operations/increase-debt/index.js +12 -0
- package/dist/cjs/sdk/accounts/intents/operations/index.js +27 -0
- package/dist/cjs/sdk/accounts/intents/operations/quota-update/index.js +22 -0
- package/dist/cjs/sdk/accounts/intents/operations/repay-credit-account/index.js +22 -0
- package/dist/cjs/sdk/accounts/intents/operations/start-delayed-withdrawal/index.js +18 -0
- package/dist/cjs/sdk/accounts/intents/operations/swap/index.js +18 -0
- package/dist/cjs/sdk/accounts/intents/operations/types.js +1 -0
- package/dist/cjs/sdk/accounts/intents/operations/unwrap-rwa-collateral/index.js +27 -0
- package/dist/cjs/sdk/accounts/intents/operations/withdraw-collateral/index.js +43 -0
- package/dist/cjs/sdk/accounts/intents/operations/wrap-rwa-collateral/index.js +27 -0
- package/dist/cjs/sdk/accounts/intents/quoters/close.js +58 -0
- package/dist/cjs/sdk/accounts/intents/quoters/index.js +7 -0
- package/dist/cjs/sdk/accounts/intents/quoters/swap.js +79 -0
- package/dist/cjs/sdk/accounts/intents/testing/expect.js +167 -0
- package/dist/cjs/sdk/accounts/intents/testing/resume.js +172 -0
- package/dist/cjs/sdk/accounts/intents/testing/sdk-mock.js +179 -0
- package/dist/cjs/sdk/accounts/intents/types.js +1 -0
- package/dist/cjs/sdk/accounts/intents/utils/assemble-operation-calls.js +7 -0
- package/dist/cjs/sdk/accounts/intents/utils/borrowed-amount-plus-interest-and-fees.js +13 -0
- package/dist/cjs/sdk/accounts/intents/utils/common.js +43 -0
- package/dist/cjs/sdk/accounts/intents/utils/convert-amount.js +37 -0
- package/dist/cjs/sdk/accounts/intents/utils/index.js +19 -0
- package/dist/cjs/sdk/accounts/intents/utils/quotas-for-update.js +122 -0
- package/dist/cjs/sdk/accounts/intents/utils/simulate-adjust-state.js +39 -0
- package/dist/cjs/sdk/accounts/intents/utils/simulate-assets.js +69 -0
- package/dist/cjs/sdk/accounts/intents/utils/with-quota-update.js +13 -0
- package/dist/cjs/sdk/index.js +2 -0
- package/dist/esm/common-utils/charts/credit-session.js +0 -1
- package/dist/esm/common-utils/index.js +1 -1
- package/dist/esm/common-utils/utils/creditAccount/index.js +1 -1
- package/dist/esm/common-utils/utils/creditAccount/quota-utils.js +0 -1
- package/dist/esm/common-utils/utils/index.js +1 -1
- package/dist/esm/common-utils/utils/strategies/strategy-info/get-cm-you-can-earn.js +1 -1
- package/dist/esm/dev/AccountOpener.js +1 -1
- package/dist/esm/dev/withdrawalUtils.js +1 -1
- package/dist/esm/preview/simulate/simulatePoolOperation.js +1 -1
- package/dist/esm/preview/trace/extractTransfers.js +1 -1
- package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +83 -68
- package/dist/esm/sdk/accounts/index.js +2 -1
- package/dist/esm/sdk/accounts/intents/index.js +387 -0
- package/dist/esm/sdk/accounts/intents/intents/index.js +8 -0
- package/dist/esm/sdk/accounts/intents/intents/resume/add-collateral/add-collateral.fixtures.js +107 -0
- package/dist/esm/sdk/accounts/intents/intents/resume/add-collateral/add-collateral.js +14 -0
- package/dist/esm/sdk/accounts/intents/intents/resume/close/close.fixtures.js +94 -0
- package/dist/esm/sdk/accounts/intents/intents/resume/close/close.js +50 -0
- package/dist/esm/sdk/accounts/intents/intents/resume/decrease-leverage/decrease-leverage.fixtures.js +114 -0
- package/dist/esm/sdk/accounts/intents/intents/resume/decrease-leverage/decrease-leverage.js +82 -0
- package/dist/esm/sdk/accounts/intents/intents/resume/deposit/deposit.fixtures.js +110 -0
- package/dist/esm/sdk/accounts/intents/intents/resume/deposit/deposit.js +14 -0
- package/dist/esm/sdk/accounts/intents/intents/resume/increase-leverage/increase-leverage.fixtures.js +88 -0
- package/dist/esm/sdk/accounts/intents/intents/resume/increase-leverage/increase-leverage.js +14 -0
- package/dist/esm/sdk/accounts/intents/intents/resume/index.js +7 -0
- package/dist/esm/sdk/accounts/intents/intents/resume/withdraw/withdraw.fixtures.js +121 -0
- package/dist/esm/sdk/accounts/intents/intents/resume/withdraw/withdraw.js +160 -0
- package/dist/esm/sdk/accounts/intents/operations/add-collateral/index.js +16 -0
- package/dist/esm/sdk/accounts/intents/operations/claim-delayed/index.js +52 -0
- package/dist/esm/sdk/accounts/intents/operations/close-credit-account/index.js +25 -0
- package/dist/esm/sdk/accounts/intents/operations/decrease-debt/index.js +11 -0
- package/dist/esm/sdk/accounts/intents/operations/increase-debt/index.js +11 -0
- package/dist/esm/sdk/accounts/intents/operations/index.js +14 -0
- package/dist/esm/sdk/accounts/intents/operations/quota-update/index.js +21 -0
- package/dist/esm/sdk/accounts/intents/operations/repay-credit-account/index.js +21 -0
- package/dist/esm/sdk/accounts/intents/operations/start-delayed-withdrawal/index.js +17 -0
- package/dist/esm/sdk/accounts/intents/operations/swap/index.js +17 -0
- package/dist/esm/sdk/accounts/intents/operations/types.js +1 -0
- package/dist/esm/sdk/accounts/intents/operations/unwrap-rwa-collateral/index.js +26 -0
- package/dist/esm/sdk/accounts/intents/operations/withdraw-collateral/index.js +42 -0
- package/dist/esm/sdk/accounts/intents/operations/wrap-rwa-collateral/index.js +26 -0
- package/dist/esm/sdk/accounts/intents/quoters/close.js +56 -0
- package/dist/esm/sdk/accounts/intents/quoters/index.js +3 -0
- package/dist/esm/sdk/accounts/intents/quoters/swap.js +77 -0
- package/dist/esm/sdk/accounts/intents/testing/expect.js +162 -0
- package/dist/esm/sdk/accounts/intents/testing/resume.js +157 -0
- package/dist/esm/sdk/accounts/intents/testing/sdk-mock.js +172 -0
- package/dist/esm/sdk/accounts/intents/types.js +1 -0
- package/dist/esm/sdk/accounts/intents/utils/assemble-operation-calls.js +6 -0
- package/dist/esm/sdk/accounts/intents/utils/borrowed-amount-plus-interest-and-fees.js +12 -0
- package/dist/esm/sdk/accounts/intents/utils/common.js +40 -0
- package/dist/esm/sdk/accounts/intents/utils/convert-amount.js +36 -0
- package/dist/esm/sdk/accounts/intents/utils/index.js +9 -0
- package/dist/esm/sdk/accounts/intents/utils/quotas-for-update.js +121 -0
- package/dist/esm/sdk/accounts/intents/utils/simulate-adjust-state.js +38 -0
- package/dist/esm/sdk/accounts/intents/utils/simulate-assets.js +68 -0
- package/dist/esm/sdk/accounts/intents/utils/with-quota-update.js +12 -0
- package/dist/esm/sdk/accounts/liquidations/LiquidationsService.js +1 -1
- package/dist/esm/sdk/accounts/withdrawal-compressor/RedemptionLoggerV310Contract.js +1 -1
- package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV310Contract.js +1 -1
- package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV311Contract.js +1 -1
- package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV313Contract.js +1 -1
- package/dist/esm/sdk/base/TokensMeta.js +2 -2
- package/dist/esm/sdk/chain/detectNetwork.js +1 -1
- package/dist/esm/sdk/core/createAddressProvider.js +1 -1
- package/dist/esm/sdk/index.js +2 -1
- package/dist/esm/sdk/market/credit/CreditFacadeV310BaseContract.js +1 -1
- package/dist/esm/sdk/market/pool/PoolV310Contract.js +1 -1
- package/dist/esm/sdk/market/zapper/IETHZapperContract.js +1 -1
- package/dist/esm/sdk/market/zapper/ZapperContract.js +1 -1
- package/dist/esm/sdk/pools/PoolService.js +1 -1
- package/dist/esm/sdk/utils/viem/simulateWithPriceUpdates.js +1 -1
- package/dist/types/common-utils/index.d.ts +19 -19
- package/dist/types/common-utils/static/index.d.ts +1 -1
- package/dist/types/common-utils/utils/apy/index.d.ts +1 -1
- package/dist/types/common-utils/utils/creditAccount/calc-health-factor.d.ts +1 -1
- package/dist/types/common-utils/utils/creditAccount/calc-overall-apy.d.ts +1 -1
- package/dist/types/common-utils/utils/creditAccount/calc-quota-borrow-rate.d.ts +1 -1
- package/dist/types/common-utils/utils/creditAccount/debt.d.ts +1 -1
- package/dist/types/common-utils/utils/creditAccount/index.d.ts +1 -1
- package/dist/types/common-utils/utils/creditAccount/liquidation-price.d.ts +1 -1
- package/dist/types/common-utils/utils/creditAccount/quota-utils.d.ts +0 -1
- package/dist/types/common-utils/utils/creditAccount/sort.d.ts +1 -1
- package/dist/types/common-utils/utils/index.d.ts +3 -3
- package/dist/types/common-utils/utils/strategies/credit-managers/get-wallet-balances-allowed-on-cm.d.ts +1 -1
- package/dist/types/common-utils/utils/strategies/strategy-info/calculate-total-apy.d.ts +1 -1
- package/dist/types/common-utils/utils/strategies/strategy-info/calculate-total-points.d.ts +1 -1
- package/dist/types/common-utils/utils/strategies/strategy-info/types.d.ts +1 -1
- package/dist/types/common-utils/utils/strategies/tokens/add-amount-in-target.d.ts +1 -1
- package/dist/types/common-utils/utils/strategies/tokens/get-list-with-amount-in-target.d.ts +1 -1
- package/dist/types/common-utils/utils/strategies/types/credit-manager-data-legacy.d.ts +1 -1
- package/dist/types/common-utils/utils/strategies/types/strategies-cm-list.d.ts +1 -1
- package/dist/types/common-utils/utils/strategies/types/strategy-earnings.d.ts +1 -1
- package/dist/types/common-utils/utils/strategies/types/strategy.d.ts +1 -1
- package/dist/types/common-utils/utils/validation/validate-open-account.d.ts +1 -1
- package/dist/types/common-utils/utils/validation/validate-quota.d.ts +1 -1
- package/dist/types/plugins/apy/ApyPlugin.d.ts +1 -1
- package/dist/types/plugins/apy/pool-apy-utils.d.ts +1 -1
- package/dist/types/plugins/apy/strategy-data-source.d.ts +1 -1
- package/dist/types/plugins/apy/types.d.ts +1 -1
- package/dist/types/plugins/remote-configs/CustomConfigSource.d.ts +1 -1
- package/dist/types/plugins/remote-configs/RemoteConfigSource.d.ts +1 -1
- package/dist/types/plugins/remote-configs/RemoteConfigsPlugin.d.ts +1 -1
- package/dist/types/plugins/remote-configs/index.d.ts +1 -1
- package/dist/types/plugins/remote-configs/types.d.ts +1 -1
- package/dist/types/rewards/rewards/extra-apy.d.ts +1 -1
- package/dist/types/sdk/accounts/CreditAccountsServiceV310.d.ts +30 -10
- package/dist/types/sdk/accounts/index.d.ts +2 -1
- package/dist/types/sdk/accounts/intents/index.d.ts +22 -0
- package/dist/types/sdk/accounts/intents/intents/index.d.ts +8 -0
- package/dist/types/sdk/accounts/intents/intents/resume/add-collateral/add-collateral.d.ts +13 -0
- package/dist/types/sdk/accounts/intents/intents/resume/add-collateral/add-collateral.fixtures.d.ts +38 -0
- package/dist/types/sdk/accounts/intents/intents/resume/close/close.d.ts +26 -0
- package/dist/types/sdk/accounts/intents/intents/resume/close/close.fixtures.d.ts +63 -0
- package/dist/types/sdk/accounts/intents/intents/resume/decrease-leverage/decrease-leverage.d.ts +26 -0
- package/dist/types/sdk/accounts/intents/intents/resume/decrease-leverage/decrease-leverage.fixtures.d.ts +86 -0
- package/dist/types/sdk/accounts/intents/intents/resume/deposit/deposit.d.ts +13 -0
- package/dist/types/sdk/accounts/intents/intents/resume/deposit/deposit.fixtures.d.ts +48 -0
- package/dist/types/sdk/accounts/intents/intents/resume/increase-leverage/increase-leverage.d.ts +13 -0
- package/dist/types/sdk/accounts/intents/intents/resume/increase-leverage/increase-leverage.fixtures.d.ts +46 -0
- package/dist/types/sdk/accounts/intents/intents/resume/index.d.ts +7 -0
- package/dist/types/sdk/accounts/intents/intents/resume/withdraw/withdraw.d.ts +33 -0
- package/dist/types/sdk/accounts/intents/intents/resume/withdraw/withdraw.fixtures.d.ts +68 -0
- package/dist/types/sdk/accounts/intents/operations/add-collateral/index.d.ts +23 -0
- package/dist/types/sdk/accounts/intents/operations/claim-delayed/index.d.ts +55 -0
- package/dist/types/sdk/accounts/intents/operations/close-credit-account/index.d.ts +22 -0
- package/dist/types/sdk/accounts/intents/operations/decrease-debt/index.d.ts +18 -0
- package/dist/types/sdk/accounts/intents/operations/increase-debt/index.d.ts +18 -0
- package/dist/types/sdk/accounts/intents/operations/index.d.ts +14 -0
- package/dist/types/sdk/accounts/intents/operations/quota-update/index.d.ts +24 -0
- package/dist/types/sdk/accounts/intents/operations/repay-credit-account/index.d.ts +32 -0
- package/dist/types/sdk/accounts/intents/operations/start-delayed-withdrawal/index.d.ts +23 -0
- package/dist/types/sdk/accounts/intents/operations/swap/index.d.ts +28 -0
- package/dist/types/sdk/accounts/intents/operations/types.d.ts +21 -0
- package/dist/types/sdk/accounts/intents/operations/unwrap-rwa-collateral/index.d.ts +30 -0
- package/dist/types/sdk/accounts/intents/operations/withdraw-collateral/index.d.ts +36 -0
- package/dist/types/sdk/accounts/intents/operations/wrap-rwa-collateral/index.d.ts +30 -0
- package/dist/types/sdk/accounts/intents/quoters/close.d.ts +43 -0
- package/dist/types/sdk/accounts/intents/quoters/index.d.ts +3 -0
- package/dist/types/sdk/accounts/intents/quoters/swap.d.ts +43 -0
- package/dist/types/sdk/accounts/intents/testing/expect.d.ts +52 -0
- package/dist/types/sdk/accounts/intents/testing/resume.d.ts +84 -0
- package/dist/types/sdk/accounts/intents/testing/sdk-mock.d.ts +90 -0
- package/dist/types/sdk/accounts/intents/types.d.ts +75 -0
- package/dist/types/sdk/accounts/intents/utils/assemble-operation-calls.d.ts +10 -0
- package/dist/types/sdk/accounts/intents/utils/borrowed-amount-plus-interest-and-fees.d.ts +17 -0
- package/dist/types/sdk/accounts/intents/utils/common.d.ts +19 -0
- package/dist/types/sdk/accounts/intents/utils/convert-amount.d.ts +13 -0
- package/dist/types/sdk/accounts/intents/utils/index.d.ts +9 -0
- package/dist/types/sdk/accounts/intents/utils/quotas-for-update.d.ts +35 -0
- package/dist/types/sdk/accounts/intents/utils/simulate-adjust-state.d.ts +19 -0
- package/dist/types/sdk/accounts/intents/utils/simulate-assets.d.ts +21 -0
- package/dist/types/sdk/accounts/intents/utils/with-quota-update.d.ts +16 -0
- package/dist/types/sdk/accounts/types.d.ts +20 -0
- package/dist/types/sdk/index.d.ts +2 -1
- package/package.json +1 -1
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_sdk_utils_bigint_math = require("../../../../../utils/bigint-math.js");
|
|
3
|
+
const require_sdk_accounts_intents_utils_common = require("../../../utils/common.js");
|
|
4
|
+
const require_sdk_accounts_intents_utils_convert_amount = require("../../../utils/convert-amount.js");
|
|
5
|
+
const require_sdk_accounts_intents_utils_simulate_adjust_state = require("../../../utils/simulate-adjust-state.js");
|
|
6
|
+
require("../../../utils/index.js");
|
|
7
|
+
const require_sdk_accounts_intents_operations_claim_delayed_index = require("../../../operations/claim-delayed/index.js");
|
|
8
|
+
const require_sdk_accounts_intents_operations_decrease_debt_index = require("../../../operations/decrease-debt/index.js");
|
|
9
|
+
const require_sdk_accounts_intents_operations_swap_index = require("../../../operations/swap/index.js");
|
|
10
|
+
const require_sdk_accounts_intents_operations_withdraw_collateral_index = require("../../../operations/withdraw-collateral/index.js");
|
|
11
|
+
require("../../../operations/index.js");
|
|
12
|
+
//#region src/sdk/accounts/intents/intents/resume/withdraw/withdraw.ts
|
|
13
|
+
const ZERO_QUOTE = {
|
|
14
|
+
amount: 0n,
|
|
15
|
+
minAmount: 0n,
|
|
16
|
+
calls: []
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Resume withdraw — linear op chain following the W-first matrix:
|
|
20
|
+
* claim → (swap?) → decreaseDebt? → (unwrapRwa?) → withdrawCollateral.
|
|
21
|
+
* Only the underlying token (or the unwrapped rwa.asset on RWA markets) can
|
|
22
|
+
* be withdrawn (enforced below).
|
|
23
|
+
* The delayed lean intent records debt already paid at start; only its
|
|
24
|
+
* residual `debtRepaid` may consume claimed proceeds.
|
|
25
|
+
*
|
|
26
|
+
* Matrix arithmetic (split sizing) is oracle-priced; emitted swap legs go
|
|
27
|
+
* through the given quoter (`amountOut` on the op, `minAmountOut` caps the
|
|
28
|
+
* debt repay funded by the leg). Mirrors legacy
|
|
29
|
+
* `buildWithdrawResumeLogicalOps` + `resolveSwapBranches` (simplified: no
|
|
30
|
+
* min/avg op branches).
|
|
31
|
+
*/
|
|
32
|
+
async function buildResumeWithdrawOperations(props, quoter) {
|
|
33
|
+
const { intent, options, creditAccount, sdk } = props;
|
|
34
|
+
const underlying = creditAccount.underlying;
|
|
35
|
+
const rwaMeta = sdk.tokensMeta.rwaUnderlyings.get(underlying);
|
|
36
|
+
if (!require_sdk_accounts_intents_utils_common.eq(intent.withdrawToken, underlying) && !require_sdk_accounts_intents_utils_common.eq(intent.withdrawToken, rwaMeta?.asset ?? "")) throw new Error("Withdraw intent should withdraw underlying token only");
|
|
37
|
+
const claimOp = require_sdk_accounts_intents_operations_claim_delayed_index.buildClaimDelayedWithdrawalOperation({
|
|
38
|
+
creditAccount,
|
|
39
|
+
sdk
|
|
40
|
+
}, options);
|
|
41
|
+
const primary = require_sdk_accounts_intents_operations_claim_delayed_index.primaryInstantOutput(claimOp.outputs);
|
|
42
|
+
if (!primary || primary.amount <= 0n) throw new Error("No claimable assets");
|
|
43
|
+
const convert = require_sdk_accounts_intents_utils_convert_amount.convertAmount(sdk, creditAccount.creditManager);
|
|
44
|
+
const operations = [claimOp];
|
|
45
|
+
const swap = async (tokenIn, amountIn, tokenOut) => {
|
|
46
|
+
if (amountIn <= 0n) return ZERO_QUOTE;
|
|
47
|
+
const { state } = require_sdk_accounts_intents_utils_simulate_adjust_state.simulateState({
|
|
48
|
+
operations,
|
|
49
|
+
creditAccount,
|
|
50
|
+
sdk,
|
|
51
|
+
quotaReserve: props.quotaReserve
|
|
52
|
+
});
|
|
53
|
+
const tokenInBalance = state.assets.find((asset) => require_sdk_accounts_intents_utils_common.eq(asset.token, tokenIn))?.balance ?? 0n;
|
|
54
|
+
const quote = await quoter({
|
|
55
|
+
from: [{
|
|
56
|
+
token: tokenIn,
|
|
57
|
+
balance: amountIn
|
|
58
|
+
}],
|
|
59
|
+
tokenOut,
|
|
60
|
+
tokenInBalance
|
|
61
|
+
});
|
|
62
|
+
operations.push(require_sdk_accounts_intents_operations_swap_index.buildSwapOperation({
|
|
63
|
+
tokenIn,
|
|
64
|
+
amountIn,
|
|
65
|
+
tokenOut,
|
|
66
|
+
amountOut: quote.minAmount,
|
|
67
|
+
calls: quote.calls,
|
|
68
|
+
creditAccount,
|
|
69
|
+
sdk
|
|
70
|
+
}, options));
|
|
71
|
+
return quote;
|
|
72
|
+
};
|
|
73
|
+
const decreaseDebt = (amount) => {
|
|
74
|
+
if (amount > 0n) operations.push(require_sdk_accounts_intents_operations_decrease_debt_index.buildDecreaseDebtOperation({
|
|
75
|
+
amount,
|
|
76
|
+
creditAccount,
|
|
77
|
+
sdk
|
|
78
|
+
}, options));
|
|
79
|
+
};
|
|
80
|
+
const withdrawCollateral = async (amount) => {
|
|
81
|
+
const withdrawOperations = await require_sdk_accounts_intents_operations_withdraw_collateral_index.buildWithdrawCollateralOperation({
|
|
82
|
+
token: intent.withdrawToken,
|
|
83
|
+
amount,
|
|
84
|
+
to: intent.to,
|
|
85
|
+
underlying,
|
|
86
|
+
creditAccount,
|
|
87
|
+
sdk
|
|
88
|
+
}, options);
|
|
89
|
+
operations.push(...withdrawOperations);
|
|
90
|
+
};
|
|
91
|
+
if (intent.debtRepaid === 0n) {
|
|
92
|
+
/**
|
|
93
|
+
* 2.2.x: debtRepaid === 0n means that debt was repayed on withdrawal start,
|
|
94
|
+
* So all claimed amount should be withdrawn
|
|
95
|
+
*/
|
|
96
|
+
const available = require_sdk_accounts_intents_utils_common.eq(primary.token, intent.withdrawToken) ? primary.amount : (await swap(primary.token, primary.amount, intent.withdrawToken)).minAmount;
|
|
97
|
+
await withdrawCollateral(require_sdk_utils_bigint_math.BigIntMath.min(intent.withdrawAmount, available));
|
|
98
|
+
} else if (require_sdk_accounts_intents_utils_common.eq(intent.sourceToken, intent.withdrawToken)) {
|
|
99
|
+
/**
|
|
100
|
+
* 2.5.x: This can happen when source=target and source has delayedConfig.
|
|
101
|
+
* In this case claimed amount should be used only to repay debt
|
|
102
|
+
* and withdrawAmount can be withdrawn as is
|
|
103
|
+
*/
|
|
104
|
+
const undQuote = require_sdk_accounts_intents_utils_common.eq(primary.token, underlying) ? {
|
|
105
|
+
...ZERO_QUOTE,
|
|
106
|
+
amount: primary.amount,
|
|
107
|
+
minAmount: primary.amount
|
|
108
|
+
} : await swap(primary.token, primary.amount, underlying);
|
|
109
|
+
decreaseDebt(require_sdk_utils_bigint_math.BigIntMath.min(undQuote.minAmount, intent.debtRepaid));
|
|
110
|
+
await withdrawCollateral(intent.withdrawAmount);
|
|
111
|
+
} else if (require_sdk_accounts_intents_utils_common.eq(intent.withdrawToken, underlying)) {
|
|
112
|
+
/**
|
|
113
|
+
* 2.3.x: Special case - withdrawToken = underlying,
|
|
114
|
+
* so the full claim amount can be swapped int underlying;
|
|
115
|
+
* reserve W from the U result, then repay from the remainder.
|
|
116
|
+
*/
|
|
117
|
+
const undQuote = require_sdk_accounts_intents_utils_common.eq(primary.token, underlying) ? {
|
|
118
|
+
...ZERO_QUOTE,
|
|
119
|
+
amount: primary.amount,
|
|
120
|
+
minAmount: primary.amount
|
|
121
|
+
} : await swap(primary.token, primary.amount, underlying);
|
|
122
|
+
const withdrawn = require_sdk_utils_bigint_math.BigIntMath.min(intent.withdrawAmount, undQuote.minAmount);
|
|
123
|
+
decreaseDebt(require_sdk_utils_bigint_math.BigIntMath.min(require_sdk_utils_bigint_math.BigIntMath.max(undQuote.minAmount - withdrawn, 0n), intent.debtRepaid));
|
|
124
|
+
withdrawCollateral(withdrawn);
|
|
125
|
+
} else if (require_sdk_accounts_intents_utils_common.eq(primary.token, intent.withdrawToken)) {
|
|
126
|
+
/**
|
|
127
|
+
* 2.4.2: Special case - withdrawToken = claimedToken,
|
|
128
|
+
* so we can take part of claimed amount without conversion and swap the rest into underlying;
|
|
129
|
+
* reserve claimed T for W; only the remainder funds debt.
|
|
130
|
+
*/
|
|
131
|
+
const withdrawn = require_sdk_utils_bigint_math.BigIntMath.min(intent.withdrawAmount, primary.amount);
|
|
132
|
+
const undQuote = await swap(primary.token, primary.amount - withdrawn, underlying);
|
|
133
|
+
decreaseDebt(require_sdk_utils_bigint_math.BigIntMath.min(undQuote.minAmount, intent.debtRepaid));
|
|
134
|
+
withdrawCollateral(withdrawn);
|
|
135
|
+
} else if (require_sdk_accounts_intents_utils_common.eq(primary.token, underlying)) {
|
|
136
|
+
/**
|
|
137
|
+
* 2.4.1: Special case - underlying = claimedToken,
|
|
138
|
+
* so we can decrease debt using part of claimed amount and swap the rest into target;
|
|
139
|
+
* reserve U equivalent of W, repay with the rest, then swap W.
|
|
140
|
+
*/
|
|
141
|
+
const withdrawInUnderlying = require_sdk_utils_bigint_math.BigIntMath.min(convert(intent.withdrawToken, primary.token, intent.withdrawAmount), primary.amount);
|
|
142
|
+
decreaseDebt(require_sdk_utils_bigint_math.BigIntMath.min(primary.amount - withdrawInUnderlying, intent.debtRepaid));
|
|
143
|
+
const withdrawQuote = await swap(primary.token, withdrawInUnderlying, intent.withdrawToken);
|
|
144
|
+
withdrawCollateral(require_sdk_utils_bigint_math.BigIntMath.min(intent.withdrawAmount, withdrawQuote.minAmount));
|
|
145
|
+
} else {
|
|
146
|
+
/**
|
|
147
|
+
* 2.4.3: Common case - both target and claimed token are independent tokens;
|
|
148
|
+
* Here we should reserve part of claimed amount for withdrawal and
|
|
149
|
+
* use the rest for debt repayment.
|
|
150
|
+
* split a third-token claim into independent debt and W legs.
|
|
151
|
+
*/
|
|
152
|
+
const withdrawInClaim = require_sdk_utils_bigint_math.BigIntMath.min(convert(intent.withdrawToken, primary.token, intent.withdrawAmount), primary.amount);
|
|
153
|
+
const debtQuote = await swap(primary.token, require_sdk_utils_bigint_math.BigIntMath.max(primary.amount - withdrawInClaim, 0n), underlying);
|
|
154
|
+
decreaseDebt(require_sdk_utils_bigint_math.BigIntMath.min(debtQuote.minAmount, intent.debtRepaid));
|
|
155
|
+
const withdrawQuote = await swap(primary.token, withdrawInClaim, intent.withdrawToken);
|
|
156
|
+
withdrawCollateral(require_sdk_utils_bigint_math.BigIntMath.min(intent.withdrawAmount, withdrawQuote.minAmount));
|
|
157
|
+
}
|
|
158
|
+
return operations;
|
|
159
|
+
}
|
|
160
|
+
//#endregion
|
|
161
|
+
exports.buildResumeWithdrawOperations = buildResumeWithdrawOperations;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/sdk/accounts/intents/operations/add-collateral/index.ts
|
|
3
|
+
function buildAddCollateralOperation(input, option) {
|
|
4
|
+
const calls = option.kind === "onchain" ? input.sdk.accounts.prepareAddCollateral(input.creditAccount.creditFacade, [{
|
|
5
|
+
token: input.token,
|
|
6
|
+
balance: input.amount
|
|
7
|
+
}], {}) : [];
|
|
8
|
+
return {
|
|
9
|
+
type: "addCollateral",
|
|
10
|
+
token: input.token,
|
|
11
|
+
amount: input.amount,
|
|
12
|
+
value: input.value,
|
|
13
|
+
calls
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
exports.buildAddCollateralOperation = buildAddCollateralOperation;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_sdk_accounts_intents_utils_common = require("../../utils/common.js");
|
|
3
|
+
require("../../utils/index.js");
|
|
4
|
+
//#region src/sdk/accounts/intents/operations/claim-delayed/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* Build the claim op from a matured claimable, or a simulation-only op from
|
|
7
|
+
* resume claimedToken/amount when claimable is not attached.
|
|
8
|
+
* Stores balances for raw call
|
|
9
|
+
*/
|
|
10
|
+
function buildClaimDelayedWithdrawalOperation(input, option) {
|
|
11
|
+
if (option.kind === "onchain") {
|
|
12
|
+
const calls = input.sdk.accounts.assembleClaimDelayedCalls({
|
|
13
|
+
creditFacade: input.creditAccount.creditFacade,
|
|
14
|
+
claimableNow: option.claimableWithdrawal
|
|
15
|
+
});
|
|
16
|
+
return {
|
|
17
|
+
type: "claimDelayedWithdrawal",
|
|
18
|
+
token: option.claimableWithdrawal.token.toLowerCase(),
|
|
19
|
+
withdrawalPhantomToken: option.claimableWithdrawal.withdrawalPhantomToken.toLowerCase(),
|
|
20
|
+
withdrawalTokenSpent: option.claimableWithdrawal.withdrawalTokenSpent,
|
|
21
|
+
outputs: option.claimableWithdrawal.outputs.map((o) => ({
|
|
22
|
+
...o,
|
|
23
|
+
token: o.token.toLowerCase()
|
|
24
|
+
})),
|
|
25
|
+
calls
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
type: "claimDelayedWithdrawal",
|
|
30
|
+
token: option.withdrawalConfig.token,
|
|
31
|
+
withdrawalPhantomToken: option.withdrawalConfig.withdrawalPhantomToken,
|
|
32
|
+
withdrawalTokenSpent: option.phantomSpent,
|
|
33
|
+
outputs: [{
|
|
34
|
+
token: option.withdrawalConfig.underlying,
|
|
35
|
+
amount: require_sdk_accounts_intents_utils_common.toTargetDecimals(option.phantomSpent, option.withdrawalConfig.withdrawalPhantomToken, option.withdrawalConfig.underlying, input.sdk),
|
|
36
|
+
isDelayed: false
|
|
37
|
+
}],
|
|
38
|
+
calls: []
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** First positive non-delayed output (optionally restricted to `token`). */
|
|
42
|
+
function primaryInstantOutput(outputs, token) {
|
|
43
|
+
for (const out of outputs) {
|
|
44
|
+
if (out.isDelayed || out.amount <= 0n) continue;
|
|
45
|
+
if (token != null && !require_sdk_accounts_intents_utils_common.eq(out.token, token)) continue;
|
|
46
|
+
return {
|
|
47
|
+
token: out.token,
|
|
48
|
+
amount: out.amount
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
exports.buildClaimDelayedWithdrawalOperation = buildClaimDelayedWithdrawalOperation;
|
|
54
|
+
exports.primaryInstantOutput = primaryInstantOutput;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/sdk/accounts/intents/operations/close-credit-account/index.ts
|
|
3
|
+
async function buildCloseCreditAccountOperation(input, option) {
|
|
4
|
+
const { quote } = input;
|
|
5
|
+
if (option.kind === "onchain") {
|
|
6
|
+
const rwaConfig = input.sdk.tokensMeta.rwaUnderlyings.get(option.creditAccount.underlying);
|
|
7
|
+
const closeCalls = await input.sdk.accounts.assembleCloseCreditAccountCalls({
|
|
8
|
+
creditAccount: option.creditAccount,
|
|
9
|
+
routerCalls: quote.calls,
|
|
10
|
+
assetsToWithdraw: [(rwaConfig?.asset ?? option.creditAccount.underlying).toLowerCase()],
|
|
11
|
+
to: option.to
|
|
12
|
+
});
|
|
13
|
+
return {
|
|
14
|
+
...quote,
|
|
15
|
+
calls: closeCalls,
|
|
16
|
+
type: "closeCreditAccount"
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
...quote,
|
|
21
|
+
calls: [],
|
|
22
|
+
type: "closeCreditAccount"
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
exports.buildCloseCreditAccountOperation = buildCloseCreditAccountOperation;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/sdk/accounts/intents/operations/decrease-debt/index.ts
|
|
3
|
+
function buildDecreaseDebtOperation(input, option) {
|
|
4
|
+
const calls = option.kind === "onchain" ? [input.sdk.accounts.prepareChangeDebt(input.creditAccount.creditFacade, input.amount, true)] : [];
|
|
5
|
+
return {
|
|
6
|
+
type: "decreaseDebt",
|
|
7
|
+
amount: input.amount,
|
|
8
|
+
calls
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
exports.buildDecreaseDebtOperation = buildDecreaseDebtOperation;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/sdk/accounts/intents/operations/increase-debt/index.ts
|
|
3
|
+
function buildIncreaseDebtOperation(input, option) {
|
|
4
|
+
const calls = option.kind === "onchain" ? [input.sdk.accounts.prepareIncreaseDebt(input.creditAccount.creditFacade, input.amount)] : [];
|
|
5
|
+
return {
|
|
6
|
+
type: "increaseDebt",
|
|
7
|
+
amount: input.amount,
|
|
8
|
+
calls
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
exports.buildIncreaseDebtOperation = buildIncreaseDebtOperation;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_sdk_accounts_intents_operations_add_collateral_index = require("./add-collateral/index.js");
|
|
3
|
+
const require_sdk_accounts_intents_operations_claim_delayed_index = require("./claim-delayed/index.js");
|
|
4
|
+
const require_sdk_accounts_intents_operations_close_credit_account_index = require("./close-credit-account/index.js");
|
|
5
|
+
const require_sdk_accounts_intents_operations_decrease_debt_index = require("./decrease-debt/index.js");
|
|
6
|
+
const require_sdk_accounts_intents_operations_increase_debt_index = require("./increase-debt/index.js");
|
|
7
|
+
const require_sdk_accounts_intents_operations_quota_update_index = require("./quota-update/index.js");
|
|
8
|
+
const require_sdk_accounts_intents_operations_repay_credit_account_index = require("./repay-credit-account/index.js");
|
|
9
|
+
const require_sdk_accounts_intents_operations_start_delayed_withdrawal_index = require("./start-delayed-withdrawal/index.js");
|
|
10
|
+
const require_sdk_accounts_intents_operations_swap_index = require("./swap/index.js");
|
|
11
|
+
require("./types.js");
|
|
12
|
+
const require_sdk_accounts_intents_operations_unwrap_rwa_collateral_index = require("./unwrap-rwa-collateral/index.js");
|
|
13
|
+
const require_sdk_accounts_intents_operations_withdraw_collateral_index = require("./withdraw-collateral/index.js");
|
|
14
|
+
const require_sdk_accounts_intents_operations_wrap_rwa_collateral_index = require("./wrap-rwa-collateral/index.js");
|
|
15
|
+
exports.buildAddCollateralOperation = require_sdk_accounts_intents_operations_add_collateral_index.buildAddCollateralOperation;
|
|
16
|
+
exports.buildClaimDelayedWithdrawalOperation = require_sdk_accounts_intents_operations_claim_delayed_index.buildClaimDelayedWithdrawalOperation;
|
|
17
|
+
exports.buildCloseCreditAccountOperation = require_sdk_accounts_intents_operations_close_credit_account_index.buildCloseCreditAccountOperation;
|
|
18
|
+
exports.buildDecreaseDebtOperation = require_sdk_accounts_intents_operations_decrease_debt_index.buildDecreaseDebtOperation;
|
|
19
|
+
exports.buildIncreaseDebtOperation = require_sdk_accounts_intents_operations_increase_debt_index.buildIncreaseDebtOperation;
|
|
20
|
+
exports.buildQuotaUpdateOperation = require_sdk_accounts_intents_operations_quota_update_index.buildQuotaUpdateOperation;
|
|
21
|
+
exports.buildRepayCreditAccountOperation = require_sdk_accounts_intents_operations_repay_credit_account_index.buildRepayCreditAccountOperation;
|
|
22
|
+
exports.buildStartDelayedWithdrawalOperation = require_sdk_accounts_intents_operations_start_delayed_withdrawal_index.buildStartDelayedWithdrawalOperation;
|
|
23
|
+
exports.buildSwapOperation = require_sdk_accounts_intents_operations_swap_index.buildSwapOperation;
|
|
24
|
+
exports.buildUnwrapRwaCollateralOperation = require_sdk_accounts_intents_operations_unwrap_rwa_collateral_index.buildUnwrapRwaCollateralOperation;
|
|
25
|
+
exports.buildWithdrawCollateralOperation = require_sdk_accounts_intents_operations_withdraw_collateral_index.buildWithdrawCollateralOperation;
|
|
26
|
+
exports.buildWrapRwaCollateralOperation = require_sdk_accounts_intents_operations_wrap_rwa_collateral_index.buildWrapRwaCollateralOperation;
|
|
27
|
+
exports.primaryInstantOutput = require_sdk_accounts_intents_operations_claim_delayed_index.primaryInstantOutput;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/sdk/accounts/intents/operations/quota-update/index.ts
|
|
3
|
+
function buildQuotaUpdateOperation(input, option) {
|
|
4
|
+
const { update, creditAccount, sdk } = input;
|
|
5
|
+
let calls = [];
|
|
6
|
+
if (option.kind === "onchain") {
|
|
7
|
+
const quotaAssets = [...update.quotaIncrease, ...update.quotaDecrease];
|
|
8
|
+
if (quotaAssets.length > 0) calls = sdk.accounts.prepareUpdateQuotas(creditAccount.creditFacade, {
|
|
9
|
+
averageQuota: quotaAssets,
|
|
10
|
+
minQuota: quotaAssets
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
type: "changeQuota",
|
|
15
|
+
desiredQuota: update.desiredQuota,
|
|
16
|
+
quotaIncrease: update.quotaIncrease,
|
|
17
|
+
quotaDecrease: update.quotaDecrease,
|
|
18
|
+
calls
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
exports.buildQuotaUpdateOperation = buildQuotaUpdateOperation;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/sdk/accounts/intents/operations/repay-credit-account/index.ts
|
|
3
|
+
async function buildRepayCreditAccountOperation(input, option) {
|
|
4
|
+
const calls = option.kind === "onchain" ? await input.sdk.accounts.assembleRepayCreditAccountCalls({
|
|
5
|
+
collateralAssets: input.expectedRepayAsset ?? [],
|
|
6
|
+
assetsToWithdraw: input.expectedWithdrawAssets ?? [],
|
|
7
|
+
creditAccount: option.creditAccount,
|
|
8
|
+
permits: option.permits,
|
|
9
|
+
to: option.to,
|
|
10
|
+
tokensToClaim: option.tokensToClaim,
|
|
11
|
+
calls: option.wrapCalls
|
|
12
|
+
}) : [];
|
|
13
|
+
return {
|
|
14
|
+
type: "repayCreditAccount",
|
|
15
|
+
expectedRepayAsset: input.expectedRepayAsset,
|
|
16
|
+
expectedWithdrawAssets: input.expectedWithdrawAssets,
|
|
17
|
+
value: input.value,
|
|
18
|
+
calls
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
exports.buildRepayCreditAccountOperation = buildRepayCreditAccountOperation;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/sdk/accounts/intents/operations/start-delayed-withdrawal/index.ts
|
|
3
|
+
function buildStartDelayedWithdrawalOperation(input, option) {
|
|
4
|
+
const calls = option.kind === "onchain" ? input.sdk.accounts.assembleStartDelayedWithdrawalCalls({
|
|
5
|
+
creditFacade: input.creditAccount.creditFacade,
|
|
6
|
+
preview: input.preview
|
|
7
|
+
}) : [];
|
|
8
|
+
return {
|
|
9
|
+
type: "startDelayedWithdrawal",
|
|
10
|
+
token: input.preview.token,
|
|
11
|
+
amountIn: input.preview.amountIn,
|
|
12
|
+
outputs: [...input.preview.outputs],
|
|
13
|
+
settlement: input.settlement,
|
|
14
|
+
calls
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
exports.buildStartDelayedWithdrawalOperation = buildStartDelayedWithdrawalOperation;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/sdk/accounts/intents/operations/swap/index.ts
|
|
3
|
+
/** One-to-one swap op (withdraw resume conversion legs). */
|
|
4
|
+
function buildSwapOperation(input, option) {
|
|
5
|
+
if (option.kind === "onchain" && input.calls.length === 0) throw new Error("swap: missing router calls");
|
|
6
|
+
return {
|
|
7
|
+
type: "swap",
|
|
8
|
+
from: [{
|
|
9
|
+
token: input.tokenIn,
|
|
10
|
+
balance: input.amountIn
|
|
11
|
+
}],
|
|
12
|
+
tokenOut: input.tokenOut,
|
|
13
|
+
amountOut: input.amountOut,
|
|
14
|
+
calls: input.calls
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
exports.buildSwapOperation = buildSwapOperation;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/sdk/accounts/intents/operations/unwrap-rwa-collateral/index.ts
|
|
3
|
+
/** One-to-one swap op (withdraw resume conversion legs). */
|
|
4
|
+
async function buildUnwrapRwaCollateralOperation(input, option) {
|
|
5
|
+
if (option.kind === "onchain") {
|
|
6
|
+
const calls = await input.sdk.accounts.getRWAUnwrapCalls(input.amountIn, input.creditAccount.creditManager);
|
|
7
|
+
if (!calls) throw new Error("unwrapRwaCollateral: no wrap calls found");
|
|
8
|
+
return {
|
|
9
|
+
type: "unwrapRwaCollateral",
|
|
10
|
+
tokenIn: input.tokenIn,
|
|
11
|
+
amount: input.amountIn,
|
|
12
|
+
tokenOut: input.tokenOut,
|
|
13
|
+
amountOut: input.amountOut,
|
|
14
|
+
calls
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
type: "unwrapRwaCollateral",
|
|
19
|
+
tokenIn: input.tokenIn,
|
|
20
|
+
amount: input.amountIn,
|
|
21
|
+
tokenOut: input.tokenOut,
|
|
22
|
+
amountOut: input.amountOut,
|
|
23
|
+
calls: []
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
exports.buildUnwrapRwaCollateralOperation = buildUnwrapRwaCollateralOperation;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_sdk_accounts_intents_utils_common = require("../../utils/common.js");
|
|
3
|
+
require("../../utils/index.js");
|
|
4
|
+
const require_sdk_accounts_intents_operations_unwrap_rwa_collateral_index = require("../unwrap-rwa-collateral/index.js");
|
|
5
|
+
//#region src/sdk/accounts/intents/operations/withdraw-collateral/index.ts
|
|
6
|
+
/**
|
|
7
|
+
* Partial-withdraw tail: RWA forced unwrap, then withdrawCollateral to wallet.
|
|
8
|
+
*
|
|
9
|
+
* When withdrawing the wrapped RWA underlying, forces unwrap to `rwa.asset`
|
|
10
|
+
* and withdraws the unwrapped asset instead — even if the intent asked for
|
|
11
|
+
* underlying (mirrors legacy `resolveRwaForcedUnwrapWithdraw`).
|
|
12
|
+
*/
|
|
13
|
+
async function buildWithdrawCollateralOperation(input, option) {
|
|
14
|
+
const { token, amount, to, underlying, creditAccount, sdk } = input;
|
|
15
|
+
if (amount <= 0n) throw new Error("Nothing to withdraw");
|
|
16
|
+
const buildWithdraw = (withdrawToken, withdrawAmount) => {
|
|
17
|
+
let calls = [];
|
|
18
|
+
if (option.kind === "onchain") {
|
|
19
|
+
if (to === void 0) throw new Error("withdrawCollateral.to is required for onchain calls");
|
|
20
|
+
calls = [sdk.accounts.prepareWithdrawToken(creditAccount.creditFacade, withdrawToken, withdrawAmount, to)];
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
type: "withdrawCollateral",
|
|
24
|
+
token: withdrawToken,
|
|
25
|
+
amount: withdrawAmount,
|
|
26
|
+
to,
|
|
27
|
+
calls
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
const rwa = require_sdk_accounts_intents_utils_common.eq(token, underlying) ? sdk.tokensMeta.rwaUnderlyings.get(underlying) : void 0;
|
|
31
|
+
if (!rwa) return [buildWithdraw(token, amount)];
|
|
32
|
+
const amountOut = require_sdk_accounts_intents_utils_common.toTargetDecimals(amount, underlying, rwa.asset, sdk);
|
|
33
|
+
return [await require_sdk_accounts_intents_operations_unwrap_rwa_collateral_index.buildUnwrapRwaCollateralOperation({
|
|
34
|
+
amountIn: amount,
|
|
35
|
+
tokenIn: underlying,
|
|
36
|
+
tokenOut: rwa.asset,
|
|
37
|
+
amountOut,
|
|
38
|
+
creditAccount,
|
|
39
|
+
sdk
|
|
40
|
+
}, option), buildWithdraw(rwa.asset, amountOut)];
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
exports.buildWithdrawCollateralOperation = buildWithdrawCollateralOperation;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/sdk/accounts/intents/operations/wrap-rwa-collateral/index.ts
|
|
3
|
+
/** One-to-one wrap op (decrease-leverage resume repay from rwa.asset). */
|
|
4
|
+
async function buildWrapRwaCollateralOperation(input, option) {
|
|
5
|
+
if (option.kind === "onchain") {
|
|
6
|
+
const calls = await input.sdk.accounts.getRWAWrapCalls(input.amountIn, input.creditAccount.creditManager);
|
|
7
|
+
if (!calls) throw new Error("wrapRwaCollateral: no wrap calls found");
|
|
8
|
+
return {
|
|
9
|
+
type: "wrapRwaCollateral",
|
|
10
|
+
tokenIn: input.tokenIn,
|
|
11
|
+
amount: input.amountIn,
|
|
12
|
+
tokenOut: input.tokenOut,
|
|
13
|
+
amountOut: input.amountOut,
|
|
14
|
+
calls
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
type: "wrapRwaCollateral",
|
|
19
|
+
tokenIn: input.tokenIn,
|
|
20
|
+
amount: input.amountIn,
|
|
21
|
+
tokenOut: input.tokenOut,
|
|
22
|
+
amountOut: input.amountOut,
|
|
23
|
+
calls: []
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
exports.buildWrapRwaCollateralOperation = buildWrapRwaCollateralOperation;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_sdk_utils_bigint_math = require("../../../utils/bigint-math.js");
|
|
3
|
+
const require_sdk_accounts_intents_utils_common = require("../utils/common.js");
|
|
4
|
+
const require_sdk_accounts_intents_utils_convert_amount = require("../utils/convert-amount.js");
|
|
5
|
+
require("../utils/index.js");
|
|
6
|
+
//#region src/sdk/accounts/intents/quoters/close.ts
|
|
7
|
+
/**
|
|
8
|
+
* Offchain quoter: debt is repaid from NON-underlying value
|
|
9
|
+
* (`amount = Σ convert(non-underlying → underlying) − debt`, clamped at 0),
|
|
10
|
+
* underlying already on the account goes straight to the wallet
|
|
11
|
+
* (`underlyingBalance = underlying + amount`). No router involved.
|
|
12
|
+
*/
|
|
13
|
+
function createCloseOracleQuoter(args) {
|
|
14
|
+
const { sdk, creditAccount } = args;
|
|
15
|
+
const underlyingLc = creditAccount.underlying.toLowerCase();
|
|
16
|
+
return async ({ assets, debt }) => {
|
|
17
|
+
const convert = require_sdk_accounts_intents_utils_convert_amount.convertAmount(sdk, creditAccount.creditManager);
|
|
18
|
+
const underlyingBalance = assets.find((a) => require_sdk_accounts_intents_utils_common.eq(a.token, underlyingLc))?.balance ?? 0n;
|
|
19
|
+
const assetsSum = assets.reduce((acc, a) => acc + convert(a.token, creditAccount.underlying, a.balance), 0n);
|
|
20
|
+
const totalToRepay = require_sdk_utils_bigint_math.BigIntMath.max(assetsSum - underlyingBalance, 0n);
|
|
21
|
+
const amount = require_sdk_utils_bigint_math.BigIntMath.max(totalToRepay - debt, 0n);
|
|
22
|
+
return {
|
|
23
|
+
amount,
|
|
24
|
+
minAmount: amount,
|
|
25
|
+
underlyingBalance: underlyingBalance + amount,
|
|
26
|
+
calls: []
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Onchain quoter: router close path over post-claim balances
|
|
32
|
+
* (phantom already spent). `amount` mirrors legacy
|
|
33
|
+
* `patchCloseOperationReceiveAmount` (debt 0n — closed preview is zeroed).
|
|
34
|
+
*/
|
|
35
|
+
function createRouterCloseQuoter(args) {
|
|
36
|
+
const { sdk, creditAccount, slippage } = args;
|
|
37
|
+
const creditManager = sdk.marketRegister.findCreditManager(creditAccount.creditManager);
|
|
38
|
+
const router = sdk.routerFor({ creditFacade: creditAccount.creditFacade });
|
|
39
|
+
return async ({ assets }) => {
|
|
40
|
+
return await router.findBestClosePath({
|
|
41
|
+
creditAccount: require_sdk_accounts_intents_utils_common.toRouterCaSlice(creditAccount, assets),
|
|
42
|
+
creditManager: {
|
|
43
|
+
address: creditManager.creditManager.address.toLowerCase(),
|
|
44
|
+
creditFacade: creditManager.creditFacade.address.toLowerCase(),
|
|
45
|
+
collateralTokens: creditManager.creditManager.collateralTokens.map((t) => t.toLowerCase())
|
|
46
|
+
},
|
|
47
|
+
slippage,
|
|
48
|
+
balances: {
|
|
49
|
+
expectedBalances: assets,
|
|
50
|
+
leftoverBalances: [],
|
|
51
|
+
tokensToClaim: []
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
exports.createCloseOracleQuoter = createCloseOracleQuoter;
|
|
58
|
+
exports.createRouterCloseQuoter = createRouterCloseQuoter;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_sdk_accounts_intents_quoters_close = require("./close.js");
|
|
3
|
+
const require_sdk_accounts_intents_quoters_swap = require("./swap.js");
|
|
4
|
+
exports.createCloseOracleQuoter = require_sdk_accounts_intents_quoters_close.createCloseOracleQuoter;
|
|
5
|
+
exports.createOracleSwapQuoter = require_sdk_accounts_intents_quoters_swap.createOracleSwapQuoter;
|
|
6
|
+
exports.createRouterCloseQuoter = require_sdk_accounts_intents_quoters_close.createRouterCloseQuoter;
|
|
7
|
+
exports.createRouterSwapQuoter = require_sdk_accounts_intents_quoters_swap.createRouterSwapQuoter;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_sdk_accounts_intents_utils_common = require("../utils/common.js");
|
|
3
|
+
const require_sdk_accounts_intents_utils_convert_amount = require("../utils/convert-amount.js");
|
|
4
|
+
require("../utils/index.js");
|
|
5
|
+
//#region src/sdk/accounts/intents/quoters/swap.ts
|
|
6
|
+
/**
|
|
7
|
+
* Offchain quoter: oracle prices via {@link convertAmount} (with an RWA
|
|
8
|
+
* bridge through the wrapped underlying when `rwa.asset` has no direct pool
|
|
9
|
+
* price).
|
|
10
|
+
*/
|
|
11
|
+
function createOracleSwapQuoter(args) {
|
|
12
|
+
const { sdk, creditAccount } = args;
|
|
13
|
+
const { creditManager } = creditAccount;
|
|
14
|
+
const convert = require_sdk_accounts_intents_utils_convert_amount.convertAmount(sdk, creditManager);
|
|
15
|
+
return async ({ from, tokenOut }) => {
|
|
16
|
+
if (!from[0]) return {
|
|
17
|
+
amount: 0n,
|
|
18
|
+
minAmount: 0n,
|
|
19
|
+
calls: []
|
|
20
|
+
};
|
|
21
|
+
let amount = 0n;
|
|
22
|
+
for (const entry of from) amount += convert(entry.token, tokenOut, entry.balance);
|
|
23
|
+
return {
|
|
24
|
+
amount,
|
|
25
|
+
minAmount: amount,
|
|
26
|
+
calls: []
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Onchain quoter: a full token-in spend uses `findOneTokenPath`; a partial
|
|
32
|
+
* spend uses `findManyToOnePath` with the exact post-operations balance and
|
|
33
|
+
* leftover. RWA wrap/unwrap calls are resolved by the router.
|
|
34
|
+
*/
|
|
35
|
+
function createRouterSwapQuoter(args) {
|
|
36
|
+
const { sdk, creditAccount, slippage } = args;
|
|
37
|
+
const creditManager = sdk.marketRegister.findCreditManager(creditAccount.creditManager);
|
|
38
|
+
const cmSlice = {
|
|
39
|
+
address: creditManager.creditManager.address.toLowerCase(),
|
|
40
|
+
creditFacade: creditManager.creditFacade.address.toLowerCase(),
|
|
41
|
+
collateralTokens: creditManager.creditManager.collateralTokens.map((t) => t.toLowerCase())
|
|
42
|
+
};
|
|
43
|
+
const router = sdk.routerFor({ creditFacade: creditManager.creditFacade.address });
|
|
44
|
+
return async (request) => {
|
|
45
|
+
const input = request.from[0];
|
|
46
|
+
if (!input) return {
|
|
47
|
+
amount: 0n,
|
|
48
|
+
minAmount: 0n,
|
|
49
|
+
calls: []
|
|
50
|
+
};
|
|
51
|
+
const leftover = request.tokenInBalance - input.balance;
|
|
52
|
+
if (leftover < 0n) throw new Error("createRouterSwapQuoter: swap amount exceeds token-in balance");
|
|
53
|
+
if (leftover > 0n) return await router.findManyToOnePath({
|
|
54
|
+
creditAccount: require_sdk_accounts_intents_utils_common.toRouterCaSlice(creditAccount),
|
|
55
|
+
creditManager: cmSlice,
|
|
56
|
+
expectedBalances: [{
|
|
57
|
+
token: input.token,
|
|
58
|
+
balance: request.tokenInBalance
|
|
59
|
+
}],
|
|
60
|
+
leftoverBalances: [{
|
|
61
|
+
token: input.token,
|
|
62
|
+
balance: leftover
|
|
63
|
+
}],
|
|
64
|
+
target: request.tokenOut,
|
|
65
|
+
slippage
|
|
66
|
+
});
|
|
67
|
+
return await router.findOneTokenPath({
|
|
68
|
+
creditAccount: require_sdk_accounts_intents_utils_common.toRouterCaSlice(creditAccount),
|
|
69
|
+
creditManager: cmSlice,
|
|
70
|
+
tokenIn: input.token,
|
|
71
|
+
tokenOut: request.tokenOut,
|
|
72
|
+
amount: input.balance,
|
|
73
|
+
slippage
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
//#endregion
|
|
78
|
+
exports.createOracleSwapQuoter = createOracleSwapQuoter;
|
|
79
|
+
exports.createRouterSwapQuoter = createRouterSwapQuoter;
|