Skip to content

Commit

Permalink
Merge pull request #35 from skip-mev/feat/update-pools-in-route
Browse files Browse the repository at this point in the history
feat(update-pools-in-route): update only pools visited in potential bundle
  • Loading branch information
NotJeremyLiu authored Oct 20, 2023
2 parents ed69d6f + 7816544 commit 36aead0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
30 changes: 19 additions & 11 deletions skipper-py/src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ 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
# Iterate through each tx and assess for profitable opportunities
pools_to_update = set[str]()
transactions_with_contracts = list[tuple[Transaction, dict[str, Pool]]]()

# Iterate through each tx
for tx_str in backrun_list:
# Create a transaction object
tx_hash = sha256(b64decode(tx_str)).digest().hex()
Expand All @@ -158,14 +159,7 @@ 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 +171,20 @@ async def run(self):
# If there are no routes, continue
if not transaction.routes:
continue

pools_in_routes = transaction.get_unique_pools_from_routes()
pools_to_update |= set(pools_in_routes)
transactions_with_contracts.append((transaction, contracts_copy))

self.state.set_routes_jobs(list(pools_to_update), 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}")

# Iterate through each profitable opportunities
for (transaction, contracts_copy) in transactions_with_contracts:
# 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 36aead0

Please sign in to comment.