@gearbox-protocol/sdk 14.12.0-next.74 → 14.12.0-next.75
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/plugins/accounts-counter/AccountsCounterPlugin.js +1 -1
- package/dist/cjs/sdk/accounts/CreditAccountsServiceV310.js +73 -445
- package/dist/cjs/sdk/accounts/credit-account-compressor/CreditAccountCompressor.js +280 -0
- package/dist/cjs/sdk/accounts/credit-account-compressor/CreditAccountCompressorV310Contract.js +141 -0
- package/dist/cjs/sdk/accounts/credit-account-compressor/index.js +6 -0
- package/dist/cjs/sdk/accounts/credit-account-compressor/types.js +1 -0
- package/dist/cjs/sdk/accounts/index.js +5 -0
- package/dist/cjs/sdk/index.js +4 -0
- package/dist/cjs/sdk/market/oracle/PriceOracleBaseContract.js +41 -1
- package/dist/cjs/sdk/market/oracle/PriceOracleV310Contract.js +0 -23
- package/dist/cjs/sdk/market/pricefeeds/PriceFeedsRegister.js +1 -1
- package/dist/esm/plugins/accounts-counter/AccountsCounterPlugin.js +1 -1
- package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +74 -446
- package/dist/esm/sdk/accounts/credit-account-compressor/CreditAccountCompressor.js +279 -0
- package/dist/esm/sdk/accounts/credit-account-compressor/CreditAccountCompressorV310Contract.js +141 -0
- package/dist/esm/sdk/accounts/credit-account-compressor/index.js +4 -0
- package/dist/esm/sdk/accounts/credit-account-compressor/types.js +1 -0
- package/dist/esm/sdk/accounts/index.js +4 -1
- package/dist/esm/sdk/index.js +3 -1
- package/dist/esm/sdk/market/oracle/PriceOracleBaseContract.js +41 -1
- package/dist/esm/sdk/market/oracle/PriceOracleV310Contract.js +0 -23
- package/dist/esm/sdk/market/pricefeeds/PriceFeedsRegister.js +1 -1
- package/dist/types/plugins/accounts/AccountsPlugin.d.ts +1 -1
- package/dist/types/sdk/accounts/CreditAccountsServiceV310.d.ts +10 -44
- package/dist/types/sdk/accounts/credit-account-compressor/CreditAccountCompressor.d.ts +60 -0
- package/dist/types/sdk/accounts/credit-account-compressor/CreditAccountCompressorV310Contract.d.ts +879 -0
- package/dist/types/sdk/accounts/credit-account-compressor/index.d.ts +4 -0
- package/dist/types/sdk/accounts/credit-account-compressor/types.d.ts +164 -0
- package/dist/types/sdk/accounts/index.d.ts +7 -3
- package/dist/types/sdk/accounts/types.d.ts +5 -112
- package/dist/types/sdk/base/index.d.ts +2 -2
- package/dist/types/sdk/base/types.d.ts +6 -1
- package/dist/types/sdk/index.d.ts +8 -5
- package/dist/types/sdk/market/index.d.ts +2 -2
- package/dist/types/sdk/market/oracle/PriceOracleBaseContract.d.ts +15 -4
- package/dist/types/sdk/market/oracle/PriceOracleV310Contract.d.ts +0 -11
- package/dist/types/sdk/market/oracle/index.d.ts +2 -2
- package/dist/types/sdk/market/oracle/types.d.ts +30 -14
- package/package.json +1 -1
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_abi_compressors_creditAccountCompressor = require("../../abi/compressors/creditAccountCompressor.js");
|
|
3
2
|
const require_sdk_utils_AddressMap = require("../../sdk/utils/AddressMap.js");
|
|
4
3
|
const require_sdk_utils_mappers = require("../../sdk/utils/mappers.js");
|
|
5
4
|
const require_sdk_constants_address_provider = require("../../sdk/constants/address-provider.js");
|
|
6
5
|
const require_sdk_constants_addresses = require("../../sdk/constants/addresses.js");
|
|
7
6
|
const require_sdk_constants_math = require("../../sdk/constants/math.js");
|
|
8
7
|
const require_sdk_constants_versions = require("../../sdk/constants/versions.js");
|
|
8
|
+
const require_abi_compressors_creditAccountCompressor = require("../../abi/compressors/creditAccountCompressor.js");
|
|
9
9
|
const require_sdk_plugins_BasePlugin = require("../../sdk/plugins/BasePlugin.js");
|
|
10
10
|
require("../../sdk/index.js");
|
|
11
11
|
//#region src/plugins/accounts-counter/AccountsCounterPlugin.ts
|
|
@@ -2,27 +2,17 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
const require_abi_iBaseRewardPool = require("../../abi/iBaseRewardPool.js");
|
|
3
3
|
const require_abi_ierc4626Adapter = require("../../abi/ierc4626Adapter.js");
|
|
4
4
|
const require_abi_310_generated = require("../../abi/310/generated.js");
|
|
5
|
-
const require_abi_compressors_creditAccountCompressor = require("../../abi/compressors/creditAccountCompressor.js");
|
|
6
5
|
const require_abi_compressors_peripheryCompressor = require("../../abi/compressors/peripheryCompressor.js");
|
|
7
6
|
const require_abi_compressors_rewardsCompressor = require("../../abi/compressors/rewardsCompressor.js");
|
|
8
|
-
const require_abi_rwa_iRWAFactory = require("../../abi/rwa/iRWAFactory.js");
|
|
9
|
-
const require_sdk_utils_AddressMap = require("../utils/AddressMap.js");
|
|
10
|
-
const require_sdk_utils_AddressSet = require("../utils/AddressSet.js");
|
|
11
7
|
const require_sdk_constants_address_provider = require("../constants/address-provider.js");
|
|
12
8
|
const require_sdk_constants_addresses = require("../constants/addresses.js");
|
|
13
9
|
const require_sdk_constants_math = require("../constants/math.js");
|
|
14
10
|
const require_sdk_constants_versions = require("../constants/versions.js");
|
|
15
11
|
require("../constants/index.js");
|
|
16
|
-
const require_sdk_utils_hex = require("../utils/hex.js");
|
|
17
|
-
require("../utils/index.js");
|
|
18
12
|
const require_sdk_base_SDKConstruct = require("../base/SDKConstruct.js");
|
|
19
13
|
require("../base/index.js");
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
const require_sdk_utils_viem_simulateWithPriceUpdates = require("../utils/viem/simulateWithPriceUpdates.js");
|
|
23
|
-
require("../utils/viem/index.js");
|
|
24
|
-
const require_sdk_market_pricefeeds_getRawPriceUpdates = require("../market/pricefeeds/getRawPriceUpdates.js");
|
|
25
|
-
require("../market/index.js");
|
|
14
|
+
const require_sdk_accounts_credit_account_compressor_CreditAccountCompressor = require("./credit-account-compressor/CreditAccountCompressor.js");
|
|
15
|
+
require("./credit-account-compressor/index.js");
|
|
26
16
|
const require_sdk_accounts_multicall_utils = require("./multicall-utils.js");
|
|
27
17
|
let viem = require("viem");
|
|
28
18
|
//#region src/sdk/accounts/CreditAccountsServiceV310.ts
|
|
@@ -37,268 +27,33 @@ let viem = require("viem");
|
|
|
37
27
|
**/
|
|
38
28
|
var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKConstruct {
|
|
39
29
|
#compressor;
|
|
40
|
-
|
|
41
|
-
constructor(sdk, options) {
|
|
30
|
+
constructor(sdk) {
|
|
42
31
|
super(sdk);
|
|
43
|
-
this.#
|
|
44
|
-
}
|
|
45
|
-
setBatchSize(batchSize) {
|
|
46
|
-
this.#batchSize = batchSize;
|
|
32
|
+
this.#compressor = new require_sdk_accounts_credit_account_compressor_CreditAccountCompressor.CreditAccountCompressor(sdk);
|
|
47
33
|
}
|
|
48
34
|
/**
|
|
49
35
|
* {@inheritDoc ICreditAccountsService.getCreditAccountData}
|
|
50
36
|
**/
|
|
51
37
|
async getCreditAccountData(account, blockNumber) {
|
|
52
|
-
|
|
53
|
-
try {
|
|
54
|
-
raw = await this.client.readContract({
|
|
55
|
-
abi: require_abi_compressors_creditAccountCompressor.creditAccountCompressorAbi,
|
|
56
|
-
address: this.compressor,
|
|
57
|
-
functionName: "getCreditAccountData",
|
|
58
|
-
args: [account],
|
|
59
|
-
blockNumber,
|
|
60
|
-
gas: this.sdk.gasLimit
|
|
61
|
-
});
|
|
62
|
-
} catch (_e) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
const factory = this.sdk.marketRegister.findByCreditManager(raw.creditManager).rwaFactory;
|
|
66
|
-
let ca;
|
|
67
|
-
let investor;
|
|
68
|
-
if (raw.success) {
|
|
69
|
-
ca = raw;
|
|
70
|
-
investor = await factory?.getInvestor(raw.creditAccount, false);
|
|
71
|
-
} else {
|
|
72
|
-
const { txs: priceUpdateTxs } = await this.#getUpdateForAccount(raw);
|
|
73
|
-
[ca, investor] = await require_sdk_utils_viem_simulateWithPriceUpdates.simulateWithPriceUpdates(this.client, {
|
|
74
|
-
priceUpdates: priceUpdateTxs,
|
|
75
|
-
contracts: [{
|
|
76
|
-
abi: require_abi_compressors_creditAccountCompressor.creditAccountCompressorAbi,
|
|
77
|
-
address: this.compressor,
|
|
78
|
-
functionName: "getCreditAccountData",
|
|
79
|
-
args: [account]
|
|
80
|
-
}, ...factory ? [{
|
|
81
|
-
abi: require_abi_rwa_iRWAFactory.iRWAFactoryAbi,
|
|
82
|
-
address: factory.address,
|
|
83
|
-
functionName: "getInvestor",
|
|
84
|
-
args: [raw.creditAccount]
|
|
85
|
-
}] : []],
|
|
86
|
-
blockNumber,
|
|
87
|
-
gas: this.sdk.gasLimit
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
return {
|
|
91
|
-
...ca,
|
|
92
|
-
investor
|
|
93
|
-
};
|
|
38
|
+
return this.#compressor.getCreditAccountData(account, blockNumber);
|
|
94
39
|
}
|
|
95
40
|
/**
|
|
96
41
|
* {@inheritDoc ICreditAccountsService.getCreditAccounts}
|
|
97
42
|
**/
|
|
98
43
|
async getCreditAccounts(options, blockNumber) {
|
|
99
|
-
|
|
100
|
-
const arg0 = creditManager ?? {
|
|
101
|
-
configurators: this.marketConfigurators,
|
|
102
|
-
creditManagers: [],
|
|
103
|
-
pools: [],
|
|
104
|
-
underlying: "0x0000000000000000000000000000000000000000"
|
|
105
|
-
};
|
|
106
|
-
const caFilter = {
|
|
107
|
-
owner,
|
|
108
|
-
includeZeroDebt,
|
|
109
|
-
minHealthFactor,
|
|
110
|
-
maxHealthFactor,
|
|
111
|
-
reverting: false
|
|
112
|
-
};
|
|
113
|
-
const { txs: priceUpdateTxs } = await this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(ignoreReservePrices ? { main: true } : void 0);
|
|
114
|
-
const allCAs = [];
|
|
115
|
-
let revertingOffset = 0;
|
|
116
|
-
for (const reverting of [false, true]) {
|
|
117
|
-
let offset = 0n;
|
|
118
|
-
revertingOffset = allCAs.length;
|
|
119
|
-
do {
|
|
120
|
-
const [accounts, newOffset] = await this.#getCreditAccounts(this.#batchSize ? [
|
|
121
|
-
arg0,
|
|
122
|
-
{
|
|
123
|
-
...caFilter,
|
|
124
|
-
reverting
|
|
125
|
-
},
|
|
126
|
-
offset,
|
|
127
|
-
BigInt(this.#batchSize)
|
|
128
|
-
] : [
|
|
129
|
-
arg0,
|
|
130
|
-
{
|
|
131
|
-
...caFilter,
|
|
132
|
-
reverting
|
|
133
|
-
},
|
|
134
|
-
offset
|
|
135
|
-
], priceUpdateTxs, blockNumber);
|
|
136
|
-
allCAs.push(...accounts);
|
|
137
|
-
offset = newOffset;
|
|
138
|
-
} while (offset !== 0n);
|
|
139
|
-
}
|
|
140
|
-
this.logger?.debug(`loaded ${allCAs.length} credit accounts (${allCAs.length - revertingOffset} reverting)`);
|
|
141
|
-
return allCAs.sort((a, b) => Number(a.healthFactor - b.healthFactor));
|
|
44
|
+
return this.#compressor.getCreditAccounts(options, blockNumber);
|
|
142
45
|
}
|
|
143
46
|
/**
|
|
144
47
|
* {@inheritDoc ICreditAccountsService.getBorrowerCreditAccounts}
|
|
145
48
|
**/
|
|
146
49
|
async getBorrowerCreditAccounts(borrower, options, blockNumber) {
|
|
147
|
-
|
|
148
|
-
const { txs: priceUpdateTxs } = await this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(ignoreReservePrices ? { main: true } : void 0);
|
|
149
|
-
const rwaAccountAddresses = (await this.sdk.rwa.getInvestorData(borrower)).flatMap((d) => d.creditAccounts.map((ca) => ca.creditAccount));
|
|
150
|
-
const cmFilter = creditManager ? {
|
|
151
|
-
configurators: [],
|
|
152
|
-
creditManagers: [creditManager],
|
|
153
|
-
pools: [],
|
|
154
|
-
underlying: require_sdk_constants_addresses.ADDRESS_0X0
|
|
155
|
-
} : {
|
|
156
|
-
configurators: this.marketConfigurators,
|
|
157
|
-
creditManagers: [],
|
|
158
|
-
pools: [],
|
|
159
|
-
underlying: require_sdk_constants_addresses.ADDRESS_0X0
|
|
160
|
-
};
|
|
161
|
-
const permissiveFilter = {
|
|
162
|
-
owner: borrower,
|
|
163
|
-
includeZeroDebt: true,
|
|
164
|
-
minHealthFactor: 0n,
|
|
165
|
-
maxHealthFactor: require_sdk_constants_math.MAX_UINT256,
|
|
166
|
-
reverting: false
|
|
167
|
-
};
|
|
168
|
-
const rwaContracts = rwaAccountAddresses.map((account) => ({
|
|
169
|
-
abi: require_abi_compressors_creditAccountCompressor.creditAccountCompressorAbi,
|
|
170
|
-
address: this.compressor,
|
|
171
|
-
functionName: "getCreditAccountData",
|
|
172
|
-
args: [account]
|
|
173
|
-
}));
|
|
174
|
-
const getCreditAccountsContracts = [false, true].map((reverting) => ({
|
|
175
|
-
abi: require_abi_compressors_creditAccountCompressor.creditAccountCompressorAbi,
|
|
176
|
-
address: this.compressor,
|
|
177
|
-
functionName: "getCreditAccounts",
|
|
178
|
-
args: [
|
|
179
|
-
cmFilter,
|
|
180
|
-
{
|
|
181
|
-
...permissiveFilter,
|
|
182
|
-
reverting
|
|
183
|
-
},
|
|
184
|
-
0n
|
|
185
|
-
]
|
|
186
|
-
}));
|
|
187
|
-
const allContracts = [...rwaContracts, ...getCreditAccountsContracts];
|
|
188
|
-
const results = await require_sdk_utils_viem_simulateWithPriceUpdates.simulateWithPriceUpdates(this.client, {
|
|
189
|
-
priceUpdates: priceUpdateTxs,
|
|
190
|
-
contracts: allContracts,
|
|
191
|
-
blockNumber,
|
|
192
|
-
gas: this.sdk.gasLimit
|
|
193
|
-
});
|
|
194
|
-
const rwaResults = results.slice(0, rwaAccountAddresses.length);
|
|
195
|
-
const normalResults = results.slice(rwaAccountAddresses.length);
|
|
196
|
-
const seen = new require_sdk_utils_AddressSet.AddressSet();
|
|
197
|
-
const allCAs = [];
|
|
198
|
-
for (const ca of rwaResults) if (!seen.has(ca.creditAccount)) {
|
|
199
|
-
seen.add(ca.creditAccount);
|
|
200
|
-
allCAs.push({
|
|
201
|
-
...ca,
|
|
202
|
-
investor: borrower
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
for (const [accounts] of normalResults) for (const ca of accounts) if (!seen.has(ca.creditAccount)) {
|
|
206
|
-
seen.add(ca.creditAccount);
|
|
207
|
-
allCAs.push({
|
|
208
|
-
...ca,
|
|
209
|
-
investor: void 0
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
const filtered = allCAs.filter((ca) => {
|
|
213
|
-
if (!includeZeroDebt && ca.debt === 0n) return false;
|
|
214
|
-
if (ca.healthFactor < minHealthFactor) return false;
|
|
215
|
-
if (ca.healthFactor > maxHealthFactor) return false;
|
|
216
|
-
if (creditManager && !require_sdk_utils_hex.hexEq(ca.creditManager, creditManager)) return false;
|
|
217
|
-
return true;
|
|
218
|
-
});
|
|
219
|
-
this.logger?.debug(`loaded ${allCAs.length} borrower credit accounts (${rwaResults.length} RWA, ${filtered.length} after filter)`);
|
|
220
|
-
return filtered.sort((a, b) => Number(a.healthFactor - b.healthFactor));
|
|
50
|
+
return this.#compressor.getBorrowerCreditAccounts(borrower, options, blockNumber);
|
|
221
51
|
}
|
|
222
52
|
/**
|
|
223
53
|
* {@inheritDoc ICreditAccountsService.listPositions}
|
|
224
54
|
**/
|
|
225
55
|
async listPositions(props) {
|
|
226
|
-
|
|
227
|
-
const [accounts] = await Promise.all([this.getBorrowerCreditAccounts(owner, { includeZeroDebt }), this.sdk.withdrawalCompressor?.loadWithdrawableAssets()]);
|
|
228
|
-
const describable = accounts.filter((ca) => {
|
|
229
|
-
if (!ca.success) this.logger?.warn(`cannot describe position of ${this.labelAddress(ca.creditAccount)}: collateral computation failed`);
|
|
230
|
-
return ca.success;
|
|
231
|
-
});
|
|
232
|
-
const withdrawals = await Promise.all(describable.map((ca) => this.#accountWithdrawals(ca)));
|
|
233
|
-
return describable.map((ca, i) => this.#strategyPosition(ca, withdrawals[i] ?? new require_sdk_utils_AddressMap.AddressMap()));
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Builds one strategy position from an account snapshot.
|
|
237
|
-
*
|
|
238
|
-
* @param withdrawals - Delayed withdrawals of the account, keyed by the
|
|
239
|
-
* phantom token that represents them on it.
|
|
240
|
-
**/
|
|
241
|
-
#strategyPosition(ca, withdrawals) {
|
|
242
|
-
const suite = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
243
|
-
const { market } = suite;
|
|
244
|
-
const { priceOracle } = market;
|
|
245
|
-
const { pool } = market.pool;
|
|
246
|
-
const token = this.sdk.tokensMeta.mustGetToken(market.unwrappedUnderlying);
|
|
247
|
-
const totalDebtValue = ca.debt + ca.accruedInterest + ca.accruedFees;
|
|
248
|
-
const collateral = require_sdk_market_credit_dominantCollateral.dominantCollateral(ca, market);
|
|
249
|
-
return {
|
|
250
|
-
kind: "strategy",
|
|
251
|
-
chainId: this.sdk.chainId,
|
|
252
|
-
creditManager: ca.creditManager,
|
|
253
|
-
creditAccount: ca.creditAccount,
|
|
254
|
-
name: collateral ? suite.strategyName(collateral) : token.symbol,
|
|
255
|
-
targetCollateral: collateral ? this.sdk.tokensMeta.mustGetToken(collateral) : null,
|
|
256
|
-
leverage: require_sdk_market_math.positionLeverage(totalDebtValue, ca.totalValue),
|
|
257
|
-
borrowApy: require_sdk_market_math.borrowApyBps(pool.baseInterestRate, suite.creditManager.feeInterest),
|
|
258
|
-
totalDebt: {
|
|
259
|
-
token,
|
|
260
|
-
value: totalDebtValue,
|
|
261
|
-
valueUsd: require_sdk_market_math.usdToNumber(ca.totalDebtUSD)
|
|
262
|
-
},
|
|
263
|
-
totalValue: {
|
|
264
|
-
token,
|
|
265
|
-
value: ca.totalValue,
|
|
266
|
-
valueUsd: require_sdk_market_math.usdToNumber(ca.totalValueUSD)
|
|
267
|
-
},
|
|
268
|
-
healthFactor: require_sdk_market_math.healthFactorBps(ca.healthFactor),
|
|
269
|
-
collaterals: ca.tokens.flatMap((t) => {
|
|
270
|
-
if ((t.mask & ca.enabledTokensMask) === 0n || t.balance <= 10n) return [];
|
|
271
|
-
return [{
|
|
272
|
-
collateral: priceOracle.toTokenAmount(t.token, t.balance),
|
|
273
|
-
quota: priceOracle.toTokenAmount(market.underlying, t.quota),
|
|
274
|
-
withdrawals: withdrawals.get(t.token) ?? []
|
|
275
|
-
}];
|
|
276
|
-
})
|
|
277
|
-
};
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Delayed withdrawals of one account, keyed by the phantom token that
|
|
281
|
-
* represents them on it, so that each collateral row can pick up its own.
|
|
282
|
-
**/
|
|
283
|
-
async #accountWithdrawals(ca) {
|
|
284
|
-
const compressor = this.sdk.withdrawalCompressor;
|
|
285
|
-
const byPhantomToken = new require_sdk_utils_AddressMap.AddressMap(void 0, "accountWithdrawals");
|
|
286
|
-
const holdsPhantomToken = ca.tokens.some((t) => t.balance > 10n && compressor?.getWithdrawalSourceToken(t.token) !== void 0);
|
|
287
|
-
if (!compressor || !holdsPhantomToken) return byPhantomToken;
|
|
288
|
-
const { priceOracle } = this.sdk.marketRegister.findByCreditManager(ca.creditManager);
|
|
289
|
-
const { claimable, pending } = await compressor.getCurrentWithdrawals(ca.creditAccount);
|
|
290
|
-
const add = (w, outputs, claimableAt) => {
|
|
291
|
-
const assets = outputs.map((o) => ({
|
|
292
|
-
isDelayed: true,
|
|
293
|
-
...priceOracle.toTokenAmount(o.token, o.amount),
|
|
294
|
-
redeemer: w.redeemer,
|
|
295
|
-
claimableAt: claimableAt === void 0 ? void 0 : Number(claimableAt)
|
|
296
|
-
}));
|
|
297
|
-
byPhantomToken.upsert(w.withdrawalPhantomToken, [...byPhantomToken.get(w.withdrawalPhantomToken) ?? [], ...assets]);
|
|
298
|
-
};
|
|
299
|
-
for (const w of claimable) add(w, w.outputs);
|
|
300
|
-
for (const w of pending) add(w, w.expectedOutputs, w.claimableAt);
|
|
301
|
-
return byPhantomToken;
|
|
56
|
+
return this.#compressor.listPositions(props);
|
|
302
57
|
}
|
|
303
58
|
/**
|
|
304
59
|
* {@inheritDoc ICreditAccountsService.getRewards}
|
|
@@ -420,7 +175,7 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
420
175
|
keepAssets,
|
|
421
176
|
debtOnly
|
|
422
177
|
});
|
|
423
|
-
const calls = await this
|
|
178
|
+
const calls = await this.prependPriceUpdates(account.creditManager, routerCloseResult.calls, account, { ignoreReservePrices });
|
|
424
179
|
let lossPolicyData;
|
|
425
180
|
if (applyLossPolicy) {
|
|
426
181
|
lossPolicyData = await this.sdk.marketRegister.findByCreditManager(account.creditManager).lossPolicy.getLiquidationData(account.creditAccount);
|
|
@@ -568,7 +323,7 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
568
323
|
}),
|
|
569
324
|
...callsAfter ?? []
|
|
570
325
|
];
|
|
571
|
-
const calls = await this
|
|
326
|
+
const calls = await this.prependPriceUpdates(cm.address, operationCalls);
|
|
572
327
|
let tx;
|
|
573
328
|
if (reopenCreditAccount) tx = await this.#multicallTx(cmSuite, reopenCreditAccount, calls);
|
|
574
329
|
else tx = await this.#openCreditAccountTx(cmSuite, to, calls, referralCode, rwaOptions);
|
|
@@ -576,134 +331,74 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
576
331
|
return tx;
|
|
577
332
|
}
|
|
578
333
|
/**
|
|
579
|
-
*
|
|
580
|
-
*
|
|
581
|
-
* @param
|
|
582
|
-
* @param
|
|
583
|
-
* @returns
|
|
584
|
-
*/
|
|
585
|
-
async #getCreditAccounts(args, priceUpdateTxs, blockNumber) {
|
|
586
|
-
let resp;
|
|
587
|
-
if (priceUpdateTxs?.length) [resp] = await require_sdk_utils_viem_simulateWithPriceUpdates.simulateWithPriceUpdates(this.client, {
|
|
588
|
-
priceUpdates: priceUpdateTxs,
|
|
589
|
-
contracts: [{
|
|
590
|
-
abi: require_abi_compressors_creditAccountCompressor.creditAccountCompressorAbi,
|
|
591
|
-
address: this.compressor,
|
|
592
|
-
functionName: "getCreditAccounts",
|
|
593
|
-
args
|
|
594
|
-
}],
|
|
595
|
-
blockNumber,
|
|
596
|
-
gas: this.sdk.gasLimit
|
|
597
|
-
});
|
|
598
|
-
else resp = await this.client.readContract({
|
|
599
|
-
abi: require_abi_compressors_creditAccountCompressor.creditAccountCompressorAbi,
|
|
600
|
-
address: this.compressor,
|
|
601
|
-
functionName: "getCreditAccounts",
|
|
602
|
-
args,
|
|
603
|
-
blockNumber,
|
|
604
|
-
gas: this.sdk.gasLimit
|
|
605
|
-
});
|
|
606
|
-
this.logger?.debug({
|
|
607
|
-
accounts: resp[0]?.length ?? 0,
|
|
608
|
-
nextOffset: Number(resp[1])
|
|
609
|
-
}, "got credit accounts");
|
|
610
|
-
return resp;
|
|
611
|
-
}
|
|
612
|
-
/**
|
|
613
|
-
* Returns multicall entries to redeem (unwrap) RWA ERC-4626 vault shares into underlying for the given credit manager.
|
|
614
|
-
* Used when withdrawing debt from a RWA market: redeems adapter vault shares so the underlying can be withdrawn.
|
|
615
|
-
* Only applies when the credit manager's underlying is RWA-gated and has an ERC-4626 adapter configured.
|
|
616
|
-
* @param amount - Number of vault shares (adapter tokens) to redeem
|
|
334
|
+
* Encodes one ERC-4626 adapter call for an RWA market's underlying vault.
|
|
335
|
+
*
|
|
336
|
+
* @param functionName - Adapter function to call
|
|
337
|
+
* @param amount - Amount passed to the adapter, in the units the function expects
|
|
617
338
|
* @param creditManager - Credit manager address
|
|
618
|
-
* @returns Array of MultiCall to pass to credit facade multicall, or undefined
|
|
339
|
+
* @returns Array of MultiCall to pass to credit facade multicall, or undefined
|
|
340
|
+
* if the underlying is not RWA-gated or no adapter is configured for it
|
|
619
341
|
*/
|
|
620
|
-
|
|
342
|
+
#rwaVaultCalls(functionName, amount, creditManager) {
|
|
621
343
|
const suite = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
622
344
|
const meta = this.sdk.tokensMeta.mustGet(suite.underlying);
|
|
623
345
|
if (!this.sdk.tokensMeta.isRWAUnderlying(meta)) return;
|
|
624
|
-
const
|
|
625
|
-
if (!
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
346
|
+
const target = suite.creditManager.adapters.get(meta.addr)?.address;
|
|
347
|
+
if (!target) return;
|
|
348
|
+
let callData;
|
|
349
|
+
switch (functionName) {
|
|
350
|
+
case "deposit":
|
|
351
|
+
callData = (0, viem.encodeFunctionData)({
|
|
352
|
+
abi: require_abi_ierc4626Adapter.ierc4626AdapterAbi,
|
|
353
|
+
functionName,
|
|
354
|
+
args: [amount, require_sdk_constants_addresses.ADDRESS_0X0]
|
|
355
|
+
});
|
|
356
|
+
break;
|
|
357
|
+
case "redeem":
|
|
358
|
+
callData = (0, viem.encodeFunctionData)({
|
|
359
|
+
abi: require_abi_ierc4626Adapter.ierc4626AdapterAbi,
|
|
360
|
+
functionName,
|
|
361
|
+
args: [
|
|
362
|
+
amount,
|
|
363
|
+
require_sdk_constants_addresses.ADDRESS_0X0,
|
|
364
|
+
require_sdk_constants_addresses.ADDRESS_0X0
|
|
365
|
+
]
|
|
366
|
+
});
|
|
367
|
+
break;
|
|
368
|
+
default: callData = (0, viem.encodeFunctionData)({
|
|
629
369
|
abi: require_abi_ierc4626Adapter.ierc4626AdapterAbi,
|
|
630
|
-
functionName
|
|
631
|
-
args: [
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
370
|
+
functionName,
|
|
371
|
+
args: [amount]
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
return [{
|
|
375
|
+
target,
|
|
376
|
+
callData
|
|
637
377
|
}];
|
|
638
378
|
}
|
|
639
379
|
/**
|
|
640
|
-
*
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
380
|
+
* {@inheritDoc ICreditAccountsService.assembleRWAUnwrapCalls}
|
|
381
|
+
*/
|
|
382
|
+
async assembleRWAUnwrapCalls(amount, creditManager) {
|
|
383
|
+
return this.#rwaVaultCalls("redeem", amount, creditManager);
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* {@inheritDoc ICreditAccountsService.assembleRWAWrapCalls}
|
|
646
387
|
*/
|
|
647
388
|
async assembleRWAWrapCalls(amount, creditManager) {
|
|
648
|
-
|
|
649
|
-
const meta = this.sdk.tokensMeta.mustGet(suite.underlying);
|
|
650
|
-
if (!this.sdk.tokensMeta.isRWAUnderlying(meta)) return;
|
|
651
|
-
const adapterAddress = suite.creditManager.adapters.get(meta.addr)?.address;
|
|
652
|
-
if (!adapterAddress) return;
|
|
653
|
-
return [{
|
|
654
|
-
target: adapterAddress,
|
|
655
|
-
callData: (0, viem.encodeFunctionData)({
|
|
656
|
-
abi: require_abi_ierc4626Adapter.ierc4626AdapterAbi,
|
|
657
|
-
functionName: "deposit",
|
|
658
|
-
args: [amount, require_sdk_constants_addresses.ADDRESS_0X0]
|
|
659
|
-
})
|
|
660
|
-
}];
|
|
389
|
+
return this.#rwaVaultCalls("deposit", amount, creditManager);
|
|
661
390
|
}
|
|
662
391
|
/**
|
|
663
|
-
*
|
|
664
|
-
* Redeems the leftover vault shares (e.g. after repaying debt) so the account does not hold excess RWA vault tokens.
|
|
665
|
-
* Only applies when the credit manager's underlying is RWA-gated and has an ERC-4626 adapter configured.
|
|
666
|
-
* @param amount - Leftover vault share amount to redeem (in adapter/vault decimals)
|
|
667
|
-
* @param creditManager - Credit manager address
|
|
668
|
-
* @returns Array of MultiCall to pass to credit facade multicall, or undefined if underlying is not RWA or no adapter is configured
|
|
392
|
+
* {@inheritDoc ICreditAccountsService.assembleRedeemDiffCalls}
|
|
669
393
|
*/
|
|
670
394
|
async assembleRedeemDiffCalls(amount, creditManager) {
|
|
671
|
-
|
|
672
|
-
const meta = this.sdk.tokensMeta.mustGet(suite.underlying);
|
|
673
|
-
if (!this.sdk.tokensMeta.isRWAUnderlying(meta)) return;
|
|
674
|
-
const adapterAddress = suite.creditManager.adapters.get(meta.addr)?.address;
|
|
675
|
-
if (!adapterAddress) return;
|
|
676
|
-
return [{
|
|
677
|
-
target: adapterAddress,
|
|
678
|
-
callData: (0, viem.encodeFunctionData)({
|
|
679
|
-
abi: require_abi_ierc4626Adapter.ierc4626AdapterAbi,
|
|
680
|
-
functionName: "redeemDiff",
|
|
681
|
-
args: [amount]
|
|
682
|
-
})
|
|
683
|
-
}];
|
|
395
|
+
return this.#rwaVaultCalls("redeemDiff", amount, creditManager);
|
|
684
396
|
}
|
|
685
397
|
/**
|
|
686
|
-
*
|
|
687
|
-
* Deposits the leftover underlying (e.g. after decreasing debt) into the vault so the account does not hold excess underlying.
|
|
688
|
-
* Only applies when the credit manager's underlying is RWA-gated and has an ERC-4626 adapter configured.
|
|
689
|
-
* @param amount - Leftover underlying amount to deposit into the vault (in underlying decimals)
|
|
690
|
-
* @param creditManager - Credit manager address
|
|
691
|
-
* @returns Array of MultiCall to pass to credit facade multicall, or undefined if underlying is not RWA or no adapter is configured
|
|
398
|
+
* {@inheritDoc ICreditAccountsService.assembleDepositDiffCalls}
|
|
692
399
|
*/
|
|
693
400
|
async assembleDepositDiffCalls(amount, creditManager) {
|
|
694
|
-
|
|
695
|
-
const meta = this.sdk.tokensMeta.mustGet(suite.underlying);
|
|
696
|
-
if (!this.sdk.tokensMeta.isRWAUnderlying(meta)) return;
|
|
697
|
-
const adapterAddress = suite.creditManager.adapters.get(meta.addr)?.address;
|
|
698
|
-
if (!adapterAddress) return;
|
|
699
|
-
return [{
|
|
700
|
-
target: adapterAddress,
|
|
701
|
-
callData: (0, viem.encodeFunctionData)({
|
|
702
|
-
abi: require_abi_ierc4626Adapter.ierc4626AdapterAbi,
|
|
703
|
-
functionName: "depositDiff",
|
|
704
|
-
args: [amount]
|
|
705
|
-
})
|
|
706
|
-
}];
|
|
401
|
+
return this.#rwaVaultCalls("depositDiff", amount, creditManager);
|
|
707
402
|
}
|
|
708
403
|
/**
|
|
709
404
|
* {@inheritDoc ICreditAccountsService.setBot}
|
|
@@ -726,7 +421,7 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
726
421
|
}]
|
|
727
422
|
}).read.requiredPermissions();
|
|
728
423
|
const addBotCall = cm.creditFacade.prepareSetBotPermissions(botAddress, permissions);
|
|
729
|
-
const calls = targetContract.type === "creditAccount" ? await this
|
|
424
|
+
const calls = targetContract.type === "creditAccount" ? await this.prependPriceUpdates(targetContract.creditManager, [addBotCall], targetContract) : [addBotCall];
|
|
730
425
|
return {
|
|
731
426
|
tx: targetContract.type === "creditAccount" ? await this.#multicallTx(cm, targetContract.creditAccount, calls) : void 0,
|
|
732
427
|
calls,
|
|
@@ -772,7 +467,7 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
772
467
|
minQuota,
|
|
773
468
|
averageQuota
|
|
774
469
|
})];
|
|
775
|
-
const calls = await this
|
|
470
|
+
const calls = await this.prependPriceUpdates(ca.creditManager, operationCalls, ca);
|
|
776
471
|
return await this.#multicallTx(cm, ca.creditAccount, calls);
|
|
777
472
|
}
|
|
778
473
|
/**
|
|
@@ -780,80 +475,26 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
780
475
|
* {@inheritDoc ICreditAccountsService.getOnDemandPriceUpdates}
|
|
781
476
|
**/
|
|
782
477
|
async getOnDemandPriceUpdates(account, ignoreReservePrices) {
|
|
783
|
-
const {
|
|
784
|
-
|
|
785
|
-
const update = await this.#getUpdateForAccount(account, ignoreReservePrices);
|
|
786
|
-
this.logger?.debug({
|
|
787
|
-
account: creditAccount,
|
|
788
|
-
manager: cm.name
|
|
789
|
-
}, `getting on demand price updates from ${update.txs.length} txs`);
|
|
790
|
-
return require_sdk_market_pricefeeds_getRawPriceUpdates.getRawPriceUpdates(update);
|
|
478
|
+
const { priceOracle } = this.sdk.marketRegister.findByCreditManager(account.creditManager);
|
|
479
|
+
return priceOracle.priceUpdatesForAccount(account, { reserve: !ignoreReservePrices });
|
|
791
480
|
}
|
|
792
481
|
/**
|
|
793
|
-
*
|
|
794
|
-
*
|
|
795
|
-
* Deduplicates existing `onDemandPriceUpdates` calls
|
|
796
|
-
*
|
|
797
|
-
* @param creditManager - Address of the credit manager
|
|
798
|
-
* @param calls - The multicall array to prepend price updates to
|
|
799
|
-
* @param ca - Credit account slice, undefined when opening a new account
|
|
800
|
-
* @param options - Optional settings for price update generation
|
|
801
|
-
* @returns A new array with a single consolidated price update call prepended,
|
|
802
|
-
* followed by the non-price-update calls in their original order
|
|
482
|
+
* {@inheritDoc ICreditAccountsService.prependPriceUpdates}
|
|
803
483
|
*/
|
|
804
|
-
async
|
|
805
|
-
const
|
|
484
|
+
async prependPriceUpdates(creditManager, calls, creditAccount, options) {
|
|
485
|
+
const { priceOracle } = this.sdk.marketRegister.findByCreditManager(creditManager);
|
|
806
486
|
const suite = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
807
|
-
const cm = suite.creditManager;
|
|
808
487
|
const { priceUpdates: existingUpdates, remainingCalls } = require_sdk_accounts_multicall_utils.extractPriceUpdates(calls);
|
|
809
|
-
const
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
}
|
|
814
|
-
const
|
|
815
|
-
const priceFeeds = market.priceOracle.priceFeedsForTokens(Array.from(tokens), {
|
|
816
|
-
main: true,
|
|
817
|
-
reserve: !ignoreReservePrices
|
|
818
|
-
});
|
|
819
|
-
const tStr = tokens.map((t) => this.labelAddress(t)).join(", ");
|
|
820
|
-
const remark = ignoreReservePrices ? " main" : "";
|
|
821
|
-
this.logger?.debug({
|
|
822
|
-
account: ca?.creditAccount,
|
|
823
|
-
manager: cm.name
|
|
824
|
-
}, `prependPriceUpdates for ${tStr} from ${priceFeeds.length}${remark} price feeds`);
|
|
825
|
-
const generatedUpdates = await this.sdk.priceFeeds.generatePriceFeedsUpdates(priceFeeds);
|
|
488
|
+
const extraTokens = require_sdk_accounts_multicall_utils.extractQuotaTokens(calls).asArray();
|
|
489
|
+
const opts = {
|
|
490
|
+
reserve: !options?.ignoreReservePrices,
|
|
491
|
+
extraTokens
|
|
492
|
+
};
|
|
493
|
+
const generatedUpdates = creditAccount ? await priceOracle.priceUpdatesForAccount(creditAccount, opts) : await priceOracle.priceUpdatesForTokens([suite.creditManager.underlying, ...extraTokens], opts);
|
|
826
494
|
const merged = require_sdk_accounts_multicall_utils.mergePriceUpdates(existingUpdates, generatedUpdates);
|
|
827
495
|
if (merged.length === 0) return remainingCalls;
|
|
828
496
|
return [suite.creditFacade.prepareOnDemandPriceUpdates(merged), ...remainingCalls];
|
|
829
497
|
}
|
|
830
|
-
async #getUpdateForAccount(account, ignoreReservePrices) {
|
|
831
|
-
const { creditManager, creditAccount, enabledTokensMask } = account;
|
|
832
|
-
const market = this.sdk.marketRegister.findByCreditManager(creditManager);
|
|
833
|
-
const cm = this.sdk.marketRegister.findCreditManager(creditManager).creditManager;
|
|
834
|
-
const tokens = new require_sdk_utils_AddressSet.AddressSet([cm.underlying]);
|
|
835
|
-
for (const t of account.tokens) {
|
|
836
|
-
const isEnabled = (t.mask & enabledTokensMask) !== 0n;
|
|
837
|
-
if (t.balance > 10n && isEnabled) tokens.add(t.token);
|
|
838
|
-
}
|
|
839
|
-
const priceFeeds = market.priceOracle.priceFeedsForTokens(Array.from(tokens), {
|
|
840
|
-
main: true,
|
|
841
|
-
reserve: !ignoreReservePrices
|
|
842
|
-
});
|
|
843
|
-
const tStr = tokens.map((t) => this.labelAddress(t)).join(", ");
|
|
844
|
-
const remark = ignoreReservePrices ? " main" : "";
|
|
845
|
-
this.logger?.debug({
|
|
846
|
-
account: creditAccount,
|
|
847
|
-
manager: cm.name
|
|
848
|
-
}, `generating price feed updates for ${tStr} from ${priceFeeds.length}${remark} price feeds`);
|
|
849
|
-
return this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(priceFeeds);
|
|
850
|
-
}
|
|
851
|
-
/**
|
|
852
|
-
* {@inheritDoc ICreditAccountsService.prependPriceUpdates}
|
|
853
|
-
*/
|
|
854
|
-
async prependPriceUpdates(creditManager, calls, creditAccount, options) {
|
|
855
|
-
return this.#prependPriceUpdates(creditManager, calls, creditAccount, options);
|
|
856
|
-
}
|
|
857
498
|
/**
|
|
858
499
|
* {@inheritDoc ICreditAccountsService.assembleCaOperations}
|
|
859
500
|
*/
|
|
@@ -899,7 +540,7 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
899
540
|
*/
|
|
900
541
|
async executeCaUpdate(creditAccount, calls, options) {
|
|
901
542
|
const cm = this.sdk.marketRegister.findCreditManager(creditAccount.creditManager);
|
|
902
|
-
const callsWithPrices = await this
|
|
543
|
+
const callsWithPrices = await this.prependPriceUpdates(creditAccount.creditManager, calls, creditAccount, { ignoreReservePrices: options?.ignoreReservePrices });
|
|
903
544
|
const tx = await this.#multicallTx(cm, creditAccount.creditAccount, callsWithPrices);
|
|
904
545
|
if (options?.ethAmount && options.ethAmount > 0n) tx.value = options.ethAmount.toString(10);
|
|
905
546
|
return tx;
|
|
@@ -934,25 +575,12 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
934
575
|
prepareAddCollateral(creditFacade, assets, permits) {
|
|
935
576
|
return this.sdk.marketRegister.findCreditFacade(creditFacade).prepareAddCollateral(assets, permits);
|
|
936
577
|
}
|
|
937
|
-
/**
|
|
938
|
-
* Returns addresses of market configurators
|
|
939
|
-
*/
|
|
940
|
-
get marketConfigurators() {
|
|
941
|
-
return this.sdk.marketRegister.marketConfigurators.map((mc) => mc.address);
|
|
942
|
-
}
|
|
943
578
|
get rewardCompressor() {
|
|
944
579
|
return this.sdk.addressProvider.mustGetLatest(require_sdk_constants_address_provider.AP_REWARDS_COMPRESSOR, require_sdk_constants_versions.VERSION_RANGE_310)[0];
|
|
945
580
|
}
|
|
946
581
|
get peripheryCompressor() {
|
|
947
582
|
return this.sdk.addressProvider.mustGetLatest(require_sdk_constants_address_provider.AP_PERIPHERY_COMPRESSOR, require_sdk_constants_versions.VERSION_RANGE_310)[0];
|
|
948
583
|
}
|
|
949
|
-
get compressor() {
|
|
950
|
-
if (!this.#compressor) {
|
|
951
|
-
[this.#compressor] = this.sdk.addressProvider.mustGetLatest(require_sdk_constants_address_provider.AP_CREDIT_ACCOUNT_COMPRESSOR, require_sdk_constants_versions.VERSION_RANGE_310);
|
|
952
|
-
this.logger?.debug(`credit account compressor address: ${this.#compressor}`);
|
|
953
|
-
}
|
|
954
|
-
return this.#compressor;
|
|
955
|
-
}
|
|
956
584
|
/**
|
|
957
585
|
* Wrapper that selects between credit facade and RWA factory
|
|
958
586
|
* @param suite
|