@gearbox-protocol/sdk 14.12.0-next.66 → 14.12.0-next.67
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/index.js +2 -2
- package/dist/cjs/common-utils/utils/apy/calculate-safe-borrow-rate.js +3 -3
- package/dist/cjs/common-utils/utils/assets-math.js +7 -7
- package/dist/cjs/common-utils/utils/creditAccount/calc-health-factor.js +2 -2
- package/dist/cjs/common-utils/utils/creditAccount/debt.js +2 -2
- package/dist/cjs/common-utils/utils/creditAccount/quota-utils.js +5 -5
- package/dist/cjs/common-utils/utils/index.js +2 -2
- package/dist/cjs/common-utils/utils/strategies/leverage/calculate-max-leverage-factor.js +2 -2
- package/dist/cjs/common-utils/utils/strategies/leverage/calculate-max-strategy-debt.js +5 -5
- package/dist/cjs/common-utils/utils/strategies/strategy-info/calculate-total-points.js +2 -2
- package/dist/cjs/common-utils/utils/strategies/strategy-info/get-cm-you-can-earn.js +2 -2
- package/dist/cjs/common-utils/utils/validation/validate-open-account-pool-status.js +4 -4
- package/dist/cjs/model/history.js +28 -0
- package/dist/cjs/model/history.schema.js +84 -0
- package/dist/cjs/model/index.js +64 -0
- package/dist/cjs/model/liquidations.js +23 -0
- package/dist/cjs/model/liquidations.schema.js +91 -0
- package/dist/cjs/model/opportunities.js +55 -0
- package/dist/cjs/model/opportunities.schema.js +220 -0
- package/dist/cjs/model/package.json +1 -0
- package/dist/cjs/model/positions.js +1 -0
- package/dist/cjs/model/primitives.js +1 -0
- package/dist/cjs/model/primitives.schema.js +85 -0
- package/dist/cjs/new-sdk/AbstractNamespace.js +158 -0
- package/dist/cjs/new-sdk/GearboxSDK.js +122 -0
- package/dist/cjs/new-sdk/index.js +11 -0
- package/dist/cjs/new-sdk/opportunities/OpportunitiesNamespace.js +122 -0
- package/dist/cjs/new-sdk/opportunities/index.js +4 -0
- package/dist/cjs/new-sdk/opportunities/types.js +1 -0
- package/dist/cjs/new-sdk/package.json +1 -0
- package/dist/cjs/new-sdk/types.js +22 -0
- package/dist/cjs/new-sdk/utils/history.js +1 -0
- package/dist/cjs/new-sdk/utils/index.js +2 -0
- package/dist/cjs/offchain/GearboxAPI.js +33 -0
- package/dist/cjs/offchain/index.js +8 -0
- package/dist/cjs/offchain/opportunities/OffchainOpportunities.js +97 -0
- package/dist/cjs/offchain/opportunities/index.js +4 -0
- package/dist/cjs/offchain/package.json +1 -0
- package/dist/cjs/offchain/types.js +1 -0
- package/dist/cjs/preview/preview/buildDelayedPreview.js +5 -5
- package/dist/cjs/rewards/rewards/api.js +2 -2
- package/dist/cjs/rewards/rewards/extra-apy.js +2 -2
- package/dist/cjs/sdk/MultichainSDK.js +18 -2
- package/dist/cjs/sdk/OnchainSDK.js +8 -1
- package/dist/cjs/sdk/accounts/index.js +2 -0
- package/dist/cjs/sdk/accounts/liquidations/LiquidationsService.js +150 -106
- package/dist/cjs/sdk/accounts/liquidations/MultichainLiquidationsService.js +32 -38
- package/dist/cjs/sdk/accounts/liquidations/constants.js +13 -0
- package/dist/cjs/sdk/accounts/liquidations/index.js +2 -0
- package/dist/cjs/sdk/base/MultichainConstruct.js +80 -0
- package/dist/cjs/sdk/base/TokensMeta.js +51 -0
- package/dist/cjs/sdk/base/index.js +2 -0
- package/dist/cjs/sdk/chain/chains.js +76 -0
- package/dist/cjs/sdk/chain/index.js +4 -0
- package/dist/cjs/sdk/core/errors.js +13 -0
- package/dist/cjs/sdk/core/index.js +2 -1
- package/dist/cjs/sdk/index.js +25 -1
- package/dist/cjs/sdk/market/MarketConfiguratorContract.js +14 -0
- package/dist/cjs/sdk/market/MarketSuite.js +163 -0
- package/dist/cjs/sdk/market/credit/CreditManagerV310Contract.js +26 -0
- package/dist/cjs/sdk/market/credit/CreditSuite.js +102 -0
- package/dist/cjs/sdk/market/math.js +104 -0
- package/dist/cjs/sdk/market/oracle/PriceOracleBaseContract.js +52 -0
- package/dist/cjs/sdk/market/pool/LinearInterestRateModelContract.js +30 -0
- package/dist/cjs/sdk/market/pool/PoolQuotaKeeperV310Contract.js +26 -0
- package/dist/cjs/sdk/market/pool/PoolSuite.js +28 -0
- package/dist/cjs/sdk/market/pool/PoolV310Contract.js +27 -0
- package/dist/cjs/sdk/market/pool/math.js +44 -0
- package/dist/cjs/sdk/market/pricefeeds/AbstractPriceFeed.js +17 -0
- package/dist/cjs/sdk/opportunities/MultichainOpportunitiesService.js +60 -0
- package/dist/cjs/sdk/opportunities/OpportunitiesService.js +103 -0
- package/dist/cjs/sdk/opportunities/index.js +12 -0
- package/dist/cjs/sdk/types/multichain.js +1 -0
- package/dist/cjs/{common-utils → sdk}/utils/bigint-math.js +11 -11
- package/dist/cjs/sdk/utils/index.js +3 -0
- package/dist/cjs/sdk/utils/zod.js +12 -0
- package/dist/esm/common-utils/index.js +1 -1
- package/dist/esm/common-utils/utils/apy/calculate-safe-borrow-rate.js +1 -1
- package/dist/esm/common-utils/utils/assets-math.js +1 -1
- package/dist/esm/common-utils/utils/creditAccount/calc-health-factor.js +1 -1
- package/dist/esm/common-utils/utils/creditAccount/debt.js +1 -1
- package/dist/esm/common-utils/utils/creditAccount/quota-utils.js +1 -1
- package/dist/esm/common-utils/utils/index.js +1 -1
- package/dist/esm/common-utils/utils/strategies/leverage/calculate-max-leverage-factor.js +1 -1
- package/dist/esm/common-utils/utils/strategies/leverage/calculate-max-strategy-debt.js +1 -1
- package/dist/esm/common-utils/utils/strategies/strategy-info/calculate-total-points.js +1 -1
- package/dist/esm/common-utils/utils/strategies/strategy-info/get-cm-you-can-earn.js +1 -1
- package/dist/esm/common-utils/utils/validation/validate-open-account-pool-status.js +1 -1
- package/dist/esm/model/history.js +26 -0
- package/dist/esm/model/history.schema.js +76 -0
- package/dist/esm/model/index.js +9 -0
- package/dist/esm/model/liquidations.js +22 -0
- package/dist/esm/model/liquidations.schema.js +83 -0
- package/dist/esm/model/opportunities.js +51 -0
- package/dist/esm/model/opportunities.schema.js +198 -0
- package/dist/esm/model/package.json +1 -0
- package/dist/esm/model/positions.js +1 -0
- package/dist/esm/model/primitives.js +1 -0
- package/dist/esm/model/primitives.schema.js +75 -0
- package/dist/esm/new-sdk/AbstractNamespace.js +157 -0
- package/dist/esm/new-sdk/GearboxSDK.js +121 -0
- package/dist/esm/new-sdk/index.js +7 -0
- package/dist/esm/new-sdk/opportunities/OpportunitiesNamespace.js +121 -0
- package/dist/esm/new-sdk/opportunities/index.js +3 -0
- package/dist/esm/new-sdk/opportunities/types.js +1 -0
- package/dist/esm/new-sdk/package.json +1 -0
- package/dist/esm/new-sdk/types.js +21 -0
- package/dist/esm/new-sdk/utils/history.js +1 -0
- package/dist/esm/new-sdk/utils/index.js +2 -0
- package/dist/esm/offchain/GearboxAPI.js +32 -0
- package/dist/esm/offchain/index.js +5 -0
- package/dist/esm/offchain/opportunities/OffchainOpportunities.js +95 -0
- package/dist/esm/offchain/opportunities/index.js +2 -0
- package/dist/esm/offchain/package.json +1 -0
- package/dist/esm/offchain/types.js +1 -0
- package/dist/esm/preview/preview/buildDelayedPreview.js +1 -1
- package/dist/esm/rewards/rewards/api.js +1 -1
- package/dist/esm/rewards/rewards/extra-apy.js +1 -1
- package/dist/esm/sdk/MultichainSDK.js +18 -2
- package/dist/esm/sdk/OnchainSDK.js +8 -1
- package/dist/esm/sdk/accounts/index.js +2 -2
- package/dist/esm/sdk/accounts/liquidations/LiquidationsService.js +152 -108
- package/dist/esm/sdk/accounts/liquidations/MultichainLiquidationsService.js +32 -38
- package/dist/esm/sdk/accounts/liquidations/constants.js +12 -1
- package/dist/esm/sdk/accounts/liquidations/index.js +2 -2
- package/dist/esm/sdk/base/MultichainConstruct.js +79 -0
- package/dist/esm/sdk/base/TokensMeta.js +51 -0
- package/dist/esm/sdk/base/index.js +2 -1
- package/dist/esm/sdk/chain/chains.js +74 -2
- package/dist/esm/sdk/chain/index.js +2 -2
- package/dist/esm/sdk/core/errors.js +13 -1
- package/dist/esm/sdk/core/index.js +2 -2
- package/dist/esm/sdk/index.js +11 -5
- package/dist/esm/sdk/market/MarketConfiguratorContract.js +14 -0
- package/dist/esm/sdk/market/MarketSuite.js +163 -0
- package/dist/esm/sdk/market/credit/CreditManagerV310Contract.js +26 -0
- package/dist/esm/sdk/market/credit/CreditSuite.js +102 -0
- package/dist/esm/sdk/market/math.js +98 -0
- package/dist/esm/sdk/market/oracle/PriceOracleBaseContract.js +52 -0
- package/dist/esm/sdk/market/pool/LinearInterestRateModelContract.js +30 -0
- package/dist/esm/sdk/market/pool/PoolQuotaKeeperV310Contract.js +26 -0
- package/dist/esm/sdk/market/pool/PoolSuite.js +28 -0
- package/dist/esm/sdk/market/pool/PoolV310Contract.js +27 -0
- package/dist/esm/sdk/market/pool/math.js +41 -0
- package/dist/esm/sdk/market/pricefeeds/AbstractPriceFeed.js +17 -0
- package/dist/esm/sdk/opportunities/MultichainOpportunitiesService.js +59 -0
- package/dist/esm/sdk/opportunities/OpportunitiesService.js +102 -0
- package/dist/esm/sdk/opportunities/index.js +4 -0
- package/dist/esm/sdk/types/multichain.js +1 -0
- package/dist/esm/{common-utils → sdk}/utils/bigint-math.js +11 -11
- package/dist/esm/sdk/utils/index.js +3 -2
- package/dist/esm/sdk/utils/zod.js +13 -2
- package/dist/types/common-utils/index.d.ts +1 -1
- package/dist/types/common-utils/utils/index.d.ts +1 -1
- package/dist/types/model/history.d.ts +105 -0
- package/dist/types/model/history.schema.d.ts +66 -0
- package/dist/types/model/index.d.ts +9 -0
- package/dist/types/model/liquidations.d.ts +230 -0
- package/dist/types/model/liquidations.schema.d.ts +292 -0
- package/dist/types/model/opportunities.d.ts +543 -0
- package/dist/types/model/opportunities.schema.d.ts +1066 -0
- package/dist/types/model/positions.d.ts +11 -0
- package/dist/types/model/primitives.d.ts +193 -0
- package/dist/types/model/primitives.schema.d.ts +81 -0
- package/dist/types/new-sdk/AbstractNamespace.d.ts +119 -0
- package/dist/types/new-sdk/GearboxSDK.d.ts +78 -0
- package/dist/types/new-sdk/index.d.ts +9 -0
- package/dist/types/new-sdk/opportunities/OpportunitiesNamespace.d.ts +69 -0
- package/dist/types/new-sdk/opportunities/index.d.ts +3 -0
- package/dist/types/new-sdk/opportunities/types.d.ts +66 -0
- package/dist/types/new-sdk/types.d.ts +149 -0
- package/dist/types/new-sdk/utils/history.d.ts +44 -0
- package/dist/types/new-sdk/utils/index.d.ts +2 -0
- package/dist/types/offchain/GearboxAPI.d.ts +31 -0
- package/dist/types/offchain/index.d.ts +5 -0
- package/dist/types/offchain/opportunities/OffchainOpportunities.d.ts +69 -0
- package/dist/types/offchain/opportunities/index.d.ts +2 -0
- package/dist/types/offchain/types.d.ts +50 -0
- package/dist/types/sdk/MultichainSDK.d.ts +15 -2
- package/dist/types/sdk/OnchainSDK.d.ts +9 -3
- package/dist/types/sdk/accounts/index.d.ts +3 -3
- package/dist/types/sdk/accounts/liquidations/LiquidationsService.d.ts +24 -16
- package/dist/types/sdk/accounts/liquidations/MultichainLiquidationsService.d.ts +26 -20
- package/dist/types/sdk/accounts/liquidations/constants.d.ts +12 -1
- package/dist/types/sdk/accounts/liquidations/index.d.ts +3 -3
- package/dist/types/sdk/accounts/liquidations/types.d.ts +34 -309
- package/dist/types/sdk/base/MultichainConstruct.d.ts +57 -0
- package/dist/types/sdk/base/TokensMeta.d.ts +26 -0
- package/dist/types/sdk/base/index.d.ts +2 -1
- package/dist/types/sdk/chain/chains.d.ts +68 -1
- package/dist/types/sdk/chain/index.d.ts +2 -2
- package/dist/types/sdk/core/errors.d.ts +10 -1
- package/dist/types/sdk/core/index.d.ts +2 -2
- package/dist/types/sdk/index.d.ts +15 -8
- package/dist/types/sdk/market/MarketConfiguratorContract.d.ts +9 -0
- package/dist/types/sdk/market/MarketSuite.d.ts +109 -1
- package/dist/types/sdk/market/credit/CreditManagerV310Contract.d.ts +14 -0
- package/dist/types/sdk/market/credit/CreditSuite.d.ts +47 -0
- package/dist/types/sdk/market/credit/types.d.ts +22 -0
- package/dist/types/sdk/market/index.d.ts +2 -2
- package/dist/types/sdk/market/math.d.ts +74 -0
- package/dist/types/sdk/market/oracle/PriceOracleBaseContract.d.ts +23 -0
- package/dist/types/sdk/market/oracle/types.d.ts +46 -0
- package/dist/types/sdk/market/pool/LinearInterestRateModelContract.d.ts +12 -0
- package/dist/types/sdk/market/pool/PoolQuotaKeeperV310Contract.d.ts +22 -1
- package/dist/types/sdk/market/pool/PoolSuite.d.ts +23 -0
- package/dist/types/sdk/market/pool/PoolV310Contract.d.ts +18 -0
- package/dist/types/sdk/market/pool/math.d.ts +57 -0
- package/dist/types/sdk/market/pool/types.d.ts +23 -0
- package/dist/types/sdk/market/pricefeeds/AbstractPriceFeed.d.ts +6 -0
- package/dist/types/sdk/market/pricefeeds/types.d.ts +10 -0
- package/dist/types/sdk/opportunities/MultichainOpportunitiesService.d.ts +40 -0
- package/dist/types/sdk/opportunities/OpportunitiesService.d.ts +47 -0
- package/dist/types/sdk/opportunities/index.d.ts +4 -0
- package/dist/types/sdk/types/index.d.ts +2 -1
- package/dist/types/sdk/types/multichain.d.ts +65 -0
- package/dist/types/sdk/types/state.d.ts +1 -1
- package/dist/types/sdk/types/transactions.d.ts +3 -0
- package/dist/types/{common-utils → sdk}/utils/bigint-math.d.ts +7 -11
- package/dist/types/sdk/utils/index.d.ts +3 -2
- package/dist/types/sdk/utils/zod.d.ts +6 -2
- package/package.json +16 -1
- package/dist/cjs/sdk/accounts/liquidations/helpers.js +0 -201
- package/dist/esm/sdk/accounts/liquidations/helpers.js +0 -192
- package/dist/types/sdk/accounts/liquidations/helpers.d.ts +0 -140
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_sdk_utils_bigint_math = require("../../utils/bigint-math.js");
|
|
3
|
+
const require_sdk_chain_chains = require("../../chain/chains.js");
|
|
4
|
+
require("../../constants/math.js");
|
|
5
|
+
require("../../constants/index.js");
|
|
2
6
|
const require_sdk_base_SDKConstruct = require("../../base/SDKConstruct.js");
|
|
3
7
|
require("../../base/index.js");
|
|
8
|
+
const require_sdk_market_math = require("../math.js");
|
|
4
9
|
const require_sdk_market_credit_createCreditConfigurator = require("./createCreditConfigurator.js");
|
|
5
10
|
const require_sdk_market_credit_createCreditFacade = require("./createCreditFacade.js");
|
|
6
11
|
const require_sdk_market_credit_createCreditManager = require("./createCreditManager.js");
|
|
@@ -99,6 +104,103 @@ var CreditSuite = class extends require_sdk_base_SDKConstruct.SDKConstruct {
|
|
|
99
104
|
return this.creditFacade.expirable && this.creditFacade.expirationDate > 0 && this.creditFacade.expirationDate < this.sdk.timestamp;
|
|
100
105
|
}
|
|
101
106
|
/**
|
|
107
|
+
* Moment the facade expires, after which positions can no longer be opened
|
|
108
|
+
* and open ones become liquidatable, or `null` when it is not expirable.
|
|
109
|
+
*
|
|
110
|
+
* @remarks
|
|
111
|
+
* The facade stores `0` for a non-expirable suite, which as a timestamp
|
|
112
|
+
* would read as 1970 rather than as "never".
|
|
113
|
+
*/
|
|
114
|
+
get expirationDate() {
|
|
115
|
+
const { expirationDate } = this.creditFacade;
|
|
116
|
+
return expirationDate > 0 ? expirationDate : null;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Whether this suite can be used right now. A paused pool blocks borrowing,
|
|
120
|
+
* so the suite is unusable even when its own facade is live.
|
|
121
|
+
*/
|
|
122
|
+
get isPaused() {
|
|
123
|
+
return this.creditFacade.isPaused || this.market.pool.isPaused;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Collateral tokens a leveraged position can be built around in this suite:
|
|
127
|
+
* the ones the credit manager can lever up, narrowed to those the market
|
|
128
|
+
* still accepts quota for.
|
|
129
|
+
*/
|
|
130
|
+
get strategyCollaterals() {
|
|
131
|
+
const { pqk } = this.market.pool;
|
|
132
|
+
return this.creditManager.leverageableCollaterals.filter((token) => pqk.hasActiveQuota(token));
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Largest debt a single new position can take on right now: the tightest of
|
|
136
|
+
* this manager's remaining debt limit, the pool's free liquidity and the
|
|
137
|
+
* facade's per-account maximum.
|
|
138
|
+
*/
|
|
139
|
+
get maxBorrowAmount() {
|
|
140
|
+
const { pool } = this.market.pool;
|
|
141
|
+
const debtParams = pool.creditManagerDebtParams.get(this.creditManager.address);
|
|
142
|
+
return require_sdk_utils_bigint_math.BigIntMath.min(debtParams?.available ?? 115792089237316195423570985008687907853269984665640564039457584007913129639935n, pool.availableLiquidity, this.creditFacade.maxDebt);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Describes a leveraged position built on one collateral token as the shared
|
|
146
|
+
* read model does.
|
|
147
|
+
*
|
|
148
|
+
* @param collateral - Target collateral of the position.
|
|
149
|
+
* @param totalSupply - Summed worth of the credit accounts backing it, which
|
|
150
|
+
* only a credit-account query can establish. Defaults to zero, so a caller
|
|
151
|
+
* that does not care about size can omit it.
|
|
152
|
+
* @throws If the credit manager does not value the collateral.
|
|
153
|
+
*/
|
|
154
|
+
strategyOpportunity(collateral, totalSupply_) {
|
|
155
|
+
const totalSupply = totalSupply_ ?? {
|
|
156
|
+
value: 0n,
|
|
157
|
+
valueUsd: 0
|
|
158
|
+
};
|
|
159
|
+
const { market, creditManager: cm } = this;
|
|
160
|
+
const { pool } = market.pool;
|
|
161
|
+
const oracle = market.priceOracle;
|
|
162
|
+
const liquidationThreshold = cm.liquidationThresholds.mustGet(collateral);
|
|
163
|
+
const maxLeverage = cm.maxLeverage(collateral);
|
|
164
|
+
const borrowed = pool.creditManagerDebtParams.get(cm.address)?.borrowed ?? 0n;
|
|
165
|
+
return {
|
|
166
|
+
kind: "strategy",
|
|
167
|
+
chainId: this.chainId,
|
|
168
|
+
creditManager: cm.address,
|
|
169
|
+
targetCollateral: this.tokensMeta.mustGetToken(collateral),
|
|
170
|
+
title: `${this.tokensMeta.symbol(collateral)} / ${market.underlyingToken.symbol}`,
|
|
171
|
+
curator: market.curator,
|
|
172
|
+
underlyingToken: market.underlyingToken,
|
|
173
|
+
totalSupply,
|
|
174
|
+
totalBorrow: oracle.toAmount(pool.underlying, borrowed),
|
|
175
|
+
utilization: require_sdk_market_math.utilizationBps(borrowed, totalSupply.value),
|
|
176
|
+
collateralTokens: market.collateralTokens,
|
|
177
|
+
paused: this.isPaused,
|
|
178
|
+
rwa: market.rwa,
|
|
179
|
+
sunset: require_sdk_chain_chains.isSunsetStrategy(cm.address, collateral, this.sdk.networkType),
|
|
180
|
+
liquidationThreshold,
|
|
181
|
+
liquidationPremium: cm.liquidationPremium,
|
|
182
|
+
liquidationFee: cm.feeLiquidation,
|
|
183
|
+
expirationDate: this.expirationDate,
|
|
184
|
+
borrowApy: require_sdk_market_math.borrowApyBps(pool.baseInterestRate, cm.feeInterest),
|
|
185
|
+
additionalBorrowApy: require_sdk_market_math.additionalBorrowApyBps(market.pool.pqk.quotaRate(collateral), maxLeverage),
|
|
186
|
+
maxBorrowAmount: oracle.toAmount(pool.underlying, this.maxBorrowAmount),
|
|
187
|
+
maxLeverage
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* {@link strategyOpportunity} plus the data only its detail screen needs.
|
|
192
|
+
*
|
|
193
|
+
* @param collateral - Target collateral of the position.
|
|
194
|
+
* @param totalSupply - Summed worth of the credit accounts backing it.
|
|
195
|
+
*/
|
|
196
|
+
strategyOpportunityDetail(collateral, totalSupply) {
|
|
197
|
+
return {
|
|
198
|
+
...this.strategyOpportunity(collateral, totalSupply),
|
|
199
|
+
rateCurve: this.market.pool.rateCurve,
|
|
200
|
+
priceFeeds: this.market.priceFeedSummary(collateral)
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
102
204
|
* Whether the facade, manager, or configurator has observed logs that require
|
|
103
205
|
* a credit-suite resync.
|
|
104
206
|
*/
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_sdk_constants_math = require("../constants/math.js");
|
|
3
|
+
//#region src/sdk/market/math.ts
|
|
4
|
+
/**
|
|
5
|
+
* Conversions between the units the protocol stores and the basis points the
|
|
6
|
+
* read model exposes.
|
|
7
|
+
*
|
|
8
|
+
* These live here rather than inside the service so that the on-chain adapter,
|
|
9
|
+
* the merger and any future consumer of the read model share one definition of
|
|
10
|
+
* every derived value.
|
|
11
|
+
**/
|
|
12
|
+
const FULL = Number(require_sdk_constants_math.PERCENTAGE_FACTOR);
|
|
13
|
+
/**
|
|
14
|
+
* Converts a ray-scaled rate (`10 ** 27` = 100%) to basis points, truncating
|
|
15
|
+
* towards zero.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* rayToBps(50_000_000_000_000_000_000_000_000n) // 500, i.e. 5%
|
|
20
|
+
* ```
|
|
21
|
+
**/
|
|
22
|
+
function rayToBps(ray) {
|
|
23
|
+
return Number(ray * require_sdk_constants_math.PERCENTAGE_FACTOR / require_sdk_constants_math.RAY);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Converts a USD value in the oracle's 8-decimal fixed point to a float.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* usdToNumber(150_050_000_000n) // 1500.5
|
|
31
|
+
* ```
|
|
32
|
+
**/
|
|
33
|
+
function usdToNumber(usd) {
|
|
34
|
+
return Number(usd) / Number(require_sdk_constants_math.PRICE_DECIMALS);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Share of capital currently borrowed, in basis points. Returns `0` when there
|
|
38
|
+
* is nothing to borrow from, and never exceeds 100%.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* utilizationBps(750n, 1000n) // 7500, i.e. 75%
|
|
43
|
+
* ```
|
|
44
|
+
**/
|
|
45
|
+
function utilizationBps(borrowed, total) {
|
|
46
|
+
if (total <= 0n || borrowed <= 0n) return 0;
|
|
47
|
+
const utilization = Number(borrowed * require_sdk_constants_math.PERCENTAGE_FACTOR / total);
|
|
48
|
+
return Math.min(utilization, FULL);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Annual cost of debt for a credit manager, in basis points: the pool's base
|
|
52
|
+
* rate plus the protocol's cut of the accrued interest.
|
|
53
|
+
*
|
|
54
|
+
* @param baseInterestRate - Pool base rate in ray.
|
|
55
|
+
* @param feeInterest - Credit manager interest fee in basis points.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* // 5% base rate, 50% interest fee
|
|
60
|
+
* borrowApyBps(50_000_000_000_000_000_000_000_000n, 5000) // 750, i.e. 7.5%
|
|
61
|
+
* ```
|
|
62
|
+
**/
|
|
63
|
+
function borrowApyBps(baseInterestRate, feeInterest) {
|
|
64
|
+
return rayToBps(baseInterestRate * (require_sdk_constants_math.PERCENTAGE_FACTOR + BigInt(feeInterest)) / require_sdk_constants_math.PERCENTAGE_FACTOR);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Highest leverage a liquidation threshold allows: `1 / (1 - lt)`.
|
|
68
|
+
*
|
|
69
|
+
* A threshold of 100% or more would allow unbounded leverage; such tokens are
|
|
70
|
+
* not strategies and are filtered out before this is called, so the guard here
|
|
71
|
+
* only exists to keep the function total.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* maxLeverage(9000) // 10
|
|
76
|
+
* maxLeverage(8000) // 5
|
|
77
|
+
* ```
|
|
78
|
+
**/
|
|
79
|
+
function maxLeverage(liquidationThreshold) {
|
|
80
|
+
const equity = FULL - liquidationThreshold;
|
|
81
|
+
return equity > 0 ? FULL / equity : Number.POSITIVE_INFINITY;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Annual quota cost scaled to the debt a maximally leveraged position carries,
|
|
85
|
+
* in basis points. Every unit of own capital carries `maxLeverage - 1` units of
|
|
86
|
+
* debt, and the quota is paid on the whole quoted position.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* // 2.5% quota rate at 5x leverage
|
|
91
|
+
* additionalBorrowApyBps(250, 5) // 1000, i.e. 10%
|
|
92
|
+
* ```
|
|
93
|
+
**/
|
|
94
|
+
function additionalBorrowApyBps(quotaRate, leverage) {
|
|
95
|
+
if (!Number.isFinite(leverage)) return 0;
|
|
96
|
+
return Math.round(quotaRate * Math.max(leverage - 1, 0));
|
|
97
|
+
}
|
|
98
|
+
//#endregion
|
|
99
|
+
exports.additionalBorrowApyBps = additionalBorrowApyBps;
|
|
100
|
+
exports.borrowApyBps = borrowApyBps;
|
|
101
|
+
exports.maxLeverage = maxLeverage;
|
|
102
|
+
exports.rayToBps = rayToBps;
|
|
103
|
+
exports.usdToNumber = usdToNumber;
|
|
104
|
+
exports.utilizationBps = utilizationBps;
|
|
@@ -7,6 +7,7 @@ const require_sdk_utils_formatter = require("../../utils/formatter.js");
|
|
|
7
7
|
require("../../utils/index.js");
|
|
8
8
|
const require_sdk_base_BaseContract = require("../../base/BaseContract.js");
|
|
9
9
|
require("../../base/index.js");
|
|
10
|
+
const require_sdk_market_math = require("../math.js");
|
|
10
11
|
const require_sdk_market_pricefeeds_PriceFeedRef = require("../pricefeeds/PriceFeedRef.js");
|
|
11
12
|
const require_abi_compressors_priceFeedCompressor = require("../../../abi/compressors/priceFeedCompressor.js");
|
|
12
13
|
require("../pricefeeds/index.js");
|
|
@@ -104,6 +105,57 @@ var PriceOracleBaseContract = class extends require_sdk_base_BaseContract.BaseCo
|
|
|
104
105
|
return amount * 10n ** BigInt(this.tokensMeta.decimals(to)) / price;
|
|
105
106
|
}
|
|
106
107
|
/**
|
|
108
|
+
* {@inheritDoc IPriceOracleContract.safeUsdValue}
|
|
109
|
+
**/
|
|
110
|
+
safeUsdValue(token, amount) {
|
|
111
|
+
try {
|
|
112
|
+
return require_sdk_market_math.usdToNumber(this.convertToUSD(token, amount));
|
|
113
|
+
} catch (e) {
|
|
114
|
+
this.logger?.debug(`cannot price ${this.labelAddress(token)}: ${e}`);
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* {@inheritDoc IPriceOracleContract.toAmount}
|
|
120
|
+
**/
|
|
121
|
+
toAmount = (token, value) => {
|
|
122
|
+
return {
|
|
123
|
+
value,
|
|
124
|
+
valueUsd: this.safeUsdValue(token, value)
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* {@inheritDoc IPriceOracleContract.toTokenAmount}
|
|
129
|
+
**/
|
|
130
|
+
toTokenAmount = (token, value) => {
|
|
131
|
+
return {
|
|
132
|
+
token: this.tokensMeta.mustGetToken(token),
|
|
133
|
+
...this.toAmount(token, value)
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* {@inheritDoc IPriceOracleContract.priceFeedData}
|
|
138
|
+
**/
|
|
139
|
+
priceFeedData(token) {
|
|
140
|
+
const ref = this.mainPriceFeeds.get(token);
|
|
141
|
+
if (!ref) throw new Error(`no main price feed for ${this.labelAddress(token)} in oracle ${this.labelAddress(this.address)}`);
|
|
142
|
+
return ref.priceFeed.describe();
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* {@inheritDoc IPriceOracleContract.priceFeedSummary}
|
|
146
|
+
**/
|
|
147
|
+
priceFeedSummary(underlying, collateral) {
|
|
148
|
+
const collateralUnit = 10n ** BigInt(this.tokensMeta.decimals(collateral));
|
|
149
|
+
const underlyingUnit = 10n ** BigInt(this.tokensMeta.decimals(underlying));
|
|
150
|
+
return {
|
|
151
|
+
underlyingPriceInUsd: require_sdk_market_math.usdToNumber(this.mainPrice(underlying)),
|
|
152
|
+
collateralPriceInUsd: require_sdk_market_math.usdToNumber(this.mainPrice(collateral)),
|
|
153
|
+
collateralPriceInUnderlying: Number(this.convert(collateral, underlying, collateralUnit)) / Number(underlyingUnit),
|
|
154
|
+
underlyingFeed: this.priceFeedData(underlying),
|
|
155
|
+
collateralFeed: this.priceFeedData(collateral)
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
107
159
|
* {@inheritDoc IPriceOracleContract.priceFeeds}
|
|
108
160
|
**/
|
|
109
161
|
get priceFeeds() {
|
|
@@ -4,6 +4,7 @@ require("../../utils/index.js");
|
|
|
4
4
|
const require_sdk_base_BaseContract = require("../../base/BaseContract.js");
|
|
5
5
|
require("../../base/index.js");
|
|
6
6
|
const require_abi_310_iLinearInterestRateModelV310 = require("../../../abi/310/iLinearInterestRateModelV310.js");
|
|
7
|
+
const require_sdk_market_pool_math = require("./math.js");
|
|
7
8
|
let viem = require("viem");
|
|
8
9
|
//#region src/sdk/market/pool/LinearInterestRateModelContract.ts
|
|
9
10
|
const abi = require_abi_310_iLinearInterestRateModelV310.iLinearInterestRateModelV310Abi;
|
|
@@ -59,6 +60,35 @@ var LinearInterestRateModelContract = class extends require_sdk_base_BaseContrac
|
|
|
59
60
|
this.Rslope3 = Rslope3;
|
|
60
61
|
this.isBorrowingMoreU2Forbidden = isBorrowingMoreU2Forbidden;
|
|
61
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* The model's own parameters, gathered for the rate math.
|
|
65
|
+
*/
|
|
66
|
+
get params() {
|
|
67
|
+
return {
|
|
68
|
+
U1: this.U1,
|
|
69
|
+
U2: this.U2,
|
|
70
|
+
Rbase: this.Rbase,
|
|
71
|
+
Rslope1: this.Rslope1,
|
|
72
|
+
Rslope2: this.Rslope2,
|
|
73
|
+
Rslope3: this.Rslope3,
|
|
74
|
+
isBorrowingMoreU2Forbidden: this.isBorrowingMoreU2Forbidden
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The model evaluated into chart-ready points, so that no consumer has to
|
|
79
|
+
* reimplement it.
|
|
80
|
+
*/
|
|
81
|
+
rateCurve() {
|
|
82
|
+
const { params } = this;
|
|
83
|
+
return {
|
|
84
|
+
points: require_sdk_market_pool_math.rateCurveUtilizations(params).map((utilization) => ({
|
|
85
|
+
utilization,
|
|
86
|
+
supplyApy: require_sdk_market_pool_math.supplyRateAtUtilization(utilization, params),
|
|
87
|
+
borrowApy: require_sdk_market_pool_math.borrowRateAtUtilization(utilization, params)
|
|
88
|
+
})),
|
|
89
|
+
borrowingLimitUtilization: params.isBorrowingMoreU2Forbidden ? params.U2 : null
|
|
90
|
+
};
|
|
91
|
+
}
|
|
62
92
|
stateHuman(raw) {
|
|
63
93
|
return {
|
|
64
94
|
...super.stateHuman(raw),
|
|
@@ -21,6 +21,32 @@ var PoolQuotaKeeperV310Contract = class extends require_sdk_base_BaseContract.Ba
|
|
|
21
21
|
return [q.token, q];
|
|
22
22
|
}), "quotas");
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Whether the market still accepts quota for a token: a token whose quota is
|
|
26
|
+
* inactive or whose limit is exhausted can no longer back a new position.
|
|
27
|
+
*
|
|
28
|
+
* @param token - Token address.
|
|
29
|
+
*/
|
|
30
|
+
hasActiveQuota(token) {
|
|
31
|
+
const quota = this.quotas.get(token);
|
|
32
|
+
return !!quota?.isActive && quota.limit > 0n;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Every token the market still accepts quota for, see
|
|
36
|
+
* {@link hasActiveQuota}.
|
|
37
|
+
*/
|
|
38
|
+
get activeQuotaTokens() {
|
|
39
|
+
return this.quotas.keys().filter((token) => this.hasActiveQuota(token));
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Annual quota rate paid on a quoted token, in basis points, or `0` when the
|
|
43
|
+
* market does not quote it.
|
|
44
|
+
*
|
|
45
|
+
* @param token - Quoted token address.
|
|
46
|
+
*/
|
|
47
|
+
quotaRate(token) {
|
|
48
|
+
return this.quotas.get(token)?.rate ?? 0;
|
|
49
|
+
}
|
|
24
50
|
stateHuman(raw = true) {
|
|
25
51
|
return {
|
|
26
52
|
...super.stateHuman(raw),
|
|
@@ -100,6 +100,34 @@ var PoolSuite = class extends require_sdk_base_SDKConstruct.SDKConstruct {
|
|
|
100
100
|
return this.pool.underlying;
|
|
101
101
|
}
|
|
102
102
|
/**
|
|
103
|
+
* {@inheritDoc IPoolContract.unwrappedUnderlying}
|
|
104
|
+
*/
|
|
105
|
+
get unwrappedUnderlying() {
|
|
106
|
+
return this.pool.unwrappedUnderlying;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* {@inheritDoc IPoolContract.utilization}
|
|
110
|
+
*/
|
|
111
|
+
get utilization() {
|
|
112
|
+
return this.pool.utilization;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Whether the pool is paused, which blocks borrowing across every connected
|
|
116
|
+
* credit suite.
|
|
117
|
+
*/
|
|
118
|
+
get isPaused() {
|
|
119
|
+
return this.pool.isPaused;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Interest rate curve of the pool's rate model.
|
|
123
|
+
*
|
|
124
|
+
* @throws If the market uses an interest-rate model that is not linear, and
|
|
125
|
+
* therefore has no curve the SDK can evaluate.
|
|
126
|
+
*/
|
|
127
|
+
get rateCurve() {
|
|
128
|
+
return this.linearModel.rateCurve();
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
103
131
|
* RWA factory associated with the pool's underlying, undefined for non-RWA markets.
|
|
104
132
|
*/
|
|
105
133
|
get rwaFactory() {
|
|
@@ -2,10 +2,13 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
const require_abi_iPausable = require("../../../abi/iPausable.js");
|
|
3
3
|
const require_abi_310_generated = require("../../../abi/310/generated.js");
|
|
4
4
|
const require_sdk_utils_AddressMap = require("../../utils/AddressMap.js");
|
|
5
|
+
const require_sdk_constants_math = require("../../constants/math.js");
|
|
6
|
+
require("../../constants/index.js");
|
|
5
7
|
const require_sdk_utils_formatter = require("../../utils/formatter.js");
|
|
6
8
|
require("../../utils/index.js");
|
|
7
9
|
const require_sdk_base_BaseContract = require("../../base/BaseContract.js");
|
|
8
10
|
require("../../base/index.js");
|
|
11
|
+
const require_sdk_market_math = require("../math.js");
|
|
9
12
|
//#region src/sdk/market/pool/PoolV310Contract.ts
|
|
10
13
|
const abi = [...require_abi_310_generated.iPoolV310Abi, ...require_abi_iPausable.iPausableAbi];
|
|
11
14
|
var PoolV310Contract = class extends require_sdk_base_BaseContract.BaseContract {
|
|
@@ -32,6 +35,30 @@ var PoolV310Contract = class extends require_sdk_base_BaseContract.BaseContract
|
|
|
32
35
|
const meta = this.#sdk.tokensMeta.mustGet(this.underlying);
|
|
33
36
|
if (this.#sdk.tokensMeta.isRWAUnderlying(meta)) return this.#sdk.mustGetContract(meta.rwaFactory);
|
|
34
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* {@inheritDoc IPoolContract.borrowed}
|
|
40
|
+
*/
|
|
41
|
+
get borrowed() {
|
|
42
|
+
return this.expectedLiquidity > this.availableLiquidity ? this.expectedLiquidity - this.availableLiquidity : 0n;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* {@inheritDoc IPoolContract.totalAssets}
|
|
46
|
+
*/
|
|
47
|
+
get totalAssets() {
|
|
48
|
+
return this.totalSupply * this.dieselRate / require_sdk_constants_math.RAY;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* {@inheritDoc IPoolContract.utilization}
|
|
52
|
+
*/
|
|
53
|
+
get utilization() {
|
|
54
|
+
return require_sdk_market_math.utilizationBps(this.borrowed, this.expectedLiquidity);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* {@inheritDoc IPoolContract.unwrappedUnderlying}
|
|
58
|
+
*/
|
|
59
|
+
get unwrappedUnderlying() {
|
|
60
|
+
return this.tokensMeta.unwrapRWA(this.underlying);
|
|
61
|
+
}
|
|
35
62
|
stateHuman(raw = true) {
|
|
36
63
|
return {
|
|
37
64
|
...super.stateHuman(raw),
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_sdk_constants_math = require("../../constants/math.js");
|
|
3
|
+
//#region src/sdk/market/pool/math.ts
|
|
4
|
+
const FULL = Number(require_sdk_constants_math.PERCENTAGE_FACTOR);
|
|
5
|
+
/**
|
|
6
|
+
* Base rate borrowers pay at a given utilization, in basis points, following
|
|
7
|
+
* the linear interest rate model's three segments.
|
|
8
|
+
*
|
|
9
|
+
* The result excludes the per-credit-manager interest fee: the model belongs to
|
|
10
|
+
* the pool, and every credit manager of a market can charge a different one.
|
|
11
|
+
**/
|
|
12
|
+
function borrowRateAtUtilization(utilization, params) {
|
|
13
|
+
const { U1, U2, Rbase, Rslope1, Rslope2, Rslope3 } = params;
|
|
14
|
+
const u = Math.min(Math.max(utilization, 0), FULL);
|
|
15
|
+
if (u <= 0) return Rbase;
|
|
16
|
+
if (u <= U1) return Math.round(Rbase + Rslope1 * u / U1);
|
|
17
|
+
if (u <= U2) return Math.round(Rbase + Rslope1 + Rslope2 * (u - U1) / Math.max(U2 - U1, 1));
|
|
18
|
+
return Math.round(Rbase + Rslope1 + Rslope2 + Rslope3 * (u - U2) / Math.max(FULL - U2, 1));
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Rate depositors earn at a given utilization, in basis points: the interest
|
|
22
|
+
* borrowers pay, spread over the pool's whole liquidity.
|
|
23
|
+
**/
|
|
24
|
+
function supplyRateAtUtilization(utilization, params) {
|
|
25
|
+
const u = Math.min(Math.max(utilization, 0), FULL);
|
|
26
|
+
return Math.round(borrowRateAtUtilization(u, params) * u / FULL);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Utilizations the rate curve is sampled at: a fixed grid plus both kinks of
|
|
30
|
+
* the model, so the borrow leg is exact and the supply leg — which is
|
|
31
|
+
* quadratic between kinks — is smooth.
|
|
32
|
+
**/
|
|
33
|
+
function rateCurveUtilizations(params) {
|
|
34
|
+
const step = FULL / 20;
|
|
35
|
+
const grid = /* @__PURE__ */ new Set();
|
|
36
|
+
for (let u = 0; u <= FULL; u += step) grid.add(u);
|
|
37
|
+
grid.add(params.U1);
|
|
38
|
+
grid.add(params.U2);
|
|
39
|
+
return [...grid].filter((u) => u >= 0 && u <= FULL).sort((a, b) => a - b);
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
exports.borrowRateAtUtilization = borrowRateAtUtilization;
|
|
43
|
+
exports.rateCurveUtilizations = rateCurveUtilizations;
|
|
44
|
+
exports.supplyRateAtUtilization = supplyRateAtUtilization;
|
|
@@ -89,6 +89,23 @@ var AbstractPriceFeedContract = class extends require_sdk_base_BaseContract.Base
|
|
|
89
89
|
const underlying = this.underlyingPriceFeeds.flatMap((f) => f.priceFeed.updatableDependencies());
|
|
90
90
|
return require_sdk_market_pricefeeds_isUpdatablePriceFeed.isUpdatablePriceFeed(this) ? [this, ...underlying] : underlying;
|
|
91
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* {@inheritDoc IPriceFeedContract.describe}
|
|
94
|
+
*/
|
|
95
|
+
describe() {
|
|
96
|
+
let dependencies = [];
|
|
97
|
+
try {
|
|
98
|
+
dependencies = this.underlyingPriceFeeds.map((ref) => ref.priceFeed.describe());
|
|
99
|
+
} catch {
|
|
100
|
+
dependencies = [];
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
name: this.name,
|
|
104
|
+
type: this.contractType,
|
|
105
|
+
feedAddress: this.address,
|
|
106
|
+
dependencies
|
|
107
|
+
};
|
|
108
|
+
}
|
|
92
109
|
};
|
|
93
110
|
//#endregion
|
|
94
111
|
exports.AbstractPriceFeedContract = AbstractPriceFeedContract;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_sdk_chain_chains = require("../chain/chains.js");
|
|
3
|
+
const require_sdk_base_MultichainConstruct = require("../base/MultichainConstruct.js");
|
|
4
|
+
require("../base/index.js");
|
|
5
|
+
//#region src/sdk/opportunities/MultichainOpportunitiesService.ts
|
|
6
|
+
/**
|
|
7
|
+
* Cross-chain counterpart of {@link OpportunitiesService}.
|
|
8
|
+
*
|
|
9
|
+
* Fans out over every chain configured in {@link MultichainSDK}. A chain that
|
|
10
|
+
* fails is logged and skipped so one dead RPC does not empty the list; its
|
|
11
|
+
* failure is reported in {@link MultichainResult.meta}.
|
|
12
|
+
*
|
|
13
|
+
* Detail reads need no fan-out: an opportunity key names its chain.
|
|
14
|
+
*
|
|
15
|
+
* @typeParam Plugins - Map of attached plugin types.
|
|
16
|
+
**/
|
|
17
|
+
var MultichainOpportunitiesService = class extends require_sdk_base_MultichainConstruct.MultichainConstruct {
|
|
18
|
+
/**
|
|
19
|
+
* Opportunities of all queried chains, see {@link OpportunitiesService.list}.
|
|
20
|
+
*
|
|
21
|
+
* A filter that names chains narrows the fan-out itself, so chains whose rows
|
|
22
|
+
* would be discarded are never queried and never appear in the meta.
|
|
23
|
+
**/
|
|
24
|
+
async list(filter) {
|
|
25
|
+
return this.queryChains({
|
|
26
|
+
networks: this.#networksOf(filter),
|
|
27
|
+
label: "list opportunities",
|
|
28
|
+
run: (sdk) => sdk.opportunities.list(filter)
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* {@inheritDoc OpportunitiesService.getPool}
|
|
33
|
+
**/
|
|
34
|
+
async getPool(key) {
|
|
35
|
+
return this.sdk.chain(key.chainId).opportunities.getPool(key);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* {@inheritDoc OpportunitiesService.getStrategy}
|
|
39
|
+
**/
|
|
40
|
+
async getStrategy(key) {
|
|
41
|
+
return this.sdk.chain(key.chainId).opportunities.getStrategy(key);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Chains named by the filter, or `undefined` to query all of them. Chain ids
|
|
45
|
+
* the SDK does not support are dropped here rather than reported as failures:
|
|
46
|
+
* a filter naming them is a narrowing, not a request.
|
|
47
|
+
**/
|
|
48
|
+
#networksOf(filter) {
|
|
49
|
+
if (!filter?.chainIds) return;
|
|
50
|
+
const networks = [];
|
|
51
|
+
for (const chainId of filter.chainIds) try {
|
|
52
|
+
networks.push(require_sdk_chain_chains.getNetworkType(chainId));
|
|
53
|
+
} catch {
|
|
54
|
+
this.sdk.logger?.debug(`ignoring unsupported chain ${chainId} in opportunities filter`);
|
|
55
|
+
}
|
|
56
|
+
return networks;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
//#endregion
|
|
60
|
+
exports.MultichainOpportunitiesService = MultichainOpportunitiesService;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_sdk_base_SDKConstruct = require("../base/SDKConstruct.js");
|
|
3
|
+
require("../base/index.js");
|
|
4
|
+
const require_sdk_market_math = require("../market/math.js");
|
|
5
|
+
//#region src/sdk/opportunities/OpportunitiesService.ts
|
|
6
|
+
/**
|
|
7
|
+
* A lookup that knows of no strategy, used when a filter rules strategies out
|
|
8
|
+
* and the credit-account query is skipped altogether.
|
|
9
|
+
**/
|
|
10
|
+
const NO_TOTALS = () => void 0;
|
|
11
|
+
/**
|
|
12
|
+
* Builds the `opportunities` read model from the chain.
|
|
13
|
+
*
|
|
14
|
+
* Everything except the credit-account totals comes from the market state the
|
|
15
|
+
* SDK already holds, so a list costs one RPC round-trip at most. Yield figures
|
|
16
|
+
* that fold in incentives, points or history are deliberately absent: they are
|
|
17
|
+
* the backend's job, and this service never guesses them.
|
|
18
|
+
*
|
|
19
|
+
* The rows themselves are assembled by the market wrappers — see
|
|
20
|
+
* {@link MarketSuite.opportunities} — because every value in them is market
|
|
21
|
+
* state. What is left here is the one thing no single market can answer: how
|
|
22
|
+
* much the credit accounts of a strategy are worth.
|
|
23
|
+
**/
|
|
24
|
+
var OpportunitiesService = class extends require_sdk_base_SDKConstruct.SDKConstruct {
|
|
25
|
+
/**
|
|
26
|
+
* Every pool and strategy of every loaded market on this chain.
|
|
27
|
+
*
|
|
28
|
+
* Strategies are measured by the value locked in their credit accounts, so
|
|
29
|
+
* the list issues one credit-account query unless the filter rules strategies
|
|
30
|
+
* out entirely.
|
|
31
|
+
*
|
|
32
|
+
* @param filter - Optional narrowing, applied to the built rows.
|
|
33
|
+
**/
|
|
34
|
+
async list(filter) {
|
|
35
|
+
if (filter?.chainIds && !filter.chainIds.includes(this.chainId)) return [];
|
|
36
|
+
const { markets } = this.sdk.marketRegister;
|
|
37
|
+
const totals = filter?.kind === "pool" ? NO_TOTALS : await this.#strategyTotals(markets);
|
|
38
|
+
return markets.flatMap((market) => market.opportunities(totals, filter));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A single pool opportunity plus its interest rate curve and quotas.
|
|
42
|
+
*
|
|
43
|
+
* @throws If no loaded market has this pool.
|
|
44
|
+
**/
|
|
45
|
+
async getPool(key) {
|
|
46
|
+
return this.sdk.marketRegister.findByPool(key.pool).poolOpportunityDetail();
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A single strategy opportunity plus the rate curve of the pool it borrows
|
|
50
|
+
* from and the price feeds its liquidation price depends on.
|
|
51
|
+
*
|
|
52
|
+
* @throws If the credit manager is unknown, or does not accept the requested
|
|
53
|
+
* collateral as a strategy.
|
|
54
|
+
**/
|
|
55
|
+
async getStrategy(key) {
|
|
56
|
+
const market = this.sdk.marketRegister.findByCreditManager(key.creditManager);
|
|
57
|
+
const { suite } = market.mustFindStrategy(key.creditManager, key.targetCollateral);
|
|
58
|
+
const totals = await this.#strategyTotals([market]);
|
|
59
|
+
return suite.strategyOpportunityDetail(key.targetCollateral, totals(key.creditManager, key.targetCollateral));
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Total value held by the credit accounts backing every strategy of the given
|
|
63
|
+
* markets.
|
|
64
|
+
*
|
|
65
|
+
* An account that holds several strategy collaterals counts in full towards
|
|
66
|
+
* each of them: the read model reports what a strategy's accounts are worth,
|
|
67
|
+
* not how that worth splits across the collaterals inside them.
|
|
68
|
+
**/
|
|
69
|
+
async #strategyTotals(markets) {
|
|
70
|
+
const wanted = /* @__PURE__ */ new Map();
|
|
71
|
+
for (const market of markets) for (const { suite, collateral } of market.strategies) {
|
|
72
|
+
const cm = suite.creditManager.address.toLowerCase();
|
|
73
|
+
const tokens = wanted.get(cm) ?? /* @__PURE__ */ new Set();
|
|
74
|
+
tokens.add(collateral.toLowerCase());
|
|
75
|
+
wanted.set(cm, tokens);
|
|
76
|
+
}
|
|
77
|
+
if (wanted.size === 0) return NO_TOTALS;
|
|
78
|
+
const accounts = await this.sdk.accounts.getCreditAccounts({ includeZeroDebt: true });
|
|
79
|
+
const totals = /* @__PURE__ */ new Map();
|
|
80
|
+
for (const account of accounts) {
|
|
81
|
+
const tokens = wanted.get(account.creditManager.toLowerCase());
|
|
82
|
+
if (!tokens) continue;
|
|
83
|
+
for (const token of account.tokens) {
|
|
84
|
+
if (token.balance <= 0n || !tokens.has(token.token.toLowerCase())) continue;
|
|
85
|
+
const key = strategyKey(account.creditManager, token.token);
|
|
86
|
+
const current = totals.get(key);
|
|
87
|
+
totals.set(key, {
|
|
88
|
+
value: (current?.value ?? 0n) + account.totalValue,
|
|
89
|
+
valueUsd: (current?.valueUsd ?? 0) + require_sdk_market_math.usdToNumber(account.totalValueUSD)
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return (creditManager, collateral) => totals.get(strategyKey(creditManager, collateral));
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Both halves of a strategy key folded into one map key.
|
|
98
|
+
**/
|
|
99
|
+
function strategyKey(creditManager, collateral) {
|
|
100
|
+
return `${creditManager.toLowerCase()}:${collateral.toLowerCase()}`;
|
|
101
|
+
}
|
|
102
|
+
//#endregion
|
|
103
|
+
exports.OpportunitiesService = OpportunitiesService;
|