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

Added some more data fields for block collector #24

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
12 changes: 4 additions & 8 deletions crates/artemis-core/src/collectors/block_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ use async_trait::async_trait;
use ethers::{
prelude::Middleware,
providers::PubsubClient,
types::{H256, U64},
types::{Block, TxHash},
};
use std::sync::Arc;
use tokio_stream::StreamExt;

/// A collector that listens for new blocks, and generates a stream of
/// [events](NewBlock) which contain the block number and hash.
/// [events](NewBlock) which contain the entire block
pub struct BlockCollector<M> {
provider: Arc<M>,
}

/// A new block event, containing the block number and hash.
#[derive(Debug, Clone)]
pub struct NewBlock {
pub hash: H256,
pub number: U64,
pub block: Block<TxHash>,
}

impl<M> BlockCollector<M> {
Expand All @@ -39,10 +38,7 @@ where
{
async fn get_event_stream(&self) -> Result<CollectorStream<'_, NewBlock>> {
let stream = self.provider.subscribe_blocks().await?;
let stream = stream.filter_map(|block| match block.hash {
Some(hash) => block.number.map(|number| NewBlock { hash, number }),
None => None,
});
let stream = stream.filter_map(|block| Some(NewBlock { block }));
Ok(Box::pin(stream))
}
}
2 changes: 1 addition & 1 deletion crates/artemis-core/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async fn test_block_collector_sends_blocks() {
.await
.unwrap()
.unwrap();
assert_eq!(block_a.hash, block_b.hash.unwrap());
assert_eq!(block_a.block.hash.unwrap(), block_b.hash.unwrap());
}

/// Test that mempool collector correctly emits blocks.
Expand Down
12 changes: 9 additions & 3 deletions crates/strategies/opensea-sudo-arb/src/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,20 @@ impl<M: Middleware + 'static> OpenseaSudoArb<M> {

/// Process new block events, updating the internal state.
async fn process_new_block_event(&mut self, event: NewBlock) -> Result<()> {
info!("processing new block {}", event.number);
info!("processing new block {}", event.block.number.unwrap());
// Find new pools tthat were created in the last block.
let new_pools = self
.get_new_pools(event.number.as_u64(), event.number.as_u64())
.get_new_pools(
event.block.number.unwrap().as_u64(),
event.block.number.unwrap().as_u64(),
)
.await?;
// Find existing pools that were touched in the last block.
let touched_pools = self
.get_touched_pools(event.number.as_u64(), event.number.as_u64())
.get_touched_pools(
event.block.number.unwrap().as_u64(),
event.block.number.unwrap().as_u64(),
)
.await?;
// Get quotes for all new and touched pools and update state.
let quotes = self
Expand Down