Skip to content

Commit

Permalink
Use Pool Created Events to verify VIP Uni v2 Events
Browse files Browse the repository at this point in the history
  • Loading branch information
Ktl-XV committed Jun 26, 2023
1 parent 3a3c29d commit 991ec9b
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 77 deletions.
26 changes: 13 additions & 13 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,31 @@ services:
FEAT_UNISWAP_V2_VIP_SWAP_EVENT: "true"
UNISWAP_V2_VIP_SWAP_SOURCES: "UniswapV2,SushiSwap"
UNISWAP_V2_VIP_SWAP_START_BLOCK: 10917104
FEAT_UNISWAP_V3_VIP_SWAP_EVENT: "true"
FEAT_UNISWAP_V3_VIP_SWAP_EVENT: "false"
UNISWAP_V3_VIP_SWAP_START_BLOCK: 12553659
FEAT_UNISWAP_V3_SWAP_EVENT: "true"
FEAT_UNISWAP_V3_SWAP_EVENT: "false"
UNISWAP_V3_SWAP_START_BLOCK: 16670838
FEAT_LIMIT_ORDERS: "true"
FEAT_LIMIT_ORDERS: "false"
V4_NATIVE_FILL_START_BLOCK: "11591021"
FEAT_PLP_SWAP_EVENT: "true"
FEAT_PLP_SWAP_EVENT: "false"
PLP_VIP_START_BLOCK: 11377457
FEAT_OTC_ORDERS: "true"
FEAT_OTC_ORDERS: "false"
OTC_ORDERS_FEATURE_START_BLOCK: 13143075
FEAT_CANCEL_EVENTS: "true"
FEAT_STAKING: "true"
FEAT_CANCEL_EVENTS: "false"
FEAT_STAKING: "false"
STAKING_DEPLOYMENT_BLOCK: 8952581
FEAT_RFQ_EVENT: "true"
FEAT_V3_NATIVE_FILL: "true"
FEAT_ERC20_BRIDGE_TRANSFER_FLASHWALLET: "true"
FEAT_RFQ_EVENT: "false"
FEAT_V3_NATIVE_FILL: "false"
FEAT_ERC20_BRIDGE_TRANSFER_FLASHWALLET: "false"
FLASHWALLET_ADDRESS: "0x22f9dcf4647084d6c31b2765f6910cd85c178c18"
FLASHWALLET_DEPLOYMENT_BLOCK: 12231666
FEAT_NFT: "true"
FEAT_NFT: "false"
NFT_FEATURE_START_BLOCK: 14258205
FEAT_UNISWAP_V2_PAIR_CREATED_EVENT: "true"
UNISWAP_V2_PAIR_CREATED_PROTOCOL_CONTRACT_ADDRESSES_AND_START_BLOCKS: "UniswapV2:0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f:10000835,SushiSwap:0xc0aee478e3658e2610c5f7a4a2e1777ce9e4f2ac:10794229"
FEAT_UNISWAP_V2_SYNC_EVENT: "true"
FEAT_UNISWAP_V2_SYNC_EVENT: "false"
UNISWAP_V2_SYNC_START_BLOCK: 10000835
FEAT_ONCHAIN_GOVERNANCE: "true"
FEAT_ONCHAIN_GOVERNANCE: "false"
ONCHAIN_GOVERNANCE_START_BLOCK: 16990159

event-pipeline-bsc:
Expand Down
37 changes: 37 additions & 0 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ import {
} from './parsers/events/onchain_governance_events';

import { TokenMetadataMap } from './scripts/utils/web3_utils';
import { UniV2PoolSingleton } from './uniV2PoolSingleton';

function uniV2PoolSingletonCallback(pools: UniswapV2PairCreatedEvent[]) {
const uniV2PoolSingleton = UniV2PoolSingleton.getInstance();
uniV2PoolSingleton.addNewPools(pools);
}

