Skip to content

Commit d1683fb

Browse files
feat: support optional block timestamp (#111)
1 parent cf3b98c commit d1683fb

File tree

7 files changed

+35
-13
lines changed

7 files changed

+35
-13
lines changed

cli/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ start_sophon:
4747
start_xenea:
4848
RUSTFLAGS='-C target-cpu=native' RUST_BACKTRACE='full' cargo run --release --features jemalloc -- start --path $(CURDIR)/../examples/xenea-bug graphql
4949
codegen_typings_lens:
50-
RUSTFLAGS='-C target-cpu=native' cargo run --release --features jemalloc -- codegen --path $(CURDIR)/../../lens-backend/crates/indexer typings
50+
cargo run -- codegen --path $(CURDIR)/../../lens-backend/crates/lens-indexer typings
5151
codegen_indexer_lens:
5252
RUSTFLAGS='-C target-cpu=native' cargo run --release --features jemalloc -- codegen --path $(CURDIR)/../../lens-backend/crates/indexer indexer
5353
start_uniswap_base:

core/src/event/callback_registry.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use tracing::{debug, error};
1414
use crate::{
1515
event::contract_setup::{ContractInformation, NetworkContract},
1616
indexer::start::ProcessedNetworkContract,
17+
provider::WrappedLog,
1718
};
1819

1920
pub type Decoder = Arc<dyn Fn(Vec<H256>, Bytes) -> Arc<dyn Any + Send + Sync> + Send + Sync>;
@@ -30,6 +31,7 @@ pub struct TxInformation {
3031
pub address: Address,
3132
pub block_hash: H256,
3233
pub block_number: U64,
34+
pub block_timestamp: Option<U256>,
3335
pub transaction_hash: H256,
3436
pub log_index: U256,
3537
pub transaction_index: U64,
@@ -52,20 +54,21 @@ pub struct EventResult {
5254
impl EventResult {
5355
pub fn new(
5456
network_contract: Arc<NetworkContract>,
55-
log: Log,
57+
log: WrappedLog,
5658
start_block: U64,
5759
end_block: U64,
5860
) -> Self {
59-
let log_meta = LogMeta::from(&log);
60-
let log_address = log.address;
61+
let log_meta = LogMeta::from(&log.inner);
62+
let log_address = log.inner.address;
6163
Self {
62-
log: log.clone(),
63-
decoded_data: network_contract.decode_log(log),
64+
log: log.inner.clone(),
65+
decoded_data: network_contract.decode_log(log.inner),
6466
tx_information: TxInformation {
6567
network: network_contract.network.to_string(),
6668
address: log_address,
6769
block_hash: log_meta.block_hash,
6870
block_number: log_meta.block_number,
71+
block_timestamp: log.block_timestamp,
6972
transaction_hash: log_meta.transaction_hash,
7073
transaction_index: log_meta.transaction_index,
7174
log_index: log_meta.log_index,

core/src/indexer/fetch_logs.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{error::Error, str::FromStr, sync::Arc, time::Duration};
33
use ethers::{
44
addressbook::Address,
55
middleware::MiddlewareError,
6-
prelude::{BlockNumber, JsonRpcError, Log, ValueOrArray, H256, U64},
6+
prelude::{BlockNumber, JsonRpcError, ValueOrArray, H256, U64},
77
};
88
use regex::Regex;
99
use tokio::{
@@ -16,11 +16,11 @@ use tracing::{debug, error, info, warn};
1616
use crate::{
1717
event::{config::EventProcessingConfig, RindexerEventFilter},
1818
indexer::{log_helpers::is_relevant_block, IndexingEventProgressStatus},
19-
provider::JsonRpcCachedProvider,
19+
provider::{JsonRpcCachedProvider, WrappedLog},
2020
};
2121

2222
pub struct FetchLogsResult {
23-
pub logs: Vec<Log>,
23+
pub logs: Vec<WrappedLog>,
2424
pub from_block: U64,
2525
pub to_block: U64,
2626
}
@@ -243,6 +243,7 @@ async fn fetch_historic_logs_stream(
243243

244244
if let Some(last_log) = last_log {
245245
let next_from_block = last_log
246+
.inner
246247
.block_number
247248
.expect("block number should always be present in a log") +
248249
U64::from(1);
@@ -466,7 +467,9 @@ async fn live_indexing_stream(
466467
to_block
467468
);
468469
} else if let Some(last_log) = last_log {
469-
if let Some(last_log_block_number) = last_log.block_number {
470+
if let Some(last_log_block_number) =
471+
last_log.inner.block_number
472+
{
470473
current_filter = current_filter.set_from_block(
471474
last_log_block_number + U64::from(1),
472475
);

core/src/indexer/no_code.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ fn no_code_callback(params: Arc<NoCodeCallbackParams>) -> EventCallbackType {
241241
block_hash,
242242
block_number,
243243
transaction_hash,
244+
block_timestamp: None,
244245
log_index,
245246
transaction_index,
246247
},

core/src/indexer/process.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ async fn live_indexing_for_contract_event_dependencies<'a>(
409409
);
410410
} else if let Some(last_log) = last_log {
411411
if let Some(last_log_block_number) =
412-
last_log.block_number
412+
last_log.inner.block_number
413413
{
414414
ordering_live_indexing_details.filter =
415415
ordering_live_indexing_details

core/src/provider.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use ethers::{
1010
types::{Block, BlockNumber, H256, U256, U64},
1111
};
1212
use reqwest::header::HeaderMap;
13+
use serde::{Deserialize, Serialize};
1314
use thiserror::Error;
1415
use tokio::sync::Mutex;
1516
use url::Url;
@@ -23,6 +24,16 @@ pub struct JsonRpcCachedProvider {
2324
pub max_block_range: Option<U64>,
2425
}
2526

27+
/// TODO: This is a temporary type until we migrate to alloy
28+
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
29+
pub struct WrappedLog {
30+
#[serde(flatten)]
31+
pub inner: Log,
32+
#[serde(rename = "blockTimestamp")]
33+
#[serde(skip_serializing_if = "Option::is_none")]
34+
pub block_timestamp: Option<U256>,
35+
}
36+
2637
impl JsonRpcCachedProvider {
2738
pub fn new(provider: Provider<RetryClient<Http>>, max_block_range: Option<U64>) -> Self {
2839
JsonRpcCachedProvider {
@@ -58,8 +69,11 @@ impl JsonRpcCachedProvider {
5869
self.provider.get_block_number().await
5970
}
6071

61-
pub async fn get_logs(&self, filter: &RindexerEventFilter) -> Result<Vec<Log>, ProviderError> {
62-
self.provider.get_logs(filter.raw_filter()).await
72+
pub async fn get_logs(
73+
&self,
74+
filter: &RindexerEventFilter,
75+
) -> Result<Vec<WrappedLog>, ProviderError> {
76+
self.provider.request("eth_getLogs", [filter.raw_filter()]).await
6377
}
6478

6579
pub async fn get_chain_id(&self) -> Result<U256, ProviderError> {

documentation/docs/pages/docs/start-building/rust-project-deep-dive/indexers.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ pub struct TxInformation {
272272
pub address: Address,
273273
pub block_hash: H256,
274274
pub block_number: U64,
275+
pub block_timestamp: Option<U256>,
275276
pub transaction_hash: H256,
276277
pub log_index: U256,
277278
pub transaction_index: U64,

0 commit comments

Comments
 (0)