Skip to content

Commit 72d6805

Browse files
committed
fix: use common getKYCFactory helper
1 parent a0822b0 commit 72d6805

File tree

1 file changed

+42
-46
lines changed

1 file changed

+42
-46
lines changed

src/sdk/accounts/AbstractCreditAccountsService.ts

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ import {
2626
VERSION_RANGE_310,
2727
} from "../constants/index.js";
2828
import type { GearboxSDK } from "../GearboxSDK.js";
29-
import {
30-
type CreditSuite,
31-
type IPriceFeedContract,
32-
type IPriceOracleContract,
33-
type MarketSuite,
34-
type OnDemandPriceUpdates,
35-
type PriceUpdateV300,
36-
type PriceUpdateV310,
29+
import type {
30+
CreditSuite,
31+
IPriceFeedContract,
32+
IPriceOracleContract,
33+
MarketSuite,
34+
OnDemandPriceUpdates,
35+
PriceUpdateV300,
36+
PriceUpdateV310,
3737
SecuritizeKYCFactory,
38-
type UpdatePriceFeedsResult,
38+
UpdatePriceFeedsResult,
3939
} from "../market/index.js";
4040
import { type Asset, assetsMap, type RouterCASlice } from "../router/index.js";
4141
import { BigIntMath } from "../sdk-legacy/index.js";
@@ -119,6 +119,8 @@ const COMPRESSORS: Record<number, Address> = {
119119
[chains.Monad.id]: "0x36F3d0Bb73CBC2E94fE24dF0f26a689409cF9023",
120120
};
121121

122+
const INVESTORS: AddressMap<Address> = new AddressMap([], "investors");
123+
122124
export function getWithdrawalCompressorAddress(chainId: number) {
123125
return COMPRESSORS[chainId];
124126
}
@@ -202,23 +204,29 @@ export abstract class AbstractCreditAccountService extends SDKConstruct {
202204
const ca = await this.getCreditAccountData(account, blockNumber);
203205
if (!ca) return ca;
204206

205-
const factory = await this.getKYCFactory(ca.underlying);
206-
const investor = await (factory
207-
? this.client.multicall({
208-
contracts: [
209-
{
210-
abi: factory.abi,
211-
address: factory.address,
212-
functionName: "getInvestor",
213-
args: [ca.creditAccount],
214-
},
215-
],
216-
allowFailure: true,
217-
batchSize: 0,
218-
})
219-
: undefined);
207+
const marketSuite = this.sdk.marketRegister.findByCreditManager(
208+
ca.creditManager,
209+
);
210+
const factory = await marketSuite.getKYCFactory();
211+
212+
const investor = factory
213+
? (
214+
await this.client.multicall({
215+
contracts: [
216+
{
217+
abi: factory.abi,
218+
address: factory.address,
219+
functionName: "getInvestor",
220+
args: [ca.creditAccount],
221+
},
222+
],
223+
allowFailure: true,
224+
batchSize: 0,
225+
})
226+
)?.[0]?.result
227+
: undefined;
220228

221-
return { ...ca, investor: investor?.[0]?.result ?? ca.owner };
229+
return { ...ca, investor: investor ?? ca.owner };
222230
}
223231

224232
/**
@@ -1174,7 +1182,8 @@ export abstract class AbstractCreditAccountService extends SDKConstruct {
11741182
): Promise<Address> {
11751183
const { creditManager } = options;
11761184
const suite = this.sdk.marketRegister.findCreditManager(creditManager);
1177-
const factory = await this.getKYCFactory(suite.underlying);
1185+
const marketSuite = this.sdk.marketRegister.findByPool(suite.pool);
1186+
const factory = await marketSuite.getKYCFactory();
11781187

11791188
if (factory) {
11801189
if ("creditAccount" in options) {
@@ -1710,7 +1719,8 @@ export abstract class AbstractCreditAccountService extends SDKConstruct {
17101719
calls: MultiCall[],
17111720
referralCode?: bigint,
17121721
): Promise<RawTx> {
1713-
const factory = await this.getKYCFactory(suite.underlying);
1722+
const marketSuite = this.sdk.marketRegister.findByPool(suite.pool);
1723+
const factory = await marketSuite.getKYCFactory();
17141724

17151725
if (factory) {
17161726
const tokensToRegister = await factory.getDSTokens();
@@ -1736,7 +1746,10 @@ export abstract class AbstractCreditAccountService extends SDKConstruct {
17361746
creditAccount: Address,
17371747
calls: MultiCall[],
17381748
): Promise<RawTx> {
1739-
const factory = await this.getKYCFactory(suite.underlying);
1749+
const marketSuite = this.sdk.marketRegister.findByCreditManager(
1750+
suite.creditManager.address,
1751+
);
1752+
const factory = await marketSuite.getKYCFactory();
17401753

17411754
if (factory) {
17421755
// TODO: get tokens to register
@@ -1775,21 +1788,6 @@ export abstract class AbstractCreditAccountService extends SDKConstruct {
17751788
: suite.creditFacade.multicall(creditAccount, calls);
17761789
}
17771790

1778-
/**
1779-
* Resolves the KYC factory for a pool underlying token, if the underlying is KYC-gated.
1780-
* Loads token metadata, checks isKYCUnderlying; if true returns a SecuritizeKYCFactory instance for the token's kycFactory address.
1781-
* @param underlyingAddress - Pool underlying token address
1782-
* @returns SecuritizeKYCFactory when the underlying is KYC, undefined otherwise
1783-
*/
1784-
protected async getKYCFactory(underlyingAddress: Address) {
1785-
await this.sdk.tokensMeta.loadTokenData(underlyingAddress);
1786-
const underlying = this.sdk.tokensMeta.mustGet(underlyingAddress);
1787-
if (this.sdk.tokensMeta.isKYCUnderlying(underlying)) {
1788-
const factory = new SecuritizeKYCFactory(this.sdk, underlying.kycFactory);
1789-
return factory;
1790-
}
1791-
return undefined;
1792-
}
17931791
/**
17941792
* Returns all KYC credit account addresses for an investor across the given market suites.
17951793
* Resolves KYC factory per suite, then multicalls each factory's getCreditAccounts(investor).
@@ -1804,9 +1802,7 @@ export abstract class AbstractCreditAccountService extends SDKConstruct {
18041802
if (suites.length === 0 || investor === ADDRESS_0X0) return [];
18051803

18061804
const factories = await Promise.all(
1807-
suites.map(suite =>
1808-
suite ? this.getKYCFactory(suite.underlying) : undefined,
1809-
),
1805+
suites.map(suite => (suite ? suite.getKYCFactory() : undefined)),
18101806
);
18111807
const safeFactories = factories.reduce<Array<SecuritizeKYCFactory>>(
18121808
(acc, v) => {

0 commit comments

Comments
 (0)