@gearbox-protocol/sdk 13.5.0 → 13.6.0-kyc.2
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/abi/kyc/iKYCCompressor.js +196 -0
- package/dist/cjs/abi/kyc/iKYCFactory.js +122 -0
- package/dist/cjs/abi/kyc/iKYCUnderlying.js +401 -0
- package/dist/cjs/abi/kyc/iSecuritizeDegenNFT.js +326 -0
- package/dist/cjs/abi/kyc/iSecuritizeKYCFactory.js +319 -0
- package/dist/cjs/dev/AccountOpener.js +45 -5
- package/dist/cjs/sdk/GearboxSDK.js +51 -4
- package/dist/cjs/sdk/accounts/AbstractCreditAccountsService.js +331 -16
- package/dist/cjs/sdk/accounts/CreditAccountsServiceV310.js +7 -1
- package/dist/cjs/sdk/base/TokensMeta.js +32 -43
- package/dist/cjs/sdk/base/token-types.js +9 -0
- package/dist/cjs/sdk/chain/chains.js +2 -1
- package/dist/cjs/sdk/constants/address-provider.js +3 -0
- package/dist/cjs/sdk/market/MarketRegister.js +70 -116
- package/dist/cjs/sdk/market/MarketSuite.js +3 -0
- package/dist/cjs/sdk/market/kyc/KYCRegister.js +255 -0
- package/dist/cjs/sdk/market/kyc/index.js +24 -0
- package/dist/cjs/sdk/market/kyc/securitize/SecuritizeKYCFactory.js +229 -0
- package/dist/cjs/sdk/market/kyc/securitize/constants.js +28 -0
- package/dist/cjs/sdk/market/kyc/securitize/index.js +26 -0
- package/dist/cjs/sdk/market/kyc/securitize/types.js +16 -0
- package/dist/cjs/sdk/market/kyc/types.js +29 -0
- package/dist/cjs/sdk/market/pool/PoolSuite.js +3 -0
- package/dist/cjs/sdk/market/pool/PoolV310Contract.js +11 -2
- package/dist/cjs/sdk/market/pool/index.js +2 -0
- package/dist/cjs/sdk/market/pricefeeds/PriceFeedsRegister.js +3 -3
- package/dist/cjs/sdk/options.js +6 -0
- package/dist/cjs/sdk/pools/PoolService.js +104 -12
- package/dist/cjs/sdk/utils/viem/executeDelegatedMulticalls.js +38 -0
- package/dist/cjs/sdk/utils/viem/index.js +2 -0
- package/dist/esm/abi/kyc/iKYCCompressor.js +172 -0
- package/dist/esm/abi/kyc/iKYCFactory.js +98 -0
- package/dist/esm/abi/kyc/iKYCUnderlying.js +377 -0
- package/dist/esm/abi/kyc/iSecuritizeDegenNFT.js +302 -0
- package/dist/esm/abi/kyc/iSecuritizeKYCFactory.js +295 -0
- package/dist/esm/dev/AccountOpener.js +47 -6
- package/dist/esm/sdk/GearboxSDK.js +55 -5
- package/dist/esm/sdk/accounts/AbstractCreditAccountsService.js +332 -17
- package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +7 -1
- package/dist/esm/sdk/base/TokensMeta.js +32 -45
- package/dist/esm/sdk/base/token-types.js +6 -0
- package/dist/esm/sdk/chain/chains.js +2 -1
- package/dist/esm/sdk/constants/address-provider.js +2 -0
- package/dist/esm/sdk/market/MarketRegister.js +74 -118
- package/dist/esm/sdk/market/MarketSuite.js +3 -0
- package/dist/esm/sdk/market/kyc/KYCRegister.js +239 -0
- package/dist/esm/sdk/market/kyc/index.js +2 -0
- package/dist/esm/sdk/market/kyc/securitize/SecuritizeKYCFactory.js +205 -0
- package/dist/esm/sdk/market/kyc/securitize/constants.js +4 -0
- package/dist/esm/sdk/market/kyc/securitize/index.js +3 -0
- package/dist/esm/sdk/market/kyc/securitize/types.js +0 -0
- package/dist/esm/sdk/market/kyc/types.js +5 -0
- package/dist/esm/sdk/market/pool/PoolSuite.js +3 -0
- package/dist/esm/sdk/market/pool/PoolV310Contract.js +11 -2
- package/dist/esm/sdk/market/pool/index.js +1 -0
- package/dist/esm/sdk/market/pricefeeds/PriceFeedsRegister.js +3 -3
- package/dist/esm/sdk/options.js +6 -0
- package/dist/esm/sdk/pools/PoolService.js +109 -13
- package/dist/esm/sdk/utils/viem/executeDelegatedMulticalls.js +14 -0
- package/dist/esm/sdk/utils/viem/index.js +1 -0
- package/dist/types/abi/kyc/iKYCCompressor.d.ts +228 -0
- package/dist/types/abi/kyc/iKYCFactory.d.ts +139 -0
- package/dist/types/abi/kyc/iKYCUnderlying.d.ts +548 -0
- package/dist/types/abi/kyc/iSecuritizeDegenNFT.d.ts +404 -0
- package/dist/types/abi/kyc/iSecuritizeKYCFactory.d.ts +376 -0
- package/dist/types/sdk/GearboxSDK.d.ts +7 -0
- package/dist/types/sdk/accounts/AbstractCreditAccountsService.d.ts +55 -2
- package/dist/types/sdk/accounts/CreditAccountsServiceV310.d.ts +1 -1
- package/dist/types/sdk/accounts/types.d.ts +93 -13
- package/dist/types/sdk/base/TokensMeta.d.ts +14 -3
- package/dist/types/sdk/base/token-types.d.ts +44 -4
- package/dist/types/sdk/base/types.d.ts +116 -2
- package/dist/types/sdk/chain/chains.d.ts +5 -1
- package/dist/types/sdk/constants/address-provider.d.ts +1 -0
- package/dist/types/sdk/market/MarketRegister.d.ts +6 -9
- package/dist/types/sdk/market/MarketSuite.d.ts +2 -0
- package/dist/types/sdk/market/kyc/KYCRegister.d.ts +31 -0
- package/dist/types/sdk/market/kyc/index.d.ts +2 -0
- package/dist/types/sdk/market/kyc/securitize/SecuritizeKYCFactory.d.ts +420 -0
- package/dist/types/sdk/market/kyc/securitize/constants.d.ts +1 -0
- package/dist/types/sdk/market/kyc/securitize/index.d.ts +3 -0
- package/dist/types/sdk/market/kyc/securitize/types.d.ts +62 -0
- package/dist/types/sdk/market/kyc/types.d.ts +93 -0
- package/dist/types/sdk/market/oracle/PriceOracleBaseContract.d.ts +3 -2
- package/dist/types/sdk/market/oracle/types.d.ts +3 -10
- package/dist/types/sdk/market/pool/PoolSuite.d.ts +2 -0
- package/dist/types/sdk/market/pool/PoolV310Contract.d.ts +6 -2
- package/dist/types/sdk/market/pool/index.d.ts +1 -0
- package/dist/types/sdk/market/pricefeeds/PriceFeedsRegister.d.ts +1 -1
- package/dist/types/sdk/market/types.d.ts +1 -1
- package/dist/types/sdk/options.d.ts +1 -0
- package/dist/types/sdk/pools/PoolService.d.ts +8 -8
- package/dist/types/sdk/pools/types.d.ts +1 -1
- package/dist/types/sdk/types/state-human.d.ts +2 -0
- package/dist/types/sdk/types/state.d.ts +5 -0
- package/dist/types/sdk/utils/viem/executeDelegatedMulticalls.d.ts +11 -0
- package/dist/types/sdk/utils/viem/index.d.ts +1 -0
- package/package.json +3 -2
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
VERSION_RANGE_310
|
|
17
17
|
} from "./constants/index.js";
|
|
18
18
|
import { createAddressProvider, hydrateAddressProvider } from "./core/index.js";
|
|
19
|
+
import { KYCRegister } from "./market/kyc/index.js";
|
|
19
20
|
import { MarketRegister } from "./market/MarketRegister.js";
|
|
20
21
|
import { PriceFeedRegister } from "./market/pricefeeds/index.js";
|
|
21
22
|
import {
|
|
@@ -24,7 +25,10 @@ import {
|
|
|
24
25
|
import { createRouter } from "./router/index.js";
|
|
25
26
|
import { formatTimestamp, TypedObjectUtils, toAddress } from "./utils/index.js";
|
|
26
27
|
import { Hooks } from "./utils/internal/index.js";
|
|
27
|
-
import {
|
|
28
|
+
import {
|
|
29
|
+
executeDelegatedMulticalls,
|
|
30
|
+
getLogsSafe
|
|
31
|
+
} from "./utils/viem/index.js";
|
|
28
32
|
const ERR_NOT_ATTACHED = new Error("Gearbox SDK not attached");
|
|
29
33
|
const STATE_VERSION = 1;
|
|
30
34
|
function createClient(opts, network) {
|
|
@@ -77,6 +81,7 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
77
81
|
#addressProvider;
|
|
78
82
|
#attachConfig;
|
|
79
83
|
#marketRegister;
|
|
84
|
+
#kyc;
|
|
80
85
|
#priceFeeds;
|
|
81
86
|
/**
|
|
82
87
|
* Gas limit applied to read-only `eth_call` requests.
|
|
@@ -121,6 +126,7 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
121
126
|
ignoreUpdateablePrices,
|
|
122
127
|
ignoreMarkets,
|
|
123
128
|
marketConfigurators: mcs,
|
|
129
|
+
kycFactories: kfs,
|
|
124
130
|
strictContractTypes,
|
|
125
131
|
gasLimit
|
|
126
132
|
} = options;
|
|
@@ -130,6 +136,7 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
130
136
|
addressProvider = ADDRESS_PROVIDER_V310;
|
|
131
137
|
}
|
|
132
138
|
const marketConfigurators = mcs ?? TypedObjectUtils.keys(client.chain.defaultMarketConfigurators);
|
|
139
|
+
const kycFactories = kfs ?? client.chain.kycFactories;
|
|
133
140
|
return new GearboxSDK({
|
|
134
141
|
client,
|
|
135
142
|
logger,
|
|
@@ -142,6 +149,7 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
142
149
|
ignoreUpdateablePrices,
|
|
143
150
|
ignoreMarkets,
|
|
144
151
|
marketConfigurators,
|
|
152
|
+
kycFactories,
|
|
145
153
|
redstone,
|
|
146
154
|
pyth
|
|
147
155
|
});
|
|
@@ -191,6 +199,7 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
191
199
|
ignoreUpdateablePrices,
|
|
192
200
|
ignoreMarkets,
|
|
193
201
|
marketConfigurators,
|
|
202
|
+
kycFactories,
|
|
194
203
|
redstone,
|
|
195
204
|
pyth
|
|
196
205
|
} = opts;
|
|
@@ -201,6 +210,7 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
201
210
|
chainId: this.chainId,
|
|
202
211
|
addressProvider,
|
|
203
212
|
marketConfigurators,
|
|
213
|
+
kycFactories,
|
|
204
214
|
blockNumber,
|
|
205
215
|
ignoreMarkets
|
|
206
216
|
},
|
|
@@ -235,10 +245,34 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
235
245
|
);
|
|
236
246
|
await this.#addressProvider.syncState(this.currentBlock);
|
|
237
247
|
this.#marketRegister = new MarketRegister(this, ignoreMarkets);
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
248
|
+
this.#kyc = new KYCRegister(this);
|
|
249
|
+
if (!marketConfigurators.length) {
|
|
250
|
+
this.logger?.warn(
|
|
251
|
+
"no market configurators provided, skipping market loading"
|
|
252
|
+
);
|
|
253
|
+
} else {
|
|
254
|
+
const delegated = [
|
|
255
|
+
...this.#marketRegister.getLoadMulticalls(marketConfigurators),
|
|
256
|
+
...this.#kyc.getLoadMulticalls(marketConfigurators, kycFactories)
|
|
257
|
+
];
|
|
258
|
+
let txs = [];
|
|
259
|
+
if (!ignoreUpdateablePrices) {
|
|
260
|
+
const updatables = await this.#priceFeeds.getPartialUpdatablePriceFeeds(
|
|
261
|
+
marketConfigurators
|
|
262
|
+
);
|
|
263
|
+
const updates = await this.#priceFeeds.generatePriceFeedsUpdateTxs(updatables);
|
|
264
|
+
txs = updates.txs;
|
|
265
|
+
}
|
|
266
|
+
this.logger?.debug(
|
|
267
|
+
{ configurators: marketConfigurators },
|
|
268
|
+
`calling getMarkets with ${txs.length} price updates in block ${this.currentBlock}`
|
|
269
|
+
);
|
|
270
|
+
await executeDelegatedMulticalls(this.client, delegated, {
|
|
271
|
+
priceUpdates: txs,
|
|
272
|
+
blockNumber: this.currentBlock,
|
|
273
|
+
gas: this.gasLimit
|
|
274
|
+
});
|
|
275
|
+
}
|
|
242
276
|
const pluginsList = TypedObjectUtils.entries(this.plugins);
|
|
243
277
|
const pluginResponse = await Promise.allSettled(
|
|
244
278
|
pluginsList.map(([name, plugin]) => {
|
|
@@ -284,12 +318,15 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
284
318
|
);
|
|
285
319
|
this.#marketRegister = new MarketRegister(this, ignoreMarkets);
|
|
286
320
|
this.#marketRegister.hydrate(state.markets);
|
|
321
|
+
this.#kyc = new KYCRegister(this);
|
|
322
|
+
this.#kyc.setState(state.kyc);
|
|
287
323
|
this.#attachConfig = {
|
|
288
324
|
...opts,
|
|
289
325
|
addressProvider: this.addressProvider.address,
|
|
290
326
|
marketConfigurators: this.marketRegister.marketConfigurators.map(
|
|
291
327
|
(m) => m.address
|
|
292
328
|
),
|
|
329
|
+
kycFactories: this.kyc.factories.map((f) => f.address),
|
|
293
330
|
blockNumber: this.currentBlock
|
|
294
331
|
};
|
|
295
332
|
for (const [name, plugin] of TypedObjectUtils.entries(this.plugins)) {
|
|
@@ -373,6 +410,7 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
373
410
|
addressProviderV3: this.addressProvider.stateHuman(raw)
|
|
374
411
|
},
|
|
375
412
|
tokens: this.tokensMeta.values(),
|
|
413
|
+
kyc: this.kyc.stateHuman(raw),
|
|
376
414
|
plugins: Object.fromEntries(
|
|
377
415
|
TypedObjectUtils.entries(this.plugins).map(([name, plugin]) => [
|
|
378
416
|
name,
|
|
@@ -398,6 +436,7 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
398
436
|
timestamp: this.timestamp,
|
|
399
437
|
addressProvider: this.addressProvider.state,
|
|
400
438
|
markets: this.marketRegister.state,
|
|
439
|
+
kyc: this.kyc.state,
|
|
401
440
|
plugins: Object.fromEntries(
|
|
402
441
|
TypedObjectUtils.entries(this.plugins).map(([name, plugin]) => [
|
|
403
442
|
name,
|
|
@@ -594,6 +633,17 @@ class GearboxSDK extends ChainContractsRegister {
|
|
|
594
633
|
}
|
|
595
634
|
return this.#marketRegister;
|
|
596
635
|
}
|
|
636
|
+
/**
|
|
637
|
+
* KYC register for KYC-wrapped underlying tokens and factories.
|
|
638
|
+
*
|
|
639
|
+
* @throws If the SDK has not been attached or hydrated yet.
|
|
640
|
+
**/
|
|
641
|
+
get kyc() {
|
|
642
|
+
if (this.#kyc === void 0) {
|
|
643
|
+
throw ERR_NOT_ATTACHED;
|
|
644
|
+
}
|
|
645
|
+
return this.#kyc;
|
|
646
|
+
}
|
|
597
647
|
/**
|
|
598
648
|
* Resolves the appropriate router contract for a given credit manager,
|
|
599
649
|
* credit facade, or explicit version range.
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ierc4626AdapterAbi } from "@gearbox-protocol/integrations-v3";
|
|
1
2
|
import { encodeFunctionData, getContract } from "viem";
|
|
2
3
|
import {
|
|
3
4
|
iBotListV310Abi,
|
|
@@ -8,6 +9,7 @@ import { peripheryCompressorAbi } from "../../abi/compressors/peripheryCompresso
|
|
|
8
9
|
import { rewardsCompressorAbi } from "../../abi/compressors/rewardsCompressor.js";
|
|
9
10
|
import { iWithdrawalCompressorV310Abi } from "../../abi/IWithdrawalCompressorV310.js";
|
|
10
11
|
import { iBaseRewardPoolAbi } from "../../abi/iBaseRewardPool.js";
|
|
12
|
+
import { iKYCFactoryAbi } from "../../abi/kyc/iKYCFactory.js";
|
|
11
13
|
import { SDKConstruct } from "../base/index.js";
|
|
12
14
|
import { chains } from "../chain/chains.js";
|
|
13
15
|
import {
|
|
@@ -25,7 +27,7 @@ import {
|
|
|
25
27
|
getRawPriceUpdates
|
|
26
28
|
} from "../market/index.js";
|
|
27
29
|
import { assetsMap } from "../router/index.js";
|
|
28
|
-
import { AddressMap, AddressSet } from "../utils/index.js";
|
|
30
|
+
import { AddressMap, AddressSet, hexEq } from "../utils/index.js";
|
|
29
31
|
import { simulateWithPriceUpdates } from "../utils/viem/index.js";
|
|
30
32
|
import {
|
|
31
33
|
extractPriceUpdates,
|
|
@@ -71,24 +73,40 @@ class AbstractCreditAccountService extends SDKConstruct {
|
|
|
71
73
|
} catch (_e) {
|
|
72
74
|
return void 0;
|
|
73
75
|
}
|
|
76
|
+
const marketSuite = this.sdk.marketRegister.findByCreditManager(
|
|
77
|
+
raw.creditManager
|
|
78
|
+
);
|
|
79
|
+
const factory = marketSuite.kycFactory;
|
|
80
|
+
let ca;
|
|
81
|
+
let investor;
|
|
74
82
|
if (raw.success) {
|
|
75
|
-
|
|
83
|
+
ca = raw;
|
|
84
|
+
investor = await factory?.getInvestor(raw.creditAccount, false);
|
|
85
|
+
} else {
|
|
86
|
+
const { txs: priceUpdateTxs } = await this.getUpdateForAccount(raw);
|
|
87
|
+
[ca, investor] = await simulateWithPriceUpdates(this.client, {
|
|
88
|
+
priceUpdates: priceUpdateTxs,
|
|
89
|
+
contracts: [
|
|
90
|
+
{
|
|
91
|
+
abi: creditAccountCompressorAbi,
|
|
92
|
+
address: this.#compressor,
|
|
93
|
+
functionName: "getCreditAccountData",
|
|
94
|
+
args: [account]
|
|
95
|
+
},
|
|
96
|
+
...factory ? [
|
|
97
|
+
{
|
|
98
|
+
abi: iKYCFactoryAbi,
|
|
99
|
+
address: factory.address,
|
|
100
|
+
functionName: "getInvestor",
|
|
101
|
+
args: [raw.creditAccount]
|
|
102
|
+
}
|
|
103
|
+
] : []
|
|
104
|
+
],
|
|
105
|
+
blockNumber,
|
|
106
|
+
gas: this.sdk.gasLimit
|
|
107
|
+
});
|
|
76
108
|
}
|
|
77
|
-
|
|
78
|
-
const [cad] = await simulateWithPriceUpdates(this.client, {
|
|
79
|
-
priceUpdates: priceUpdateTxs,
|
|
80
|
-
contracts: [
|
|
81
|
-
{
|
|
82
|
-
abi: creditAccountCompressorAbi,
|
|
83
|
-
address: this.#compressor,
|
|
84
|
-
functionName: "getCreditAccountData",
|
|
85
|
-
args: [account]
|
|
86
|
-
}
|
|
87
|
-
],
|
|
88
|
-
blockNumber,
|
|
89
|
-
gas: this.sdk.gasLimit
|
|
90
|
-
});
|
|
91
|
-
return cad;
|
|
109
|
+
return { ...ca, investor };
|
|
92
110
|
}
|
|
93
111
|
/**
|
|
94
112
|
* {@inheritDoc ICreditAccountsService.getCreditAccounts}
|
|
@@ -144,6 +162,99 @@ class AbstractCreditAccountService extends SDKConstruct {
|
|
|
144
162
|
);
|
|
145
163
|
return allCAs.sort((a, b) => Number(a.healthFactor - b.healthFactor));
|
|
146
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* {@inheritDoc ICreditAccountsService.getBorrowerCreditAccounts}
|
|
167
|
+
**/
|
|
168
|
+
async getBorrowerCreditAccounts(borrower, options, blockNumber) {
|
|
169
|
+
const {
|
|
170
|
+
creditManager,
|
|
171
|
+
includeZeroDebt = false,
|
|
172
|
+
maxHealthFactor = MAX_UINT256,
|
|
173
|
+
minHealthFactor = 0n,
|
|
174
|
+
ignoreReservePrices = false
|
|
175
|
+
} = options ?? {};
|
|
176
|
+
const { txs: priceUpdateTxs } = await this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(
|
|
177
|
+
ignoreReservePrices ? { main: true } : void 0
|
|
178
|
+
);
|
|
179
|
+
const investorDataList = await this.sdk.kyc.getInvestorData(borrower);
|
|
180
|
+
const kycAccountAddresses = investorDataList.flatMap(
|
|
181
|
+
(d) => d.creditAccounts.map((ca) => ca.creditAccount)
|
|
182
|
+
);
|
|
183
|
+
const cmFilter = creditManager ? {
|
|
184
|
+
configurators: [],
|
|
185
|
+
creditManagers: [creditManager],
|
|
186
|
+
pools: [],
|
|
187
|
+
underlying: ADDRESS_0X0
|
|
188
|
+
} : {
|
|
189
|
+
configurators: this.marketConfigurators,
|
|
190
|
+
creditManagers: [],
|
|
191
|
+
pools: [],
|
|
192
|
+
underlying: ADDRESS_0X0
|
|
193
|
+
};
|
|
194
|
+
const permissiveFilter = {
|
|
195
|
+
owner: borrower,
|
|
196
|
+
includeZeroDebt: true,
|
|
197
|
+
minHealthFactor: 0n,
|
|
198
|
+
maxHealthFactor: MAX_UINT256,
|
|
199
|
+
reverting: false
|
|
200
|
+
};
|
|
201
|
+
const kycContracts = kycAccountAddresses.map(
|
|
202
|
+
(account) => ({
|
|
203
|
+
abi: creditAccountCompressorAbi,
|
|
204
|
+
address: this.#compressor,
|
|
205
|
+
functionName: "getCreditAccountData",
|
|
206
|
+
args: [account]
|
|
207
|
+
})
|
|
208
|
+
);
|
|
209
|
+
const getCreditAccountsContracts = [false, true].map(
|
|
210
|
+
(reverting) => ({
|
|
211
|
+
abi: creditAccountCompressorAbi,
|
|
212
|
+
address: this.#compressor,
|
|
213
|
+
functionName: "getCreditAccounts",
|
|
214
|
+
args: [cmFilter, { ...permissiveFilter, reverting }, 0n]
|
|
215
|
+
})
|
|
216
|
+
);
|
|
217
|
+
const allContracts = [...kycContracts, ...getCreditAccountsContracts];
|
|
218
|
+
const results = await simulateWithPriceUpdates(this.client, {
|
|
219
|
+
priceUpdates: priceUpdateTxs,
|
|
220
|
+
contracts: allContracts,
|
|
221
|
+
blockNumber,
|
|
222
|
+
gas: this.sdk.gasLimit
|
|
223
|
+
});
|
|
224
|
+
const kycResults = results.slice(
|
|
225
|
+
0,
|
|
226
|
+
kycAccountAddresses.length
|
|
227
|
+
);
|
|
228
|
+
const normalResults = results.slice(kycAccountAddresses.length);
|
|
229
|
+
const seen = new AddressSet();
|
|
230
|
+
const allCAs = [];
|
|
231
|
+
for (const ca of kycResults) {
|
|
232
|
+
if (!seen.has(ca.creditAccount)) {
|
|
233
|
+
seen.add(ca.creditAccount);
|
|
234
|
+
allCAs.push({ ...ca, investor: borrower });
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
for (const [accounts] of normalResults) {
|
|
238
|
+
for (const ca of accounts) {
|
|
239
|
+
if (!seen.has(ca.creditAccount)) {
|
|
240
|
+
seen.add(ca.creditAccount);
|
|
241
|
+
allCAs.push({ ...ca, investor: void 0 });
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
const filtered = allCAs.filter((ca) => {
|
|
246
|
+
if (!includeZeroDebt && ca.debt === 0n) return false;
|
|
247
|
+
if (ca.healthFactor < minHealthFactor) return false;
|
|
248
|
+
if (ca.healthFactor > maxHealthFactor) return false;
|
|
249
|
+
if (creditManager && !hexEq(ca.creditManager, creditManager))
|
|
250
|
+
return false;
|
|
251
|
+
return true;
|
|
252
|
+
});
|
|
253
|
+
this.logger?.debug(
|
|
254
|
+
`loaded ${allCAs.length} borrower credit accounts (${kycResults.length} KYC, ${filtered.length} after filter)`
|
|
255
|
+
);
|
|
256
|
+
return filtered.sort((a, b) => Number(a.healthFactor - b.healthFactor));
|
|
257
|
+
}
|
|
147
258
|
/**
|
|
148
259
|
* {@inheritDoc ICreditAccountsService.getRewards}
|
|
149
260
|
**/
|
|
@@ -353,6 +464,13 @@ class AbstractCreditAccountService extends SDKConstruct {
|
|
|
353
464
|
closePath
|
|
354
465
|
}) {
|
|
355
466
|
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
467
|
+
await this.sdk.tokensMeta.loadTokenData(cm.underlying);
|
|
468
|
+
const underlying = this.sdk.tokensMeta.mustGet(cm.underlying);
|
|
469
|
+
if (this.sdk.tokensMeta.isKYCUnderlying(underlying)) {
|
|
470
|
+
throw new Error(
|
|
471
|
+
"closeCreditAccount is not supported for KYC underlying credit accounts"
|
|
472
|
+
);
|
|
473
|
+
}
|
|
356
474
|
const routerCloseResult = closePath || await this.sdk.routerFor(ca).findBestClosePath({
|
|
357
475
|
creditAccount: ca,
|
|
358
476
|
creditManager: cm.creditManager,
|
|
@@ -458,8 +576,18 @@ class AbstractCreditAccountService extends SDKConstruct {
|
|
|
458
576
|
],
|
|
459
577
|
{}
|
|
460
578
|
) : [];
|
|
579
|
+
const unwrapCalls = collateral && isDecrease ? await this.getKYCUnwrapCalls(
|
|
580
|
+
collateral[0].balance,
|
|
581
|
+
creditAccount.creditManager
|
|
582
|
+
) || [] : [];
|
|
583
|
+
if (addCollateralCalls.length > 0 && unwrapCalls.length === 0 && collateral && collateral?.[0].token !== creditAccount.underlying) {
|
|
584
|
+
throw new Error(
|
|
585
|
+
"Can't use collateral other than underlying for non KYC market"
|
|
586
|
+
);
|
|
587
|
+
}
|
|
461
588
|
const operationCalls = [
|
|
462
589
|
...addCollateralCalls,
|
|
590
|
+
...unwrapCalls,
|
|
463
591
|
this.#prepareChangeDebt(creditAccount.creditFacade, change, isDecrease)
|
|
464
592
|
];
|
|
465
593
|
const calls = await this.prependPriceUpdates(
|
|
@@ -667,6 +795,36 @@ class AbstractCreditAccountService extends SDKConstruct {
|
|
|
667
795
|
const tx = await this.multicallTx(cm, creditAccount.creditAccount, calls);
|
|
668
796
|
return { tx, calls, creditFacade: cm.creditFacade };
|
|
669
797
|
}
|
|
798
|
+
/**
|
|
799
|
+
* Returns address to which approval should be given on collateral token
|
|
800
|
+
* It's credit manager for classical markets and special wallet for KYC markets
|
|
801
|
+
* @param options - {@link GetApprovalAddressProps}
|
|
802
|
+
* @returns
|
|
803
|
+
**/
|
|
804
|
+
async getApprovalAddress(options) {
|
|
805
|
+
const { creditManager } = options;
|
|
806
|
+
const suite = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
807
|
+
const marketSuite = this.sdk.marketRegister.findByPool(suite.pool);
|
|
808
|
+
const factory = marketSuite.kycFactory;
|
|
809
|
+
if (factory) {
|
|
810
|
+
if ("creditAccount" in options) {
|
|
811
|
+
return factory.getWallet(options.creditAccount);
|
|
812
|
+
}
|
|
813
|
+
return factory.precomputeWalletAddress(creditManager, options.borrower);
|
|
814
|
+
}
|
|
815
|
+
return suite.creditManager.address;
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* {@inheritDoc ICreditAccountsService.getOpenAccountRequirements}
|
|
819
|
+
*/
|
|
820
|
+
async getOpenAccountRequirements(borrower, props) {
|
|
821
|
+
const { creditManager } = props;
|
|
822
|
+
const { kycFactory } = this.sdk.marketRegister.findByCreditManager(creditManager);
|
|
823
|
+
if (!kycFactory) {
|
|
824
|
+
return void 0;
|
|
825
|
+
}
|
|
826
|
+
return kycFactory.getOpenAccountRequirements(borrower);
|
|
827
|
+
}
|
|
670
828
|
/**
|
|
671
829
|
* {@inheritDoc ICreditAccountsService.openCA}
|
|
672
830
|
**/
|
|
@@ -801,6 +959,131 @@ class AbstractCreditAccountService extends SDKConstruct {
|
|
|
801
959
|
return resp;
|
|
802
960
|
}
|
|
803
961
|
/**
|
|
962
|
+
* Returns multicall entries to redeem (unwrap) KYC ERC-4626 vault shares into underlying for the given credit manager.
|
|
963
|
+
* Used when withdrawing debt from a KYC market: redeems adapter vault shares so the underlying can be withdrawn.
|
|
964
|
+
* Only applies when the credit manager's underlying is KYC-gated and has an ERC-4626 adapter configured.
|
|
965
|
+
* @param amount - Number of vault shares (adapter tokens) to redeem
|
|
966
|
+
* @param creditManager - Credit manager address
|
|
967
|
+
* @returns Array of MultiCall to pass to credit facade multicall, or undefined if underlying is not KYC or no adapter is configured
|
|
968
|
+
*/
|
|
969
|
+
async getKYCUnwrapCalls(amount, creditManager) {
|
|
970
|
+
const suite = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
971
|
+
const meta = this.sdk.tokensMeta.mustGet(suite.underlying);
|
|
972
|
+
if (!this.sdk.tokensMeta.isKYCUnderlying(meta)) {
|
|
973
|
+
return void 0;
|
|
974
|
+
}
|
|
975
|
+
const adapter = suite.creditManager.adapters.get(meta.addr);
|
|
976
|
+
const adapterAddress = adapter?.address;
|
|
977
|
+
if (!adapterAddress) {
|
|
978
|
+
return void 0;
|
|
979
|
+
}
|
|
980
|
+
const mc = [
|
|
981
|
+
{
|
|
982
|
+
target: adapterAddress,
|
|
983
|
+
callData: encodeFunctionData({
|
|
984
|
+
abi: ierc4626AdapterAbi,
|
|
985
|
+
functionName: "redeem",
|
|
986
|
+
args: [amount, ADDRESS_0X0, ADDRESS_0X0]
|
|
987
|
+
})
|
|
988
|
+
}
|
|
989
|
+
];
|
|
990
|
+
return mc;
|
|
991
|
+
}
|
|
992
|
+
/**
|
|
993
|
+
* Returns multicall entries to deposit (wrap) underlying into KYC ERC-4626 vault shares for the given credit manager.
|
|
994
|
+
* Used when adding debt on a KYC market: deposits underlying into the adapter vault so shares are minted on the account.
|
|
995
|
+
* Only applies when the credit manager's underlying is KYC-gated and has an ERC-4626 adapter configured.
|
|
996
|
+
* @param amount - Amount of underlying assets to deposit into the vault (in underlying decimals)
|
|
997
|
+
* @param creditManager - Credit manager address
|
|
998
|
+
* @returns Array of MultiCall to pass to credit facade multicall, or undefined if underlying is not KYC or no adapter is configured
|
|
999
|
+
*/
|
|
1000
|
+
async getKYCWrapCalls(amount, creditManager) {
|
|
1001
|
+
const suite = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
1002
|
+
const meta = this.sdk.tokensMeta.mustGet(suite.underlying);
|
|
1003
|
+
if (!this.sdk.tokensMeta.isKYCUnderlying(meta)) {
|
|
1004
|
+
return void 0;
|
|
1005
|
+
}
|
|
1006
|
+
const adapter = suite.creditManager.adapters.get(meta.addr);
|
|
1007
|
+
const adapterAddress = adapter?.address;
|
|
1008
|
+
if (!adapterAddress) {
|
|
1009
|
+
return void 0;
|
|
1010
|
+
}
|
|
1011
|
+
const mc = [
|
|
1012
|
+
{
|
|
1013
|
+
target: adapterAddress,
|
|
1014
|
+
callData: encodeFunctionData({
|
|
1015
|
+
abi: ierc4626AdapterAbi,
|
|
1016
|
+
functionName: "deposit",
|
|
1017
|
+
args: [amount, ADDRESS_0X0]
|
|
1018
|
+
})
|
|
1019
|
+
}
|
|
1020
|
+
];
|
|
1021
|
+
return mc;
|
|
1022
|
+
}
|
|
1023
|
+
/**
|
|
1024
|
+
* Returns multicall entries to call redeemDiff on the KYC ERC-4626 adapter for the given credit manager.
|
|
1025
|
+
* Redeems the leftover vault shares (e.g. after repaying debt) so the account does not hold excess KYC vault tokens.
|
|
1026
|
+
* Only applies when the credit manager's underlying is KYC-gated and has an ERC-4626 adapter configured.
|
|
1027
|
+
* @param amount - Leftover vault share amount to redeem (in adapter/vault decimals)
|
|
1028
|
+
* @param creditManager - Credit manager address
|
|
1029
|
+
* @returns Array of MultiCall to pass to credit facade multicall, or undefined if underlying is not KYC or no adapter is configured
|
|
1030
|
+
*/
|
|
1031
|
+
async getRedeemDiffCalls(amount, creditManager) {
|
|
1032
|
+
const suite = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
1033
|
+
const meta = this.sdk.tokensMeta.mustGet(suite.underlying);
|
|
1034
|
+
if (!this.sdk.tokensMeta.isKYCUnderlying(meta)) {
|
|
1035
|
+
return void 0;
|
|
1036
|
+
}
|
|
1037
|
+
const adapter = suite.creditManager.adapters.get(meta.addr);
|
|
1038
|
+
const adapterAddress = adapter?.address;
|
|
1039
|
+
if (!adapterAddress) {
|
|
1040
|
+
return void 0;
|
|
1041
|
+
}
|
|
1042
|
+
const mc = [
|
|
1043
|
+
{
|
|
1044
|
+
target: adapterAddress,
|
|
1045
|
+
callData: encodeFunctionData({
|
|
1046
|
+
abi: ierc4626AdapterAbi,
|
|
1047
|
+
functionName: "redeemDiff",
|
|
1048
|
+
args: [amount]
|
|
1049
|
+
})
|
|
1050
|
+
}
|
|
1051
|
+
];
|
|
1052
|
+
return mc;
|
|
1053
|
+
}
|
|
1054
|
+
/**
|
|
1055
|
+
* Returns multicall entries to call depositDiff on the KYC ERC-4626 adapter for the given credit manager.
|
|
1056
|
+
* Deposits the leftover underlying (e.g. after decreasing debt) into the vault so the account does not hold excess underlying.
|
|
1057
|
+
* Only applies when the credit manager's underlying is KYC-gated and has an ERC-4626 adapter configured.
|
|
1058
|
+
* @param amount - Leftover underlying amount to deposit into the vault (in underlying decimals)
|
|
1059
|
+
* @param creditManager - Credit manager address
|
|
1060
|
+
* @returns Array of MultiCall to pass to credit facade multicall, or undefined if underlying is not KYC or no adapter is configured
|
|
1061
|
+
*/
|
|
1062
|
+
async getDepositDiffCalls(amount, creditManager) {
|
|
1063
|
+
const suite = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
1064
|
+
const meta = this.sdk.tokensMeta.mustGet(suite.underlying);
|
|
1065
|
+
if (!this.sdk.tokensMeta.isKYCUnderlying(meta)) {
|
|
1066
|
+
return void 0;
|
|
1067
|
+
}
|
|
1068
|
+
const adapter = suite.creditManager.adapters.get(meta.addr);
|
|
1069
|
+
const adapterAddress = adapter?.address;
|
|
1070
|
+
if (!adapterAddress) {
|
|
1071
|
+
return void 0;
|
|
1072
|
+
}
|
|
1073
|
+
const mc = [
|
|
1074
|
+
{
|
|
1075
|
+
target: adapterAddress,
|
|
1076
|
+
callData: encodeFunctionData({
|
|
1077
|
+
abi: ierc4626AdapterAbi,
|
|
1078
|
+
functionName: "depositDiff",
|
|
1079
|
+
args: [amount]
|
|
1080
|
+
})
|
|
1081
|
+
}
|
|
1082
|
+
];
|
|
1083
|
+
return mc;
|
|
1084
|
+
}
|
|
1085
|
+
/**
|
|
1086
|
+
* Returns raw txs that are needed to update all price feeds so that all credit accounts (possibly from different markets) compute
|
|
804
1087
|
* {@inheritDoc ICreditAccountsService.getOnDemandPriceUpdates}
|
|
805
1088
|
**/
|
|
806
1089
|
async getOnDemandPriceUpdates(account, ignoreReservePrices) {
|
|
@@ -1058,6 +1341,19 @@ class AbstractCreditAccountService extends SDKConstruct {
|
|
|
1058
1341
|
* @returns
|
|
1059
1342
|
*/
|
|
1060
1343
|
async openCreditAccountTx(suite, to, calls, referralCode) {
|
|
1344
|
+
const marketSuite = this.sdk.marketRegister.findByPool(suite.pool);
|
|
1345
|
+
const factory = marketSuite.kycFactory;
|
|
1346
|
+
if (factory) {
|
|
1347
|
+
const tokensToRegister = factory.dsTokens.map(
|
|
1348
|
+
(t) => t.address
|
|
1349
|
+
);
|
|
1350
|
+
return factory.openCreditAccount(
|
|
1351
|
+
suite.creditManager.address,
|
|
1352
|
+
calls,
|
|
1353
|
+
tokensToRegister,
|
|
1354
|
+
[]
|
|
1355
|
+
);
|
|
1356
|
+
}
|
|
1061
1357
|
return suite.creditFacade.openCreditAccount(to, calls, referralCode ?? 0n);
|
|
1062
1358
|
}
|
|
1063
1359
|
/**
|
|
@@ -1068,6 +1364,13 @@ class AbstractCreditAccountService extends SDKConstruct {
|
|
|
1068
1364
|
* @returns
|
|
1069
1365
|
*/
|
|
1070
1366
|
async multicallTx(suite, creditAccount, calls) {
|
|
1367
|
+
const marketSuite = this.sdk.marketRegister.findByCreditManager(
|
|
1368
|
+
suite.creditManager.address
|
|
1369
|
+
);
|
|
1370
|
+
const factory = marketSuite.kycFactory;
|
|
1371
|
+
if (factory) {
|
|
1372
|
+
return factory.multicall(creditAccount, calls, [], []);
|
|
1373
|
+
}
|
|
1071
1374
|
return suite.creditFacade.multicall(creditAccount, calls);
|
|
1072
1375
|
}
|
|
1073
1376
|
/**
|
|
@@ -1079,9 +1382,21 @@ class AbstractCreditAccountService extends SDKConstruct {
|
|
|
1079
1382
|
* @returns
|
|
1080
1383
|
*/
|
|
1081
1384
|
async closeCreditAccountTx(suite, creditAccount, calls, operation) {
|
|
1385
|
+
const marketSuite = this.sdk.marketRegister.findByCreditManager(
|
|
1386
|
+
suite.creditManager.address
|
|
1387
|
+
);
|
|
1388
|
+
const factory = marketSuite.kycFactory;
|
|
1082
1389
|
if (operation === "close") {
|
|
1390
|
+
if (factory) {
|
|
1391
|
+
throw new Error(
|
|
1392
|
+
"CloseOptions=close is not supported for KYC underlying credit accounts"
|
|
1393
|
+
);
|
|
1394
|
+
}
|
|
1083
1395
|
return suite.creditFacade.closeCreditAccount(creditAccount, calls);
|
|
1084
1396
|
}
|
|
1397
|
+
if (factory) {
|
|
1398
|
+
return factory.multicall(creditAccount, calls, [], []);
|
|
1399
|
+
}
|
|
1085
1400
|
return suite.creditFacade.multicall(creditAccount, calls);
|
|
1086
1401
|
}
|
|
1087
1402
|
}
|
|
@@ -90,19 +90,23 @@ class CreditAccountServiceV310 extends AbstractCreditAccountService {
|
|
|
90
90
|
creditAccount: ca,
|
|
91
91
|
permits,
|
|
92
92
|
to,
|
|
93
|
-
tokensToClaim
|
|
93
|
+
tokensToClaim,
|
|
94
|
+
calls: wrapCalls = []
|
|
94
95
|
}) {
|
|
95
96
|
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
96
97
|
const addCollateral = collateralAssets.filter((a) => a.balance > 0);
|
|
97
98
|
const router = this.sdk.routerFor(ca);
|
|
99
|
+
const unwrapCalls = await this.getRedeemDiffCalls(1n, ca.creditManager) ?? [];
|
|
98
100
|
const claimPath = await router.findClaimAllRewards({
|
|
99
101
|
tokensToClaim,
|
|
100
102
|
creditAccount: ca
|
|
101
103
|
});
|
|
102
104
|
const operationCalls = [
|
|
103
105
|
...this.prepareAddCollateral(ca.creditFacade, addCollateral, permits),
|
|
106
|
+
...wrapCalls,
|
|
104
107
|
...this.prepareDisableQuotas(ca),
|
|
105
108
|
...this.prepareDecreaseDebt(ca),
|
|
109
|
+
...unwrapCalls,
|
|
106
110
|
...claimPath.calls,
|
|
107
111
|
...assetsToWithdraw.map(
|
|
108
112
|
(t) => this.prepareWithdrawToken(ca.creditFacade, t.token, MAX_UINT256, to)
|
|
@@ -134,10 +138,12 @@ class CreditAccountServiceV310 extends AbstractCreditAccountService {
|
|
|
134
138
|
tokensToClaim,
|
|
135
139
|
creditAccount: ca
|
|
136
140
|
});
|
|
141
|
+
const wrapCalls = await this.getDepositDiffCalls(1n, ca.creditManager) ?? [];
|
|
137
142
|
const addCollateral = collateralAssets.filter((a) => a.balance > 0);
|
|
138
143
|
const operationCalls = [
|
|
139
144
|
...this.prepareAddCollateral(ca.creditFacade, addCollateral, permits),
|
|
140
145
|
...claimPath.calls,
|
|
146
|
+
...wrapCalls,
|
|
141
147
|
...assetsToWithdraw.map(
|
|
142
148
|
(t) => this.prepareWithdrawToken(ca.creditFacade, t.token, MAX_UINT256, to)
|
|
143
149
|
)
|