@gearbox-protocol/sdk 14.12.0-next.72 → 14.12.0-next.74

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.
Files changed (67) hide show
  1. package/dist/cjs/dev/AccountOpener.js +2 -5
  2. package/dist/cjs/sdk/accounts/CreditAccountsServiceV310.js +49 -533
  3. package/dist/cjs/sdk/accounts/index.js +0 -4
  4. package/dist/cjs/sdk/accounts/intents/operations/unwrap-rwa-collateral/index.js +1 -1
  5. package/dist/cjs/sdk/accounts/intents/operations/wrap-rwa-collateral/index.js +1 -1
  6. package/dist/cjs/sdk/accounts/intents/testing/sdk-mock.js +2 -2
  7. package/dist/cjs/sdk/accounts/liquidations/LiquidationsService.js +3 -3
  8. package/dist/cjs/sdk/accounts/liquidations/constants.js +0 -2
  9. package/dist/cjs/sdk/accounts/liquidations/index.js +0 -2
  10. package/dist/cjs/sdk/constants/index.js +1 -0
  11. package/dist/cjs/sdk/constants/math.js +5 -0
  12. package/dist/cjs/sdk/index.js +8 -4
  13. package/dist/cjs/sdk/market/MarketRegister.js +10 -0
  14. package/dist/cjs/sdk/market/credit/CreditFacadeV310Contract.js +181 -0
  15. package/dist/cjs/sdk/market/credit/CreditSuite.js +107 -1
  16. package/dist/cjs/sdk/{accounts → market/credit}/dominantCollateral.js +18 -4
  17. package/dist/cjs/sdk/market/credit/index.js +3 -0
  18. package/dist/cjs/sdk/market/index.js +3 -0
  19. package/dist/cjs/sdk/market/math.js +59 -0
  20. package/dist/cjs/sdk/market/oracle/PriceOracleV310Contract.js +2 -9
  21. package/dist/cjs/sdk/opportunities/index.js +4 -0
  22. package/dist/esm/dev/AccountOpener.js +2 -5
  23. package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +50 -534
  24. package/dist/esm/sdk/accounts/index.js +1 -3
  25. package/dist/esm/sdk/accounts/intents/operations/unwrap-rwa-collateral/index.js +1 -1
  26. package/dist/esm/sdk/accounts/intents/operations/wrap-rwa-collateral/index.js +1 -1
  27. package/dist/esm/sdk/accounts/intents/testing/sdk-mock.js +2 -2
  28. package/dist/esm/sdk/accounts/liquidations/LiquidationsService.js +2 -2
  29. package/dist/esm/sdk/accounts/liquidations/constants.js +1 -2
  30. package/dist/esm/sdk/accounts/liquidations/index.js +1 -2
  31. package/dist/esm/sdk/constants/index.js +2 -2
  32. package/dist/esm/sdk/constants/math.js +5 -1
  33. package/dist/esm/sdk/index.js +4 -5
  34. package/dist/esm/sdk/market/MarketRegister.js +10 -0
  35. package/dist/esm/sdk/market/credit/CreditFacadeV310Contract.js +181 -0
  36. package/dist/esm/sdk/market/credit/CreditSuite.js +108 -2
  37. package/dist/esm/sdk/{accounts → market/credit}/dominantCollateral.js +18 -5
  38. package/dist/esm/sdk/market/credit/index.js +2 -1
  39. package/dist/esm/sdk/market/index.js +2 -1
  40. package/dist/esm/sdk/market/math.js +56 -1
  41. package/dist/esm/sdk/market/oracle/PriceOracleV310Contract.js +3 -10
  42. package/dist/esm/sdk/opportunities/index.js +2 -2
  43. package/dist/types/sdk/OnchainSDK.d.ts +1 -1
  44. package/dist/types/sdk/accounts/CreditAccountsServiceV310.d.ts +11 -78
  45. package/dist/types/sdk/accounts/index.d.ts +2 -4
  46. package/dist/types/sdk/accounts/liquidations/constants.d.ts +1 -2
  47. package/dist/types/sdk/accounts/liquidations/index.d.ts +1 -2
  48. package/dist/types/sdk/accounts/types.d.ts +42 -346
  49. package/dist/types/sdk/constants/index.d.ts +2 -2
  50. package/dist/types/sdk/constants/math.d.ts +5 -1
  51. package/dist/types/sdk/index.d.ts +10 -11
  52. package/dist/types/sdk/market/MarketRegister.d.ts +7 -0
  53. package/dist/types/sdk/market/credit/CreditFacadeV310Contract.d.ts +47 -2
  54. package/dist/types/sdk/market/credit/CreditSuite.d.ts +24 -2
  55. package/dist/types/sdk/{accounts → market/credit}/dominantCollateral.d.ts +13 -6
  56. package/dist/types/sdk/market/credit/index.d.ts +3 -2
  57. package/dist/types/sdk/market/credit/types.d.ts +112 -2
  58. package/dist/types/sdk/market/index.d.ts +3 -2
  59. package/dist/types/sdk/market/math.d.ts +60 -1
  60. package/dist/types/sdk/market/oracle/PriceOracleV310Contract.d.ts +1 -0
  61. package/dist/types/sdk/market/rwa/securitize/SecuritizeRWAFactory.d.ts +1 -1
  62. package/dist/types/sdk/market/rwa/types.d.ts +1 -1
  63. package/dist/types/sdk/opportunities/index.d.ts +2 -2
  64. package/package.json +1 -1
  65. package/dist/cjs/sdk/accounts/constants.js +0 -12
  66. package/dist/esm/sdk/accounts/constants.js +0 -11
  67. package/dist/types/sdk/accounts/constants.d.ts +0 -11
