Skip to content

Commit

Permalink
fix returns and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Nov 7, 2024
1 parent 6f189bc commit 7cd2ea5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 10 deletions.
11 changes: 11 additions & 0 deletions src/modes/interOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export async function findOpp({
reason: undefined,
spanAttributes,
};
const allNoneNodeErrors: (string | undefined)[] = [];

const opposingOrderbookOrders = orderbooksOrders
.map((v) => {
Expand Down Expand Up @@ -284,6 +285,9 @@ export async function findOpp({
}),
);
} catch (e: any) {
for (const err of (e as AggregateError).errors) {
allNoneNodeErrors.push(err?.value?.noneNodeError);
}
maximumInput = maximumInput.div(2);
try {
// try to find the first resolving binary search
Expand Down Expand Up @@ -321,6 +325,13 @@ export async function findOpp({
e.errors[i].spanAttributes;
}
spanAttributes["againstOrderbooks"] = JSON.stringify(allOrderbooksAttributes);
const noneNodeErrors = allNoneNodeErrors.filter((v) => !!v);
if (allNoneNodeErrors.length && noneNodeErrors.length / allNoneNodeErrors.length > 0.5) {
result.value = {
noneNodeError: noneNodeErrors[0],
estimatedProfit: ethers.constants.Zero,
};
}
return Promise.reject(result);
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/modes/intraOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ export async function findOpp({
if (!opposingOrders || !opposingOrders.length) throw undefined;

const allErrorAttributes: string[] = [];
const allNoneNodeErrors: (string | undefined)[] = [];
const erc20 = new ethers.utils.Interface(erc20Abi);
const inputBalance = ethers.BigNumber.from(
(
Expand Down Expand Up @@ -302,9 +303,17 @@ export async function findOpp({
outputBalance,
});
} catch (e: any) {
allNoneNodeErrors.push(e?.value?.noneNodeError);
allErrorAttributes.push(JSON.stringify(e.spanAttributes));
}
}
spanAttributes["intraOrderbook"] = allErrorAttributes;
const noneNodeErrors = allNoneNodeErrors.filter((v) => !!v);
if (allNoneNodeErrors.length && noneNodeErrors.length / allNoneNodeErrors.length > 0.5) {
result.value = {
noneNodeError: noneNodeErrors[0],
estimatedProfit: ethers.constants.Zero,
};
}
return Promise.reject(result);
}
23 changes: 21 additions & 2 deletions src/modes/routeProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ export async function findOpp({

const allSuccessHops: DryrunResult[] = [];
const allHopsAttributes: string[] = [];
const allNoneNodeErrors: (string | undefined)[] = [];
for (let i = 1; i < config.hops + 1; i++) {
try {
const dryrunResult = await dryrun({
Expand Down Expand Up @@ -354,8 +355,8 @@ export async function findOpp({
if (i !== 1) {
delete e.spanAttributes["error"];
delete e.spanAttributes["rawtx"];
delete e.spanAttributes["isNodeError"];
}
allNoneNodeErrors.push(e?.value?.noneNodeError);
allHopsAttributes.push(JSON.stringify(e.spanAttributes));

// set the maxInput for next hop by decreasing
Expand All @@ -370,7 +371,19 @@ export async function findOpp({
spanAttributes["hops"] = allHopsAttributes;

if (noRoute) result.reason = RouteProcessorDryrunHaltReason.NoRoute;
else result.reason = RouteProcessorDryrunHaltReason.NoOpportunity;
else {
const noneNodeErrors = allNoneNodeErrors.filter((v) => !!v);
if (
allNoneNodeErrors.length &&
noneNodeErrors.length / allNoneNodeErrors.length > 0.5
) {
result.value = {
noneNodeError: noneNodeErrors[0],
estimatedProfit: ethers.constants.Zero,
};
}
result.reason = RouteProcessorDryrunHaltReason.NoOpportunity;
}

return Promise.reject(result);
}
Expand Down Expand Up @@ -458,6 +471,12 @@ export async function findOppWithRetries({
for (const attrKey in (allPromises[0] as any).reason.spanAttributes) {
spanAttributes[attrKey] = (allPromises[0] as any).reason.spanAttributes[attrKey];
}
if ((allPromises[0] as any)?.reason?.value?.noneNodeError) {
result.value = {
noneNodeError: (allPromises[0] as any).reason.value.noneNodeError,
estimatedProfit: ethers.constants.Zero,
};
}
result.reason = RouteProcessorDryrunHaltReason.NoOpportunity;
throw result;
}
Expand Down
5 changes: 4 additions & 1 deletion test/mode-interOrderbook.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,10 @@ describe("Test inter-orderbook find opp", async function () {
from: signer.account.address,
};
const expected = {
value: undefined,
value: {
estimatedProfit: ethers.constants.Zero,
noneNodeError: `\nReason: ${ethers.errors.UNPREDICTABLE_GAS_LIMIT}`,
},
reason: undefined,
spanAttributes: {
againstOrderbooks: JSON.stringify({
Expand Down
5 changes: 4 additions & 1 deletion test/mode-intraOrderbook.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,10 @@ describe("Test intra-orderbook find opp", async function () {
from: signer.account.address,
};
const expected = {
value: undefined,
value: {
estimatedProfit: ethers.constants.Zero,
noneNodeError: `\nReason: ${ethers.errors.UNPREDICTABLE_GAS_LIMIT}`,
},
reason: undefined,
spanAttributes: {
intraOrderbook: [
Expand Down
18 changes: 12 additions & 6 deletions test/mode-routeProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,13 +455,16 @@ describe("Test route processor find opp", async function () {
from: signer.account.address,
});
const expected = {
value: undefined,
value: {
estimatedProfit: ethers.constants.Zero,
noneNodeError: `\nReason: ${ethers.errors.UNPREDICTABLE_GAS_LIMIT}`,
},
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}}`,
`{"amountIn":"${formatUnits(vaultBalance.div(4))}","amountOut":"${formatUnits(getAmountOut(vaultBalance.div(4)), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance.div(4)))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber}}`,
`{"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}`,
],
},
};
Expand Down Expand Up @@ -650,13 +653,16 @@ describe("Test find opp with retries", async function () {
from: signer.account.address,
});
const expected = {
value: undefined,
value: {
estimatedProfit: ethers.constants.Zero,
noneNodeError: `\nReason: ${ethers.errors.UNPREDICTABLE_GAS_LIMIT}`,
},
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}}`,
`{"amountIn":"${formatUnits(vaultBalance.div(4))}","amountOut":"${formatUnits(getAmountOut(vaultBalance.div(4)), 6)}","marketPrice":"${formatUnits(getCurrentPrice(vaultBalance.div(4)))}","route":${JSON.stringify(expectedRouteVisual)},"blockNumber":${oppBlockNumber}}`,
`{"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}`,
],
},
};
Expand Down

0 comments on commit 7cd2ea5

Please sign in to comment.