Skip to content

Commit

Permalink
refactor: simplify scanner service
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed Jul 15, 2024
1 parent 8f576b5 commit e26df46
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 81 deletions.
6 changes: 3 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type Client from "./services/Client.js";
import type HealthCheckerService from "./services/HealthCheckerService.js";
import type { IOptimisticOutputWriter } from "./services/output/index.js";
import type { RedstoneServiceV3 } from "./services/RedstoneServiceV3.js";
import type { ScanServiceV3 } from "./services/scan/index.js";
import type { Scanner } from "./services/scanner/index.js";
import type { ISwapper } from "./services/swap/index.js";
import version from "./version.js";

Expand All @@ -22,7 +22,7 @@ class App {
addressProvider!: AddressProviderService;

@DI.Inject(DI.Scanner)
scanServiceV3!: ScanServiceV3;
scanner!: Scanner;

@DI.Inject(DI.HealthChecker)
healthChecker!: HealthCheckerService;
Expand Down Expand Up @@ -56,7 +56,7 @@ class App {

this.healthChecker.launch();
await this.swapper.launch(this.config.network);
await this.scanServiceV3.launch();
await this.scanner.launch();

if (this.config.optimistic) {
this.log.debug("optimistic liquidation finished, writing output");
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "./services/Client.js";
import "./services/HealthCheckerService.js";
import "./services/OracleServiceV3.js";
import "./services/RedstoneServiceV3.js";
import "./services/scan/index.js";
import "./services/scanner/index.js";
import "./services/liquidate/index.js";
import "./services/output/index.js";
import "./services/notifier/index.js";
Expand Down
8 changes: 4 additions & 4 deletions src/services/HealthCheckerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DI } from "../di.js";
import type { ILogger } from "../log/index.js";
import { Logger } from "../log/index.js";
import version from "../version.js";
import type { ScanServiceV3 } from "./scan/index.js";
import type { Scanner } from "./scanner/index.js";

const nanoid = customAlphabet("1234567890abcdef", 8);

Expand All @@ -17,7 +17,7 @@ export default class HealthCheckerService {
log!: ILogger;

@DI.Inject(DI.Scanner)
scanServiceV3!: ScanServiceV3;
scanner!: Scanner;

@DI.Inject(DI.Config)
config!: Config;
Expand All @@ -40,7 +40,7 @@ export default class HealthCheckerService {
res.end(
JSON.stringify({
start_time: this.#start,
block_number: this.scanServiceV3.lastUpdated,
block_number: this.scanner.lastUpdated,
version,
}),
);
Expand Down Expand Up @@ -97,7 +97,7 @@ start_time{${labels}} ${this.#start}
# HELP block_number Latest processed block
# TYPE block_number gauge
block_number{${labels}} ${this.scanServiceV3.lastUpdated}
block_number{${labels}} ${this.scanner.lastUpdated}
`;
}
Expand Down
65 changes: 0 additions & 65 deletions src/services/scan/AbstractScanService.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/services/scan/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import {
} from "@gearbox-protocol/types/abi";
import { getContract } from "viem";

import type { Config } from "../../config/index.js";
import type { CreditAccountDataRaw, PriceOnDemand } from "../../data/index.js";
import { CreditAccountData } from "../../data/index.js";
import { DI } from "../../di.js";
import { type ILogger, Logger } from "../../log/index.js";
import type { IDataCompressorContract } from "../../utils/index.js";
import type { AddressProviderService } from "../AddressProviderService.js";
import type Client from "../Client.js";
import type { ILiquidatorService } from "../liquidate/index.js";
import type OracleServiceV3 from "../OracleServiceV3.js";
import type { RedstoneServiceV3 } from "../RedstoneServiceV3.js";
import AbstractScanService from "./AbstractScanService.js";

const RESTAKING_CMS: Partial<Record<NetworkType, Address>> = {
Mainnet:
Expand All @@ -34,10 +36,19 @@ interface AccountSelection {
}

@DI.Injectable(DI.Scanner)
export class ScanServiceV3 extends AbstractScanService {
export class Scanner {
@Logger("Scanner")
log!: ILogger;

@DI.Inject(DI.Config)
config!: Config;

@DI.Inject(DI.AddressProvider)
addressProvider!: AddressProviderService;

@DI.Inject(DI.Client)
client!: Client;

@DI.Inject(DI.Oracle)
oracle!: OracleServiceV3;

Expand All @@ -51,8 +62,10 @@ export class ScanServiceV3 extends AbstractScanService {
#processing: bigint | null = null;
#restakingCMAddr?: Address;
#restakingMinHF?: bigint;
#lastUpdated = 0n;

protected override async _launch(): Promise<void> {
public async launch(): Promise<void> {
await this.liquidatorService.launch();
if (this.config.restakingWorkaround) {
await this.#setupRestakingWorkaround();
}
Expand All @@ -70,10 +83,15 @@ export class ScanServiceV3 extends AbstractScanService {
await this.oracle.launch(block);
// we should not pin block during optimistic liquidations
// because during optimistic liquidations we need to call evm_mine to make redstone work
await this.updateAccounts(this.config.optimistic ? undefined : block);
await this.#updateAccounts(this.config.optimistic ? undefined : block);
if (!this.config.optimistic) {
this.client.pub.watchBlockNumber({
onBlockNumber: n => this.#onBlock(n),
});
}
}

protected override async onBlock(blockNumber: bigint): Promise<void> {
async #onBlock(blockNumber: bigint): Promise<void> {
if (this.#processing) {
this.log.debug(
`skipping block ${blockNumber}, still processing block ${this.#processing}`,
Expand All @@ -82,15 +100,16 @@ export class ScanServiceV3 extends AbstractScanService {
}
this.#processing = blockNumber;
await this.oracle.update(blockNumber);
await this.updateAccounts(blockNumber);
await this.#updateAccounts(blockNumber);
this.#processing = null;
this.#lastUpdated = blockNumber;
}

/**
* Loads new data and recompute all health factors
* @param atBlock Fiex block for archive node which is needed to get data
*/
protected async updateAccounts(atBlock?: bigint): Promise<void> {
async #updateAccounts(atBlock?: bigint): Promise<void> {
const start = new Date().getTime();
const blockS = atBlock ? ` in ${atBlock}` : "";
let [accounts, failedTokens] = await this.#potentialLiquidations(
Expand Down Expand Up @@ -385,6 +404,10 @@ export class ScanServiceV3 extends AbstractScanService {
}
return this.#dataCompressor;
}

public get lastUpdated(): bigint {
return this.#lastUpdated;
}
}

function printTokens(tokens: Address[]): string {
Expand Down
1 change: 1 addition & 0 deletions src/services/scanner/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./Scanner.js";

0 comments on commit e26df46

Please sign in to comment.