mev-inspect
Version:
A JS port of 'mev-inspect-py' optimised for ease of use.
102 lines • 3.27 kB
JavaScript
function getBlock(mev) {
return getEvent(mev)?.block || null;
}
function getTransaction(mev) {
return getEvent(mev)?.transaction || null;
}
function getEvent(mev) {
if (isArbitrage(mev)) {
const arbitrage = mev;
if (arbitrage.swaps.length === 0) {
return null;
}
return arbitrage.swaps[0];
}
if (isLiquidation(mev)) {
const liquidation = mev;
return liquidation.repayment;
}
if (isSandwich(mev)) {
const sandwich = mev;
return sandwich.frontSwap;
}
return null;
}
function isArbitrage(mev) {
return ('arbitrager' in mev &&
mev.swaps.every((swap) => swap.assetIn.type === 'erc20' && swap.assetOut.type === 'erc20'));
}
function isLiquidation(mev) {
return 'liquidator' in mev;
}
function isSandwich(mev) {
return 'frontSwap' in mev;
}
function isJitSandwich(mev) {
return 'deposit' in mev;
}
function isNftArbitrage(mev) {
return ('arbitrager' in mev &&
mev.swaps.some((swap) => swap.assetIn.type === 'erc721' || swap.assetOut.type === 'erc721'));
}
function getArbitrages(mevList) {
return mevList.filter((mev) => isArbitrage(mev));
}
function getLiquidations(mevList) {
return mevList.filter((mev) => isLiquidation(mev));
}
function getSandwiches(mevList) {
return mevList.filter((mev) => isSandwich(mev));
}
function getJitSandwiches(mevList) {
return mevList.filter((mev) => isJitSandwich(mev));
}
function getNftArbitrages(mevList) {
return mevList.filter((mev) => isNftArbitrage(mev));
}
function equalWithTolerance(firstValue, secondValue, threshold) {
// Special case: either of values is zero
if (firstValue === 0n || secondValue === 0n) {
if (firstValue === 0n && secondValue === 0n) {
return true;
}
if (firstValue === 0n) {
return false;
}
return threshold >= 1;
}
// Should be of the same sign
if (firstValue > 0 !== secondValue > 0) {
return false;
}
// Compare in absolute values
if (firstValue < 0n) {
firstValue = -firstValue;
secondValue = -secondValue;
}
// a * (1 + threshold) >= b >= a * (1 - threshold)
const divisionMultiplier = 1000000000n;
const thresholdMultiplier = Number(divisionMultiplier);
const rate = (divisionMultiplier * secondValue) / firstValue;
const isWithinLowerBound = threshold * thresholdMultiplier >= rate - divisionMultiplier;
const isWithinHigherBound = threshold * thresholdMultiplier >= divisionMultiplier - rate;
return isWithinLowerBound && isWithinHigherBound;
}
function minByAbs(a, b) {
const absA = a > 0 ? a : -a;
const absB = b > 0 ? b : -b;
return absA < absB ? a : b;
}
function groupBy(arr, filter) {
const grouped = {};
for (const item of arr) {
const key = filter(item);
if (!grouped[key]) {
grouped[key] = [];
}
grouped[key].push(item);
}
return grouped;
}
export { equalWithTolerance, getArbitrages, getBlock, getJitSandwiches, getLiquidations, getNftArbitrages, getSandwiches, getTransaction, groupBy, isArbitrage, isLiquidation, isSandwich, isJitSandwich, isNftArbitrage, minByAbs, };
//# sourceMappingURL=utils.js.map