Skip to content

Commit 9679033

Browse files
authored
Merge pull request #412 from Gearbox-protocol/bot
fix: concentrate bots related data inside bots plugin
2 parents 2c31958 + 3f5587d commit 9679033

File tree

11 files changed

+64
-131
lines changed

11 files changed

+64
-131
lines changed

src/plugins/bots/BotsPlugin.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
AP_PERIPHERY_COMPRESSOR,
1515
AP_TREASURY,
1616
BasePlugin,
17+
chains as CHAINS,
1718
hexEq,
1819
isV300,
1920
isV310,
@@ -22,6 +23,7 @@ import {
2223
} from "../../sdk/index.js";
2324
import { iPartialLiquidationBotV310Abi } from "./abi/iPartialLiquidationBotV310.js";
2425
import {
26+
LEGACY_MIGRATION_BOT,
2527
PARTIAL_LIQUIDATION_BOT_CONFIGS,
2628
PARTIAL_LIQUIDATION_BOT_DEPLOYER,
2729
PARTIAL_LIQUIDATION_BOT_SALT,
@@ -35,6 +37,7 @@ import {
3537
type BotsPluginState,
3638
type BotsPluginStateHuman,
3739
LIQUIDATION_BOT_TYPES,
40+
type MigrationBotState,
3841
} from "./types.js";
3942

4043
export class UnsupportedBotVersionError extends Error {
@@ -280,4 +283,10 @@ export class BotsPlugin
280283
throw new Error(`unsupported bot version: ${v}`);
281284
}
282285
}
286+
287+
public static getMigrationBotData(
288+
chainId: number,
289+
): MigrationBotState | undefined {
290+
return chainId === CHAINS.Mainnet.id ? LEGACY_MIGRATION_BOT : undefined;
291+
}
283292
}

src/plugins/bots/config.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Address } from "viem";
2-
import type { NetworkType } from "../../sdk/index.js";
3-
import type { BotParameters } from "./types.js";
2+
import { BotPermissions, type NetworkType } from "../../sdk/index.js";
3+
import type { BotBaseType, BotParameters, MigrationBotState } from "./types.js";
44

55
export const PARTIAL_LIQUIDATION_BOT_CONFIGS: Partial<
66
Record<NetworkType, Omit<BotParameters, "treasury">[]>
@@ -34,3 +34,29 @@ export const PARTIAL_LIQUIDATION_BOT_CONFIGS: Partial<
3434
export const PARTIAL_LIQUIDATION_BOT_SALT = "GEARBOX";
3535
export const PARTIAL_LIQUIDATION_BOT_DEPLOYER: Address =
3636
"0xc93155E0a835Cf4E17a19463Fa67ed43c164d06a";
37+
38+
// TODO: HARDCODED
39+
const ACCOUNT_MIGRATOR_BOT =
40+
"0x286Fe53994f5668D56538Aa10eaa3Ac36f878e9C".toLowerCase() as Address;
41+
const ACCOUNT_MIGRATOR_PREVIEWER =
42+
"0x6523B8c9daB92eea7944a79b4Dbb598c7934DCca".toLowerCase() as Address;
43+
export const LEGACY_MIGRATION_BOT: MigrationBotState = {
44+
address: ACCOUNT_MIGRATOR_BOT,
45+
previewer: ACCOUNT_MIGRATOR_PREVIEWER,
46+
version: 310,
47+
botType: "MIGRATION_BOT",
48+
};
49+
50+
export const PERMISSION_BY_TYPE: Record<BotBaseType, bigint> = {
51+
LIQUIDATION_PROTECTION: BigInt(
52+
BotPermissions.ADD_COLLATERAL |
53+
BotPermissions.WITHDRAW_COLLATERAL |
54+
BotPermissions.DECREASE_DEBT,
55+
),
56+
57+
MIGRATION: BigInt(
58+
BotPermissions.EXTERNAL_CALLS |
59+
BotPermissions.UPDATE_QUOTA |
60+
BotPermissions.DECREASE_DEBT,
61+
),
62+
};