@@ -130,11 +130,70 @@ function additionalBorrowApyBps(quotaRate, leverage) {
130
130
  if (!Number.isFinite(leverage)) return 0;
131
131
  return Math.round(quotaRate * Math.max(leverage - 1, 0));
132
132
  }
133
+ /**
134
+ * {@link PERCENTAGE_FACTOR} less a 0.1% safety buffer.
135
+ *
136
+ * Partial liquidation amounts are computed off prices that can drift between
137
+ * quoting and execution, so both the seized and the repaid amount are pulled
138
+ * this far away from the boundary the contracts would revert on.
139
+ **/
140
+ const PARTIAL_LIQUIDATION_BUFFER_BPS = 9990n;
141
+ /**
142
+ * Minimum collateral a partial liquidation must seize for a given repayment,
143
+ * derived from the liquidation discount and buffered by
144
+ * {@link PARTIAL_LIQUIDATION_BUFFER_BPS}.
145
+ *
146
+ * @param tokenAmount - Repaid amount converted from underlying into the seized
147
+ * token by the oracle.
148
+ * @param liquidationDiscount - Discount in effect for this account, in basis
149
+ * points (the expired variant once the credit manager has expired).
150
+ **/
151
+ function minSeizedAmount(tokenAmount, liquidationDiscount) {
152
+ return tokenAmount * PARTIAL_LIQUIDATION_BUFFER_BPS / BigInt(liquidationDiscount);
153
+ }
154
+ /**
155
+ * Amount of underlying whose repayment brings the account's health factor close
156
+ * to `optimalHF`, capped so the account keeps at least `minDebt` of debt.
157
+ *
158
+ * Ported from solidity:
159
+ * https://github.com/Gearbox-protocol/router-v3/blob/56e2d515ec6d9bb1e324e71c3708e59710779b24/contracts/liquidation/AbstractLiquidator.sol#L292
160
+ *
161
+ * @returns The repaid amount, or `0n` when the account is already healthy
162
+ * enough or carries less than the minimum debt.
163
+ * @throws If the discounted target health factor does not exceed the seized
164
+ * token's liquidation threshold, in which case no repayment improves the
165
+ * account.
166
+ **/
167
+ function optimalRepaidAmount({ totalDebt, twvUnderlying, minDebt, optimalHF, discount, ltTokenOut }) {
168
+ const denominator = discount * optimalHF / require_sdk_constants_math.PERCENTAGE_FACTOR - ltTokenOut;
169
+ if (denominator <= 0n) throw new Error("cannot compute optimal repaid amount: invalid liquidation parameters (discount * hfOptimal <= ltTokenOut)");
170
+ const numerator = totalDebt * optimalHF - twvUnderlying * require_sdk_constants_math.PERCENTAGE_FACTOR;
171
+ if (numerator <= 0n) return 0n;
172
+ const repaidAmount = numerator / denominator * discount / require_sdk_constants_math.PERCENTAGE_FACTOR;
173
+ if (totalDebt < minDebt) return 0n;
174
+ const surplusDebt = totalDebt - minDebt;
175
+ if (repaidAmount > surplusDebt) return surplusDebt * PARTIAL_LIQUIDATION_BUFFER_BPS / require_sdk_constants_math.PERCENTAGE_FACTOR;
176
+ return repaidAmount;
177
+ }
178
+ /**
179
+ * Health factor a partial liquidation should target, in basis points: just
180
+ * above 1, by enough to cover up to 1% of borrow cost so the account does not
181
+ * fall back under water immediately.
182
+ *
183
+ * @param borrowRate - Blended borrow rate of the account, in basis points.
184
+ **/
185
+ function optimalHFForPartialLiquidation(borrowRate) {
186
+ return require_sdk_constants_math.PERCENTAGE_FACTOR + (borrowRate < 100n ? borrowRate : 100n);
187
+ }
133
188
  //#endregion
189
+ exports.PARTIAL_LIQUIDATION_BUFFER_BPS = PARTIAL_LIQUIDATION_BUFFER_BPS;
134
190
  exports.additionalBorrowApyBps = additionalBorrowApyBps;
135
191
  exports.borrowApyBps = borrowApyBps;
136
192
  exports.healthFactorBps = healthFactorBps;
137
193
  exports.maxLeverage = maxLeverage;
