Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify and improve event-fetching #364

Merged
merged 5 commits into from
Feb 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 18 additions & 94 deletions run_blockchain_terraformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@
AERODROME_V2_NAME: 12,
}

EXCHANGE_POOL_CREATION_EVENT_NAMES = {
UNISWAP_V2_NAME: "PairCreated",
UNISWAP_V3_NAME: "PoolCreated",
AERODROME_V2_NAME: "PoolCreated",
VELOCIMETER_V2_NAME: "PairCreated",
SCALE_V2_NAME: "PairCreated",
}

dataframe_key = [
"cid",
"last_updated",
Expand Down Expand Up @@ -737,91 +745,13 @@ def organize_pool_details_solidly_v2(
return pool


def get_uni_pool_creation_events_v3(
factory_contract, block_number: int, web3: Web3, block_chunk_size=50000
) -> List:
"""
This function retrieves Uniswap V3 pool generation events

:param factory_contract: the initialized Factory contract
:param block_number: the block number from which to start
:param web3: the Web3 object
:param block_chunk_size: the number of blocks to fetch at a time
returns: a list of raw pool creation events
"""
current_block = web3.eth.block_number
events = []

for idx in range(int(math.ceil((current_block - block_number) / block_chunk_size))):
from_block = block_number + idx * block_chunk_size
to_block = (
from_block + block_chunk_size
if from_block + block_chunk_size < current_block
else current_block
)
events += factory_contract.events.PoolCreated.get_logs(
fromBlock=from_block, toBlock=to_block
)
return events


def get_uni_pool_creation_events_v2(
factory_contract, block_number: int, web3: Web3, block_chunk_size=50000
) -> List:
"""
This function retrieves Uniswap V2 pool generation events

:param factory_contract: the initialized Factory contract
:param block_number: the block number from which to start
:param web3: the Web3 object
:param block_chunk_size: the number of blocks to fetch at a time
returns: a list of raw pool creation events
"""
current_block = web3.eth.block_number
events = []
for idx in range(int(math.ceil((current_block - block_number) / block_chunk_size))):
from_block = block_number + idx * block_chunk_size
to_block = (
from_block + block_chunk_size
if from_block + block_chunk_size < current_block
else current_block
)
events += factory_contract.events.PairCreated.get_logs(
fromBlock=from_block, toBlock=to_block
)
return events


def get_solidly_pool_creation_events_v2(
exchange, factory_contract, block_number: int, web3: Web3, block_chunk_size=50000
) -> List:
"""
This function retrieves Solidly pool generation events

:param factory_contract: the initialized Factory contract
:param block_number: the block number from which to start
:param web3: the Web3 object
:param block_chunk_size: the number of blocks to fetch at a time
returns: a list of raw pool creation events
"""
current_block = web3.eth.block_number
events = []
for idx in range(int(math.ceil((current_block - block_number) / block_chunk_size))):
from_block = block_number + idx * block_chunk_size
to_block = (
from_block + block_chunk_size
if from_block + block_chunk_size < current_block
else current_block
)
if exchange in ["aerodrome_v2",]:
events += factory_contract.events.PoolCreated.get_logs(
fromBlock=from_block, toBlock=to_block
)
elif exchange in ["velocimeter_v2", "scale_v2"]:
events += factory_contract.events.PairCreated.get_logs(
fromBlock=from_block, toBlock=to_block
)
return events
def _get_events(contract, blockchain: str, exchange: str, start_block: int) -> list:
end_block = contract.w3.eth.block_number + 1
chunk_size = BLOCK_CHUNK_SIZE_MAP[blockchain]
block_numbers = list(range(start_block, end_block, chunk_size)) + [end_block]
event_method = contract.events[EXCHANGE_POOL_CREATION_EVENT_NAMES[exchange]].get_logs
events_list = [event_method(fromBlock=block_numbers[n-1], toBlock=block_numbers[n]-1) for n in range(1, len(block_numbers))]
return [event for events in events_list for event in events]


def get_uni_v3_pools(
Expand All @@ -844,9 +774,7 @@ def get_uni_v3_pools(

returns: a tuple containing a Dataframe of pool creation and a Dataframe of Uni V3 pool mappings
"""
pool_data = get_uni_pool_creation_events_v3(
factory_contract=factory_contract, block_number=start_block, web3=web3, block_chunk_size=BLOCK_CHUNK_SIZE_MAP[blockchain]
)
pool_data = _get_events(factory_contract, blockchain, exchange, start_block)

with parallel_backend(n_jobs=-1, backend="threading"):
pools = Parallel(n_jobs=-1)(
Expand Down Expand Up @@ -891,9 +819,7 @@ def get_uni_v2_pools(
:param blockchain: the blockchain name
returns: a tuple containing a Dataframe of pool creation and a Dataframe of Uni V3 pool mappings
"""
pool_data = get_uni_pool_creation_events_v2(
factory_contract=factory_contract, block_number=start_block, web3=web3, block_chunk_size=BLOCK_CHUNK_SIZE_MAP[blockchain]
)
pool_data = _get_events(factory_contract, blockchain, exchange, start_block)

with parallel_backend(n_jobs=-1, backend="threading"):
pools = Parallel(n_jobs=-1)(
Expand Down Expand Up @@ -938,9 +864,7 @@ def get_solidly_v2_pools(

returns: a tuple containing a Dataframe of pool creation and a Dataframe of Uni V3 pool mappings
"""
pool_data = get_solidly_pool_creation_events_v2(
exchange=exchange, factory_contract=factory_contract, block_number=start_block, web3=web3, block_chunk_size=BLOCK_CHUNK_SIZE_MAP[blockchain]
)
pool_data = _get_events(factory_contract, blockchain, exchange, start_block)

with parallel_backend(n_jobs=-1, backend="threading"):
pools = Parallel(n_jobs=-1)(
Expand Down
Loading