Skip to content

Commit

Permalink
fix: add traces to failed batch liquidations
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed Aug 9, 2024
1 parent 87cf00b commit 1efc156
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 37 deletions.
92 changes: 57 additions & 35 deletions src/errors/ErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ export class ErrorHandler {
};
}

public async saveTransactionTrace(hash: string): Promise<string | undefined> {
return this.#runCast([
"run",
"--rpc-url",
this.config.ethProviderRpcs[0],
hash,
]);
}

/**
* Safely tries to save trace of failed transaction to configured output
* @param error
Expand All @@ -75,52 +84,65 @@ export class ErrorHandler {
e: BaseError,
context?: CreditAccountData,
): Promise<string | undefined> {
const logger = this.#caLogger(context);
if (!this.config.castBin || !this.config.outDir) {
return undefined;
}
try {
let cast: string[] = [];
if (e instanceof TransactionRevertedError) {
let cast: string[] = [];
if (e instanceof TransactionRevertedError) {
cast = [
"run",
"--rpc-url",
this.config.ethProviderRpcs[0],
e.receipt.transactionHash,
];
} else {
const exErr = e.walk(
err => err instanceof ContractFunctionExecutionError,
);
if (
exErr instanceof ContractFunctionExecutionError &&
exErr.contractAddress
) {
const data = encodeFunctionData({
abi: exErr.abi,
args: exErr.args,
functionName: exErr.functionName,
});
cast = [
"run",
"call",
"--trace",
"--rpc-url",
this.config.ethProviderRpcs[0],
e.receipt.transactionHash,
exErr.contractAddress,
data,
];
} else {
const exErr = e.walk(
err => err instanceof ContractFunctionExecutionError,
);
if (
exErr instanceof ContractFunctionExecutionError &&
exErr.contractAddress
) {
const data = encodeFunctionData({
abi: exErr.abi,
args: exErr.args,
functionName: exErr.functionName,
});
cast = [
"call",
"--trace",
"--rpc-url",
this.config.ethProviderRpcs[0],
exErr.contractAddress,
data,
];
}
}
if (!cast.length) {
return undefined;
}
}
if (!cast.length) {
return undefined;
}
return this.#runCast(cast, context);
}

/**
* Runs cast cli command and saves output to a unique file
* @param args
* @param context
* @returns
*/
async #runCast(
args: string[],
context?: CreditAccountData,
): Promise<string | undefined> {
if (!this.config.castBin || !this.config.outDir) {
return undefined;
}

const logger = this.#caLogger(context);
try {
const traceId = `${nanoid()}.trace`;
const traceFile = path.resolve(this.config.outDir, traceId);
const out = createWriteStream(traceFile, "utf-8");
await events.once(out, "open");
// use node-pty instead of node:child_process to have colored output
const pty = spawn(this.config.castBin, cast, { cols: 1024 });
const pty = spawn(this.config.castBin, args, { cols: 1024 });
pty.onData(data => out.write(data));
await new Promise(resolve => {
pty.onExit(() => resolve(undefined));
Expand Down
11 changes: 9 additions & 2 deletions src/services/liquidate/BatchLiquidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,21 @@ export default class BatchLiquidator
this.logger.debug(
`processing batch of ${batch.length} for ${batch[0]?.cmName}: ${batch.map(ca => ca.addr)}`,
);
const { results } = await this.#liquidateBatch(
const { results, receipt } = await this.#liquidateBatch(
batch,
cms,
i,
batches.length,
);
const hasErrors = results.some(r => !!r.isError);
let traceId: string | undefined;
if (hasErrors && !!receipt) {
traceId = await this.errorHandler.saveTransactionTrace(
receipt.transactionHash,
);
}
for (const r of results) {
this.optimistic.push(r);
this.optimistic.push({ ...r, traceFile: traceId });
}
}
const success = this.optimistic.get().filter(r => !r.isError).length;
Expand Down

0 comments on commit 1efc156

Please sign in to comment.