194
+ exports.minSeizedAmount = minSeizedAmount;
195
+ exports.optimalHFForPartialLiquidation = optimalHFForPartialLiquidation;
196
+ exports.optimalRepaidAmount = optimalRepaidAmount;
138
197
  exports.positionLeverage = positionLeverage;
139
198
  exports.rayToBps = rayToBps;
140
199
  exports.usdToNumber = usdToNumber;
@@ -4,7 +4,6 @@ const require_sdk_utils_viem_simulateWithPriceUpdates = require("../../utils/vie
4
4
  const require_sdk_market_pricefeeds_getRawPriceUpdates = require("../pricefeeds/getRawPriceUpdates.js");
5
5
  require("../pricefeeds/index.js");
6
6
  const require_sdk_market_oracle_PriceOracleBaseContract = require("./PriceOracleBaseContract.js");
7
- let viem = require("viem");
8
7
  //#region src/sdk/market/oracle/PriceOracleV310Contract.ts
9
8
  const abi = require_abi_310_generated.iPriceOracleV310Abi;
10
9
  var PriceOracleV310Contract = class extends require_sdk_market_oracle_PriceOracleBaseContract.PriceOracleBaseContract {
@@ -20,6 +19,7 @@ var PriceOracleV310Contract = class extends require_sdk_market_oracle_PriceOracl
20
19
  * @param creditFacade
21
20
  * @param updates
22
21
  * @returns
22
+ * @throws If `creditFacade` does not belong to a loaded market.
23
23
  */
24
24
  onDemandPriceUpdates(creditFacade, updates) {
25
25
  if (!updates) {
@@ -32,14 +32,7 @@ var PriceOracleV310Contract = class extends require_sdk_market_oracle_PriceOracl
32
32
  const raw = require_sdk_market_pricefeeds_getRawPriceUpdates.getRawPriceUpdates(updates);
33
33
  return {
34
34
  raw,
35
- multicall: [{
36
- target: creditFacade,
37
- callData: (0, viem.encodeFunctionData)({
38
- abi: require_abi_310_generated.iCreditFacadeMulticallV310Abi,
39
- functionName: "onDemandPriceUpdates",
40
- args: [raw]
41
- })
42
- }]
35
+ multicall: [this.sdk.marketRegister.findCreditFacade(creditFacade).prepareOnDemandPriceUpdates(raw)]
43
36
  };
44
37
  }
45
38
  /**
@@ -4,10 +4,14 @@ const require_sdk_opportunities_MultichainOpportunitiesService = require("./Mult
4
4
  const require_sdk_opportunities_OpportunitiesService = require("./OpportunitiesService.js");
5
5
  exports.MultichainOpportunitiesService = require_sdk_opportunities_MultichainOpportunitiesService.MultichainOpportunitiesService;
6
6
  exports.OpportunitiesService = require_sdk_opportunities_OpportunitiesService.OpportunitiesService;
7
+ exports.PARTIAL_LIQUIDATION_BUFFER_BPS = require_sdk_market_math.PARTIAL_LIQUIDATION_BUFFER_BPS;
7
8
  exports.additionalBorrowApyBps = require_sdk_market_math.additionalBorrowApyBps;
8
9
  exports.borrowApyBps = require_sdk_market_math.borrowApyBps;
9
10
  exports.healthFactorBps = require_sdk_market_math.healthFactorBps;
10
11
  exports.maxLeverage = require_sdk_market_math.maxLeverage;
12
+ exports.minSeizedAmount = require_sdk_market_math.minSeizedAmount;
13
+ exports.optimalHFForPartialLiquidation = require_sdk_market_math.optimalHFForPartialLiquidation;
14
+ exports.optimalRepaidAmount = require_sdk_market_math.optimalRepaidAmount;
11
15
  exports.positionLeverage = require_sdk_market_math.positionLeverage;
12
16
  exports.rayToBps = require_sdk_market_math.rayToBps;
13
17
  exports.usdToNumber = require_sdk_market_math.usdToNumber;
@@ -276,7 +276,7 @@ var AccountOpener = class extends SDKConstruct {
276
276
  averageQuota,
277
277
  minQuota
278
278
  }, "calculated quotas");
279
- const { tx, calls } = await this.#service.openCA({
279
+ const tx = await this.#service.openCA({
280
280
  creditManager: cm.creditManager.address,
281
281
  averageQuota,
282
282
  minQuota,
@@ -291,10 +291,7 @@ var AccountOpener = class extends SDKConstruct {
291
291
  to: borrower.address,
292
292
  referralCode: 0n
293
293
  });
294
- for (let i = 0; i < calls.length; i++) {
295
- const call = calls[i];
296
- logger?.debug(`call #${i + 1}: ${this.sdk.parseFunctionData(call.target, call.callData)}`);
297
- }
294
+ logger?.debug(`open account tx: ${this.sdk.stringifyFunctionData(tx.to, tx.callData)}`);
298
295
  logger?.debug("prepared open account transaction");
299
296
  return {
300
297
  tx,