Skip to content

Commit b35bcb8

Browse files
committed
fix: fetch all bots in 1 call
1 parent 3ddaa4e commit b35bcb8

File tree

1 file changed

+123
-51
lines changed

1 file changed

+123
-51
lines changed

src/sdk/accounts/AbstractCreditAccountsService.ts

Lines changed: 123 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,37 @@ import type {
7070
UpdateQuotasProps,
7171
} from "./types.js";
7272

73+
type MulticallWithFailure<T> = (
74+
| {
75+
error?: undefined;
76+
result: T;
77+
status: "success";
78+
}
79+
| {
80+
error: Error;
81+
result?: undefined;
82+
status: "failure";
83+
}
84+
)[];
85+
86+
type BotResponseCompressor = MulticallWithFailure<
87+
readonly {
88+
baseParams: {
89+
addr: `0x${string}`;
90+
version: bigint;
91+
contractType: `0x${string}`;
92+
serializedParams: `0x${string}`;
93+
};
94+
requiredPermissions: bigint;
95+
creditAccount: `0x${string}`;
96+
permissions: bigint;
97+
forbidden: boolean;
98+
}[]
99+
>;
100+
type BotsDirectResponse = MulticallWithFailure<
101+
readonly [bigint, boolean, boolean] | readonly [bigint, boolean]
102+
>;
103+
73104
type CompressorAbi = typeof creditAccountCompressorAbi;
74105