src/plugins/bots/types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,16 @@ export interface BotsPluginState {
6767
*/
6868
bots: Record<Address, BotState[]>;
6969
}
70+
71+
export type BotBaseType = "LIQUIDATION_PROTECTION" | "MIGRATION";
72+
73+
export const MIGRATION_BOT_TYPES = ["MIGRATION_BOT"] as const;
74+
export type MigrationBotType = (typeof MIGRATION_BOT_TYPES)[number];
75+
76+
export type MigrationBotState = {
77+
address: Address;
78+
version: 310;
79+
80+
previewer: Address;
81+
botType: MigrationBotType;
82+
};

src/sdk/accountMigration/AbstractMigrateCreditAccountsService.ts

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import type { ILogger } from "../types/index.js";
2121
import { childLogger } from "../utils/index.js";
2222
import type {
2323
MigrateCreditAccountProps,
24-
MigrationBotState,
2524
PreviewCreditAccountMigrationProps,
2625
PreviewMigrationResult,
2726
} from "./types.js";
@@ -49,14 +48,6 @@ export abstract class AbstractMigrateCreditAccountsService extends SDKConstruct
4948
},
5049
};
5150

52-
// TODO: HARDCODED
53-
private static readonly accountMigratorBot =
54-
"0x286Fe53994f5668D56538Aa10eaa3Ac36f878e9C".toLowerCase() as Address;
55-
56-
// TODO: HARDCODED
57-
private static readonly accountMigratorPreviewer =
58-
"0x6523B8c9daB92eea7944a79b4Dbb598c7934DCca".toLowerCase() as Address;
59-
6051
constructor(sdk: GearboxSDK, version: number) {
6152
super(sdk);
6253

@@ -167,41 +158,6 @@ export abstract class AbstractMigrateCreditAccountsService extends SDKConstruct
167158
]?.[source] ?? source
168159
);
169160
}
170-
public static getMigrationBotAddress(chainId: number) {
171-
const botAddress =
172-
chainId === CHAINS.Mainnet.id
173-
? AbstractMigrateCreditAccountsService.accountMigratorBot
174-
: undefined;
175-
const previewerAddress =
176-
chainId === CHAINS.Mainnet.id
177-
? AbstractMigrateCreditAccountsService.accountMigratorPreviewer
178-
: undefined;
179-
180-
return previewerAddress && botAddress
181-
? { botAddress, previewerAddress }
182-
: undefined;
183-
}
184-
185-
public static getMigrationBotData(
186-
chainId: number,
187-
): MigrationBotState | undefined {
188-
const { botAddress } =
189-
AbstractMigrateCreditAccountsService.getMigrationBotAddress(chainId) ||
190-
{};
191-
192-
// TODO: HARDCODED
193-
return botAddress
194-
? {
195-
baseParams: {
196-
addr: botAddress,
197-
version: 310,
198-
},
199-
address: botAddress,
200-
version: 310,
201-
botType: "MIGRATION_BOT",
202-
}
203-
: undefined;
204-
}
205161