export type CommonEventParams = {
connection: Connection;
Expand All @@ -156,6 +162,7 @@ export type EventScraperProps = {
parser: (decodedLog: RawLogEntry) => any;
deleteOptions: DeleteOptions;
tokenMetadataMap: TokenMetadataMap;
callback: any | null;
};

export const eventScrperProps: EventScraperProps[] = [
Expand All @@ -170,6 +177,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseTransformedERC20Event,
deleteOptions: {},
tokenMetadataMap: null,
callback: null,
},
{
enabled: FEAT_UNISWAP_V3_VIP_SWAP_EVENT,
Expand All @@ -182,6 +190,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseUniswapV3VIPSwapEvent,
deleteOptions: { isDirectTrade: true, directProtocol: ['UniswapV3'] },
tokenMetadataMap: { tokenA: 'fromToken', tokenB: 'toToken' },
callback: null,
},
{
enabled: FEAT_ERC20_BRIDGE_TRANSFER_FLASHWALLET,
Expand All @@ -194,6 +203,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseBridgeFill,
deleteOptions: { isDirectTrade: false },
tokenMetadataMap: { tokenA: 'fromToken', tokenB: 'toToken' },
callback: null,
},
{
enabled: FEAT_UNISWAP_V2_VIP_SWAP_EVENT,
Expand All @@ -206,6 +216,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseUniswapV2SwapEvent,
deleteOptions: { isDirectTrade: true, directProtocol: UNISWAP_V2_VIP_SWAP_SOURCES },
tokenMetadataMap: { tokenA: 'fromToken', tokenB: 'toToken' },
callback: null,
},
{
enabled: FEAT_UNISWAP_V3_VIP_SWAP_EVENT,
Expand All @@ -218,6 +229,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseUniswapV3SwapEvent,
deleteOptions: { isDirectTrade: true, directProtocol: ['UniswapV3'] },
tokenMetadataMap: { tokenA: 'fromToken', tokenB: 'toToken' },
callback: null,
},
{
enabled: FEAT_RFQ_EVENT,
Expand All @@ -230,6 +242,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseV4RfqOrderFilledEvent,
deleteOptions: {},
tokenMetadataMap: { tokenA: 'makerToken', tokenB: 'takerToken' },
callback: null,
},
{
enabled: FEAT_RFQ_EVENT,
Expand All @@ -242,6 +255,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseNativeFillFromV4RfqOrderFilledEvent,
deleteOptions: { protocolVersion: 'v4', nativeOrderType: 'RFQ Order' },
tokenMetadataMap: null,
callback: null,
},
{
enabled: FEAT_RFQ_EVENT,
Expand All @@ -254,6 +268,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseExpiredRfqOrderEvent,
deleteOptions: {},
tokenMetadataMap: null,
callback: null,
},
{
enabled: FEAT_LIMIT_ORDERS,
Expand All @@ -266,6 +281,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseV4LimitOrderFilledEvent,
deleteOptions: {},
tokenMetadataMap: { tokenA: 'makerToken', tokenB: 'takerToken' },
callback: null,
},
{
enabled: FEAT_LIMIT_ORDERS,
Expand All @@ -278,6 +294,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseNativeFillFromV4LimitOrderFilledEvent,
deleteOptions: { protocolVersion: 'v4', nativeOrderType: 'Limit Order' },
tokenMetadataMap: null,
callback: null,
},
{
enabled: FEAT_RFQ_EVENT || FEAT_LIMIT_ORDERS,
Expand All @@ -290,6 +307,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseV4CancelEvent,
deleteOptions: {},
tokenMetadataMap: null,
callback: null,
},
{
enabled: FEAT_OTC_ORDERS,
Expand All @@ -302,6 +320,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseOtcOrderFilledEvent,
deleteOptions: {},
tokenMetadataMap: { tokenA: 'makerTokenAddress', tokenB: 'takerTokenAddress' },
callback: null,
},
{
enabled: FEAT_OTC_ORDERS,
Expand All @@ -314,6 +333,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseNativeFillFromV4OtcOrderFilledEvent,
deleteOptions: { protocolVersion: 'v4', nativeOrderType: 'OTC Order' },
tokenMetadataMap: null,
callback: null,
},
{
enabled: FEAT_V3_FILL_EVENT,
Expand All @@ -326,6 +346,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseFillEvent,
deleteOptions: {},
tokenMetadataMap: { tokenA: 'makerTokenAddress', tokenB: 'takerTokenAddress' },
callback: null,
},
{
enabled: FEAT_V3_NATIVE_FILL,
Expand All @@ -338,6 +359,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseNativeFillFromFillEvent,
deleteOptions: { protocolVersion: 'v3' },
tokenMetadataMap: null,
callback: null,
},
{
enabled: FEAT_NFT,
Expand All @@ -350,6 +372,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseErc721OrderFilledEvent,
deleteOptions: {},
tokenMetadataMap: { tokenA: 'erc20Token', tokenB: 'erc721Token' },
callback: null,
},
{
enabled: FEAT_NFT,
Expand All @@ -362,6 +385,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseErc721OrderCancelledEvent,
deleteOptions: {},
tokenMetadataMap: null,
callback: null,
},
{
enabled: FEAT_NFT,
Expand All @@ -374,6 +398,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseErc721OrderPresignedEvent,
deleteOptions: {},
tokenMetadataMap: { tokenA: 'erc20Token', tokenB: 'erc721Token' },
callback: null,
},
{
enabled: FEAT_NFT,
Expand All @@ -386,6 +411,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseErc1155OrderFilledEvent,
deleteOptions: {},
tokenMetadataMap: { tokenA: 'erc20Token', tokenB: 'erc1155Token' },
callback: null,
},
{
enabled: FEAT_NFT,
Expand All @@ -398,6 +424,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseErc1155OrderCancelledEvent,
deleteOptions: {},
tokenMetadataMap: null,
callback: null,
},
{
enabled: FEAT_NFT,
Expand All @@ -410,6 +437,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseErc1155OrderPresignedEvent,
deleteOptions: {},
tokenMetadataMap: { tokenA: 'erc20Token', tokenB: 'erc1155Token' },
callback: null,
},
{
enabled: FEAT_UNISWAP_V2_SYNC_EVENT,
Expand All @@ -422,6 +450,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseUniswapV2SyncEvent,
deleteOptions: {},
tokenMetadataMap: null,
callback: null,
},
{
enabled: FEAT_META_TRANSACTION_EXECUTED_EVENT,
Expand All @@ -434,6 +463,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseMetaTransactionExecutedEvent,
deleteOptions: {},
tokenMetadataMap: null,
callback: null,
},
{
enabled: FEAT_UNISWAP_V3_SWAP_EVENT,
Expand All @@ -446,6 +476,7 @@ export const eventScrperProps: EventScraperProps[] = [
parser: parseUniswapV3SwapEvent,
deleteOptions: {},
tokenMetadataMap: null,
callback: null,
},
{
enabled: FEAT_ONCHAIN_GOVERNANCE,
Expand All @@ -459,6 +490,7 @@ export const eventScrperProps: EventScraperProps[] = [
parseOnchainGovernanceProposalCreatedEvent(decodedLog, 'ZeroexTreasuryGovernor'),
deleteOptions: {},
tokenMetadataMap: null,
callback: null,
},
{
enabled: FEAT_ONCHAIN_GOVERNANCE,
Expand All @@ -473,6 +505,7 @@ export const eventScrperProps: EventScraperProps[] = [
parseOnchainGovernanceProposalCreatedEvent(decodedLog, 'ZeroexProtocolGovernor'),
deleteOptions: {},
tokenMetadataMap: null,
callback: null,
},
{
enabled: FEAT_ONCHAIN_GOVERNANCE,
Expand All @@ -486,6 +519,7 @@ export const eventScrperProps: EventScraperProps[] = [
parseOnchainGovernanceCallScheduledEvent(decodedLog, 'TreasuryZeroexTimelock'),
deleteOptions: {},
tokenMetadataMap: null,
callback: null,
},
{
enabled: FEAT_ONCHAIN_GOVERNANCE,
Expand All @@ -499,6 +533,7 @@ export const eventScrperProps: EventScraperProps[] = [
parseOnchainGovernanceCallScheduledEvent(decodedLog, 'ProtocolZeroexTimelock'),
deleteOptions: {},
tokenMetadataMap: null,
callback: null,
},
];

Expand All @@ -519,6 +554,7 @@ for (const payment_recipient of POLYGON_RFQM_PAYMENTS_ADDRESSES) {
parser: parseLogTransferEvent,
deleteOptions: { recipient: payment_recipient },
tokenMetadataMap: null,
callback: null,
});
}

Expand All @@ -534,6 +570,7 @@ for (const protocol of UNISWAP_V2_PAIR_CREATED_PROTOCOL_CONTRACT_ADDRESSES_AND_S
parser: (decodedLog: RawLogEntry) => parseUniswapV2PairCreatedEvent(decodedLog, protocol.name),
deleteOptions: { protocol: protocol.name },
tokenMetadataMap: { tokenA: 'token0', tokenB: 'token1' },
callback: uniV2PoolSingletonCallback,
});
}

Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { ChainIdChecker } from './scripts/check_chain_id';
import { CurrentBlockMonitor } from './scripts/monitor_current_block';
import { startMetricsServer } from './utils/metrics';
import { TokenMetadataSingleton } from './tokenMetadataSingleton';
import { UniV2PoolSingleton } from './uniV2PoolSingleton';

const kafka = new Kafka({
clientId: 'event-pipeline',
Expand Down Expand Up @@ -69,6 +70,11 @@ createConnection(ormConfig as ConnectionOptions)
.then(async (connection) => {
await producer.connect();
await TokenMetadataSingleton.getInstance(connection, producer);
await UniV2PoolSingleton.initInstance(connection);

const uniV2PoolSingleton = UniV2PoolSingleton.getInstance();
const poolInfo = uniV2PoolSingleton.getPool('0x1bb1d4ad0a83cbf44f111bb0688c78aa94f4255e');

schedule(null, null, currentBlockMonitor.monitor, 'Current Block');

schedule(connection, producer, blockScraper.getParseSaveEventsAsync, 'Pull and Save Blocks');
Expand Down
24 changes: 19 additions & 5 deletions src/parsers/events/uniswap_v2_events.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const abiCoder = require('web3-eth-abi');
import { RawLogEntry } from 'ethereum-types';
import { logger } from '../../utils/logger';
import { ERC20BridgeTransferEvent, UniswapV2PairCreatedEvent, UniswapV2SyncEvent } from '../../entities';

import { parseEvent } from './parse_event';
import { UNISWAP_V2_SWAP_ABI, UNISWAP_V2_SYNC_ABI, UNISWAP_V2_PAIR_CREATED_ABI } from '../../constants';
import { BigNumber } from '@0x/utils';
import { UniV2PoolSingleton } from '../../uniV2PoolSingleton';

export function parseUniswapV2SwapEvent(eventLog: RawLogEntry): ERC20BridgeTransferEvent {
export function parseUniswapV2SwapEvent(eventLog: RawLogEntry): ERC20BridgeTransferEvent | null {
const eRC20BridgeTransferEvent = new ERC20BridgeTransferEvent();
parseEvent(eventLog, eRC20BridgeTransferEvent);
// decode the basic info directly into eRC20BridgeTransferEvent
Expand All @@ -15,24 +17,36 @@ export function parseUniswapV2SwapEvent(eventLog: RawLogEntry): ERC20BridgeTrans
eventLog.topics[2],
]);

const uniV2PoolSingleton = UniV2PoolSingleton.getInstance();

const poolInfo = uniV2PoolSingleton.getPool(eRC20BridgeTransferEvent.contractAddress);

if (poolInfo === undefined) {
logger.error(
`Got a Uni v2 VIP trade from an unknown pool, ignoring. Tx: ${eRC20BridgeTransferEvent.transactionHash}, Pool: ${eRC20BridgeTransferEvent.contractAddress}`,
);
return null;
}
const { token0, token1, protocol } = poolInfo;

const amount0In = new BigNumber(decodedLog.amount0In);
const amount1In = new BigNumber(decodedLog.amount1In);
const amount0Out = new BigNumber(decodedLog.amount0Out);
const amount1Out = new BigNumber(decodedLog.amount1Out);

eRC20BridgeTransferEvent.fromToken = amount0In.gt(amount0Out) ? '0' : '1'; // taker_token
eRC20BridgeTransferEvent.toToken = amount0In.gt(amount0Out) ? '1' : '0'; // maker_token
eRC20BridgeTransferEvent.fromToken = amount0In.gt(amount0Out) ? token0 : token1; // taker_token
eRC20BridgeTransferEvent.toToken = amount0In.gt(amount0Out) ? token1 : token0; // maker_token

eRC20BridgeTransferEvent.fromTokenAmount = new BigNumber(
amount0In.gt(amount0Out) ? amount0In.minus(amount0Out) : amount1In.minus(amount1Out),
); // taker_token_amount
eRC20BridgeTransferEvent.toTokenAmount = new BigNumber(
amount0In.gt(amount0Out) ? amount1Out.minus(amount1In) : amount0Out.minus(amount0In),
); // maker_token_amount
eRC20BridgeTransferEvent.from = ''; // maker
eRC20BridgeTransferEvent.from = protocol; // maker TODO(jorge): Replace with pool address, after checking downstream impact
eRC20BridgeTransferEvent.to = decodedLog.to.toLowerCase(); // taker
eRC20BridgeTransferEvent.directFlag = true;
eRC20BridgeTransferEvent.directProtocol = '';
eRC20BridgeTransferEvent.directProtocol = protocol;

return eRC20BridgeTransferEvent;
}
Expand Down
1 change: 1 addition & 0 deletions src/scripts/backfill_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class EventsBackfillScraper {
props.parser,
props.deleteOptions,
props.tokenMetadataMap,
props.callback,
backfillEventsOldestBlock.get(props.name)!,
)
.then(async ({ transactionHashes, startBlockNumber, endBlockNumber }) => {
Expand Down
1 change: 1 addition & 0 deletions src/scripts/pull_and_save_events_by_topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class EventsByTopicScraper {
props.parser,
props.deleteOptions,
props.tokenMetadataMap,
props.callback,
),
);
}
Expand Down
Loading

0 comments on commit 991ec9b

Please sign in to comment.