@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
|
@@ -9,6 +9,9 @@ const require_sdk_utils_hex = require("../../utils/hex.js");
|
|
|
9
9
|
require("../../utils/index.js");
|
|
10
10
|
const require_sdk_base_SDKConstruct = require("../../base/SDKConstruct.js");
|
|
11
11
|
require("../../base/index.js");
|
|
12
|
+
const require_sdk_market_math = require("../../market/math.js");
|
|
13
|
+
const require_model_liquidations = require("../../../model/liquidations.js");
|
|
14
|
+
require("../../../model/index.js");
|
|
12
15
|
const require_sdk_market_rwa_midas_constants = require("../../market/rwa/midas/constants.js");
|
|
13
16
|
const require_sdk_market_rwa_midas_MidasLiquidatorContract = require("../../market/rwa/midas/MidasLiquidatorContract.js");
|
|
14
17
|
require("../../market/rwa/midas/index.js");
|
|
@@ -16,95 +19,104 @@ const require_sdk_market_rwa_securitize_constants = require("../../market/rwa/se
|
|
|
16
19
|
const require_sdk_market_rwa_securitize_SecuritizeLiquidatorContract = require("../../market/rwa/securitize/SecuritizeLiquidatorContract.js");
|
|
17
20
|
require("../../market/rwa/securitize/index.js");
|
|
18
21
|
const require_sdk_accounts_liquidations_constants = require("./constants.js");
|
|
19
|
-
const require_sdk_accounts_liquidations_helpers = require("./helpers.js");
|
|
20
22
|
//#region src/sdk/accounts/liquidations/LiquidationsService.ts
|
|
21
23
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* Discovers liquidatable credit accounts (health factor below 1 plus accounts
|
|
25
|
-
* of expired credit managers with outstanding debt) and previews what a
|
|
26
|
-
* manual liquidation pays and receives.
|
|
24
|
+
* Service for discovering liquidatable credit accounts and previewing manual
|
|
25
|
+
* liquidations.
|
|
27
26
|
**/
|
|
28
27
|
var LiquidationsService = class extends require_sdk_base_SDKConstruct.SDKConstruct {
|
|
29
28
|
/**
|
|
30
|
-
*
|
|
29
|
+
* Returns all liquidatable credit accounts: accounts with health factor
|
|
30
|
+
* below 1 plus accounts of expired credit managers with outstanding debt.
|
|
31
|
+
* Accounts whose collateral computation failed are excluded.
|
|
31
32
|
**/
|
|
32
33
|
async getLiquidatableAccounts(props) {
|
|
33
34
|
await this.sdk.withdrawalCompressor?.loadWithdrawableAssets();
|
|
34
|
-
const
|
|
35
|
+
const unhealthy = await this.sdk.accounts.getCreditAccounts({
|
|
35
36
|
maxHealthFactor: require_sdk_constants_math.WAD - 1n,
|
|
36
37
|
includeZeroDebt: false
|
|
37
38
|
});
|
|
38
|
-
const seen = new require_sdk_utils_AddressSet.AddressSet(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
const allowedAssets = props?.assets?.length ? new require_sdk_utils_AddressSet.AddressSet(props.assets) : void 0;
|
|
49
|
-
return rows.filter((row) => {
|
|
50
|
-
if (props?.delayed !== void 0 && row.isDelayed !== props.delayed) return false;
|
|
51
|
-
if (allowedAssets && !allowedAssets.has(row.asset)) return false;
|
|
52
|
-
return true;
|
|
53
|
-
});
|
|
39
|
+
const seen = new require_sdk_utils_AddressSet.AddressSet(unhealthy.map((ca) => ca.creditAccount));
|
|
40
|
+
let expired = await this.#getExpiredCreditAccounts();
|
|
41
|
+
expired = expired.filter((ca) => !seen.has(ca.creditAccount));
|
|
42
|
+
return [...unhealthy, ...expired].flatMap((ca) => {
|
|
43
|
+
if (!ca.success) {
|
|
44
|
+
this.logger?.warn(`cannot compute liquidation details for ${ca.creditAccount}: collateral computation failed`);
|
|
45
|
+
return [];
|
|
46
|
+
}
|
|
47
|
+
return [this.#buildAccount(ca)];
|
|
48
|
+
}).filter((row) => require_model_liquidations.matchesLiquidatableAccountFilter(row, props));
|
|
54
49
|
}
|
|
55
50
|
/**
|
|
56
|
-
*
|
|
51
|
+
* Returns detailed information about a liquidatable credit account,
|
|
52
|
+
* including the full list of assets the liquidator receives.
|
|
53
|
+
*
|
|
54
|
+
* @throws When the account is not found or its collateral computation fails.
|
|
57
55
|
**/
|
|
58
56
|
async getLiquidationDetails(props) {
|
|
59
57
|
const { creditAccount, liquidator, ignoreReservePrices } = props;
|
|
60
58
|
const ca = await this.#getCreditAccountData(creditAccount);
|
|
59
|
+
const suite = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
60
|
+
const { priceOracle } = suite.market;
|
|
61
61
|
await this.sdk.withdrawalCompressor?.loadWithdrawableAssets();
|
|
62
|
-
const account = this.#buildAccount(ca);
|
|
62
|
+
const account = this.#buildAccount(ca, suite);
|
|
63
63
|
const data = await this.#getLiquidationData(ca, liquidator, ignoreReservePrices);
|
|
64
|
-
const suite = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
65
64
|
return {
|
|
66
65
|
...account,
|
|
67
|
-
repaymentAmount:
|
|
68
|
-
token: require_sdk_utils_hex.hexEq(data.requiredToken, "0x0000000000000000000000000000000000000000") ? account.totalValue.token : data.requiredToken,
|
|
69
|
-
balance: data.requiredAmount
|
|
70
|
-
},
|
|
66
|
+
repaymentAmount: priceOracle.toTokenAmount(require_sdk_utils_hex.hexEq(data.requiredToken, "0x0000000000000000000000000000000000000000") ? account.totalValue.token.address : data.requiredToken, data.requiredAmount),
|
|
71
67
|
isDelayed: data.expectedOutputs.some((o) => o.delayed),
|
|
72
|
-
receivedAssets:
|
|
68
|
+
receivedAssets: this.#receivedAssets(data.expectedOutputs, priceOracle),
|
|
73
69
|
isLiquidatorEligible: data.isLiquidatorEligible,
|
|
74
70
|
isCreditAccountFrozen: data.isCreditAccountFrozen,
|
|
75
71
|
kycProtocol: data.kycProtocol || void 0,
|
|
76
|
-
kycToken: require_sdk_utils_hex.hexEq(data.kycToken, "0x0000000000000000000000000000000000000000") ? void 0 : data.kycToken,
|
|
77
|
-
approve:
|
|
78
|
-
target: data.liquidationCall.target,
|
|
79
|
-
creditFacade: suite.creditFacade.address,
|
|
80
|
-
creditManager: ca.creditManager,
|
|
81
|
-
token: data.requiredToken,
|
|
82
|
-
amount: data.requiredAmount
|
|
83
|
-
})
|
|
72
|
+
kycToken: require_sdk_utils_hex.hexEq(data.kycToken, "0x0000000000000000000000000000000000000000") ? void 0 : this.sdk.tokensMeta.getToken(data.kycToken),
|
|
73
|
+
approve: this.#liquidationApproval(data, suite, priceOracle)
|
|
84
74
|
};
|
|
85
75
|
}
|
|
86
76
|
/**
|
|
87
|
-
*
|
|
77
|
+
* Builds the transaction that fully liquidates a credit account, repaying
|
|
78
|
+
* the debt from own funds and receiving the collateral from the credit account.
|
|
88
79
|
**/
|
|
89
80
|
async buildLiquidationTx(props) {
|
|
90
81
|
const { creditAccount, liquidator, ignoreReservePrices } = props;
|
|
91
82
|
const ca = await this.#getCreditAccountData(creditAccount);
|
|
92
|
-
const
|
|
93
|
-
|
|
83
|
+
const { liquidationCall } = await this.#getLiquidationData(ca, liquidator, ignoreReservePrices);
|
|
84
|
+
this.logger?.debug(`built tx to liquidate credit account ${this.labelAddress(creditAccount)} to ${this.labelAddress(liquidator)}`);
|
|
85
|
+
return {
|
|
86
|
+
to: liquidationCall.target,
|
|
87
|
+
callData: liquidationCall.callData
|
|
88
|
+
};
|
|
94
89
|
}
|
|
95
90
|
/**
|
|
96
|
-
*
|
|
91
|
+
* Returns the status of delayed-withdrawal positions (redemption receipts)
|
|
92
|
+
* owned by a liquidator wallet: what is receivable, how much, and when it
|
|
93
|
+
* becomes claimable.
|
|
97
94
|
**/
|
|
98
|
-
async
|
|
95
|
+
async getLiquidationPositions(props) {
|
|
99
96
|
const compressor = this.sdk.withdrawalCompressor;
|
|
100
97
|
if (!compressor) return [];
|
|
101
98
|
await compressor.loadWithdrawableAssets();
|
|
102
99
|
const phantomTokens = new require_sdk_utils_AddressSet.AddressSet(compressor.getWithdrawableAssets().map((a) => a.withdrawalPhantomToken));
|
|
103
|
-
const
|
|
104
|
-
|
|
100
|
+
const { claimable, pending } = await compressor.getExternalAccountCurrentWithdrawals(props.liquidator, ...phantomTokens.asArray());
|
|
101
|
+
const chainId = this.sdk.chainId;
|
|
102
|
+
return [...claimable.map((w) => ({
|
|
103
|
+
chainId,
|
|
104
|
+
sourceToken: this.sdk.tokensMeta.mustGetToken(w.token),
|
|
105
|
+
output: this.#withdrawalOutput(w.outputs, w.token),
|
|
106
|
+
claimTx: this.#claimTx(w.claimCalls, w.token),
|
|
107
|
+
redeemer: w.redeemer
|
|
108
|
+
})), ...pending.map((w) => ({
|
|
109
|
+
chainId,
|
|
110
|
+
sourceToken: this.sdk.tokensMeta.mustGetToken(w.token),
|
|
111
|
+
output: this.#withdrawalOutput(w.expectedOutputs, w.token),
|
|
112
|
+
claimableAt: Number(w.claimableAt),
|
|
113
|
+
redeemer: w.redeemer
|
|
114
|
+
}))];
|
|
105
115
|
}
|
|
106
116
|
/**
|
|
107
|
-
*
|
|
117
|
+
* Discovers the dedicated RWA liquidator contracts (Securitize, Midas)
|
|
118
|
+
* deployed for the markets of the chain and registers them in the SDK
|
|
119
|
+
* contracts register.
|
|
108
120
|
**/
|
|
109
121
|
async loadRWALiquidators() {
|
|
110
122
|
const configurators = this.sdk.marketRegister.marketConfigurators;
|
|
@@ -121,14 +133,6 @@ var LiquidationsService = class extends require_sdk_base_SDKConstruct.SDKConstru
|
|
|
121
133
|
});
|
|
122
134
|
for (const info of resp.flat()) this.#createRWALiquidator(info);
|
|
123
135
|
}
|
|
124
|
-
/**
|
|
125
|
-
* Instantiates the liquidator contract, which registers it and labels its
|
|
126
|
-
* address (see `BaseContract`).
|
|
127
|
-
*
|
|
128
|
-
* @param info - Liquidator discovered by the compressor. The same gateway
|
|
129
|
-
* can be configured in several credit managers, so duplicates are expected
|
|
130
|
-
* and skipped.
|
|
131
|
-
**/
|
|
132
136
|
#createRWALiquidator(info) {
|
|
133
137
|
const liquidator = info.liquidatorAddress;
|
|
134
138
|
if (this.sdk.getContract(liquidator)) return;
|
|
@@ -143,20 +147,64 @@ var LiquidationsService = class extends require_sdk_base_SDKConstruct.SDKConstru
|
|
|
143
147
|
return;
|
|
144
148
|
}
|
|
145
149
|
}
|
|
150
|
+
#anyMarketTokenAmount(token, value) {
|
|
151
|
+
const meta = this.sdk.tokensMeta.mustGetToken(token);
|
|
152
|
+
for (const market of this.sdk.marketRegister.markets) {
|
|
153
|
+
const valueUsd = market.priceOracle.safeUsdValue(token, value);
|
|
154
|
+
if (valueUsd !== null) return {
|
|
155
|
+
token: meta,
|
|
156
|
+
value,
|
|
157
|
+
valueUsd
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
token: meta,
|
|
162
|
+
value,
|
|
163
|
+
valueUsd: null
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
#withdrawalOutput(outputs, sourceToken) {
|
|
167
|
+
const [output] = outputs;
|
|
168
|
+
if (outputs.length !== 1 || !output) throw new Error(`expected exactly one output for withdrawal of ${sourceToken}, got ${outputs.length}`);
|
|
169
|
+
return this.#anyMarketTokenAmount(output.token, output.amount);
|
|
170
|
+
}
|
|
171
|
+
#claimTx(claimCalls, sourceToken) {
|
|
172
|
+
if (claimCalls.length > 1) throw new Error(`expected at most one claim call for withdrawal of ${sourceToken}, got ${claimCalls.length}`);
|
|
173
|
+
const [call] = claimCalls;
|
|
174
|
+
return call ? {
|
|
175
|
+
to: call.target,
|
|
176
|
+
callData: call.callData
|
|
177
|
+
} : void 0;
|
|
178
|
+
}
|
|
179
|
+
#receivedAssets(outputs, priceOracle) {
|
|
180
|
+
return outputs.map((o) => {
|
|
181
|
+
const amount = priceOracle.toTokenAmount(o.token, o.amount);
|
|
182
|
+
if (!o.delayed) return {
|
|
183
|
+
isDelayed: false,
|
|
184
|
+
...amount
|
|
185
|
+
};
|
|
186
|
+
return {
|
|
187
|
+
isDelayed: true,
|
|
188
|
+
...amount,
|
|
189
|
+
redeemerAddress: require_sdk_utils_hex.hexEq(o.redeemerAddress, "0x0000000000000000000000000000000000000000") ? void 0 : o.redeemerAddress,
|
|
190
|
+
claimableAt: o.claimableAt === 0n ? void 0 : Number(o.claimableAt)
|
|
191
|
+
};
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
#liquidationApproval(data, suite, priceOracle) {
|
|
195
|
+
const { requiredToken, requiredAmount, liquidationCall } = data;
|
|
196
|
+
if (requiredAmount === 0n) return;
|
|
197
|
+
return {
|
|
198
|
+
spender: require_sdk_utils_hex.hexEq(liquidationCall.target, suite.creditFacade.address) ? suite.creditManager.address : liquidationCall.target,
|
|
199
|
+
...priceOracle.toTokenAmount(requiredToken, requiredAmount * (require_sdk_constants_math.PERCENTAGE_FACTOR + require_sdk_accounts_liquidations_constants.LIQUIDATION_APPROVAL_BUFFER) / require_sdk_constants_math.PERCENTAGE_FACTOR)
|
|
200
|
+
};
|
|
201
|
+
}
|
|
146
202
|
async #getCreditAccountData(creditAccount) {
|
|
147
203
|
const ca = await this.sdk.accounts.getCreditAccountData(creditAccount);
|
|
148
204
|
if (!ca) throw new Error(`credit account ${creditAccount} not found`);
|
|
149
205
|
if (!ca.success) throw new Error(`cannot compute liquidation details for ${creditAccount}: collateral computation failed`);
|
|
150
206
|
return ca;
|
|
151
207
|
}
|
|
152
|
-
/**
|
|
153
|
-
* Previews the liquidation via the liquidation compressor.
|
|
154
|
-
*
|
|
155
|
-
* @param ca - Credit account to liquidate
|
|
156
|
-
* @param liquidator - Liquidator wallet, zero address when not known: it only
|
|
157
|
-
* affects the KYC eligibility fields and the receiver encoded into the calls
|
|
158
|
-
* @param ignoreReservePrices - Exclude reserve price feeds from the updates
|
|
159
|
-
**/
|
|
160
208
|
async #getLiquidationData(ca, liquidator = require_sdk_constants_addresses.ADDRESS_0X0, ignoreReservePrices) {
|
|
161
209
|
const priceUpdates = await this.sdk.accounts.getOnDemandPriceUpdates(ca, ignoreReservePrices);
|
|
162
210
|
const { result } = await this.client.simulateContract({
|
|
@@ -171,10 +219,6 @@ var LiquidationsService = class extends require_sdk_base_SDKConstruct.SDKConstru
|
|
|
171
219
|
});
|
|
172
220
|
return result;
|
|
173
221
|
}
|
|
174
|
-
/**
|
|
175
|
-
* Accounts of expired credit managers with outstanding debt are liquidatable
|
|
176
|
-
* regardless of their health factor.
|
|
177
|
-
**/
|
|
178
222
|
async #getExpiredCreditAccounts() {
|
|
179
223
|
const expiredCMs = [];
|
|
180
224
|
for (const market of this.sdk.marketRegister.markets) {
|
|
@@ -186,58 +230,58 @@ var LiquidationsService = class extends require_sdk_base_SDKConstruct.SDKConstru
|
|
|
186
230
|
}
|
|
187
231
|
if (expiredCMs.length === 0) return [];
|
|
188
232
|
this.logger?.debug(`getting credit accounts of ${expiredCMs.length} expired credit managers`);
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
includeZeroDebt: false
|
|
194
|
-
});
|
|
195
|
-
result.push(...accounts);
|
|
196
|
-
}
|
|
197
|
-
return result;
|
|
233
|
+
return (await Promise.all(expiredCMs.map((creditManager) => this.sdk.accounts.getCreditAccounts({
|
|
234
|
+
creditManager,
|
|
235
|
+
includeZeroDebt: false
|
|
236
|
+
})))).flat();
|
|
198
237
|
}
|
|
199
|
-
|
|
200
|
-
* Requires the compressor's withdrawable assets cache to be loaded
|
|
201
|
-
* (see `loadWithdrawableAssets`) so that phantom token lookups are sync.
|
|
202
|
-
**/
|
|
203
|
-
#buildAccount(ca) {
|
|
204
|
-
const compressor = this.sdk.withdrawalCompressor;
|
|
205
|
-
const suite = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
238
|
+
#buildAccount(ca, suite = this.sdk.marketRegister.findCreditManager(ca.creditManager)) {
|
|
206
239
|
const market = suite.market;
|
|
207
|
-
const
|
|
208
|
-
const unwrappedUnderlying = meta && this.sdk.tokensMeta.isRWAUnderlying(meta) ? meta.asset : suite.underlying;
|
|
240
|
+
const unwrappedUnderlying = this.sdk.tokensMeta.unwrapRWA(suite.underlying);
|
|
209
241
|
const liquidationDiscount = suite.isExpired ? suite.creditManager.liquidationDiscountExpired : suite.creditManager.liquidationDiscount;
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
return 0n;
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
if (asset) asset = compressor?.getWithdrawalSourceToken(asset) ?? asset;
|
|
218
|
-
else asset = unwrappedUnderlying;
|
|
242
|
+
const repaymentAmount = ca.totalValue * BigInt(liquidationDiscount) / require_sdk_constants_math.PERCENTAGE_FACTOR;
|
|
243
|
+
const estimatedProfit = ca.totalValue - repaymentAmount;
|
|
244
|
+
const token = this.sdk.tokensMeta.mustGetToken(unwrappedUnderlying);
|
|
245
|
+
const usd = (part) => ca.totalValue > 0n ? require_sdk_market_math.usdToNumber(ca.totalValueUSD * part / ca.totalValue) : 0;
|
|
219
246
|
return {
|
|
247
|
+
chainId: this.sdk.chainId,
|
|
220
248
|
creditAccount: ca.creditAccount,
|
|
221
249
|
creditManager: ca.creditManager,
|
|
222
|
-
|
|
223
|
-
asset,
|
|
250
|
+
asset: this.sdk.tokensMeta.mustGetToken(this.#mainAsset(ca, market, unwrappedUnderlying)),
|
|
224
251
|
totalValue: {
|
|
225
|
-
token
|
|
226
|
-
|
|
252
|
+
token,
|
|
253
|
+
value: ca.totalValue,
|
|
254
|
+
valueUsd: require_sdk_market_math.usdToNumber(ca.totalValueUSD)
|
|
227
255
|
},
|
|
228
|
-
totalValueUSD: ca.totalValueUSD,
|
|
229
256
|
repaymentAmount: {
|
|
230
|
-
token
|
|
231
|
-
|
|
257
|
+
token,
|
|
258
|
+
value: repaymentAmount,
|
|
259
|
+
valueUsd: usd(repaymentAmount)
|
|
232
260
|
},
|
|
233
261
|
estimatedProfit: {
|
|
234
|
-
token
|
|
235
|
-
|
|
262
|
+
token,
|
|
263
|
+
value: estimatedProfit,
|
|
264
|
+
valueUsd: usd(estimatedProfit)
|
|
236
265
|
},
|
|
237
|
-
isDelayed: ca.tokens.some((t) => t.balance > 10n && !!
|
|
238
|
-
paused: suite.creditFacade.isPaused
|
|
266
|
+
isDelayed: ca.tokens.some((t) => t.balance > 10n && !!this.sdk.withdrawalCompressor?.getWithdrawalSourceToken(t.token)),
|
|
267
|
+
paused: suite.creditFacade.isPaused,
|
|
268
|
+
rwa: market.rwa
|
|
239
269
|
};
|
|
240
270
|
}
|
|
271
|
+
#mainAsset(ca, market, fallback) {
|
|
272
|
+
let bestValue = 0;
|
|
273
|
+
let asset;
|
|
274
|
+
for (const t of ca.tokens) {
|
|
275
|
+
if (require_sdk_utils_hex.hexEq(t.token, ca.underlying) || (t.mask & ca.enabledTokensMask) === 0n || t.balance <= 10n) continue;
|
|
276
|
+
const value = market.priceOracle.safeUsdValue(t.token, t.balance) ?? 0;
|
|
277
|
+
if (value > bestValue) {
|
|
278
|
+
bestValue = value;
|
|
279
|
+
asset = t.token;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (!asset) return fallback;
|
|
283
|
+
return this.sdk.withdrawalCompressor?.getWithdrawalSourceToken(asset) ?? asset;
|
|
284
|
+
}
|
|
241
285
|
};
|
|
242
286
|
//#endregion
|
|
243
287
|
exports.LiquidationsService = LiquidationsService;
|
|
@@ -1,66 +1,60 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_sdk_base_MultichainConstruct = require("../../base/MultichainConstruct.js");
|
|
3
|
+
require("../../base/index.js");
|
|
2
4
|
//#region src/sdk/accounts/liquidations/MultichainLiquidationsService.ts
|
|
3
5
|
/**
|
|
4
|
-
* Cross-chain
|
|
6
|
+
* Cross-chain counterpart of {@link LiquidationsService}.
|
|
5
7
|
*
|
|
6
|
-
*
|
|
7
|
-
* {@link
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* Fans out over all chains configured in {@link MultichainSDK} (optionally
|
|
9
|
+
* restricted via {@link MultichainNetworksProps.networks}). A failed chain is
|
|
10
|
+
* logged as a warning and skipped, so one dead RPC does not empty the whole
|
|
11
|
+
* list; its failure is reported in {@link MultichainResult.meta}. Networks that
|
|
12
|
+
* are not configured in the SDK are reported there the same way.
|
|
10
13
|
**/
|
|
11
|
-
var MultichainLiquidationsService = class {
|
|
12
|
-
#sdk;
|
|
13
|
-
constructor(sdk) {
|
|
14
|
-
this.#sdk = sdk;
|
|
15
|
-
}
|
|
14
|
+
var MultichainLiquidationsService = class extends require_sdk_base_MultichainConstruct.MultichainConstruct {
|
|
16
15
|
/**
|
|
17
|
-
*
|
|
16
|
+
* Accounts of all queried chains, see
|
|
17
|
+
* {@link LiquidationsService.getLiquidatableAccounts}.
|
|
18
18
|
**/
|
|
19
19
|
async getLiquidatableAccounts(props) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const [network, chainSdk] = chains[i];
|
|
25
|
-
if (result.status === "fulfilled") accounts.push(...result.value);
|
|
26
|
-
else chainSdk.logger?.warn(result.reason, `failed to get liquidatable accounts on ${network}`);
|
|
20
|
+
return this.queryChains({
|
|
21
|
+
networks: props?.networks,
|
|
22
|
+
label: "get liquidatable accounts",
|
|
23
|
+
run: (sdk) => sdk.liquidations.getLiquidatableAccounts(props)
|
|
27
24
|
});
|
|
28
|
-
return accounts;
|
|
29
25
|
}
|
|
30
26
|
/**
|
|
31
|
-
* {@inheritDoc
|
|
27
|
+
* {@inheritDoc LiquidationsService.getLiquidationDetails}
|
|
32
28
|
**/
|
|
33
29
|
async getLiquidationDetails(props) {
|
|
34
|
-
return this
|
|
30
|
+
return this.sdk.chain(props.network).liquidations.getLiquidationDetails(props);
|
|
35
31
|
}
|
|
36
32
|
/**
|
|
37
|
-
* {@inheritDoc
|
|
33
|
+
* {@inheritDoc LiquidationsService.buildLiquidationTx}
|
|
38
34
|
**/
|
|
39
35
|
async buildLiquidationTx(props) {
|
|
40
|
-
return this
|
|
36
|
+
return this.sdk.chain(props.network).liquidations.buildLiquidationTx(props);
|
|
41
37
|
}
|
|
42
38
|
/**
|
|
43
|
-
*
|
|
39
|
+
* Liquidation positions of all queried chains, see
|
|
40
|
+
* {@link LiquidationsService.getLiquidationPositions}.
|
|
44
41
|
**/
|
|
45
|
-
async
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const [network, chainSdk] = chains[i];
|
|
51
|
-
if (result.status === "fulfilled") withdrawals.push(...result.value);
|
|
52
|
-
else chainSdk.logger?.warn(result.reason, `failed to get liquidator withdrawals on ${network}`);
|
|
42
|
+
async getLiquidationPositions(props) {
|
|
43
|
+
return this.queryChains({
|
|
44
|
+
networks: props.networks,
|
|
45
|
+
label: "get liquidation positions",
|
|
46
|
+
run: (sdk) => sdk.liquidations.getLiquidationPositions(props)
|
|
53
47
|
});
|
|
54
|
-
return withdrawals;
|
|
55
48
|
}
|
|
56
49
|
/**
|
|
57
|
-
*
|
|
50
|
+
* Loads the liquidators of all queried chains, see
|
|
51
|
+
* {@link LiquidationsService.loadRWALiquidators}.
|
|
58
52
|
**/
|
|
59
53
|
async loadRWALiquidators(props) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
54
|
+
return this.runChains({
|
|
55
|
+
networks: props?.networks,
|
|
56
|
+
label: "load RWA liquidators",
|
|
57
|
+
run: (sdk) => sdk.liquidations.loadRWALiquidators()
|
|
64
58
|
});
|
|
65
59
|
}
|
|
66
60
|
};
|
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
//#region src/sdk/accounts/liquidations/constants.ts
|
|
3
3
|
const LIQUIDATION_COMPRESSOR_V313_ADDRESS = "0xB70C4500a0afF02107eB983a348F22492fB6dC94";
|
|
4
|
+
/**
|
|
5
|
+
* Token balances at or below this threshold are treated as dust and ignored,
|
|
6
|
+
* consistent with the rest of the SDK (see `filterDust`).
|
|
7
|
+
**/
|
|
8
|
+
const DUST_THRESHOLD = 10n;
|
|
9
|
+
/**
|
|
10
|
+
* Headroom (in bps) added on top of the amount the liquidation pulls when
|
|
11
|
+
* building the liquidator's approval, so that the transaction does not revert
|
|
12
|
+
* when prices move between the preview and the execution.
|
|
13
|
+
**/
|
|
14
|
+
const LIQUIDATION_APPROVAL_BUFFER = 50n;
|
|
4
15
|
//#endregion
|
|
16
|
+
exports.DUST_THRESHOLD = DUST_THRESHOLD;
|
|
17
|
+
exports.LIQUIDATION_APPROVAL_BUFFER = LIQUIDATION_APPROVAL_BUFFER;
|
|
5
18
|
exports.LIQUIDATION_COMPRESSOR_V313_ADDRESS = LIQUIDATION_COMPRESSOR_V313_ADDRESS;
|
|
@@ -3,6 +3,8 @@ const require_sdk_accounts_liquidations_constants = require("./constants.js");
|
|
|
3
3
|
const require_sdk_accounts_liquidations_LiquidationsService = require("./LiquidationsService.js");
|
|
4
4
|
const require_sdk_accounts_liquidations_MultichainLiquidationsService = require("./MultichainLiquidationsService.js");
|
|
5
5
|
require("./types.js");
|
|
6
|
+
exports.DUST_THRESHOLD = require_sdk_accounts_liquidations_constants.DUST_THRESHOLD;
|
|
7
|
+
exports.LIQUIDATION_APPROVAL_BUFFER = require_sdk_accounts_liquidations_constants.LIQUIDATION_APPROVAL_BUFFER;
|
|
6
8
|
exports.LIQUIDATION_COMPRESSOR_V313_ADDRESS = require_sdk_accounts_liquidations_constants.LIQUIDATION_COMPRESSOR_V313_ADDRESS;
|
|
7
9
|
exports.LiquidationsService = require_sdk_accounts_liquidations_LiquidationsService.LiquidationsService;
|
|
8
10
|
exports.MultichainLiquidationsService = require_sdk_accounts_liquidations_MultichainLiquidationsService.MultichainLiquidationsService;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_sdk_core_errors = require("../core/errors.js");
|
|
3
|
+
//#region src/sdk/base/MultichainConstruct.ts
|
|
4
|
+
/**
|
|
5
|
+
* @internal
|
|
6
|
+
* Base class for services that fan out over the chains of a
|
|
7
|
+
* {@link MultichainSDK}, the cross-chain counterpart of {@link SDKConstruct}.
|
|
8
|
+
*
|
|
9
|
+
* Requests are soft-failing: a chain that rejects (including a chain that is
|
|
10
|
+
* not configured in the SDK) is logged as a warning and contributes no payload,
|
|
11
|
+
* while the remaining chains still return their results. Every queried chain is
|
|
12
|
+
* reported in {@link MultichainResult.meta}.
|
|
13
|
+
*
|
|
14
|
+
* @typeParam Plugins - Map of attached plugin types.
|
|
15
|
+
**/
|
|
16
|
+
var MultichainConstruct = class {
|
|
17
|
+
sdk;
|
|
18
|
+
constructor(sdk) {
|
|
19
|
+
this.sdk = sdk;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Fans out a request that returns a list, concatenating the lists of all
|
|
23
|
+
* chains that responded successfully.
|
|
24
|
+
**/
|
|
25
|
+
async queryChains(props) {
|
|
26
|
+
const { values, meta } = await this.#settle(props);
|
|
27
|
+
return {
|
|
28
|
+
result: values.flat(),
|
|
29
|
+
meta
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Fans out a request with no payload, reporting the outcome of every queried
|
|
34
|
+
* chain in {@link MultichainResult.meta}.
|
|
35
|
+
**/
|
|
36
|
+
async runChains(props) {
|
|
37
|
+
const { meta } = await this.#settle(props);
|
|
38
|
+
return {
|
|
39
|
+
result: void 0,
|
|
40
|
+
meta
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Runs the request on all queried chains in parallel. Duplicate networks are
|
|
45
|
+
* queried once, and {@link SettledChains.meta} follows the requested order.
|
|
46
|
+
**/
|
|
47
|
+
async #settle({ networks, label, run }) {
|
|
48
|
+
const requested = networks ? [...new Set(networks)] : [...this.sdk.chains.keys()];
|
|
49
|
+
const settled = await Promise.allSettled(requested.map(async (network) => {
|
|
50
|
+
const chainSdk = this.sdk.chains.get(network);
|
|
51
|
+
if (!chainSdk) throw new require_sdk_core_errors.ChainNotConfiguredError(network);
|
|
52
|
+
return run(chainSdk);
|
|
53
|
+
}));
|
|
54
|
+
const values = [];
|
|
55
|
+
const meta = [];
|
|
56
|
+
settled.forEach((result, i) => {
|
|
57
|
+
const network = requested[i];
|
|
58
|
+
if (result.status === "fulfilled") {
|
|
59
|
+
values.push(result.value);
|
|
60
|
+
meta.push({
|
|
61
|
+
network,
|
|
62
|
+
status: "success"
|
|
63
|
+
});
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
(this.sdk.chains.get(network)?.logger ?? this.sdk.logger)?.warn(result.reason, `failed to ${label} on ${network}`);
|
|
67
|
+
meta.push({
|
|
68
|
+
network,
|
|
69
|
+
status: "error",
|
|
70
|
+
error: result.reason
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
return {
|
|
74
|
+
values,
|
|
75
|
+
meta
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
//#endregion
|
|
80
|
+
exports.MultichainConstruct = MultichainConstruct;
|
|
@@ -4,6 +4,7 @@ const require_abi_iVersion = require("../../abi/iVersion.js");
|
|
|
4
4
|
const require_sdk_utils_AddressMap = require("../utils/AddressMap.js");
|
|
5
5
|
const require_sdk_utils_AddressSet = require("../utils/AddressSet.js");
|
|
6
6
|
const require_sdk_utils_bytes32ToString = require("../utils/bytes32ToString.js");
|
|
7
|
+
const require_sdk_chain_chains = require("../chain/chains.js");
|
|
7
8
|
const require_sdk_utils_formatter = require("../utils/formatter.js");
|
|
8
9
|
require("../utils/index.js");
|
|
9
10
|
//#region src/sdk/base/TokensMeta.ts
|
|
@@ -91,6 +92,56 @@ var TokensMeta = class extends require_sdk_utils_AddressMap.AddressMap {
|
|
|
91
92
|
for (const [token, meta] of this.entries()) if (this.isRWAUnderlying(meta)) result.upsert(token, meta);
|
|
92
93
|
return result;
|
|
93
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Describes a token the way the shared read model does.
|
|
97
|
+
*
|
|
98
|
+
* @param address - Token address.
|
|
99
|
+
* @throws If the token is not in the registry.
|
|
100
|
+
*/
|
|
101
|
+
mustGetToken(address) {
|
|
102
|
+
const meta = this.mustGet(address);
|
|
103
|
+
return {
|
|
104
|
+
chainId: this.#client.chain.id,
|
|
105
|
+
address: meta.addr,
|
|
106
|
+
symbol: meta.symbol,
|
|
107
|
+
name: meta.name,
|
|
108
|
+
decimals: meta.decimals,
|
|
109
|
+
assetType: require_sdk_chain_chains.getAssetType(meta.addr, this.#networkType)
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Like {@link mustGetToken}, but returns `undefined` instead of throwing when the
|
|
114
|
+
* token is not in the registry.
|
|
115
|
+
*
|
|
116
|
+
* @param address - Token address.
|
|
117
|
+
*/
|
|
118
|
+
getToken(address) {
|
|
119
|
+
return this.get(address) ? this.mustGetToken(address) : void 0;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The token an RWA wrapper holds, or the token itself when it is not one.
|
|
123
|
+
*
|
|
124
|
+
* An RWA market borrows a compliance wrapper (e.g. `dcUSDC`) that converts
|
|
125
|
+
* one-for-one with the token behind it (`USDC`), and only that token means
|
|
126
|
+
* anything to a reader. Pricing still goes through the wrapper, which is what
|
|
127
|
+
* a market's oracle knows.
|
|
128
|
+
*
|
|
129
|
+
* @param token - Token address, wrapper or not.
|
|
130
|
+
*/
|
|
131
|
+
unwrapRWA(token) {
|
|
132
|
+
const meta = this.get(token);
|
|
133
|
+
if (!meta || !this.isRWAUnderlying(meta)) return token;
|
|
134
|
+
if (!this.has(meta.asset)) {
|
|
135
|
+
this.#logger?.debug(`no token meta for ${meta.asset} wrapped by ${token}, reporting the wrapper instead`);
|
|
136
|
+
return token;
|
|
137
|
+
}
|
|
138
|
+
return meta.asset;
|
|
139
|
+
}
|
|
140
|
+
get #networkType() {
|
|
141
|
+
const { chain } = this.#client;
|
|
142
|
+
if ("network" in chain) return chain.network;
|
|
143
|
+
throw new Error(`chain ${chain.id} is not a Gearbox SDK chain`);
|
|
144
|
+
}
|
|
94
145
|
formatBN(arg0, arg1, arg2) {
|
|
95
146
|
const token = typeof arg0 === "object" ? arg0.token : arg0;
|
|
96
147
|
const amount = typeof arg0 === "object" ? arg0.balance : arg1;
|
|
@@ -4,6 +4,7 @@ const require_sdk_base_ChainContractsRegister = require("./ChainContractsRegiste
|
|
|
4
4
|
const require_sdk_base_Construct = require("./Construct.js");
|
|
5
5
|
const require_sdk_base_BaseContract = require("./BaseContract.js");
|
|
6
6
|
const require_sdk_base_errors = require("./errors.js");
|
|
7
|
+
const require_sdk_base_MultichainConstruct = require("./MultichainConstruct.js");
|
|
7
8
|
const require_sdk_base_PlaceholderContract = require("./PlaceholderContract.js");
|
|
8
9
|
const require_sdk_base_SDKConstruct = require("./SDKConstruct.js");
|
|
9
10
|
const require_sdk_base_token_types = require("./token-types.js");
|
|
@@ -13,6 +14,7 @@ exports.ChainContractsRegister = require_sdk_base_ChainContractsRegister.ChainCo
|
|
|
13
14
|
exports.Construct = require_sdk_base_Construct.Construct;
|
|
14
15
|
exports.ContractParseError = require_sdk_base_BaseContract.ContractParseError;
|
|
15
16
|
exports.MissingSerializedParamsError = require_sdk_base_errors.MissingSerializedParamsError;
|
|
17
|
+
exports.MultichainConstruct = require_sdk_base_MultichainConstruct.MultichainConstruct;
|
|
16
18
|
exports.PHANTOM_TOKEN_CONTRACT_TYPES = require_sdk_base_token_types.PHANTOM_TOKEN_CONTRACT_TYPES;
|
|
17
19
|
exports.PlaceholderContract = require_sdk_base_PlaceholderContract.PlaceholderContract;
|
|
18
20
|
exports.RWA_ON_DEMAND_LP_MONOPOLIZED = require_sdk_base_token_types.RWA_ON_DEMAND_LP_MONOPOLIZED;
|