206162
public static getTokensToMigrate(creditAccount: CreditAccountData_Legacy) {
207163
const sourceTokensToMigrate = Object.values(creditAccount.tokens).filter(

src/sdk/accountMigration/types.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,6 @@ import type { accountMigratorPreviewerV310Abi } from "../../abi/migration.js";
99
import type { SDKConstruct } from "../base/SDKConstruct.js";
1010
import type { Asset, RouterCASlice } from "../router/index.js";
1111

12-
export type MigrationBotState = {
13-
baseParams: {
14-
addr: Address;
15-
version: number;
16-
};
17-
address: Address;
18-
version: number;
19-
botType: MigrationBotType;
20-
};
21-
22-
export const MIGRATION_BOT_TYPES = ["MIGRATION_BOT"] as const;
23-
24-
export type MigrationBotType = (typeof MIGRATION_BOT_TYPES)[number];
25-
2612
export interface MigrateCreditAccountProps {
2713
/**
2814
* accountMigratorBot Address

src/sdk/accounts/AbstractCreditAccountsService.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
iBotListV300Abi,
1111
iCreditFacadeV300MulticallAbi,
1212
} from "../../abi/v300.js";
13-
import { AbstractMigrateCreditAccountsService } from "../accountMigration/AbstractMigrateCreditAccountsService.js";
1413
import type { CreditAccountData } from "../base/index.js";
1514
import { SDKConstruct } from "../base/index.js";
1615
import {
@@ -287,6 +286,7 @@ export abstract class AbstractCreditAccountService extends SDKConstruct {
287286
*/
288287
public async getConnectedBots(
289288
accountsToCheck: Array<{ creditAccount: Address; creditManager: Address }>,
289+
legacyMigrationBot: Address | undefined,
290290
): Promise<{
291291
legacy: GetConnectedBotsResult;
292292
legacyMigration: GetConnectedMigrationBotsResult;
@@ -307,19 +307,16 @@ export abstract class AbstractCreditAccountService extends SDKConstruct {
307307
}),
308308
allowFailure: true,
309309
}),
310-
this.getActiveMigrationBots(accountsToCheck),
310+
this.getActiveMigrationBots(accountsToCheck, legacyMigrationBot),
311311
]);
312312