75106
export interface CreditAccountServiceOptions {
@@ -298,9 +329,9 @@ export abstract class AbstractCreditAccountService extends SDKConstruct {
298329
Omit<NonNullable<GetConnectedMigrationBotsResult>, "botAddress">
299330
>;
300331
}> {
301-
const [resp, migration, additional] = await Promise.all([
302-
this.client.multicall({
303-
contracts: accountsToCheck.map(o => {
332+
const allResp = await this.client.multicall({
333+
contracts: [
334+
...accountsToCheck.map(o => {
304335
const pool = this.sdk.marketRegister.findByCreditManager(
305336
o.creditManager,
306337
);
@@ -312,41 +343,93 @@ export abstract class AbstractCreditAccountService extends SDKConstruct {
312343
args: [pool.configurator.address, o.creditAccount],
313344
} as const;
314345
}),
315-
allowFailure: true,
316-
}),
317-
this.getActiveMigrationBots(accountsToCheck, legacyMigrationBot),
318-
this.getActiveBots(accountsToCheck, additionalBots),
319-
]);
346+
...(legacyMigrationBot
347+
? accountsToCheck.map(ca => {
348+
const cm = this.sdk.marketRegister.findCreditManager(
349+
ca.creditManager,
350+
);
351+
352+
return {
353+
abi: isV300(cm.creditFacade.version)
354+
? iBotListV300Abi
355+
: iBotListV310Abi,
356+
address: cm.creditFacade.botList,
357+
functionName: "getBotStatus",
358+
args: isV300(cm.creditFacade.version)
359+
? [legacyMigrationBot, ca.creditManager, ca.creditAccount]
360+
: [legacyMigrationBot, ca.creditAccount],
361+
} as const;
362+
})
363+
: []),
364+
...accountsToCheck.flatMap(ca => {
365+
const cm = this.sdk.marketRegister.findCreditManager(
366+
ca.creditManager,
367+
);
368+
369+
return additionalBots.map(bot => {
370+
return {
371+
abi: isV300(cm.creditFacade.version)
372+
? iBotListV300Abi
373+
: iBotListV310Abi,
374+
address: cm.creditFacade.botList,
375+
functionName: "getBotStatus",
376+
args: isV300(cm.creditFacade.version)
377+
? [bot, ca.creditManager, ca.creditAccount]
378+
: [bot, ca.creditAccount],
379+
} as const;
380+
});
381+
}),
382+
],
383+
allowFailure: true,
384+
});
385+
386+
const legacyStart = 0;
387+
const legacyEnd = accountsToCheck.length;
388+
const legacy: BotResponseCompressor = allResp.slice(
389+
legacyStart,
390+
legacyEnd,
391+
) as BotResponseCompressor;
392+
393+
const migrationStart = legacyEnd;
394+
const migrationEnd = legacyMigrationBot
395+
? migrationStart + accountsToCheck.length
396+
: migrationStart;
397+
const migrationResp: BotsDirectResponse = allResp.slice(
398+
migrationStart,
399+
migrationEnd,
400+
) as BotsDirectResponse;
401+
402+
const additionalStart = migrationEnd;
403+
const additionalResp: BotsDirectResponse = allResp.slice(
404+
additionalStart,
405+
) as BotsDirectResponse;
320406

321407
return {
322-
legacy: resp,
323-
additionalBots: additional,
324-
legacyMigration: migration,
408+
legacy,
409+
additionalBots: this.getActiveBots(
410+
accountsToCheck,
411+
additionalBots,
412+
additionalResp,
413+
),
414+
legacyMigration: this.getActiveMigrationBots(
415+
accountsToCheck,
416+
legacyMigrationBot,
417+
migrationResp,
418+
),
325419
};
326420
}
327-
private async getActiveBots(
421+
private getActiveBots(
328422
accountsToCheck: Array<AccountToCheck>,
329423
bots: Array<Address>,
424+
result: BotsDirectResponse,
330425
) {
331-
const result = await this.client.multicall({
332-
contracts: accountsToCheck.flatMap(ca => {
333-
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
334-
335-
return bots.map(bot => {
336-
return {
337-
abi: isV300(cm.creditFacade.version)
338-
? iBotListV300Abi
339-
: iBotListV310Abi,
340-
address: cm.creditFacade.botList,
341-
functionName: "getBotStatus",
342-
args: isV300(cm.creditFacade.version)
343-
? [bot, ca.creditManager, ca.creditAccount]
344-
: [bot, ca.creditAccount],
345-
} as const;
346-
});
347-
}),
348-
allowFailure: true,
349-
});
426+
if (result.length !== bots.length * accountsToCheck.length) {
427+
console.error(
428+
"result length mismatch",
429+
result.length,
430+
bots.length * accountsToCheck.length,
431+
);
432+
}
350433

351434
const botsByCAIndex = accountsToCheck.reduce<
352435
Array<Omit<NonNullable<GetConnectedMigrationBotsResult>, "botAddress">>
@@ -362,30 +445,19 @@ export abstract class AbstractCreditAccountService extends SDKConstruct {
362445

363446
return botsByCAIndex;
364447
}
365-
private async getActiveMigrationBots(
448+
private getActiveMigrationBots(
366449
accountsToCheck: Array<{ creditAccount: Address; creditManager: Address }>,
367450
bot: Address | undefined,
451+
result: BotsDirectResponse,
368452
) {
369453
if (bot) {
370-
const result = await this.client.multicall({
371-
contracts: accountsToCheck.map(ca => {
372-
const cm = this.sdk.marketRegister.findCreditManager(
373-
ca.creditManager,
374-
);
375-
376-
return {
377-
abi: isV300(cm.creditFacade.version)
378-
? iBotListV300Abi
379-
: iBotListV310Abi,
380-
address: cm.creditFacade.botList,
381-
functionName: "getBotStatus",
382-
args: isV300(cm.creditFacade.version)
383-
? [bot, ca.creditManager, ca.creditAccount]
384-
: [bot, ca.creditAccount],
385-
} as const;
386-
}),
387-
allowFailure: true,
388-
});
454+
if (result.length !== accountsToCheck.length) {
455+
console.error(
456+
"result length mismatch for migration bots",
457+
result.length,
458+
accountsToCheck.length,
459+
);
460+
}
389461

390462
return { result, botAddress: bot };
391463
}

0 commit comments

Comments
 (0)