Skip to content

Commit

Permalink
fix: slice batches so each batch contains accounts from the same cm only
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed Aug 2, 2024
1 parent 0764186 commit 5610ee0
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 213 deletions.
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@
"pino-pretty": "^11.2.2"
},
"devDependencies": {
"@aws-sdk/client-s3": "^3.620.1",
"@aws-sdk/client-s3": "^3.622.0",
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
"@flashbots/ethers-provider-bundle": "^1.0.0",
"@gearbox-protocol/eslint-config": "2.0.0-next.2",
"@gearbox-protocol/liquidator-v2-contracts": "^2.1.0-next.17",
"@gearbox-protocol/liquidator-v2-contracts": "^2.1.0-next.18",
"@gearbox-protocol/prettier-config": "2.0.0-next.0",
"@gearbox-protocol/sdk-gov": "^2.13.0",
"@gearbox-protocol/sdk-gov": "^2.13.5",
"@gearbox-protocol/types": "^1.9.2",
"@redstone-finance/evm-connector": "^0.6.1",
"@types/node": "^22.0.0",
"@types/node": "^22.1.0",
"@uniswap/sdk-core": "^5.3.1",
"@uniswap/v3-sdk": "^3.13.1",
"@vlad-yakovlev/telegram-md": "^2.0.0",
"abitype": "^1.0.5",
"axios": "^1.7.2",
"axios-retry": "^4.4.2",
"axios": "^1.7.3",
"axios-retry": "^4.5.0",
"date-fns": "^3.6.0",
"di-at-home": "^0.0.7",
"dotenv": "^16.4.5",
Expand All @@ -52,10 +52,10 @@
"pino": "^9.3.2",
"prettier": "^3.3.3",
"redstone-protocol": "^1.0.5",
"tsx": "^4.16.2",
"tsx": "^4.16.5",
"typescript": "^5.5.4",
"viem": "^2.18.5",
"vitest": "^2.0.4"
"viem": "^2.18.7",
"vitest": "^2.0.5"
},
"prettier": "@gearbox-protocol/prettier-config",
"lint-staged": {
Expand Down
38 changes: 33 additions & 5 deletions src/services/liquidate/BatchLiquidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ export default class BatchLiquidator
if (!accounts.length) {
return;
}
this.logger.warn(`Need to liquidate ${accounts.length} accounts`);
this.logger.warn(`need to liquidate ${accounts.length} accounts`);
const cms = await this.getCreditManagersV3List();
const batches = this.#sliceBatches(accounts);

for (let i = 0; i < accounts.length; i += this.config.batchSize) {
const batch = accounts.slice(i, i + this.config.batchSize);
for (const batch of batches) {
this.logger.debug(
`processing batch of ${batch.length} for ${batch[0]?.cmName}: ${batch.map(ca => ca.addr)}`,
);
try {
const { receipt, results } = await this.#liquidateBatch(batch, cms);
this.notifier.notify(
Expand All @@ -67,8 +70,11 @@ export default class BatchLiquidator
const cms = await this.getCreditManagersV3List();
const total = accounts.length;
this.logger.info(`optimistic batch-liquidation for ${total} accounts`);
for (let i = 0; i < accounts.length; i += this.config.batchSize) {
const batch = accounts.slice(i, i + this.config.batchSize);
const batches = this.#sliceBatches(accounts);
for (const batch of batches) {
this.logger.debug(
`processing batch of ${batch.length} for ${batch[0]?.cmName}: ${batch.map(ca => ca.addr)}`,
);
const { results } = await this.#liquidateBatch(batch, cms);
for (const r of results) {
this.optimistic.push(r);
Expand Down Expand Up @@ -235,4 +241,26 @@ export default class BatchLiquidator
}
return this.#batchLiquidator;
}

#sliceBatches(accounts: CreditAccountData[]): CreditAccountData[][] {
const batches: CreditAccountData[][] = [];
const byCM: Record<string, CreditAccountData[]> = {};

for (const account of accounts) {
if (!byCM[account.creditManager]) {
byCM[account.creditManager] = [];
}
byCM[account.creditManager].push(account);
}

// eslint-disable-next-line guard-for-in
for (const cm in byCM) {
const cmAccs = byCM[cm];
for (let i = 0; i < cmAccs.length; i += this.config.batchSize) {
batches.push(cmAccs.slice(i, i + this.config.batchSize));
}
}

return batches;
}
}
Loading

0 comments on commit 5610ee0

Please sign in to comment.