@gearbox-protocol/sdk 16.0.0-next.22 → 16.0.0-next.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/onchain/accounts/intents/maxWithdrawCollateral.js +2 -2
- package/dist/cjs/onchain/accounts/intents/open-strategy.js +7 -4
- package/dist/cjs/onchain/accounts/intents/realize.js +10 -6
- package/dist/cjs/onchain/accounts/intents/tail.js +3 -2
- package/dist/cjs/onchain/accounts/intents/testing/market.js +1 -1
- package/dist/cjs/onchain/accounts/intents/testing/sdk-mock.js +18 -1
- package/dist/cjs/onchain/accounts/intents/tests/add-collateral.fixtures.js +4 -4
- package/dist/cjs/onchain/accounts/intents/tests/adjust-leverage.fixtures.js +15 -15
- package/dist/cjs/onchain/accounts/intents/tests/deposit.fixtures.js +13 -13
- package/dist/cjs/onchain/accounts/intents/tests/finish-decrease-leverage.fixtures.js +2 -2
- package/dist/cjs/onchain/accounts/intents/tests/finish-withdraw.fixtures.js +2 -2
- package/dist/cjs/onchain/accounts/intents/tests/withdraw-asset.fixtures.js +4 -4
- package/dist/cjs/onchain/accounts/intents/tests/withdraw.fixtures.js +7 -7
- package/dist/cjs/onchain/accounts/intents/utils/adjust-state-to-snapshot.js +8 -5
- package/dist/cjs/onchain/accounts/intents/utils/common.js +2 -2
- package/dist/cjs/onchain/accounts/intents/utils/credit-account-slice.js +4 -4
- package/dist/cjs/onchain/accounts/intents/view.js +2 -2
- package/dist/cjs/onchain/market/MarketSuite.js +15 -0
- package/dist/cjs/onchain/market/math.js +6 -2
- package/dist/cjs/onchain/pools/PoolService.js +25 -29
- package/dist/cjs/preview/index.js +3 -1
- package/dist/cjs/preview/preview/buildDelayedPreview.js +4 -4
- package/dist/cjs/preview/preview/previewAdjustCreditAccount.js +3 -3
- package/dist/cjs/preview/preview/previewCloseOrRepayCreditAccount.js +1 -1
- package/dist/cjs/preview/preview/previewOpenCreditAccount.js +7 -7
- package/dist/cjs/preview/validate/checkOperation.js +47 -37
- package/dist/cjs/preview/validate/checkSimulation.js +3 -16
- package/dist/cjs/preview/validate/index.js +3 -1
- package/dist/cjs/sdk/execute/ExecuteApi.js +9 -6
- package/dist/cjs/sdk/prepare/PrepareApi.js +4 -1
- package/dist/esm/onchain/accounts/intents/maxWithdrawCollateral.js +2 -2
- package/dist/esm/onchain/accounts/intents/open-strategy.js +7 -4
- package/dist/esm/onchain/accounts/intents/realize.js +10 -6
- package/dist/esm/onchain/accounts/intents/tail.js +3 -2
- package/dist/esm/onchain/accounts/intents/testing/market.js +1 -1
- package/dist/esm/onchain/accounts/intents/testing/sdk-mock.js +18 -1
- package/dist/esm/onchain/accounts/intents/tests/add-collateral.fixtures.js +4 -4
- package/dist/esm/onchain/accounts/intents/tests/adjust-leverage.fixtures.js +15 -15
- package/dist/esm/onchain/accounts/intents/tests/deposit.fixtures.js +13 -13
- package/dist/esm/onchain/accounts/intents/tests/finish-decrease-leverage.fixtures.js +2 -2
- package/dist/esm/onchain/accounts/intents/tests/finish-withdraw.fixtures.js +2 -2
- package/dist/esm/onchain/accounts/intents/tests/withdraw-asset.fixtures.js +4 -4
- package/dist/esm/onchain/accounts/intents/tests/withdraw.fixtures.js +7 -7
- package/dist/esm/onchain/accounts/intents/utils/adjust-state-to-snapshot.js +8 -5
- package/dist/esm/onchain/accounts/intents/utils/common.js +2 -2
- package/dist/esm/onchain/accounts/intents/utils/credit-account-slice.js +4 -4
- package/dist/esm/onchain/accounts/intents/view.js +2 -2
- package/dist/esm/onchain/market/MarketSuite.js +15 -0
- package/dist/esm/onchain/market/math.js +6 -2
- package/dist/esm/onchain/pools/PoolService.js +25 -29
- package/dist/esm/preview/index.js +2 -2
- package/dist/esm/preview/preview/buildDelayedPreview.js +4 -4
- package/dist/esm/preview/preview/previewAdjustCreditAccount.js +3 -3
- package/dist/esm/preview/preview/previewCloseOrRepayCreditAccount.js +1 -1
- package/dist/esm/preview/preview/previewOpenCreditAccount.js +7 -7
- package/dist/esm/preview/validate/checkOperation.js +45 -37
- package/dist/esm/preview/validate/checkSimulation.js +5 -18
- package/dist/esm/preview/validate/index.js +2 -2
- package/dist/esm/sdk/execute/ExecuteApi.js +9 -6
- package/dist/esm/sdk/prepare/PrepareApi.js +4 -1
- package/dist/types/model/index.d.ts +2 -2
- package/dist/types/model/previews.d.ts +59 -109
- package/dist/types/onchain/accounts/intents/open-strategy.d.ts +15 -29
- package/dist/types/onchain/accounts/intents/testing/expect.d.ts +6 -2
- package/dist/types/onchain/accounts/intents/testing/market.d.ts +1 -1
- package/dist/types/onchain/accounts/intents/testing/sdk-mock.d.ts +1 -1
- package/dist/types/onchain/accounts/intents/tests/add-collateral.fixtures.d.ts +1 -1
- package/dist/types/onchain/accounts/intents/tests/adjust-leverage.fixtures.d.ts +2 -2
- package/dist/types/onchain/accounts/intents/tests/deposit.fixtures.d.ts +2 -2
- package/dist/types/onchain/accounts/intents/tests/withdraw-asset.fixtures.d.ts +1 -1
- package/dist/types/onchain/accounts/intents/tests/withdraw.fixtures.d.ts +2 -2
- package/dist/types/onchain/accounts/intents/types.d.ts +8 -59
- package/dist/types/onchain/accounts/intents/utils/adjust-state-to-snapshot.d.ts +2 -2
- package/dist/types/onchain/accounts/intents/utils/credit-account-slice.d.ts +3 -3
- package/dist/types/onchain/market/MarketSuite.d.ts +13 -1
- package/dist/types/onchain/market/math.d.ts +5 -1
- package/dist/types/onchain/pools/types.d.ts +6 -4
- package/dist/types/preview/index.d.ts +2 -2
- package/dist/types/preview/preview/buildDelayedPreview.d.ts +1 -1
- package/dist/types/preview/validate/checkOperation.d.ts +24 -7
- package/dist/types/preview/validate/index.d.ts +2 -2
- package/package.json +1 -1
|
@@ -31,7 +31,7 @@ function maxWithdrawCollateral(props) {
|
|
|
31
31
|
const underlying = market.pool.underlying;
|
|
32
32
|
const target = creditAccount.tokens.find((t) => require_onchain_accounts_intents_utils_common.eq(t.token, token));
|
|
33
33
|
if (!target || target.balance <= 10n) return 0n;
|
|
34
|
-
if (creditAccount.
|
|
34
|
+
if (creditAccount.totalDebt === 0n) return target.balance;
|
|
35
35
|
const masked = creditAccount.enabledTokensMask !== 0n;
|
|
36
36
|
const counts = (t) => t.balance > 10n && (!masked || (t.mask & creditAccount.enabledTokensMask) !== 0n);
|
|
37
37
|
/** What a holding backs, in the check's units: USD × PERCENTAGE_FACTOR. */
|
|
@@ -48,7 +48,7 @@ function maxWithdrawCollateral(props) {
|
|
|
48
48
|
if (require_onchain_accounts_intents_utils_common.eq(t.token, token) || !counts(t)) continue;
|
|
49
49
|
otherMoney += weigh(t);
|
|
50
50
|
}
|
|
51
|
-
const borrowed = usd(priceOracle, underlying, creditAccount.
|
|
51
|
+
const borrowed = usd(priceOracle, underlying, creditAccount.totalDebt);
|
|
52
52
|
if (borrowed === void 0 || borrowed <= 0n) return 0n;
|
|
53
53
|
const required = borrowed * targetHF;
|
|
54
54
|
if (required <= otherMoney) return target.balance;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_onchain_market_math = require("../../market/math.js");
|
|
2
3
|
const require_onchain_validation_refusal = require("../../validation/refusal.js");
|
|
3
4
|
const require_onchain_accounts_intents_guards = require("./guards.js");
|
|
4
5
|
const require_onchain_accounts_intents_math = require("./math.js");
|
|
@@ -38,7 +39,7 @@ async function previewOpenStrategy(props) {
|
|
|
38
39
|
underlying,
|
|
39
40
|
enabledTokensMask: 0n,
|
|
40
41
|
totalDebtUSD: 0n,
|
|
41
|
-
|
|
42
|
+
totalDebt: 0n,
|
|
42
43
|
tokens: []
|
|
43
44
|
};
|
|
44
45
|
require_onchain_accounts_intents_math.assertDebtInBand(sdk, debt, suite.creditFacade, underlying);
|
|
@@ -88,6 +89,7 @@ async function previewOpenStrategy(props) {
|
|
|
88
89
|
const projectedPool = { availableLiquidityChange: -debt };
|
|
89
90
|
const metrics = {
|
|
90
91
|
healthFactor: sdk.positions.healthFactor(snapshot),
|
|
92
|
+
safeHealthFactor: sdk.positions.healthFactor(snapshot, { safePrices: true }),
|
|
91
93
|
borrowRate: sdk.positions.borrowRate(snapshot, projectedPool),
|
|
92
94
|
timeToLiquidation: sdk.positions.timeToLiquidation(snapshot, projectedPool),
|
|
93
95
|
liquidationPrice: sdk.positions.liquidationPrice(snapshot)
|
|
@@ -99,9 +101,10 @@ async function previewOpenStrategy(props) {
|
|
|
99
101
|
toUnderlying: (from, amount) => convert(from, underlying, amount)
|
|
100
102
|
});
|
|
101
103
|
return {
|
|
102
|
-
debt,
|
|
103
|
-
|
|
104
|
-
totalValue: margin + debt,
|
|
104
|
+
totalDebt: market.toUnderlyingAmount(debt),
|
|
105
|
+
netValue: market.toUnderlyingAmount(margin),
|
|
106
|
+
totalValue: market.toUnderlyingAmount(margin + debt),
|
|
107
|
+
leverage: require_onchain_market_math.calcPositionLeverage(margin + debt, debt),
|
|
105
108
|
priceImpact,
|
|
106
109
|
averageAssets: averageAssets.map(priced),
|
|
107
110
|
minAssets: minAssets.map(priced),
|
|
@@ -36,7 +36,7 @@ async function realize(steps, props) {
|
|
|
36
36
|
const ledger = new require_onchain_accounts_intents_utils_ledger.OperationLedger({
|
|
37
37
|
initialAssets: creditAccount.tokens,
|
|
38
38
|
underlying,
|
|
39
|
-
debt: creditAccount.
|
|
39
|
+
debt: creditAccount.totalDebt,
|
|
40
40
|
convert: price
|
|
41
41
|
});
|
|
42
42
|
const operations = [];
|
|
@@ -272,7 +272,7 @@ async function realize(steps, props) {
|
|
|
272
272
|
totalDebt: debt,
|
|
273
273
|
totalValue
|
|
274
274
|
};
|
|
275
|
-
const projectedPool = { availableLiquidityChange: creditAccount.
|
|
275
|
+
const projectedPool = { availableLiquidityChange: creditAccount.totalDebt - debt };
|
|
276
276
|
const metrics = {
|
|
277
277
|
healthFactor: sdk.positions.healthFactor(snapshot),
|
|
278
278
|
safeHealthFactor: paysOut ? sdk.positions.healthFactor(snapshot, { safePrices: true }) : void 0,
|
|
@@ -286,12 +286,16 @@ async function realize(steps, props) {
|
|
|
286
286
|
netValue: totalValue - debt,
|
|
287
287
|
toUnderlying: (from, amount) => price(from, underlying, amount)
|
|
288
288
|
});
|
|
289
|
+
const oracle = market.priceOracle;
|
|
289
290
|
const state = {
|
|
290
|
-
totalValue,
|
|
291
|
-
|
|
291
|
+
totalValue: market.toUnderlyingAmount(totalValue),
|
|
292
|
+
totalDebt: market.toUnderlyingAmount(debt),
|
|
292
293
|
leverage: require_onchain_market_math.calcPositionLeverage(totalValue, debt),
|
|
293
|
-
assets: assets.map((a) =>
|
|
294
|
-
quotas: quotasAfter
|
|
294
|
+
assets: assets.map((a) => oracle.toTokenAmount(a.token, a.balance)),
|
|
295
|
+
quotas: Object.values(quotasAfter).map((q) => ({
|
|
296
|
+
token: sdk.tokensMeta.mustGetToken(q.token),
|
|
297
|
+
...oracle.toAmount(underlying, q.balance)
|
|
298
|
+
})),
|
|
295
299
|
priceImpact,
|
|
296
300
|
...metrics
|
|
297
301
|
};
|
|
@@ -103,15 +103,16 @@ function projectedClaimable(request, phantom, phantomAmount, claim) {
|
|
|
103
103
|
*/
|
|
104
104
|
function sliceAfter(creditAccount, after) {
|
|
105
105
|
const masks = new Map(creditAccount.tokens.map((t) => [t.token.toLowerCase(), t.mask]));
|
|
106
|
+
const quotas = new Map(after.quotas.map((q) => [q.token.address.toLowerCase(), q.value]));
|
|
106
107
|
return {
|
|
107
108
|
...creditAccount,
|
|
108
|
-
|
|
109
|
+
totalDebt: after.totalDebt.value,
|
|
109
110
|
tokens: after.assets.map((a) => {
|
|
110
111
|
const token = a.token.address.toLowerCase();
|
|
111
112
|
return {
|
|
112
113
|
token,
|
|
113
114
|
balance: a.value,
|
|
114
|
-
quota:
|
|
115
|
+
quota: quotas.get(token) ?? 0n,
|
|
115
116
|
mask: masks.get(token) ?? 0n,
|
|
116
117
|
success: true
|
|
117
118
|
};
|
|
@@ -144,13 +144,30 @@ function buildMockSdk(args) {
|
|
|
144
144
|
return price;
|
|
145
145
|
};
|
|
146
146
|
const poolPaused = args.poolPaused ?? false;
|
|
147
|
+
/** {@inheritDoc MarketSuite.toUnderlyingAmount} */
|
|
148
|
+
const toUnderlyingAmount = (value) => {
|
|
149
|
+
const usd = safeUsd(args.underlying, value);
|
|
150
|
+
return {
|
|
151
|
+
token: tokenOf(args.rwaAssets?.[args.underlying.toLowerCase()] ?? args.underlying),
|
|
152
|
+
value,
|
|
153
|
+
valueUsd: usd === null ? null : require_onchain_market_math.usdToNumber(usd)
|
|
154
|
+
};
|
|
155
|
+
};
|
|
147
156
|
const market = {
|
|
157
|
+
toUnderlyingAmount,
|
|
148
158
|
priceOracle: {
|
|
149
159
|
convert,
|
|
150
160
|
safeConvert,
|
|
151
161
|
mainPrice: mainPriceOf,
|
|
152
162
|
reservePrice: reservePriceOf,
|
|
153
163
|
convertToUSD: _convertToUSD,
|
|
164
|
+
toAmount: (token, value) => {
|
|
165
|
+
const usd = safeUsd(token, value);
|
|
166
|
+
return {
|
|
167
|
+
value,
|
|
168
|
+
valueUsd: usd === null ? null : require_onchain_market_math.usdToNumber(usd)
|
|
169
|
+
};
|
|
170
|
+
},
|
|
154
171
|
toTokenAmount: (token, value) => {
|
|
155
172
|
const usd = safeUsd(token, value);
|
|
156
173
|
return {
|
|
@@ -349,7 +366,7 @@ function payloadOf(slice) {
|
|
|
349
366
|
owner: slice.creditAccount,
|
|
350
367
|
expirationDate: 0,
|
|
351
368
|
enabledTokensMask: slice.enabledTokensMask,
|
|
352
|
-
debt: slice.
|
|
369
|
+
debt: slice.totalDebt,
|
|
353
370
|
accruedInterest: 0n,
|
|
354
371
|
accruedFees: 0n,
|
|
355
372
|
totalDebtUSD: slice.totalDebtUSD,
|
|
@@ -32,7 +32,7 @@ const case_position_token = {
|
|
|
32
32
|
amount: ADD_ANY
|
|
33
33
|
},
|
|
34
34
|
tokens: [require_market.caToken(require_market.UND, BASE_UND)],
|
|
35
|
-
|
|
35
|
+
totalDebt: DEBT,
|
|
36
36
|
totalValue: BASE_UND + require_market.valueInUnd(ADD_ANY, require_market.ANY),
|
|
37
37
|
ops: [{
|
|
38
38
|
type: "addCollateral",
|
|
@@ -57,7 +57,7 @@ const case_underlying = {
|
|
|
57
57
|
amount: ADD_UND
|
|
58
58
|
},
|
|
59
59
|
tokens: [require_market.caToken(require_market.ANY, ADD_ANY, QUOTA_ANY)],
|
|
60
|
-
|
|
60
|
+
totalDebt: DEBT,
|
|
61
61
|
totalValue: require_market.valueInUnd(ADD_ANY, require_market.ANY) + ADD_UND,
|
|
62
62
|
ops: [{
|
|
63
63
|
type: "addCollateral",
|
|
@@ -74,7 +74,7 @@ const case_rwa_asset = {
|
|
|
74
74
|
amount: ADD_RWA
|
|
75
75
|
},
|
|
76
76
|
tokens: [require_market.caToken(require_market.UND, BASE_UND)],
|
|
77
|
-
|
|
77
|
+
totalDebt: DEBT,
|
|
78
78
|
totalValue: BASE_UND + require_market.valueInUnd(ADD_RWA, require_market.RWA_ASSET),
|
|
79
79
|
ops: [{
|
|
80
80
|
type: "addCollateral",
|
|
@@ -99,7 +99,7 @@ function buildAddCollateralProps(c, sdk) {
|
|
|
99
99
|
return {
|
|
100
100
|
intent: c.intent,
|
|
101
101
|
creditAccount: require_market.buildFixtureCreditAccount({
|
|
102
|
-
|
|
102
|
+
totalDebt: c.totalDebt,
|
|
103
103
|
tokens: c.tokens
|
|
104
104
|
}),
|
|
105
105
|
sdk,
|
|
@@ -37,9 +37,9 @@ const case_increase = {
|
|
|
37
37
|
token: require_market.POS
|
|
38
38
|
},
|
|
39
39
|
tokens: [require_market.caToken(require_market.POS, TVL_2X, QUOTA_1000)],
|
|
40
|
-
|
|
40
|
+
totalDebt: DEBT_2X,
|
|
41
41
|
totalValue: TVL_3X,
|
|
42
|
-
|
|
42
|
+
totalDebtAfter: DEBT_3X,
|
|
43
43
|
ops: [
|
|
44
44
|
{
|
|
45
45
|
type: "increaseDebt",
|
|
@@ -73,9 +73,9 @@ const case_increase_underlying = {
|
|
|
73
73
|
token: require_market.UND
|
|
74
74
|
},
|
|
75
75
|
tokens: [require_market.caToken(require_market.UND, TVL_2X)],
|
|
76
|
-
|
|
76
|
+
totalDebt: DEBT_2X,
|
|
77
77
|
totalValue: TVL_3X,
|
|
78
|
-
|
|
78
|
+
totalDebtAfter: DEBT_3X,
|
|
79
79
|
ops: [{
|
|
80
80
|
type: "increaseDebt",
|
|
81
81
|
amount: STEP
|
|
@@ -89,9 +89,9 @@ const case_increase_rwa = {
|
|
|
89
89
|
token: require_market.RWA_ASSET
|
|
90
90
|
},
|
|
91
91
|
tokens: [require_market.caToken(require_market.RWA_ASSET, TVL_2X, QUOTA_1000)],
|
|
92
|
-
|
|
92
|
+
totalDebt: DEBT_2X,
|
|
93
93
|
totalValue: TVL_3X,
|
|
94
|
-
|
|
94
|
+
totalDebtAfter: DEBT_3X,
|
|
95
95
|
ops: [
|
|
96
96
|
{
|
|
97
97
|
type: "increaseDebt",
|
|
@@ -124,9 +124,9 @@ const case_decrease = {
|
|
|
124
124
|
token: require_market.POS
|
|
125
125
|
},
|
|
126
126
|
tokens: [require_market.caToken(require_market.POS, TVL_3X, QUOTA_1500)],
|
|
127
|
-
|
|
127
|
+
totalDebt: DEBT_3X,
|
|
128
128
|
totalValue: TVL_2X,
|
|
129
|
-
|
|
129
|
+
totalDebtAfter: DEBT_2X,
|
|
130
130
|
ops: [
|
|
131
131
|
{
|
|
132
132
|
type: "swap",
|
|
@@ -160,9 +160,9 @@ const case_decrease_from_idle_underlying = {
|
|
|
160
160
|
token: require_market.POS
|
|
161
161
|
},
|
|
162
162
|
tokens: [require_market.caToken(require_market.POS, TVL_2X, QUOTA_1000), require_market.caToken(require_market.UND, STEP)],
|
|
163
|
-
|
|
163
|
+
totalDebt: DEBT_3X,
|
|
164
164
|
totalValue: TVL_2X,
|
|
165
|
-
|
|
165
|
+
totalDebtAfter: DEBT_2X,
|
|
166
166
|
ops: [{
|
|
167
167
|
type: "decreaseDebt",
|
|
168
168
|
amount: STEP
|
|
@@ -176,9 +176,9 @@ const case_decrease_rwa = {
|
|
|
176
176
|
token: require_market.RWA_ASSET
|
|
177
177
|
},
|
|
178
178
|
tokens: [require_market.caToken(require_market.RWA_ASSET, TVL_3X, QUOTA_1500)],
|
|
179
|
-
|
|
179
|
+
totalDebt: DEBT_3X,
|
|
180
180
|
totalValue: TVL_2X,
|
|
181
|
-
|
|
181
|
+
totalDebtAfter: DEBT_2X,
|
|
182
182
|
ops: [
|
|
183
183
|
{
|
|
184
184
|
type: "wrapRwaCollateral",
|
|
@@ -211,9 +211,9 @@ const case_noop = {
|
|
|
211
211
|
token: require_market.POS
|
|
212
212
|
},
|
|
213
213
|
tokens: [require_market.caToken(require_market.POS, TVL_2X, QUOTA_1000)],
|
|
214
|
-
|
|
214
|
+
totalDebt: DEBT_2X,
|
|
215
215
|
totalValue: TVL_2X,
|
|
216
|
-
|
|
216
|
+
totalDebtAfter: DEBT_2X,
|
|
217
217
|
ops: []
|
|
218
218
|
};
|
|
219
219
|
function buildAdjustLeverageSdk(c) {
|
|
@@ -223,7 +223,7 @@ function buildAdjustLeverageProps(c, sdk) {
|
|
|
223
223
|
return {
|
|
224
224
|
intent: c.intent,
|
|
225
225
|
creditAccount: require_market.buildFixtureCreditAccount({
|
|
226
|
-
|
|
226
|
+
totalDebt: c.totalDebt,
|
|
227
227
|
tokens: c.tokens
|
|
228
228
|
}),
|
|
229
229
|
sdk,
|
|
@@ -32,9 +32,9 @@ const case_fixed_leverage = {
|
|
|
32
32
|
positionToken: require_market.POS
|
|
33
33
|
},
|
|
34
34
|
tokens: [require_market.caToken(require_market.POS, P1000, QUOTA_1000)],
|
|
35
|
-
|
|
35
|
+
totalDebt: DEBT_START,
|
|
36
36
|
totalValue: P2000,
|
|
37
|
-
|
|
37
|
+
totalDebtAfter: P1000,
|
|
38
38
|
ops: [
|
|
39
39
|
{
|
|
40
40
|
type: "addCollateral",
|
|
@@ -76,9 +76,9 @@ const case_target_leverage = {
|
|
|
76
76
|
targetLeverage: LEV_3X
|
|
77
77
|
},
|
|
78
78
|
tokens: [require_market.caToken(require_market.POS, P1000, QUOTA_1000)],
|
|
79
|
-
|
|
79
|
+
totalDebt: DEBT_START,
|
|
80
80
|
totalValue: P3000,
|
|
81
|
-
|
|
81
|
+
totalDebtAfter: P2000,
|
|
82
82
|
ops: [
|
|
83
83
|
{
|
|
84
84
|
type: "addCollateral",
|
|
@@ -119,9 +119,9 @@ const case_position_is_underlying = {
|
|
|
119
119
|
positionToken: require_market.UND
|
|
120
120
|
},
|
|
121
121
|
tokens: [require_market.caToken(require_market.UND, P1000)],
|
|
122
|
-
|
|
122
|
+
totalDebt: DEBT_START,
|
|
123
123
|
totalValue: P2000,
|
|
124
|
-
|
|
124
|
+
totalDebtAfter: P1000,
|
|
125
125
|
ops: [{
|
|
126
126
|
type: "addCollateral",
|
|
127
127
|
token: require_market.UND,
|
|
@@ -141,9 +141,9 @@ const case_rwa_collateral = {
|
|
|
141
141
|
positionToken: require_market.POS
|
|
142
142
|
},
|
|
143
143
|
tokens: [require_market.caToken(require_market.POS, P1000, QUOTA_1000)],
|
|
144
|
-
|
|
144
|
+
totalDebt: DEBT_START,
|
|
145
145
|
totalValue: P2000,
|
|
146
|
-
|
|
146
|
+
totalDebtAfter: P1000,
|
|
147
147
|
ops: [
|
|
148
148
|
{
|
|
149
149
|
type: "addCollateral",
|
|
@@ -195,9 +195,9 @@ const case_rwa_position = {
|
|
|
195
195
|
positionToken: require_market.RWA_ASSET
|
|
196
196
|
},
|
|
197
197
|
tokens: [require_market.caToken(require_market.RWA_ASSET, P1000, QUOTA_1000)],
|
|
198
|
-
|
|
198
|
+
totalDebt: DEBT_START,
|
|
199
199
|
totalValue: P2000,
|
|
200
|
-
|
|
200
|
+
totalDebtAfter: P1000,
|
|
201
201
|
ops: [
|
|
202
202
|
{
|
|
203
203
|
type: "addCollateral",
|
|
@@ -250,9 +250,9 @@ const case_native_coin = {
|
|
|
250
250
|
value: NATIVE_VALUE
|
|
251
251
|
},
|
|
252
252
|
tokens: [require_market.caToken(require_market.POS, M32_BALANCE, quotaOf(M32_BALANCE))],
|
|
253
|
-
|
|
253
|
+
totalDebt: M32_DEBT,
|
|
254
254
|
totalValue: 1500000000n,
|
|
255
|
-
|
|
255
|
+
totalDebtAfter: 1200000000n,
|
|
256
256
|
ops: [
|
|
257
257
|
{
|
|
258
258
|
type: "addCollateral",
|
|
@@ -294,7 +294,7 @@ function buildDepositProps(c, sdk) {
|
|
|
294
294
|
return {
|
|
295
295
|
intent: c.intent,
|
|
296
296
|
creditAccount: require_market.buildFixtureCreditAccount({
|
|
297
|
-
|
|
297
|
+
totalDebt: c.totalDebt,
|
|
298
298
|
tokens: c.tokens
|
|
299
299
|
}),
|
|
300
300
|
sdk,
|
|
@@ -50,7 +50,7 @@ function buildDecreaseTailProps(args) {
|
|
|
50
50
|
underlying: require_market.UND,
|
|
51
51
|
enabledTokensMask: 0n,
|
|
52
52
|
totalDebtUSD: 0n,
|
|
53
|
-
|
|
53
|
+
totalDebt: DECREASE_PRE_D,
|
|
54
54
|
tokens: args.tokens ?? [{
|
|
55
55
|
token: "0xb1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1",
|
|
56
56
|
balance: args.claimedAmount,
|
|
@@ -181,7 +181,7 @@ function buildMatrixDecreaseTailProps(c) {
|
|
|
181
181
|
underlying: require_market.UND,
|
|
182
182
|
enabledTokensMask: 0n,
|
|
183
183
|
totalDebtUSD: 0n,
|
|
184
|
-
|
|
184
|
+
totalDebt: M7_DEBT,
|
|
185
185
|
tokens: [{
|
|
186
186
|
token: require_market.POS,
|
|
187
187
|
balance: M7_POS_LEFT,
|
|
@@ -64,7 +64,7 @@ function buildWithdrawFinishProps(args) {
|
|
|
64
64
|
underlying: require_market.UND,
|
|
65
65
|
enabledTokensMask: 0n,
|
|
66
66
|
totalDebtUSD: 0n,
|
|
67
|
-
|
|
67
|
+
totalDebt: WITHDRAW_PRE_D,
|
|
68
68
|
tokens: args.tokens ?? [
|
|
69
69
|
{
|
|
70
70
|
token: "0x1111111111111111111111111111111111111111",
|
|
@@ -309,7 +309,7 @@ function buildMatrixWithdrawTailProps(c) {
|
|
|
309
309
|
underlying: require_market.UND,
|
|
310
310
|
enabledTokensMask: 0n,
|
|
311
311
|
totalDebtUSD: 0n,
|
|
312
|
-
|
|
312
|
+
totalDebt: M4_DEBT,
|
|
313
313
|
tokens: [{
|
|
314
314
|
token: require_market.POS,
|
|
315
315
|
balance: M4_SPEND,
|
|
@@ -36,7 +36,7 @@ const case_any_token = {
|
|
|
36
36
|
to: require_market.WALLET
|
|
37
37
|
},
|
|
38
38
|
tokens: [require_market.caToken(require_market.UND, HELD_UND), require_market.caToken(require_market.ANY, HELD_ANY, QUOTA_ANY_BEFORE)],
|
|
39
|
-
|
|
39
|
+
totalDebt: DEBT,
|
|
40
40
|
totalValue: HELD_UND + require_market.valueInUnd(HELD_ANY - OUT_ANY, require_market.ANY),
|
|
41
41
|
ops: [{
|
|
42
42
|
type: "withdrawCollateral",
|
|
@@ -62,7 +62,7 @@ const case_underlying = {
|
|
|
62
62
|
to: require_market.WALLET
|
|
63
63
|
},
|
|
64
64
|
tokens: [require_market.caToken(require_market.UND, HELD_UND)],
|
|
65
|
-
|
|
65
|
+
totalDebt: DEBT,
|
|
66
66
|
totalValue: HELD_UND - OUT_UND,
|
|
67
67
|
ops: [{
|
|
68
68
|
type: "withdrawCollateral",
|
|
@@ -83,7 +83,7 @@ const case_rwa_underlying = {
|
|
|
83
83
|
to: require_market.WALLET
|
|
84
84
|
},
|
|
85
85
|
tokens: [require_market.caToken(require_market.UND, HELD_UND)],
|
|
86
|
-
|
|
86
|
+
totalDebt: DEBT,
|
|
87
87
|
totalValue: HELD_UND - OUT_UND,
|
|
88
88
|
ops: [{
|
|
89
89
|
type: "unwrapRwaCollateral",
|
|
@@ -106,7 +106,7 @@ function buildWithdrawAssetProps(c, sdk) {
|
|
|
106
106
|
return {
|
|
107
107
|
intent: c.intent,
|
|
108
108
|
creditAccount: require_market.buildFixtureCreditAccount({
|
|
109
|
-
|
|
109
|
+
totalDebt: c.totalDebt,
|
|
110
110
|
tokens: c.tokens
|
|
111
111
|
}),
|
|
112
112
|
sdk,
|
|
@@ -27,9 +27,9 @@ const QUOTA_AFTER = quotaOf(TVL_AFTER);
|
|
|
27
27
|
/** Quota ops carry signed deltas. */
|
|
28
28
|
const QUOTA_DELTA = QUOTA_AFTER - QUOTA_BEFORE;
|
|
29
29
|
const base = {
|
|
30
|
-
|
|
30
|
+
totalDebt: DEBT_BEFORE,
|
|
31
31
|
totalValue: TVL_AFTER,
|
|
32
|
-
|
|
32
|
+
totalDebtAfter: DEBT_AFTER
|
|
33
33
|
};
|
|
34
34
|
/** Row 1 — S = U, T = U: repay, then hand over the underlying. */
|
|
35
35
|
const case_und_und = {
|
|
@@ -291,9 +291,9 @@ const case_matrix_4_1 = {
|
|
|
291
291
|
sourceToken: require_market.POS
|
|
292
292
|
},
|
|
293
293
|
tokens: [require_market.caToken(require_market.POS, M4_BALANCE, quotaOf(M4_BALANCE))],
|
|
294
|
-
|
|
294
|
+
totalDebt: M4_DEBT,
|
|
295
295
|
totalValue: M4_SPEND,
|
|
296
|
-
|
|
296
|
+
totalDebtAfter: M4_DD,
|
|
297
297
|
ops: [
|
|
298
298
|
{
|
|
299
299
|
type: "swap",
|
|
@@ -342,9 +342,9 @@ const case_matrix_4_2 = {
|
|
|
342
342
|
sourceToken: require_market.POS
|
|
343
343
|
},
|
|
344
344
|
tokens: [require_market.caToken(require_market.POS, M4_BALANCE, quotaOf(M4_BALANCE))],
|
|
345
|
-
|
|
345
|
+
totalDebt: M4_DEBT,
|
|
346
346
|
totalValue: M4_SPEND,
|
|
347
|
-
|
|
347
|
+
totalDebtAfter: M4_DD,
|
|
348
348
|
ops: [
|
|
349
349
|
{
|
|
350
350
|
type: "swap",
|
|
@@ -391,7 +391,7 @@ function buildWithdrawProps(c, sdk) {
|
|
|
391
391
|
return {
|
|
392
392
|
intent: c.intent,
|
|
393
393
|
creditAccount: require_market.buildFixtureCreditAccount({
|
|
394
|
-
|
|
394
|
+
totalDebt: c.totalDebt,
|
|
395
395
|
tokens: c.tokens
|
|
396
396
|
}),
|
|
397
397
|
sdk,
|
|
@@ -2,8 +2,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
//#region src/onchain/accounts/intents/utils/adjust-state-to-snapshot.ts
|
|
3
3
|
/**
|
|
4
4
|
* Maps an intents {@link OperationState} onto the {@link AccountSnapshot} that
|
|
5
|
-
* position-metric functions take
|
|
6
|
-
*
|
|
5
|
+
* position-metric functions take: the state prices what it reports, a snapshot
|
|
6
|
+
* only names it.
|
|
7
7
|
**/
|
|
8
8
|
function adjustStateToSnapshot(creditManager, state) {
|
|
9
9
|
return {
|
|
@@ -12,9 +12,12 @@ function adjustStateToSnapshot(creditManager, state) {
|
|
|
12
12
|
token: a.token.address,
|
|
13
13
|
balance: a.value
|
|
14
14
|
})),
|
|
15
|
-
quotas:
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
quotas: state.quotas.map((q) => ({
|
|
16
|
+
token: q.token.address,
|
|
17
|
+
balance: q.value
|
|
18
|
+
})),
|
|
19
|
+
totalDebt: state.totalDebt.value,
|
|
20
|
+
totalValue: state.totalValue.value
|
|
18
21
|
};
|
|
19
22
|
}
|
|
20
23
|
//#endregion
|
|
@@ -15,7 +15,7 @@ function toTargetDecimals(fromAmount, fromToken, toToken, sdk) {
|
|
|
15
15
|
* with balance from the projection (0n when absent), new tokens get quota 0n.
|
|
16
16
|
*/
|
|
17
17
|
function toRouterCaSlice(creditAccount, expectedBalances = []) {
|
|
18
|
-
const {
|
|
18
|
+
const { totalDebt, underlying, creditFacade, creditManager, creditAccount: creditAccountAddress, tokens: _, ...restCA } = creditAccount;
|
|
19
19
|
const expected = new require_onchain_utils_AddressMap.AddressMap([]);
|
|
20
20
|
for (const asset of expectedBalances ?? []) expected.upsert(asset.token, (expected.get(asset.token) ?? 0n) + asset.balance);
|
|
21
21
|
const tokens = creditAccount.tokens.map((t) => {
|
|
@@ -33,7 +33,7 @@ function toRouterCaSlice(creditAccount, expectedBalances = []) {
|
|
|
33
33
|
creditAccount: creditAccountAddress.toLowerCase(),
|
|
34
34
|
creditFacade: creditFacade.toLowerCase(),
|
|
35
35
|
creditManager: creditManager.toLowerCase(),
|
|
36
|
-
debt: creditAccount.
|
|
36
|
+
debt: creditAccount.totalDebt,
|
|
37
37
|
tokens
|
|
38
38
|
};
|
|
39
39
|
}
|
|
@@ -4,9 +4,9 @@ const require_onchain_accounts_intents_utils_borrowed_amount_plus_interest_and_f
|
|
|
4
4
|
/**
|
|
5
5
|
* Narrows full account data down to the slice the intent engine operates on.
|
|
6
6
|
*
|
|
7
|
-
* `
|
|
8
|
-
* accrued fees) rather than
|
|
9
|
-
* settles the full outstanding amount.
|
|
7
|
+
* `totalDebt` is the total repayable debt (principal + accrued interest +
|
|
8
|
+
* accrued fees) rather than `CreditAccountData.debt`'s principal, because every
|
|
9
|
+
* debt-touching intent settles the full outstanding amount.
|
|
10
10
|
*
|
|
11
11
|
* Addresses are lowercased so that `eq` comparisons and `AddressMap` lookups
|
|
12
12
|
* behave consistently everywhere downstream.
|
|
@@ -19,7 +19,7 @@ function toCreditAccountSlice(ca) {
|
|
|
19
19
|
underlying: ca.underlying.toLowerCase(),
|
|
20
20
|
enabledTokensMask: ca.enabledTokensMask,
|
|
21
21
|
totalDebtUSD: ca.totalDebtUSD,
|
|
22
|
-
|
|
22
|
+
totalDebt: require_onchain_accounts_intents_utils_borrowed_amount_plus_interest_and_fees.calcBorrowedAmountPlusInterestAndFees(ca),
|
|
23
23
|
tokens: ca.tokens.map((t) => ({
|
|
24
24
|
...t,
|
|
25
25
|
token: t.token.toLowerCase()
|
|
@@ -20,8 +20,8 @@ function accountView(creditAccount, sdk) {
|
|
|
20
20
|
underlying,
|
|
21
21
|
sdk,
|
|
22
22
|
rwaAsset: sdk.tokensMeta.rwaUnderlyings.get(underlying)?.asset,
|
|
23
|
-
debt: creditAccount.
|
|
24
|
-
collateral: totalValue - creditAccount.
|
|
23
|
+
debt: creditAccount.totalDebt,
|
|
24
|
+
collateral: totalValue - creditAccount.totalDebt,
|
|
25
25
|
band: {
|
|
26
26
|
minDebt: creditFacade.minDebt,
|
|
27
27
|
maxDebt: creditFacade.maxDebt
|
|
@@ -128,6 +128,21 @@ var MarketSuite = class extends require_onchain_base_SDKConstruct.SDKConstruct {
|
|
|
128
128
|
};
|
|
129
129
|
}
|
|
130
130
|
/**
|
|
131
|
+
* Prices a figure already denominated in this market's underlying — a debt,
|
|
132
|
+
* a TVL, a payout — as the read model reports one.
|
|
133
|
+
*
|
|
134
|
+
* The token it names is {@link underlyingToken}, so an amount coming out of a
|
|
135
|
+
* preview or a simulation carries the same identity as the one on a
|
|
136
|
+
* `StrategyPosition`: USDC on an RWA market, not the dcUSDC wrapper the pool
|
|
137
|
+
* actually holds. The two convert one-for-one, so the figure is exact either
|
|
138
|
+
* way; only the label differs, and a caller showing both side by side must
|
|
139
|
+
* not see two.
|
|
140
|
+
**/
|
|
141
|
+
toUnderlyingAmount = (value) => ({
|
|
142
|
+
token: this.underlyingToken,
|
|
143
|
+
...this.priceOracle.toAmount(this.underlying, value)
|
|
144
|
+
});
|
|
145
|
+
/**
|
|
131
146
|
* Display name of this market's pool, e.g. `"USDC Pool"`.
|
|
132
147
|
*/
|
|
133
148
|
get poolName() {
|
|
@@ -43,16 +43,20 @@ const USD_DUST_THRESHOLD = 1000n;
|
|
|
43
43
|
/**
|
|
44
44
|
* Converts a USD value in the oracle's 8-decimal fixed point to a float.
|
|
45
45
|
*
|
|
46
|
-
* Values below {@link USD_DUST_THRESHOLD} report as `0`.
|
|
46
|
+
* Values whose magnitude is below {@link USD_DUST_THRESHOLD} report as `0`.
|
|
47
|
+
* The threshold is weighed on the magnitude because the deltas the model
|
|
48
|
+
* carries — a debt repaid, a balance sold — are negative, and dust is dust in
|
|
49
|
+
* either direction.
|
|
47
50
|
*
|
|
48
51
|
* @example
|
|
49
52
|
* ```ts
|
|
50
53
|
* // usd: $1500.50 in 8-decimal fixed point
|
|
51
54
|
* usdToNumber(150_050_000_000n) // 1500.5
|
|
55
|
+
* usdToNumber(-150_050_000_000n) // -1500.5
|
|
52
56
|
* ```
|
|
53
57
|
**/
|
|
54
58
|
function usdToNumber(usd) {
|
|
55
|
-
return usd < USD_DUST_THRESHOLD ? 0 : Number(usd) / Number(require_onchain_constants_math.PRICE_DECIMALS);
|
|
59
|
+
return (usd < 0n ? -usd : usd) < USD_DUST_THRESHOLD ? 0 : Number(usd) / Number(require_onchain_constants_math.PRICE_DECIMALS);
|
|
56
60
|
}
|
|
57
61
|
/**
|
|
58
62
|
* Share of capital currently borrowed, in basis points. Returns `0` when there
|