313313
return { legacy: resp, legacyMigration: migration };
314314
}
315315
private async getActiveMigrationBots(
316316
accountsToCheck: Array<{ creditAccount: Address; creditManager: Address }>,
317+
legacyMigrationBot: Address | undefined,
317318
) {
318-
const migrationBot =
319-
AbstractMigrateCreditAccountsService.getMigrationBotAddress(
320-
this.sdk.chainId,
321-
);
322-
if (migrationBot) {
319+
if (legacyMigrationBot) {
323320
const result = await this.client.multicall({
324321
contracts: accountsToCheck.map(ca => {
325322
const cm = this.sdk.marketRegister.findCreditManager(
@@ -333,14 +330,14 @@ export abstract class AbstractCreditAccountService extends SDKConstruct {
333330
address: cm.creditFacade.botList,
334331
functionName: "getBotStatus",
335332
args: isV300(cm.creditFacade.version)
336-
? [migrationBot.botAddress, ca.creditManager, ca.creditAccount]
337-
: [migrationBot.botAddress, ca.creditAccount],
333+
? [legacyMigrationBot, ca.creditManager, ca.creditAccount]
334+
: [legacyMigrationBot, ca.creditAccount],
338335
} as const;
339336
}),
340337
allowFailure: true,
341338
});
342339

343-
return { result, migrationBot };
340+
return { result, botAddress: legacyMigrationBot };
344341
}
345342

346343
return undefined;

src/sdk/accounts/CreditAccountsServiceV310.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { iCreditFacadeMulticallV310Abi } from "../../abi/310/generated.js";
33
import { MAX_UINT256 } from "../constants/math.js";
44
import type { MultiCall } from "../types/index.js";
55
import { AbstractCreditAccountService } from "./AbstractCreditAccountsService.js";
6-
import { PERMISSION_BY_TYPE } from "./constants.js";
76
import type {
87
ClaimFarmRewardsProps,
98
CreditAccountOperationResult,
@@ -24,8 +23,7 @@ export class CreditAccountServiceV310
2423
*/
2524
public async setBot({
2625
botAddress,
27-
botBaseType,
28-
stopBot,
26+
permissions,
2927

3028
targetContract,
3129
}: SetBotProps): Promise<
@@ -48,7 +46,7 @@ export class CreditAccountServiceV310
4846
callData: encodeFunctionData({
4947
abi: iCreditFacadeMulticallV310Abi,
5048
functionName: "setBotPermissions",
51-
args: [botAddress, stopBot ? 0n : PERMISSION_BY_TYPE[botBaseType]],
49+
args: [botAddress, permissions],
5250
}),
5351
};
5452

src/sdk/accounts/constants.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/sdk/accounts/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export * from "./AbstractCreditAccountsService.js";
22
export * from "./CreditAccountsServiceV300.js";
33
export * from "./CreditAccountsServiceV310.js";
4-
export * from "./constants.js";
54
export * from "./createCreditAccountService.js";
65
export * from "./types.js";

src/sdk/accounts/types.ts

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import type {
77
} from "viem";
88
import type { creditAccountCompressorAbi } from "../../abi/compressors/creditAccountCompressor.js";
99
import type { iWithdrawalCompressorV310Abi } from "../../abi/IWithdrawalCompressorV310.js";
10-
import type { LiquidationBotType as LiquidationBotTypeSDK } from "../../plugins/bots/types.js";
11-
import type { MigrationBotType } from "../accountMigration/types.js";
1210
import type { ConnectedBotData, CreditAccountData } from "../base/index.js";
1311
import type { SDKConstruct } from "../base/SDKConstruct.js";
1412
import type {
@@ -435,23 +433,6 @@ export interface Rewards {
435433
rewards: Array<Asset>;
436434
}
437435

438-
/**
439-
It was planned that bots related to each other like
440-
{
441-
liquidationProtection: LiquidationBotType[]
442-
someOtherBaseType: someOtherDetailedType[ ... ]
443-
}
444-
445-
BotBaseType = "liquidationProtection" | someOtherBaseType
446-
BotDetailedType = LiquidationBotType | someOtherDetailedType
447-
* */
448-
export type BotBaseType = "LIQUIDATION_PROTECTION" | "MIGRATION";
449-
export type LiquidationBotType = Exclude<
450-
LiquidationBotTypeSDK,
451-
"PARTIAL_LIQUIDATION_BOT"
452-
>;
453-
export type { MigrationBotType };
454-
455436
interface CMSlice {
456437
creditManager: Address;
457438
creditFacade: Address;
@@ -464,13 +445,9 @@ export interface SetBotProps {
464445
*/
465446
botAddress: Address;
466447
/**
467-
* Bot base type (see comments above)
468-
*/
469-
botBaseType: BotBaseType;
470-
/**
471-
* Either stop or enable bot
448+
* Permissions to set for the bot
472449
*/
473-
stopBot: boolean;
450+
permissions: bigint;
474451
/**
475452
* Minimal credit account data {@link RouterCASlice} on which operation is performed; if omitted, credit manager data is used
476453
* Minimal credit manager data {@link CMSlice} on which operation is performed; used only if credit account is omitted
@@ -508,10 +485,7 @@ export type GetConnectedMigrationBotsResult =
508485
status: "success";
509486
}
510487
)[];
511-
migrationBot: {
512-
botAddress: Address;
513-
previewerAddress: Address;
514-
};
488+
botAddress: Address;
515489
}
516490
| undefined;
517491

@@ -549,11 +523,13 @@ export interface ICreditAccountsService extends SDKConstruct {
549523
/**
550524
* Method to get all connected bots for credit account
551525
* @param {Array<{ creditAccount: Address; creditManager: Address }>} accountsToCheck - list of credit accounts
526+
* @param {Address | undefined} legacyMigrationBot - address of the bot to check connected bots on
552527
* and their credit managers to check connected bots on
553528
* @returns call result of getConnectedBots for each credit account
554529
*/
555530
getConnectedBots(
556531
accountsToCheck: Array<{ creditAccount: Address; creditManager: Address }>,
532+
legacyMigrationBot: Address | undefined,
557533
): Promise<{
558534
legacy: GetConnectedBotsResult;
559535
legacyMigration: GetConnectedMigrationBotsResult;

0 commit comments

Comments
 (0)