Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Oct 26, 2024
1 parent f7c71b0 commit e05dbb0
Show file tree
Hide file tree
Showing 6 changed files with 813 additions and 245 deletions.
40 changes: 31 additions & 9 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ import { CompressionAlgorithm } from "@opentelemetry/otlp-exporter-base";
import { BotConfig, BundledOrders, CliOptions, ViemClient } from "./types";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
import { SEMRESATTRS_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
import { handleNewLogs, watchAllOrderbooks, WatchedOrderbookOrders } from "./watcher";
import { getOrderbookOwnersProfileMapFromSg, prepareRoundProcessingOrders } from "./order";
import { getOrderbookOwnersProfileMapFromSg, prepareOrdersForRound } from "./order";
import { manageAccounts, rotateProviders, sweepToMainWallet, sweepToEth } from "./account";
import {
watchOrderbook,
watchAllOrderbooks,
WatchedOrderbookOrders,
handleOrderbooksNewLogs,
} from "./watcher";
import {
diag,
trace,
Expand Down Expand Up @@ -459,7 +464,7 @@ export const main = async (argv: any, version?: string) => {
orderbooksOwnersProfileMap.forEach((_, ob) => {
obs.push(ob.toLowerCase());
});
watchAllOrderbooks(obs, config.watchClient, watchedOrderbooksOrders);
const unwatchers = watchAllOrderbooks(obs, config.watchClient, watchedOrderbooksOrders);

const day = 24 * 60 * 60 * 1000;
let lastGasReset = Date.now() + day;
Expand All @@ -476,12 +481,32 @@ export const main = async (argv: any, version?: string) => {
while (true) {
await tracer.startActiveSpan(`round-${counter}`, async (roundSpan) => {
const roundCtx = trace.setSpan(context.active(), roundSpan);
const newMeta = await getMetaInfo(config, options.subgraph);
roundSpan.setAttributes({
...(await getMetaInfo(config, options.subgraph)),
...newMeta,
"meta.mainAccount": config.mainAccount.account.address,
"meta.gitCommitHash": process?.env?.GIT_COMMIT ?? "N/A",
"meta.dockerTag": process?.env?.DOCKER_TAG ?? "N/A",
});

// watch new obs
for (const newOb of newMeta["meta.orderbooks"]) {
const ob = newOb.toLowerCase();
if (!obs.includes(ob)) {
obs.push(ob);
if (!watchedOrderbooksOrders[ob]) {
watchedOrderbooksOrders[ob] = { orderLogs: [] };
}
if (!unwatchers[ob]) {
unwatchers[ob] = watchOrderbook(
ob,
config.watchClient,
watchedOrderbooksOrders[ob],
);
}
}
}

await tracer.startActiveSpan(
"check-wallet-balance",
{},
Expand Down Expand Up @@ -533,10 +558,7 @@ export const main = async (argv: any, version?: string) => {
update = true;
}
try {
const bundledOrders = prepareRoundProcessingOrders(
orderbooksOwnersProfileMap,
true,
);
const bundledOrders = prepareOrdersForRound(orderbooksOwnersProfileMap, true);
await rotateProviders(config, update);
const roundResult = await arbRound(
tracer,
Expand Down Expand Up @@ -659,7 +681,7 @@ export const main = async (argv: any, version?: string) => {
}
try {
// check for new orders
await handleNewLogs(
await handleOrderbooksNewLogs(
orderbooksOwnersProfileMap,
watchedOrderbooksOrders,
config.viemClient as any as ViemClient,
Expand Down
107 changes: 73 additions & 34 deletions src/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ import {
OrderbooksOwnersProfileMap,
} from "./types";

export async function getPairs(
/**
* The default owner limit
*/
export const DEFAULT_OWNER_LIMIT = 25 as const;

/**
* Get all pairs of an order
*/
export async function getOrderPairs(
orderStruct: Order,
viemClient: ViemClient,
tokens: TokenDetails[],
Expand Down Expand Up @@ -116,7 +124,12 @@ export async function getOrderbookOwnersProfileMapFromSg(
ownerProfile.orders.set(orderDetails.orderHash.toLowerCase(), {
active: true,
order: orderStruct,
takeOrders: await getPairs(orderStruct, viemClient, tokens, orderDetails),
takeOrders: await getOrderPairs(
orderStruct,
viemClient,
tokens,
orderDetails,
),
consumedTakeOrders: [],
});
}
Expand All @@ -125,11 +138,11 @@ export async function getOrderbookOwnersProfileMapFromSg(
ordersProfileMap.set(orderDetails.orderHash.toLowerCase(), {
active: true,
order: orderStruct,
takeOrders: await getPairs(orderStruct, viemClient, tokens, orderDetails),
takeOrders: await getOrderPairs(orderStruct, viemClient, tokens, orderDetails),
consumedTakeOrders: [],
});
orderbookOwnerProfileItem.set(orderStruct.owner.toLowerCase(), {
limit: ownerLimits?.[orderStruct.owner.toLowerCase()] ?? 25,
limit: ownerLimits?.[orderStruct.owner.toLowerCase()] ?? DEFAULT_OWNER_LIMIT,
orders: ordersProfileMap,
});
}
Expand All @@ -138,12 +151,12 @@ export async function getOrderbookOwnersProfileMapFromSg(
ordersProfileMap.set(orderDetails.orderHash.toLowerCase(), {
active: true,
order: orderStruct,
takeOrders: await getPairs(orderStruct, viemClient, tokens, orderDetails),
takeOrders: await getOrderPairs(orderStruct, viemClient, tokens, orderDetails),
consumedTakeOrders: [],
});
const ownerProfileMap: OwnersProfileMap = new Map();
ownerProfileMap.set(orderStruct.owner.toLowerCase(), {
limit: ownerLimits?.[orderStruct.owner.toLowerCase()] ?? 25,
limit: ownerLimits?.[orderStruct.owner.toLowerCase()] ?? DEFAULT_OWNER_LIMIT,
orders: ordersProfileMap,
});
orderbookOwnersProfileMap.set(orderbook, ownerProfileMap);
Expand All @@ -153,51 +166,57 @@ export async function getOrderbookOwnersProfileMapFromSg(
}

/**
* Get token symbol
* @param address - The address of token
* @param viemClient - The viem client
* Prepares an array of orders for a arb round by following owners limits
* @param orderbooksOwnersProfileMap - The orderbooks owners orders map
* @param shuffle - (optional) Shuffle the order of items
*/
export async function getTokenSymbol(address: string, viemClient: ViemClient): Promise<string> {
try {
return await viemClient.readContract({
address: address as `0x${string}`,
abi: parseAbi(erc20Abi),
functionName: "symbol",
});
} catch (error) {
return "Unknownsymbol";
}
}

export function prepareRoundProcessingOrders(
export function prepareOrdersForRound(
orderbooksOwnersProfileMap: OrderbooksOwnersProfileMap,
shuffle = true,
): BundledOrders[][] {
const result: BundledOrders[][] = [];
for (const [orderbook, ownersProfileMap] of orderbooksOwnersProfileMap) {
const orderbookBundledOrders: BundledOrders[] = [];
for (const [, ownerProfile] of ownersProfileMap) {
let consumedLimit = ownerProfile.limit;
let remainingLimit = ownerProfile.limit;
const activeOrdersProfiles = Array.from(ownerProfile.orders).filter((v) => v[1].active);
const remainingOrdersPairs = activeOrdersProfiles.filter(
(v) => v[1].takeOrders.length > 0,
);
if (remainingOrdersPairs.length === 0) {
for (const [, orderProfile] of activeOrdersProfiles) {
orderProfile.takeOrders.push(...orderProfile.consumedTakeOrders.splice(0));
}
for (const [orderHash, orderProfile] of activeOrdersProfiles) {
const consumingOrderPairs = orderProfile.takeOrders.splice(0, consumedLimit);
consumedLimit -= consumingOrderPairs.length;
orderProfile.consumedTakeOrders.push(...consumingOrderPairs);
gatherPairs(orderbook, orderHash, consumingOrderPairs, orderbookBundledOrders);
orderProfile.takeOrders.push(...orderProfile.consumedTakeOrders.splice(0));
if (remainingLimit > 0) {
const consumingOrderPairs = orderProfile.takeOrders.splice(
0,
remainingLimit,
);
remainingLimit -= consumingOrderPairs.length;
orderProfile.consumedTakeOrders.push(...consumingOrderPairs);
gatherPairs(
orderbook,
orderHash,
consumingOrderPairs,
orderbookBundledOrders,
);
}
}
} else {
for (const [orderHash, orderProfile] of remainingOrdersPairs) {
const consumingOrderPairs = orderProfile.takeOrders.splice(0, consumedLimit);
consumedLimit -= consumingOrderPairs.length;
orderProfile.consumedTakeOrders.push(...consumingOrderPairs);
gatherPairs(orderbook, orderHash, consumingOrderPairs, orderbookBundledOrders);
if (remainingLimit > 0) {
const consumingOrderPairs = orderProfile.takeOrders.splice(
0,
remainingLimit,
);
remainingLimit -= consumingOrderPairs.length;
orderProfile.consumedTakeOrders.push(...consumingOrderPairs);
gatherPairs(
orderbook,
orderHash,
consumingOrderPairs,
orderbookBundledOrders,
);
}
}
}
}
Expand All @@ -218,6 +237,9 @@ export function prepareRoundProcessingOrders(
return result;
}

/**
* Gathers owners orders by token pair
*/
function gatherPairs(
orderbook: string,
orderHash: string,
Expand Down Expand Up @@ -258,3 +280,20 @@ function gatherPairs(
}
}
}

/**
* Get token symbol
* @param address - The address of token
* @param viemClient - The viem client
*/
export async function getTokenSymbol(address: string, viemClient: ViemClient): Promise<string> {
try {
return await viemClient.readContract({
address: address as `0x${string}`,
abi: parseAbi(erc20Abi),
functionName: "symbol",
});
} catch (error) {
return "Unknownsymbol";
}
}
Loading

0 comments on commit e05dbb0

Please sign in to comment.