Skip to content

Commit

Permalink
fix: uin256 for health factor
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed Aug 22, 2024
1 parent cef2bfb commit 01a7bd6
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/config/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NetworkType } from "@gearbox-protocol/sdk-gov";
import { MAX_INT, type NetworkType } from "@gearbox-protocol/sdk-gov";
import { Address } from "abitype/zod";
import { type Hex, isHex } from "viem";
import { z } from "zod";
Expand Down Expand Up @@ -46,7 +46,7 @@ export const ConfigSchema = z.object({
* Filter out all accounts with HF >= threshold during scan stage
* 65535 is constant for zero-debt account
*/
hfThreshold: z.coerce.number().min(0).max(65536).int().default(65536),
hfThreshold: z.coerce.bigint().min(0n).max(MAX_INT).default(MAX_INT),
optimistic: booleanLike.pipe(z.boolean().optional()),
deployPartialLiquidatorContracts: booleanLike.pipe(z.boolean().optional()),
partialLiquidatorAddress: Address.optional(),
Expand Down
4 changes: 2 additions & 2 deletions src/data/CreditAccountData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class CreditAccountData {
readonly cmName: string;

readonly enabledTokenMask: bigint;
readonly healthFactor: number;
readonly healthFactor: bigint;
isDeleting: boolean;

readonly borrowedAmount: bigint;
Expand Down Expand Up @@ -104,7 +104,7 @@ export class CreditAccountData {
this.version = Number(payload.cfVersion);
this.cmName = payload.cmName;

this.healthFactor = Number(payload.healthFactor || 0n);
this.healthFactor = BigInt(payload.healthFactor || 0n);
this.enabledTokenMask = BigInt(payload.enabledTokensMask);
this.isDeleting = false;

Expand Down
4 changes: 2 additions & 2 deletions src/services/liquidate/AbstractLiquidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default abstract class AbstractLiquidator {
borrower: acc.borrower,
account: acc.addr,
balancesBefore: acc.filterDust(),
hfBefore: acc.healthFactor,
hfBefore: Number(acc.healthFactor),
balancesAfter: {},
hfAfter: 0,
gasUsed: 0,
Expand Down Expand Up @@ -125,7 +125,7 @@ export default abstract class AbstractLiquidator {
): Promise<OptimisticResult> {
const ca = await this.updateCreditAccountData(acc);
result.balancesAfter = ca.filterDust();
result.hfAfter = ca.healthFactor;
result.hfAfter = Number(ca.healthFactor);

const balanceAfter = await this.getExecutorBalance(ca.underlyingToken);
result.gasUsed = Number(receipt.gasUsed);
Expand Down
2 changes: 1 addition & 1 deletion src/services/liquidate/BatchLiquidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export default class BatchLiquidator
}),
balancesBefore: a.filterDust(),
balancesAfter: {},
hfBefore: a.healthFactor,
hfBefore: Number(a.healthFactor),
hfAfter: 0,
creditManager: a.creditManager,
borrower: a.borrower,
Expand Down
2 changes: 1 addition & 1 deletion src/services/notifier/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class LowBalanceMessage extends BaseMessage implements INotifierMessage {

export class StartedMessage extends BaseMessage implements INotifierMessage {
#name: string;
#hfThreshold: number;
#hfThreshold: bigint;
#restakingWA: boolean;

constructor() {
Expand Down
6 changes: 4 additions & 2 deletions src/services/scanner/Scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class Scanner {
atBlock,
);
}
accounts = accounts.sort((a, b) => a.healthFactor - b.healthFactor);
accounts = accounts.sort((a, b) => Number(a.healthFactor - b.healthFactor));
if (this.config.restakingWorkaround) {
const before = accounts.length;
accounts = this.#filterRestakingAccounts(accounts);
Expand Down Expand Up @@ -205,8 +205,10 @@ export class Scanner {

accounts = accounts.filter(ca => {
const ok = ca.healthFactor < this.config.hfThreshold;
// Currently in data compressor helathFactor is set to type(uint16).max for zero-debt accounts
// TODO: this will be changed to type(uint256).max in 3.1
// 65535 is zero-debt account, no need to warn about it
if (!ok && ca.healthFactor !== 65535) {
if (!ok && ca.healthFactor !== 65535n) {
this.log.warn(
`health factor of ${ca.name} ${ca.healthFactor} > ${this.config.hfThreshold} threshold, skipping`,
);
Expand Down

0 comments on commit 01a7bd6

Please sign in to comment.