Skip to content

Commit

Permalink
Merge branch 'master' into 2024-12-02-l1-gas
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt authored Dec 4, 2024
2 parents 14de1f8 + 6d367c2 commit a6ef419
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 131 deletions.
72 changes: 60 additions & 12 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { ViemClient } from "./types";
import { BigNumber } from "ethers";
import { RawTx, ViemClient } from "./types";
// @ts-ignore
import { abi as obAbi } from "../test/abis/OrderBook.json";
// @ts-ignore
Expand All @@ -15,6 +16,7 @@ import {
RpcRequestError,
FeeCapTooLowError,
decodeErrorResult,
TransactionReceipt,
ExecutionRevertedError,
InsufficientFundsError,
TransactionNotFoundError,
Expand Down Expand Up @@ -61,22 +63,39 @@ export type TxRevertError = {
/**
* Get error with snapshot
*/
export function errorSnapshot(header: string, err: any): string {
export function errorSnapshot(
header: string,
err: any,
data?: {
receipt: TransactionReceipt;
rawtx: RawTx;
signerBalance: BigNumber;
},
): string {
const message = [header];
if (err instanceof BaseError) {
if (err.shortMessage) message.push("Reason: " + err.shortMessage);
if (err.name) message.push("Error: " + err.name);
if (err.details) {
message.push("Details: " + err.details);
if (message.some((v) => v.includes("unknown reason"))) {
if (
message.some(
(v) => v.includes("unknown reason") || v.includes("execution reverted"),
)
) {
const { raw, decoded } = parseRevertError(err);
if (decoded) {
message.push("Error Name: " + decoded.name);
if (decoded.args.length) {
message.push("Error Args: " + JSON.stringify(decoded.args));
}
} else {
if (raw.data) message.push("Error Raw Data: " + raw.data);
} else if (raw.data) {
message.push("Error Raw Data: " + raw.data);
} else if (data) {
const gasErr = checkGasIssue(data.receipt, data.rawtx, data.signerBalance);
if (gasErr) {
message.push("Gas Error: " + gasErr);
}
}
}
}
Expand Down Expand Up @@ -143,8 +162,16 @@ export function isTimeout(err: BaseError): boolean {
export async function handleRevert(
viemClient: ViemClient,
hash: `0x${string}`,
): Promise<{ err: any; nodeError: boolean } | undefined> {
receipt: TransactionReceipt,
rawtx: RawTx,
signerBalance: BigNumber,
): Promise<{ err: any; nodeError: boolean; snapshot: string } | undefined> {
const header = "transaction reverted onchain";
try {
const gasErr = checkGasIssue(receipt, rawtx, signerBalance);
if (gasErr) {
return { err: header + gasErr, nodeError: false, snapshot: header + gasErr };
}
const tx = await viemClient.getTransaction({ hash });
await viemClient.call({
account: tx.from,
Expand All @@ -154,13 +181,19 @@ export async function handleRevert(
gasPrice: tx.gasPrice,
blockNumber: tx.blockNumber,
});
return {
err: "transaction reverted onchain and simulation failed to find out what was the revert reason, please try to simulate the tx manualy for more details",
nodeError: false,
snapshot:
"transaction reverted onchain and simulation failed to find out what was the revert reason, please try to simulate the tx manualy for more details",
};
return undefined;
} catch (err) {
if (err instanceof BaseError) {
const { raw, decoded } = parseRevertError(err);
if (decoded || raw.data) return { err, nodeError: true };
}
return { err, nodeError: false };
} catch (err: any) {
return {
err,
nodeError: containsNodeError(err),
snapshot: errorSnapshot(header, err, { receipt, rawtx, signerBalance }),
};
}
}

Expand Down Expand Up @@ -237,3 +270,18 @@ export function tryDecodeError(data: `0x${string}`): DecodedError | undefined {
}
}
}

/**
* Check if a mined transaction contains gas issue or not
*/
export function checkGasIssue(receipt: TransactionReceipt, rawtx: RawTx, signerBalance: BigNumber) {
const txGasCost = receipt.effectiveGasPrice * receipt.gasUsed;
if (signerBalance.lt(txGasCost)) {
return "account ran out of gas for transaction gas cost";
}
if (typeof rawtx.gas === "bigint") {
const percentage = (receipt.gasUsed * 100n) / rawtx.gas;
if (percentage >= 98n) return "transaction ran out of specified gas";
}
return undefined;
}
4 changes: 2 additions & 2 deletions src/modes/intraOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export async function findOpp({
]) as `0x${string}`,
})
).data,
);
).mul("1" + "0".repeat(18 - orderPairObject.buyTokenDecimals));
const outputBalance = ethers.BigNumber.from(
(
await viemClient.call({
Expand All @@ -318,7 +318,7 @@ export async function findOpp({
]) as `0x${string}`,
})
).data,
);
).mul("1" + "0".repeat(18 - orderPairObject.sellTokenDecimals));
for (let i = 0; i < opposingOrders.length; i++) {
try {
return await dryrun({
Expand Down
Loading

0 comments on commit a6ef419

Please sign in to comment.