Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Nov 20, 2024
1 parent 0295006 commit 4bb6a6c
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 41 deletions.
19 changes: 16 additions & 3 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,19 @@ export async function manageAccounts(
) {
const removedWallets: ViemClient[] = [];
let accountsToAdd = 0;
const gasPrice = await config.viemClient.getGasPrice();
try {
const balances = await getBatchEthBalance(
config.accounts.map((v) => v.account.address),
config.viemClient as any as ViemClient,
);
config.accounts.forEach((v, i) => (v.BALANCE = balances[i]));
} catch {
/**/
}
for (let i = config.accounts.length - 1; i >= 0; i--) {
if (config.accounts[i].BALANCE.lt(avgGasCost.mul(4))) {
try {
const gasPrice = await config.viemClient.getGasPrice();
await sweepToMainWallet(
config.accounts[i],
config.mainAccount,
Expand Down Expand Up @@ -529,11 +538,15 @@ export async function sweepToMainWallet(
}
}

if (cumulativeGasLimit.mul(gasPrice).gt(fromWallet.BALANCE)) {
if (cumulativeGasLimit.mul(gasPrice).mul(120).div(100).gt(fromWallet.BALANCE)) {
const span = tracer?.startSpan("fund-wallet-to-sweep", undefined, mainCtx);
span?.setAttribute("details.wallet", fromWallet.account.address);
try {
const transferAmount = cumulativeGasLimit.mul(gasPrice).sub(fromWallet.BALANCE);
const transferAmount = cumulativeGasLimit
.mul(gasPrice)
.mul(120)
.div(100)
.sub(fromWallet.BALANCE);
span?.setAttribute("details.amount", ethers.utils.formatUnits(transferAmount));
const hash = await toWallet.sendTransaction({
to: fromWallet.account.address,
Expand Down
12 changes: 8 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export const arbRound = async (
span.setAttribute("didClear", false);
}
if (avgGasCost) {
span.setAttribute("avgGasCost", avgGasCost.toString());
span.setAttribute("avgGasCost", ethers.utils.formatUnits(avgGasCost));
}
span.setStatus({ code: SpanStatusCode.OK });
span.end();
Expand Down Expand Up @@ -713,10 +713,14 @@ export const main = async (argv: any, version?: string) => {
roundSpan.setStatus({ code: SpanStatusCode.ERROR, message: snapshot });
}
if (config.accounts.length) {
roundSpan.setAttribute(
"circulatingAccounts",
config.accounts.map((v) => v.account.address),
const accountsWithBalance: Record<string, string> = {};
config.accounts.forEach(
(v) =>
(accountsWithBalance[v.account.address] = ethers.utils.formatUnits(
v.BALANCE,
)),
);
roundSpan.setAttribute("circulatingAccounts", JSON.stringify(accountsWithBalance));
}
if (avgGasCost) {
roundSpan.setAttribute("avgGasCost", ethers.utils.formatUnits(avgGasCost));
Expand Down
4 changes: 4 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
decodeErrorResult,
ExecutionRevertedError,
InsufficientFundsError,
FeeCapTooLowError,
// InvalidInputRpcError,
// TransactionRejectedRpcError,
} from "viem";
Expand Down Expand Up @@ -100,12 +101,15 @@ export function errorSnapshot(header: string, err: any): string {
*/
export function containsNodeError(err: BaseError): boolean {
try {
const snapshot = errorSnapshot("", err);
return (
// err instanceof TransactionRejectedRpcError ||
// err instanceof InvalidInputRpcError ||
err instanceof FeeCapTooLowError ||
err instanceof ExecutionRevertedError ||
err instanceof InsufficientFundsError ||
(err instanceof RpcRequestError && err.code === ExecutionRevertedError.code) ||
(snapshot.includes("exceeds allowance") && !snapshot.includes("out of gas")) ||
("cause" in err && containsNodeError(err.cause as any))
);
} catch (error) {
Expand Down
9 changes: 0 additions & 9 deletions src/modes/interOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,6 @@ export async function dryrun({
try {
blockNumber = Number(await viemClient.getBlockNumber());
spanAttributes["blockNumber"] = blockNumber;
try {
gasPrice = ethers.BigNumber.from(await viemClient.getGasPrice())
.mul(config.gasPriceMultiplier)
.div("100")
.toBigInt();
rawtx.gasPrice = gasPrice;
} catch {
/**/
}
gasLimit = ethers.BigNumber.from(await signer.estimateGas({ ...rawtx, type: "legacy" }))
.mul(config.gasLimitMultiplier)
.div(100);
Expand Down
9 changes: 0 additions & 9 deletions src/modes/intraOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,6 @@ export async function dryrun({
try {
blockNumber = Number(await viemClient.getBlockNumber());
spanAttributes["blockNumber"] = blockNumber;
try {
gasPrice = ethers.BigNumber.from(await viemClient.getGasPrice())
.mul(config.gasPriceMultiplier)
.div("100")
.toBigInt();
rawtx.gasPrice = gasPrice;
} catch {
/**/
}
gasLimit = ethers.BigNumber.from(await signer.estimateGas({ ...rawtx, type: "legacy" }))
.mul(config.gasLimitMultiplier)
.div(100);
Expand Down
9 changes: 0 additions & 9 deletions src/modes/routeProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,6 @@ export async function dryrun({
try {
blockNumber = Number(await viemClient.getBlockNumber());
spanAttributes["blockNumber"] = blockNumber;
try {
gasPrice = ethers.BigNumber.from(await viemClient.getGasPrice())
.mul(config.gasPriceMultiplier)
.div("100")
.toBigInt();
rawtx.gasPrice = gasPrice;
} catch {
/**/
}
gasLimit = ethers.BigNumber.from(await signer.estimateGas({ ...rawtx, type: "legacy" }))
.mul(config.gasLimitMultiplier)
.div(100);
Expand Down
2 changes: 1 addition & 1 deletion test/account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe("Test accounts", async function () {
it("should manage accounts successfully", async function () {
const viemClient = {
chain: { id: 137 },
multicall: async () => [10000n, 0n, 0n],
multicall: async () => [10n, 0n],
getGasPrice: async () => 3000000n,
};
const mnemonic = "test test test test test test test test test test test junk";
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 @@ -433,6 +433,7 @@ describe("Test inter-orderbook find opp", async function () {
[opposingOrderbookAddress]: {
maxInput: vaultBalance.toString(),
blockNumber: oppBlockNumber,
stage: 1,
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 @@ -334,6 +334,7 @@ describe("Test intra-orderbook find opp", async function () {
intraOrderbook: [
JSON.stringify({
blockNumber: oppBlockNumber,
stage: 1,
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 @@ -230,6 +230,7 @@ describe("Test route processor dryrun", async function () {
blockNumber: oppBlockNumber,
error: errorSnapshot("", ethers.errors.UNPREDICTABLE_GAS_LIMIT),
route: expectedRouteVisual,
stage: 1,
rawtx: JSON.stringify(rawtx),
isNodeError: false,
},
Expand Down Expand Up @@ -470,9 +471,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},"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)}","amountOut":"${formatUnits(getAmountOut(vaultBalance), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber},"stage":1,"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},"stage":1,"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},"stage":1,"isNodeError":false}`,
],
},
};
Expand Down Expand Up @@ -672,9 +673,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},"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)}","amountOut":"${formatUnits(getAmountOut(vaultBalance), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber},"stage":1,"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},"stage":1,"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},"stage":1,"isNodeError":false}`,
],
},
};
Expand Down

0 comments on commit 4bb6a6c

Please sign in to comment.