Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Nov 6, 2024
1 parent 173b2f7 commit 56c5ce7
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 15 deletions.
20 changes: 19 additions & 1 deletion src/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { BaseError } from "viem";
import {
BaseError,
RpcRequestError,
InvalidInputRpcError,
ExecutionRevertedError,
TransactionRejectedRpcError,
} from "viem";

/**
* Specifies error severity
Expand Down Expand Up @@ -32,3 +38,15 @@ export function errorSnapshot(header: string, err: any): string {
}
return message.join("\n");
}

/**
* Checks if a viem BaseError is from eth node, copied from
* "viem/_types/utils/errors/getNodeError" since not a default export
*/
export function containsNodeError(err: BaseError) {
return (
err instanceof TransactionRejectedRpcError ||
err instanceof InvalidInputRpcError ||
(err instanceof RpcRequestError && err.code === ExecutionRevertedError.code)
);
}
6 changes: 4 additions & 2 deletions src/modes/interOrderbook.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { PublicClient } from "viem";
import { orderbookAbi } from "../abis";
import { errorSnapshot } from "../error";
import { BaseError, PublicClient } from "viem";
import { getBountyEnsureBytecode } from "../config";
import { BigNumber, Contract, ethers } from "ethers";
import { containsNodeError, errorSnapshot } from "../error";
import { estimateProfit, withBigintSerializer } from "../utils";
import { BotConfig, BundledOrders, ViemClient, DryrunResult, SpanAttrs } from "../types";

