Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Created November 14, 2022 04:33
Show Gist options
  • Save yuyasugano/7ff71b62eb5c0148ac5a80cc226e70f9 to your computer and use it in GitHub Desktop.
Save yuyasugano/7ff71b62eb5c0148ac5a80cc226e70f9 to your computer and use it in GitHub Desktop.
getProfit modification
/// @notice Calculate how much profit we can by arbitraging between two pools
function getProfit(address pool0, address pool1, address adjustedpair, address adjustedtoken, uint256 adjustment0, uint256 adjustment1) external view returns (uint256 profit, address baseToken) {
(bool baseTokenSmaller, , ) = isbaseTokenSmaller(pool0, pool1);
baseToken = baseTokenSmaller ? IUniswapV2Pair(pool0).token0() : IUniswapV2Pair(pool0).token1();
Adjustments memory adj;
adj.adjustmentPool = adjustedpair;
adj.adjustmentToken = adjustedtoken;
adj.adjustment0 = adjustment0;
adj.adjustment1 = adjustment1;
(, , OrderedReserves memory orderedReserves) = getOrderedReserves(pool0, pool1, adj, baseTokenSmaller);
uint256 borrowAmount = calcBorrowAmount(orderedReserves);
// borrow quote token on lower price pool,
uint256 debtAmount = getAmountIn(borrowAmount, orderedReserves.a1, orderedReserves.b1);
// sell borrowed quote token on higher price pool
uint256 baseTokenOutAmount = getAmountOut(borrowAmount, orderedReserves.b2, orderedReserves.a2);
if (baseTokenOutAmount < debtAmount) {
profit = 0;
} else {
profit = baseTokenOutAmount - debtAmount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment