Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Feb 21, 2025
1 parent a4360fb commit d211f90
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/sushiswap
5 changes: 1 addition & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getSgOrderbooks } from "./sg";
import { sendTransaction } from "./tx";
import { WNATIVE } from "sushi/currency";
import { ChainId, ChainKey } from "sushi/chain";
import { DataFetcher, LiquidityProviders } from "sushi/router";
import { RainDataFetcher, LiquidityProviders } from "sushi/router";
import {
BotConfig,
RpcRecord,
Expand Down Expand Up @@ -206,7 +206,7 @@ export async function getDataFetcher(
useFallbacks = false,
): Promise<BotDataFetcher> {
try {
const dataFetcher = new DataFetcher(
const dataFetcher = new RainDataFetcher(
configOrViemClient.chain!.id as ChainId,
"transport" in configOrViemClient
? (configOrViemClient as PublicClient)
Expand Down
27 changes: 27 additions & 0 deletions src/processOrders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ export const processOrders = async (
}
span.setAttribute("severity", ErrorSeverity.MEDIUM);
span.setStatus({ code: SpanStatusCode.ERROR, message });
} else if (e.reason === ProcessPairHaltReason.FailedToUpdatePools) {
let message = pair + ": failed to update pool details by event data";
if (e.error) {
message = errorSnapshot(message, e.error);
span.recordException(e.error);
}
span.setAttribute("severity", ErrorSeverity.MEDIUM);
span.setStatus({ code: SpanStatusCode.ERROR, message });
} else if (e.reason === ProcessPairHaltReason.FailedToGetEthPrice) {
// set OK status because a token might not have a pool and as a result eth price cannot
// be fetched for it and if it is set to ERROR it will constantly error on each round
Expand Down Expand Up @@ -464,6 +472,25 @@ export async function processPair(args: {
});

const gasPrice = BigNumber.from(state.gasPrice);

// update pools by events watching until current block
try {
if (dataFetcher.fetchedPairPools?.length) {
let blockNumber;
if (isE2eTest && (config as any).testBlockNumberTemp) {
(config as any).testBlockNumberTemp += 10n;
blockNumber = (config as any).testBlockNumberTemp;
}
await dataFetcher.updatePools(blockNumber);
}
} catch (e) {
result.reason = ProcessPairHaltReason.FailedToUpdatePools;
result.error = e;
return async () => {
throw result;
};
}

// get pool details
if (
!dataFetcher.fetchedPairPools.includes(pair) ||
Expand Down
7 changes: 4 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BigNumber } from "ethers";
import { Token } from "sushi/currency";
import { AttributeValue } from "@opentelemetry/api";
import { DataFetcher, LiquidityProviders } from "sushi/router";
import { RainDataFetcher, LiquidityProviders } from "sushi/router";
import {
Chain,
Account,
Expand All @@ -25,7 +25,8 @@ export enum ProcessPairHaltReason {
TxFailed = 4,
TxMineFailed = 5,
TxReverted = 6,
UnexpectedError = 7,
FailedToUpdatePools = 7,
UnexpectedError = 8,
}

/**
Expand Down Expand Up @@ -183,7 +184,7 @@ export type TestViemClient = TestClient<"hardhat"> &
) => Promise<`0x${string}`>;
};

export type BotDataFetcher = DataFetcher & { fetchedPairPools: string[] };
export type BotDataFetcher = RainDataFetcher & { fetchedPairPools: string[] };

export type ChainConfig = {
chain: Chain;
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { isBytes, isHexString } from "ethers/lib/utils";
import { BigNumber, BigNumberish, ethers } from "ethers";
import { erc20Abi, parseEventLogs, TransactionReceipt } from "viem";
import { BotConfig, TakeOrderDetails, TokenDetails, ViemClient } from "./types";
import { DataFetcher, DataFetcherOptions, LiquidityProviders, Router } from "sushi/router";
import { RainDataFetcher, DataFetcherOptions, LiquidityProviders, Router } from "sushi/router";

/**
* One ether which equals to 1e18
Expand Down Expand Up @@ -159,7 +159,7 @@ export async function getEthPrice(
targetTokenAddress: string,
targetTokenDecimals: number,
gasPrice: BigNumber,
dataFetcher: DataFetcher,
dataFetcher: RainDataFetcher,
options?: DataFetcherOptions,
fetchPools = true,
): Promise<string | undefined> {
Expand Down Expand Up @@ -649,7 +649,7 @@ export async function getRpSwap(
toToken: Type,
receiverAddress: string,
routeProcessorAddress: string,
dataFetcher: DataFetcher,
dataFetcher: RainDataFetcher,
gasPrice: BigNumber,
lps?: LiquidityProviders[],
) {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ for (let i = 0; i < testData.length; i++) {
config.arbAddress = arb.address;
config.orderbookAddress = orderbook.address;
config.testBlockNumber = BigInt(blockNumber);
config.testBlockNumberTemp = BigInt(blockNumber);
config.gasCoveragePercentage = "1";
config.viemClient = viemClient;
config.dataFetcher = dataFetcher;
Expand Down
1 change: 1 addition & 0 deletions test/processPair.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ describe("Test process pair", async function () {
dataFetcher = {
fetchPoolsForToken: async () => {},
fetchedPairPools: [],
updatePools: async () => {},
};
// 0xe0e530b7
viemClient = {
Expand Down

0 comments on commit d211f90

Please sign in to comment.