Expand Down Expand Up @@ -104,6 +104,7 @@ 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);
spanAttributes["rawtx"] = JSON.stringify(
{
Expand Down Expand Up @@ -149,6 +150,7 @@ export async function dryrun({
task,
]);
} catch (e) {
spanAttributes["isNodeError"] = containsNodeError(e as BaseError);
spanAttributes["error"] = errorSnapshot("", e);
spanAttributes["rawtx"] = JSON.stringify(
{
Expand Down
6 changes: 4 additions & 2 deletions src/modes/intraOrderbook.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { PublicClient } from "viem";
import { errorSnapshot } from "../error";
import { BigNumber, ethers } from "ethers";
import { BaseError, PublicClient } from "viem";
import { erc20Abi, orderbookAbi } from "../abis";
import { getWithdrawEnsureBytecode } from "../config";
import { containsNodeError, errorSnapshot } from "../error";
import { estimateProfit, withBigintSerializer } from "../utils";
import {
BotConfig,
Expand Down Expand Up @@ -92,6 +92,7 @@ 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);
spanAttributes["rawtx"] = JSON.stringify(
{
Expand Down Expand Up @@ -162,6 +163,7 @@ export async function dryrun({
[clear2Calldata, withdrawInputCalldata, withdrawOutputCalldata],
]);
} catch (e) {
spanAttributes["isNodeError"] = containsNodeError(e as BaseError);
spanAttributes["error"] = errorSnapshot("", e);
spanAttributes["rawtx"] = JSON.stringify(
{
Expand Down
6 changes: 4 additions & 2 deletions src/modes/routeProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PublicClient } from "viem";
import { Token } from "sushi/currency";
import { errorSnapshot } from "../error";
import { BaseError, PublicClient } from "viem";
import { getBountyEnsureBytecode } from "../config";
import { ChainId, DataFetcher, Router } from "sushi";
import { BigNumber, Contract, ethers } from "ethers";
import { containsNodeError, errorSnapshot } from "../error";
import { BotConfig, BundledOrders, ViemClient, DryrunResult, SpanAttrs } from "../types";
import { estimateProfit, RPoolFilter, visualizeRoute, withBigintSerializer } from "../utils";

Expand Down Expand Up @@ -170,6 +170,7 @@ 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);
spanAttributes["rawtx"] = JSON.stringify(
{
Expand Down Expand Up @@ -216,6 +217,7 @@ export async function dryrun({
task,
]);
} catch (e) {
spanAttributes["isNodeError"] = containsNodeError(e as BaseError);
spanAttributes["error"] = errorSnapshot("", e);
spanAttributes["rawtx"] = JSON.stringify(
{
Expand Down
6 changes: 4 additions & 2 deletions src/processOrders.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { ChainId } from "sushi";
import { findOpp } from "./modes";
import { PublicClient } from "viem";
import { Token } from "sushi/currency";
import { createViemClient } from "./config";
import { BaseError, PublicClient } from "viem";
import { arbAbis, orderbookAbi } from "./abis";
import { privateKeyToAccount } from "viem/accounts";
import { BigNumber, Contract, ethers } from "ethers";
import { Tracer } from "@opentelemetry/sdk-trace-base";
import { ErrorSeverity, errorSnapshot } from "./error";
import { fundOwnedOrders, rotateAccounts } from "./account";
import { Context, SpanStatusCode } from "@opentelemetry/api";
import { containsNodeError, ErrorSeverity, errorSnapshot } from "./error";
import {
Report,
BotConfig,
Expand Down Expand Up @@ -624,6 +624,7 @@ export async function processPair(args: {
},
withBigintSerializer,
);
spanAttributes["details.isNodeError"] = containsNodeError(e as BaseError);
result.error = e;
result.reason = ProcessPairHaltReason.TxFailed;
throw result;
Expand Down Expand Up @@ -761,6 +762,7 @@ export async function processPair(args: {
result.report.actualGasCost = ethers.utils.formatUnits(actualGasCost);
}
result.error = e;
spanAttributes["details.isNodeError"] = containsNodeError(e);
result.reason = ProcessPairHaltReason.TxMineFailed;
throw result;
}
Expand Down
1 change: 1 addition & 0 deletions test/mode-interOrderbook.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ describe("Test inter-orderbook find opp", async function () {
[opposingOrderbookAddress]: {
maxInput: vaultBalance.toString(),
blockNumber: oppBlockNumber,
isNodeError: false,
error: errorSnapshot("", err),
rawtx: JSON.stringify(rawtx),
},
Expand Down
1 change: 1 addition & 0 deletions test/mode-intraOrderbook.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ describe("Test intra-orderbook find opp", async function () {
intraOrderbook: [
JSON.stringify({
blockNumber: oppBlockNumber,
isNodeError: false,
error: errorSnapshot("", err),
rawtx: JSON.stringify(rawtx),
}),
Expand Down
13 changes: 7 additions & 6 deletions test/mode-routeProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ describe("Test route processor dryrun", async function () {
error: errorSnapshot("", ethers.errors.UNPREDICTABLE_GAS_LIMIT),
route: expectedRouteVisual,
rawtx: JSON.stringify(rawtx),
isNodeError: false,
},
};
assert.deepEqual(error, expected);
Expand Down Expand Up @@ -455,9 +456,9 @@ describe("Test route processor find opp", async function () {
reason: RouteProcessorDryrunHaltReason.NoOpportunity,
spanAttributes: {
hops: [
`{"amountIn":"${formatUnits(vaultBalance)}","amountOut":"${formatUnits(getAmountOut(vaultBalance), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber},"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}}`,
`{"amountIn":"${formatUnits(vaultBalance.div(4))}","amountOut":"${formatUnits(getAmountOut(vaultBalance.div(4)), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance.div(4)))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber}}`,
`{"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}`,
],
},
};
Expand Down Expand Up @@ -650,9 +651,9 @@ describe("Test find opp with retries", async function () {
reason: RouteProcessorDryrunHaltReason.NoOpportunity,
spanAttributes: {
hops: [
`{"amountIn":"${formatUnits(vaultBalance)}","amountOut":"${formatUnits(getAmountOut(vaultBalance), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber},"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}}`,
`{"amountIn":"${formatUnits(vaultBalance.div(4))}","amountOut":"${formatUnits(getAmountOut(vaultBalance.div(4)), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance.div(4)))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber}}`,
`{"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}`,
],
},
};
Expand Down
1 change: 1 addition & 0 deletions test/processPair.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ describe("Test process pair", async function () {
"details.outputToEthPrice": "1",
"details.marketQuote.num": 0.99699,
"details.marketQuote.str": "0.99699",
"details.isNodeError": false,
"details.quote": JSON.stringify({
maxOutput: formatUnits(vaultBalance),
ratio: formatUnits(ethers.constants.Zero),
Expand Down

0 comments on commit 56c5ce7

Please sign in to comment.