Skip to content

Commit

Permalink
split up calculateAmounts into calculateAmountIn and calculateAmountO…
Browse files Browse the repository at this point in the history
…ut to handle 'PurchasePriceIsZero' when using computeExactAmountIn
  • Loading branch information
chuckbergeron committed Aug 16, 2023
1 parent ab7d0c2 commit 13421b1
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions packages/library/src/liquidatorArbitrageSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ export async function liquidatorArbitrageSwap(
//
console.log(chalk.blue(`1. Amounts:`));

const { amountOut, amountIn, amountInMin } = await calculateAmounts(
liquidationPairContract,
context,
);
const { amountOut } = await calculateAmountOut(liquidationPairContract, context);
if (amountOut.eq(0)) {
stats.push({
pair,
Expand All @@ -145,6 +142,20 @@ export async function liquidatorArbitrageSwap(
continue;
}

const getAmountInValues = async () => {
try {
return calculateAmountIn(liquidationPairContract, context, amountOut);
} catch (e) {
console.error(chalk.red(e));

console.log(chalk.yellow('---'));
console.log(chalk.yellow('Could not estimate gas costs!'));
console.log(chalk.yellow('---'));
}
};

const { amountIn, amountInMin } = await getAmountInValues();

// #3. Print balance of tokenIn for relayer
//
// env.router().swapExactAmountOut(
Expand Down Expand Up @@ -563,13 +574,11 @@ const getGasCost = async (
* Calculates necessary input parameters for the swap call based on current state of the contracts
* @returns {Promise} Promise object with the input parameters exactAmountIn and amountOutMin
*/
const calculateAmounts = async (
const calculateAmountOut = async (
liquidationPair: Contract,
context: ArbLiquidatorContext,
): Promise<{
amountOut: BigNumber;
amountIn: BigNumber;
amountInMin: BigNumber;
}> => {
const amountOut = await liquidationPair.callStatic.maxAmountOut();
logBigNumber(
Expand All @@ -587,8 +596,6 @@ const calculateAmounts = async (
);
return {
amountOut: BigNumber.from(0),
amountIn: BigNumber.from(0),
amountInMin: BigNumber.from(0),
};
}

Expand All @@ -604,16 +611,32 @@ const calculateAmounts = async (
context.tokenOut.symbol,
);

return {
amountOut: wantedAmountOut,
};
};

/**
* Calculates necessary input parameters for the swap call based on current state of the contracts
* @returns {Promise} Promise object with the input parameters exactAmountIn and amountOutMin
*/
const calculateAmountIn = async (
liquidationPair: Contract,
context: ArbLiquidatorContext,
amountOut: BigNumber,
): Promise<{
amountIn: BigNumber;
amountInMin: BigNumber;
}> => {
printSpacer();

// Necessary for determining profit
const amountIn = await liquidationPair.callStatic.computeExactAmountIn(wantedAmountOut);
const amountIn: BigNumber = await liquidationPair.callStatic.computeExactAmountIn(amountOut);
logBigNumber('Amount in:', amountIn, context.tokenIn.decimals, context.tokenIn.symbol);

const amountInMin = ethers.constants.MaxInt256;

return {
amountOut: wantedAmountOut,
amountIn,
amountInMin,
};
Expand Down

0 comments on commit 13421b1

Please sign in to comment.