Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Nov 6, 2024
1 parent 614d8c5 commit 6058195
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 44 deletions.
38 changes: 11 additions & 27 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ import {
InvalidInputRpcError,
ExecutionRevertedError,
TransactionRejectedRpcError,
// TipAboveFeeCapError,
// TransactionTypeNotSupportedError,
// IntrinsicGasTooLowError,
// IntrinsicGasTooHighError,
// InsufficientFundsError,
// NonceMaxValueError,
// NonceTooLowError,
// NonceTooHighError,
// FeeCapTooLowError,
// FeeCapTooHighError,
} from "viem";

/**
Expand Down Expand Up @@ -54,21 +44,15 @@ export function errorSnapshot(header: string, err: any): string {
* "viem/_types/utils/errors/getNodeError" since not a default export
*/
export function containsNodeError(err: BaseError): boolean {
return (
err instanceof TransactionRejectedRpcError ||
err instanceof InvalidInputRpcError ||
err instanceof ExecutionRevertedError ||
// err instanceof FeeCapTooHighError ||
// err instanceof FeeCapTooLowError ||
// err instanceof NonceTooHighError ||
// err instanceof NonceTooLowError ||
// err instanceof NonceMaxValueError ||
// err instanceof InsufficientFundsError ||
// err instanceof IntrinsicGasTooHighError ||
// err instanceof IntrinsicGasTooLowError ||
// err instanceof TransactionTypeNotSupportedError ||
// err instanceof TipAboveFeeCapError ||
(err instanceof RpcRequestError && err.code === ExecutionRevertedError.code) ||
("cause" in err && containsNodeError(err.cause as any))
);
try {
return (
err instanceof TransactionRejectedRpcError ||
err instanceof InvalidInputRpcError ||
err instanceof ExecutionRevertedError ||
(err instanceof RpcRequestError && err.code === ExecutionRevertedError.code) ||
("cause" in err && containsNodeError(err.cause as any))
);
} catch (error) {
return false;
}
}
19 changes: 19 additions & 0 deletions src/modes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export async function findOpp({
spanAttributes,
rawtx: undefined,
oppBlockNumber: undefined,
noneNodeError: undefined,
};
if ((allResults[0] as any)?.reason?.spanAttributes) {
spanAttributes["route-processor"] = JSON.stringify(
Expand All @@ -114,6 +115,24 @@ export async function findOpp({
(allResults[2] as any).reason.spanAttributes,
);
}
if (
"value" in (allResults[0] as any).reason &&
"noneNodeError" in (allResults[0] as any).reason.value
) {
result.noneNodeError = (allResults[0] as any).reason.value.noneNodeError;
} else if (
result.noneNodeError === undefined &&
"value" in (allResults[1] as any).reason &&
"noneNodeError" in (allResults[1] as any).reason.value
) {
result.noneNodeError = (allResults[1] as any).reason.value.noneNodeError;
} else if (
result.noneNodeError === undefined &&
"value" in (allResults[2] as any).reason &&
"noneNodeError" in (allResults[2] as any).reason.value
) {
result.noneNodeError = (allResults[2] as any).reason.value.noneNodeError;
}
throw result;
}
}
24 changes: 20 additions & 4 deletions src/modes/interOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,23 @@ export async function dryrun({
spanAttributes["blockNumber"] = blockNumber;
gasLimit = ethers.BigNumber.from(await signer.estimateGas(rawtx));
} catch (e) {
spanAttributes["isNodeError"] = containsNodeError(e as BaseError);
spanAttributes["error"] = errorSnapshot("", e);
const isNodeError = containsNodeError(e as BaseError);
const errMsg = errorSnapshot("", e);
spanAttributes["isNodeError"] = isNodeError;
spanAttributes["error"] = errMsg;
spanAttributes["rawtx"] = JSON.stringify(
{
...rawtx,
from: signer.account.address,
},
withBigintSerializer,
);
if (!isNodeError) {
result.value = {
noneNodeError: errMsg,
estimatedProfit: ethers.constants.Zero,
};
}
return Promise.reject(result);
}
let gasCost = gasLimit.mul(gasPrice);
Expand Down Expand Up @@ -150,15 +158,23 @@ export async function dryrun({
task,
]);
} catch (e) {
spanAttributes["isNodeError"] = containsNodeError(e as BaseError);
spanAttributes["error"] = errorSnapshot("", e);
const isNodeError = containsNodeError(e as BaseError);
const errMsg = errorSnapshot("", e);
spanAttributes["isNodeError"] = isNodeError;
spanAttributes["error"] = errMsg;
spanAttributes["rawtx"] = JSON.stringify(
{
...rawtx,
from: signer.account.address,
},
withBigintSerializer,
);
if (!isNodeError) {
result.value = {
noneNodeError: errMsg,
estimatedProfit: ethers.constants.Zero,
};
}
return Promise.reject(result);
}
}
Expand Down
24 changes: 20 additions & 4 deletions src/modes/intraOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,23 @@ export async function dryrun({
gasLimit = ethers.BigNumber.from(await signer.estimateGas(rawtx));
} catch (e) {
// reason, code, method, transaction, error, stack, message
spanAttributes["isNodeError"] = containsNodeError(e as BaseError);
spanAttributes["error"] = errorSnapshot("", e);
const isNodeError = containsNodeError(e as BaseError);
const errMsg = errorSnapshot("", e);
spanAttributes["isNodeError"] = isNodeError;
spanAttributes["error"] = errMsg;
spanAttributes["rawtx"] = JSON.stringify(
{
...rawtx,
from: signer.account.address,
},
withBigintSerializer,
);
if (!isNodeError) {
result.value = {
noneNodeError: errMsg,
estimatedProfit: ethers.constants.Zero,
};
}
return Promise.reject(result);
}
let gasCost = gasLimit.mul(gasPrice);
Expand Down Expand Up @@ -163,15 +171,23 @@ export async function dryrun({
[clear2Calldata, withdrawInputCalldata, withdrawOutputCalldata],
]);
} catch (e) {
spanAttributes["isNodeError"] = containsNodeError(e as BaseError);
spanAttributes["error"] = errorSnapshot("", e);
const isNodeError = containsNodeError(e as BaseError);
const errMsg = errorSnapshot("", e);
spanAttributes["isNodeError"] = isNodeError;
spanAttributes["error"] = errMsg;
spanAttributes["rawtx"] = JSON.stringify(
{
...rawtx,
from: signer.account.address,
},
withBigintSerializer,
);
if (!isNodeError) {
result.value = {
noneNodeError: errMsg,
estimatedProfit: ethers.constants.Zero,
};
}
return Promise.reject(result);
}
}
Expand Down
25 changes: 21 additions & 4 deletions src/modes/routeProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@ export async function dryrun({
gasLimit = ethers.BigNumber.from(await signer.estimateGas(rawtx));
} catch (e) {
// reason, code, method, transaction, error, stack, message
spanAttributes["isNodeError"] = containsNodeError(e as BaseError);
spanAttributes["error"] = errorSnapshot("", e);
const isNodeError = containsNodeError(e as BaseError);
const errMsg = errorSnapshot("", e);
spanAttributes["isNodeError"] = isNodeError;
spanAttributes["error"] = errMsg;
spanAttributes["rawtx"] = JSON.stringify(
{
...rawtx,
Expand All @@ -180,6 +182,12 @@ export async function dryrun({
withBigintSerializer,
);
result.reason = RouteProcessorDryrunHaltReason.NoOpportunity;
if (!isNodeError) {
result.value = {
noneNodeError: errMsg,
estimatedProfit: ethers.constants.Zero,
};
}
return Promise.reject(result);
}
let gasCost = gasLimit.mul(gasPrice);
Expand Down Expand Up @@ -217,8 +225,10 @@ export async function dryrun({
task,
]);
} catch (e) {
spanAttributes["isNodeError"] = containsNodeError(e as BaseError);
spanAttributes["error"] = errorSnapshot("", e);
const isNodeError = containsNodeError(e as BaseError);
const errMsg = errorSnapshot("", e);
spanAttributes["isNodeError"] = isNodeError;
spanAttributes["error"] = errMsg;
spanAttributes["rawtx"] = JSON.stringify(
{
...rawtx,
Expand All @@ -227,6 +237,12 @@ export async function dryrun({
withBigintSerializer,
);
result.reason = RouteProcessorDryrunHaltReason.NoOpportunity;
if (!isNodeError) {
result.value = {
noneNodeError: errMsg,
estimatedProfit: ethers.constants.Zero,
};
}
return Promise.reject(result);
}
}
Expand Down Expand Up @@ -338,6 +354,7 @@ export async function findOpp({
if (i !== 1) {
delete e.spanAttributes["error"];
delete e.spanAttributes["rawtx"];
delete e.spanAttributes["isNodeError"];
}
allHopsAttributes.push(JSON.stringify(e.spanAttributes));

Expand Down
7 changes: 6 additions & 1 deletion src/processOrders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,11 @@ export const processOrders = async (
if (result.report.status === ProcessPairReportStatus.ZeroOutput) {
span.setStatus({ code: SpanStatusCode.OK, message: "zero max output" });
} else if (result.report.status === ProcessPairReportStatus.NoOpportunity) {
span.setStatus({ code: SpanStatusCode.OK, message: "no opportunity" });
if (result.error && typeof result.error === "string") {
span.setStatus({ code: SpanStatusCode.ERROR, message: result.error });
} else {
span.setStatus({ code: SpanStatusCode.OK, message: "no opportunity" });
}
} else if (result.report.status === ProcessPairReportStatus.FoundOpportunity) {
span.setStatus({ code: SpanStatusCode.OK, message: "found opportunity" });
} else {
Expand Down Expand Up @@ -573,6 +577,7 @@ export async function processPair(args: {
for (const attrKey in e.spanAttributes) {
spanAttributes["details." + attrKey] = e.spanAttributes[attrKey];
}
result.error = e?.noneNodeError;
result.report = {
status: ProcessPairReportStatus.NoOpportunity,
tokenPair: pair,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export type DryrunValue = {
routeVisual?: string[];
oppBlockNumber?: number;
estimatedProfit: BigNumber;
noneNodeError?: string;
};

export type DryrunResult = {
Expand Down
8 changes: 4 additions & 4 deletions test/mode-routeProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,8 @@ describe("Test route processor find opp", async function () {
spanAttributes: {
hops: [
`{"amountIn":"${formatUnits(vaultBalance)}","amountOut":"${formatUnits(getAmountOut(vaultBalance), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber},"isNodeError":false,"error":${JSON.stringify(errorSnapshot("", ethers.errors.UNPREDICTABLE_GAS_LIMIT))},"rawtx":${JSON.stringify(rawtx)}}`,
`{"amountIn":"${formatUnits(vaultBalance.div(2))}","amountOut":"${formatUnits(getAmountOut(vaultBalance.div(2)), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance.div(2)))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber},"isNodeError":false}`,
`{"amountIn":"${formatUnits(vaultBalance.div(4))}","amountOut":"${formatUnits(getAmountOut(vaultBalance.div(4)), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance.div(4)))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber},"isNodeError":false}`,
`{"amountIn":"${formatUnits(vaultBalance.div(2))}","amountOut":"${formatUnits(getAmountOut(vaultBalance.div(2)), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance.div(2)))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber}}`,
`{"amountIn":"${formatUnits(vaultBalance.div(4))}","amountOut":"${formatUnits(getAmountOut(vaultBalance.div(4)), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance.div(4)))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber}}`,
],
},
};
Expand Down Expand Up @@ -652,8 +652,8 @@ describe("Test find opp with retries", async function () {
spanAttributes: {
hops: [
`{"amountIn":"${formatUnits(vaultBalance)}","amountOut":"${formatUnits(getAmountOut(vaultBalance), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber},"isNodeError":false,"error":${JSON.stringify(errorSnapshot("", ethers.errors.UNPREDICTABLE_GAS_LIMIT))},"rawtx":${JSON.stringify(rawtx)}}`,
`{"amountIn":"${formatUnits(vaultBalance.div(2))}","amountOut":"${formatUnits(getAmountOut(vaultBalance.div(2)), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance.div(2)))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber},"isNodeError":false}`,
`{"amountIn":"${formatUnits(vaultBalance.div(4))}","amountOut":"${formatUnits(getAmountOut(vaultBalance.div(4)), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance.div(4)))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber},"isNodeError":false}`,
`{"amountIn":"${formatUnits(vaultBalance.div(2))}","amountOut":"${formatUnits(getAmountOut(vaultBalance.div(2)), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance.div(2)))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber}}`,
`{"amountIn":"${formatUnits(vaultBalance.div(4))}","amountOut":"${formatUnits(getAmountOut(vaultBalance.div(4)), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance.div(4)))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber}}`,
],
},
};
Expand Down

0 comments on commit 6058195

Please sign in to comment.