mev-inspect
Version:
A JS port of 'mev-inspect-py' optimised for ease of use.
122 lines • 3.59 kB
JavaScript
import { Coder } from 'abi-coder';
import erc20Abi from '../../abi/erc20.js';
import exchangeAbi from '../../abi/looksRareV1.js';
function isValid(event) {
return event.name === 'TakerAsk' || event.name === 'TakerBid';
}
function getPoolCalls() {
return [];
}
function processPoolCalls(_results, address) {
return {
factoryAddress: address.toLowerCase(),
asset: '',
collection: '',
metadata: {},
};
}
function parse(pool, event, _chainId, allLogs) {
const { name, values, transactionFrom, transactionHash: hash, transactionIndex, gasUsed, logIndex, address, blockHash, blockNumber, } = event;
const maker = values.maker.toLowerCase();
const taker = values.taker.toLowerCase();
const currency = values.currency.toLowerCase();
const collection = values.collection.toLowerCase();
const tokenId = values.tokenId;
const amount = values.amount;
const price = values.price;
const txLogs = allLogs.filter((log) => log.transactionHash === hash);
const erc20Amount = name === 'TakerAsk'
? getAmount(logIndex, maker, taker, currency, txLogs)
: price;
if (amount > 1) {
return null;
}
const from = taker;
const to = taker;
return {
contract: {
address: pool.address,
protocol: {
abi: 'LooksRareV1',
factory: pool.factory,
},
},
block: {
hash: blockHash,
number: blockNumber,
},
transaction: {
from: transactionFrom.toLowerCase(),
hash,
index: transactionIndex,
gasUsed,
},
event: {
address: address.toLowerCase(),
logIndex,
},
from,
to,
assetIn: name === 'TakerAsk'
? {
type: 'erc721',
collection,
id: tokenId,
}
: {
type: 'erc20',
address: currency,
},
amountIn: name === 'TakerAsk' ? 1n : erc20Amount,
assetOut: name === 'TakerAsk'
? {
type: 'erc20',
address: currency,
}
: {
type: 'erc721',
collection,
id: tokenId,
},
amountOut: name === 'TakerAsk' ? erc20Amount : 1n,
};
}
function getAmount(swapLogIndex, maker, taker, currency, logs) {
const currencyLogs = logs.filter((log) => log.address.toLowerCase() === currency && log.logIndex < swapLogIndex);
currencyLogs.reverse();
const nftCoder = new Coder(erc20Abi);
for (const log of currencyLogs) {
try {
const event = nftCoder.decodeEvent(log.topics, log.data);
if (event.name !== 'Transfer') {
continue;
}
const from = event.values.from.toLowerCase();
const to = event.values.to.toLowerCase();
const value = event.values.value;
if (maker !== from || taker !== to) {
continue;
}
return value;
}
catch (e) {
continue;
}
}
return 0n;
}
const CLASSIFIER = {
nftSwap: {
type: 'nft_swap',
protocol: 'LooksRareV1',
abi: exchangeAbi,
isValid,
parse,
pool: {
getCalls: getPoolCalls,
processCalls: processPoolCalls,
},
},
};
export default CLASSIFIER;
//# sourceMappingURL=looksRareV1.js.map