Skip to content

Commit

Permalink
update [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Dec 6, 2024
1 parent e257b12 commit 9ea3944
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 78 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"scripts": {
"prepublish": "npm run build",
"test": "npm run unit-test && npm run e2e-test",
"unit-test": "hardhat test ./test/*.test.js",
"unit-test": "hardhat test ./test/*.test.*",
"e2e-test": "hardhat test",
"lint": "eslint ./src ./test ./arb-bot.js",
"lint-fix": "eslint ./src ./test ./arb-bot.js --fix",
Expand Down
12 changes: 5 additions & 7 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export function errorSnapshot(
if (gasErr) {
message.push("Gas Error: " + gasErr);
}
} else {
message.push("Comment: Found no additional info")
}
}
}
Expand Down Expand Up @@ -181,13 +183,9 @@ 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;
const msg = header +
" and simulation failed to find out what was the revert reason, please try to simulate the tx manualy for more details";
return { err: msg, nodeError: false, snapshot: msg };
} catch (err: any) {
return {
err,
Expand Down
8 changes: 4 additions & 4 deletions src/processOrders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ export const processOrders = async (
};

// await for first available signer
if (accounts.length) {
shuffleArray(accounts);
}
const signer = await getSigner(accounts, mainAccount);

const writeSigner = config.writeRpc
Expand Down Expand Up @@ -236,7 +239,7 @@ export const processOrders = async (
}
}

for (const { settle, pair, orderPairObject } of results) {
for (const { settle, pair, orderPairObject } of results) {
// instantiate a span for this pair
const span = tracer.startSpan(`order_${pair}`, undefined, ctx);
try {
Expand Down Expand Up @@ -704,9 +707,6 @@ export async function processPair(args: {
* Returns the first available signer by constantly polling the signers in 30ms intervals
*/
export async function getSigner(accounts: ViemClient[], mainAccount: ViemClient): Promise<ViemClient> {
if (accounts.length) {
shuffleArray(accounts);
}
const accs = accounts.length ? accounts : [mainAccount];
for (;;) {
const acc = accs.find((v) => !v.BUSY);
Expand Down
51 changes: 21 additions & 30 deletions src/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ export async function handleTransaction(
writeSigner?: ViemClient,
): Promise<() => Promise<ProcessPairResult>> {
// submit the tx
let txhash, txUrl;
try {
let txhash: `0x${string}`, txUrl: string;
let time = 0;
const sendTx = async () => {
rawtx.nonce = await getNonce(writeSigner !== undefined ? writeSigner : signer);
if (writeSigner !== undefined) {
rawtx.gas = undefined;
Expand All @@ -56,34 +57,18 @@ export async function handleTransaction(

writeSigner !== undefined ? (writeSigner.BUSY = false) : (signer.BUSY = false);
txUrl = config.chain.blockExplorers?.default.url + "/tx/" + txhash;
time = Date.now();
// eslint-disable-next-line no-console
console.log("\x1b[33m%s\x1b[0m", txUrl, "\n");
spanAttributes["details.txUrl"] = txUrl;
};
try {
await sendTx();
} catch (e) {
try {
// retry again after 5 seconds if first attempt failed
await sleep(5000);
rawtx.nonce = await getNonce(writeSigner !== undefined ? writeSigner : signer);
if (writeSigner !== undefined) {
rawtx.gas = undefined;
}
writeSigner !== undefined ? (writeSigner.BUSY = true) : (signer.BUSY = true);
txhash =
writeSigner !== undefined
? await writeSigner.sendTransaction({
...rawtx,
type: "legacy",
})
: await signer.sendTransaction({
...rawtx,
type: "legacy",
});

writeSigner !== undefined ? (writeSigner.BUSY = false) : (signer.BUSY = false);
txUrl = config.chain.blockExplorers?.default.url + "/tx/" + txhash;
// eslint-disable-next-line no-console
console.log("\x1b[33m%s\x1b[0m", txUrl, "\n");
spanAttributes["details.txUrl"] = txUrl;
await sendTx();
} catch {
writeSigner !== undefined ? (writeSigner.BUSY = false) : (signer.BUSY = false);
// record rawtx in case it is not already present in the error
Expand All @@ -105,7 +90,7 @@ export async function handleTransaction(

// start getting tx receipt in background and return the resolver fn
const receipt = viemClient.waitForTransactionReceipt({
hash: txhash,
hash: txhash!,
confirmations: 1,
timeout: 120_000,
});
Expand All @@ -116,7 +101,6 @@ export async function handleTransaction(
txhash,
await receipt,
signer,
viemClient as any as ViemClient,
spanAttributes,
rawtx,
orderbook,
Expand All @@ -129,6 +113,7 @@ export async function handleTransaction(
toToken,
fromToken,
config,
time,
);
} catch (e: any) {
try {
Expand All @@ -138,7 +123,6 @@ export async function handleTransaction(
txhash,
newReceipt,
signer,
viemClient as any as ViemClient,
spanAttributes,
rawtx,
orderbook,
Expand All @@ -151,6 +135,7 @@ export async function handleTransaction(
toToken,
fromToken,
config,
time,
);
}
} catch {}
Expand Down Expand Up @@ -194,7 +179,6 @@ export async function handleReceipt(
txhash: string,
receipt: TransactionReceipt,
signer: ViemClient,
viemClient: ViemClient,
spanAttributes: any,
rawtx: RawTx,
orderbook: Contract,
Expand All @@ -207,15 +191,16 @@ export async function handleReceipt(
toToken: Token,
fromToken: Token,
config: BotConfig,
) {
time: number,
): Promise<ProcessPairResult> {
const actualGasCost = ethers.BigNumber.from(receipt.effectiveGasPrice).mul(receipt.gasUsed);
const signerBalance = signer.BALANCE;
signer.BALANCE = signer.BALANCE.sub(actualGasCost);

if (receipt.status === "success") {
spanAttributes["didClear"] = true;

const clearActualAmount = getActualClearAmount(rawtx.to, orderbook.address, receipt);

const inputTokenIncome = getIncome(
signer.account.address,
receipt,
Expand Down Expand Up @@ -293,8 +278,14 @@ export async function handleReceipt(
}
return result;
} else {
// wait at least 60s before simulating the revert tx
// in order for rpcs to catch up
const wait = 60000 - Date.now() + time;
if (wait > 0) {
await sleep(wait);
}
const simulation = await handleRevert(
viemClient as any,
signer,
txhash as `0x${string}`,
receipt,
rawtx,
Expand Down
Loading

0 comments on commit 9ea3944

Please sign in to comment.