Skip to content

Commit

Permalink
feat(update-pools-in-route): update only pools visited in potential b…
Browse files Browse the repository at this point in the history
…undle
  • Loading branch information
janfabian committed Oct 16, 2023
1 parent ed69d6f commit ff0331f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
23 changes: 13 additions & 10 deletions skipper-py/src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ async def run(self):
backrun_list = self.querier.query_node_for_new_mempool_txs()
#print(f"{time.time()}: Found new transactions in mempool")
start = time.time()
# Set flag to update reserves once per potential backrun list
new_backrun_list = True
updated_pools = set[str]()
# Iterate through each tx and assess for profitable opportunities
for tx_str in backrun_list:
# Create a transaction object
Expand All @@ -158,14 +157,6 @@ async def run(self):
# If there are no swaps, continue
if not transaction.swaps:
continue
# Update reserves once per potential backrun list
if new_backrun_list:
print("Updating all reserves...")
start_update = time.time()
await self.state.update_all(jobs=self.state.update_all_reserves_jobs)
end_update = time.time()
logging.info(f"Time to update all reserves: {end_update - start_update}")
new_backrun_list = False
# Simulate the transaction on a copy of contract state
# and return the copied state post-transaction simulation
contracts_copy = self.state.simulate_transaction(transaction=transaction)
Expand All @@ -177,6 +168,18 @@ async def run(self):
# If there are no routes, continue
if not transaction.routes:
continue

# Update reserves once per potential backrun list
pools_in_routes = transaction.get_unique_pools_from_routes()
non_updated_pools = list(set(pools_in_routes).difference(updated_pools))
self.state.set_routes_jobs(non_updated_pools, self.querier)
print(f"Updating reserves of {len(self.state.update_route_reserves_jobs)} pools...")
start_update = time.time()
await self.state.update_all(jobs=self.state.update_route_reserves_jobs)
end_update = time.time()
logging.info(f"Time to update reserves: {end_update - start_update}")
updated_pools |= set(pools_in_routes)

# Build the most profitable bundle from
bidTx: Tx = self.build_most_profitable_bundle(
transaction=transaction,
Expand Down
10 changes: 10 additions & 0 deletions skipper-py/src/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class State:
update_all_tokens_jobs: list = field(default_factory=list)
update_all_reserves_jobs: list = field(default_factory=list)
update_all_fees_jobs: list = field(default_factory=list)
update_route_reserves_jobs: list = field(default_factory=list)

async def set_all_pool_contracts(self,
init_contracts: dict,
Expand Down Expand Up @@ -142,6 +143,15 @@ def set_all_jobs(self, querier: Querier) -> None:
querier)
for contract
in self.contracts.values()]

def set_routes_jobs(self, pools_in_route: list[str], querier: Querier) -> None:
self.update_route_reserves_jobs = [functools.partial(
contract.update_reserves,
querier)
for contract
in self.contracts.values()
if contract.contract_address in pools_in_route]


async def update_all(self, jobs: list) -> bool:
""" This function is used to update all the contracts
Expand Down
10 changes: 10 additions & 0 deletions skipper-py/src/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,13 @@ def add_route(self,
route.pools[i] = pool
# Add route to transaction
self.routes.append(route)

def get_unique_pools_from_routes(self) -> list[str]:
pool_addrs: set[str] = set()
for route in self.routes :
for pool in route.pools:
pool_addrs.add(pool.contract_address)

return list(pool_addrs)


0 comments on commit ff0331f

Please sign in to comment.