Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Dec 7, 2024
1 parent ddf3d4e commit 7b67b76
Show file tree
Hide file tree
Showing 10 changed files with 446 additions and 258 deletions.
52 changes: 10 additions & 42 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,9 @@ export async function initAccounts(
span?.setAttribute("details.wallet", accounts[i].account.address);
span?.setAttribute("details.amount", ethers.utils.formatUnits(transferAmount));
try {
const hash = await mainAccount.sendTransaction({
const hash = await mainAccount.sendTx({
to: accounts[i].account.address,
value: transferAmount.toBigInt(),
nonce: await getNonce(mainAccount),
});
const receipt = await mainAccount.waitForTransactionReceipt({
hash,
Expand Down Expand Up @@ -248,10 +247,9 @@ export async function manageAccounts(
continue;
}
try {
const hash = await config.mainAccount.sendTransaction({
const hash = await config.mainAccount.sendTx({
to: acc.account.address,
value: transferAmount.toBigInt(),
nonce: await getNonce(config.mainAccount),
});
const receipt = await config.mainAccount.waitForTransactionReceipt({
hash,
Expand Down Expand Up @@ -543,10 +541,9 @@ export async function sweepToMainWallet(
.div(100)
.sub(fromWallet.BALANCE);
span?.setAttribute("details.amount", ethers.utils.formatUnits(transferAmount));
const hash = await toWallet.sendTransaction({
const hash = await toWallet.sendTx({
to: fromWallet.account.address,
value: transferAmount.toBigInt(),
nonce: await getNonce(toWallet),
});
const receipt = await toWallet.waitForTransactionReceipt({
hash,
Expand Down Expand Up @@ -589,9 +586,7 @@ export async function sweepToMainWallet(
span?.setAttribute("details.tokenAddress", txs[i].bounty.address);
span?.setAttribute("details.balance", txs[i].balance);
try {
const nonce = await getNonce(fromWallet);
(txs[i].tx as any).nonce = nonce;
const hash = await fromWallet.sendTransaction(txs[i].tx);
const hash = await fromWallet.sendTx(txs[i].tx);
const receipt = await fromWallet.waitForTransactionReceipt({
hash,
confirmations: 4,
Expand Down Expand Up @@ -643,12 +638,11 @@ export async function sweepToMainWallet(
const transferAmount = remainingGas.sub(gasLimit.mul(gasPrice));
if (transferAmount.gt(0)) {
span?.setAttribute("details.amount", ethers.utils.formatUnits(transferAmount));
const hash = await fromWallet.sendTransaction({
const hash = await fromWallet.sendTx({
gasPrice,
to: toWallet.account.address,
value: transferAmount.toBigInt(),
gas: gasLimit.toBigInt(),
nonce: await getNonce(fromWallet),
});
const receipt = await fromWallet.waitForTransactionReceipt({
hash,
Expand Down Expand Up @@ -791,13 +785,12 @@ export async function sweepToEth(config: BotConfig, tracer?: Tracer, ctx?: Conte
).data;
if (allowance && balance.gt(allowance)) {
span?.addEvent("Approving spend limit");
const hash = await config.mainAccount.sendTransaction({
const hash = await config.mainAccount.sendTx({
to: bounty.address as `0x${string}`,
data: erc20.encodeFunctionData("approve", [
rp4Address,
balance.mul(100),
]) as `0x${string}`,
nonce: await getNonce(config.mainAccount),
});
await config.mainAccount.waitForTransactionReceipt({
hash,
Expand Down Expand Up @@ -838,9 +831,7 @@ export async function sweepToEth(config: BotConfig, tracer?: Tracer, ctx?: Conte
span?.end();
continue;
} else {
const nonce = await getNonce(config.mainAccount);
(rawtx as any).nonce = nonce;
const hash = await config.mainAccount.sendTransaction(rawtx);
const hash = await config.mainAccount.sendTx(rawtx);
span?.setAttribute("txHash", hash);
const receipt = await config.mainAccount.waitForTransactionReceipt({
hash,
Expand Down Expand Up @@ -1012,11 +1003,10 @@ export async function fundOwnedOrders(
finalRpParams!.to,
finalRpParams!.routeCode,
]) as `0x${string}`;
const swapHash = await config.mainAccount.sendTransaction({
const swapHash = await config.mainAccount.sendTx({
to: rp4Address,
value: sellAmount!.toBigInt(),
data,
nonce: await getNonce(config.mainAccount),
});
const swapReceipt = await config.mainAccount.waitForTransactionReceipt({
hash: swapHash,
Expand Down Expand Up @@ -1046,13 +1036,12 @@ export async function fundOwnedOrders(
})
).data;
if (allowance && topupAmount.gt(allowance)) {
const approveHash = await config.mainAccount.sendTransaction({
const approveHash = await config.mainAccount.sendTx({
to: ownedOrder.token as `0x${string}`,
data: erc20.encodeFunctionData("approve", [
ownedOrder.orderbook,
topupAmount.mul(20),
]) as `0x${string}`,
nonce: await getNonce(config.mainAccount),
});
const approveReceipt =
await config.mainAccount.waitForTransactionReceipt({
Expand All @@ -1070,15 +1059,14 @@ export async function fundOwnedOrders(
}
}

const hash = await config.mainAccount.sendTransaction({
const hash = await config.mainAccount.sendTx({
to: ownedOrder.orderbook as `0x${string}`,
data: ob.encodeFunctionData("deposit2", [
ownedOrder.token,
vaultId,
topupAmount,
[],
]) as `0x${string}`,
nonce: await getNonce(config.mainAccount),
});
const receipt = await config.mainAccount.waitForTransactionReceipt({
hash,
Expand Down Expand Up @@ -1138,23 +1126,3 @@ async function getTransactionCount(
);
return hexToNumber(count);
}

/**
* Get an account's nonce
* @param client - The viem client
* @param address - account address
*/
export async function getNonce(client: ViemClient): Promise<number> {
for (let i = 0; i < 3; i++) {
try {
return await client.getTransactionCount({
address: client.account.address,
blockTag: "latest",
});
} catch (error) {
if (i === 2) throw error;
else await sleep((i + 1) * 5000);
}
}
throw "Failed to get account's nonce";
}
11 changes: 10 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
ROUTE_PROCESSOR_3_1_ADDRESS,
ROUTE_PROCESSOR_3_2_ADDRESS,
} from "sushi/config";
import { sendTransaction } from "./tx";

/**
* Get the chain config for a given chain id
Expand Down Expand Up @@ -114,7 +115,7 @@ export async function createViemClient(
? fallback([...topRpcs, ...fallbacks], configuration)
: fallback(topRpcs, configuration);

return testClient
const client = testClient
? ((await testClient({ account }))
.extend(publicActions)
.extend(walletActions) as any as ViemClient)
Expand All @@ -123,6 +124,14 @@ export async function createViemClient(
chain: publicClientConfig[chainId]?.chain,
transport,
}).extend(publicActions) as any as ViemClient);

// set injected properties
client.BUSY = false;
client.sendTx = async (tx) => {
return await sendTransaction(client, tx);
};

return client;
}

/**
Expand Down
11 changes: 8 additions & 3 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function errorSnapshot(
message.push("Gas Error: " + gasErr);
}
} else {
message.push("Comment: Found no additional info")
message.push("Comment: Found no additional info");
}
}
}
Expand Down Expand Up @@ -172,7 +172,11 @@ export async function handleRevert(
try {
const gasErr = checkGasIssue(receipt, rawtx, signerBalance);
if (gasErr) {
return { err: header + ", " + gasErr, nodeError: false, snapshot: header + ", " + gasErr };
return {
err: header + ", " + gasErr,
nodeError: false,
snapshot: header + ", " + gasErr,
};
}
const tx = await viemClient.getTransaction({ hash });
await viemClient.call({
Expand All @@ -183,7 +187,8 @@ export async function handleRevert(
gasPrice: tx.gasPrice,
blockNumber: tx.blockNumber,
});
const msg = header +
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) {
Expand Down
Loading

0 comments on commit 7b67b76

Please sign in to comment.