Skip to content

Commit 87008bd

Browse files
authored
feat: bulk inserts (#3)
1 parent 4cbbb3e commit 87008bd

File tree

11 files changed

+876
-380
lines changed

11 files changed

+876
-380
lines changed

bin/snoop.rs

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::{thread, time};
1+
use std::{collections::HashMap, thread, time};
22

33
use alloy::sol_types::SolEvent;
44
use log::{info, LevelFilter};
55
use simple_logger::SimpleLogger;
66
use taya_snoop::{
77
configs::Config,
8-
db::Database,
8+
db::{Database, StorageCache},
99
handlers::{
1010
burn::{handle_burn, Burn},
1111
mint::{handle_mint, Mint},
@@ -81,12 +81,28 @@ async fn sync_chain(rpc: &Rpc, db: &Database, config: &Config) {
8181
None => return,
8282
};
8383

84-
let (mut factory, mut bundle) =
84+
handle_pairs(pair_logs, db, rpc).await;
85+
86+
let (factory, bundle) =
8587
tokio::join!(db.get_factory(), db.get_bundle());
8688

87-
handle_pairs(&mut factory, pair_logs, db, rpc).await;
89+
let mut cache = StorageCache {
90+
db: db.clone(),
91+
factory: factory.clone(),
92+
bundle: bundle.clone(),
93+
pairs: HashMap::new(),
94+
tokens: HashMap::new(),
95+
transactions: HashMap::new(),
96+
mints: HashMap::new(),
97+
swaps: HashMap::new(),
98+
burns: HashMap::new(),
99+
pairs_day_data: HashMap::new(),
100+
pairs_hour_data: HashMap::new(),
101+
tokens_day_data: HashMap::new(),
102+
dex_day_data: HashMap::new(),
103+
};
88104

89-
if !factory.pairs.is_empty() {
105+
if factory.pair_count != 0 {
90106
let pairs: Vec<String> = factory
91107
.pairs
92108
.clone()
@@ -95,7 +111,11 @@ async fn sync_chain(rpc: &Rpc, db: &Database, config: &Config) {
95111
.collect();
96112

97113
let mut event_logs = match rpc
98-
.get_pairs_logs_batch(&pairs, first_block, last_block)
114+
.get_pairs_logs_batch(
115+
&pairs,
116+
first_block as u64,
117+
last_block as u64,
118+
)
99119
.await
100120
{
101121
Some(logs) => logs,
@@ -116,72 +136,57 @@ async fn sync_chain(rpc: &Rpc, db: &Database, config: &Config) {
116136

117137
for log in event_logs {
118138
match log.topic0() {
119-
Some(topic_raw) => {
139+
Some(topic) => {
120140
let block_timestamp =
121141
log.block_timestamp.unwrap() as i32;
122142

123-
let topic = topic_raw.to_string();
124-
125-
if topic == Mint::SIGNATURE_HASH.to_string() {
143+
if topic.eq(&Mint::SIGNATURE_HASH) {
126144
handle_mint(
127145
log,
128146
block_timestamp,
129147
db,
130-
&mut factory,
131-
&bundle,
148+
&mut cache,
132149
)
133150
.await;
134151
count_mints += 1;
135-
} else if topic == Burn::SIGNATURE_HASH.to_string()
136-
{
152+
} else if topic.eq(&Burn::SIGNATURE_HASH) {
137153
handle_burn(
138154
log,
139155
block_timestamp,
140156
db,
141-
&mut factory,
142-
&bundle,
157+
&mut cache,
143158
)
144159
.await;
145160
count_burns += 1;
146-
} else if topic == Swap::SIGNATURE_HASH.to_string()
147-
{
161+
} else if topic.eq(&Swap::SIGNATURE_HASH) {
148162
handle_swap(
149163
log,
150164
block_timestamp,
151165
db,
152166
config,
153-
&mut factory,
154-
&bundle,
167+
&mut cache,
155168
)
156169
.await;
157170
count_swaps += 1;
158-
} else if topic == Sync::SIGNATURE_HASH.to_string()
159-
{
160-
handle_sync(
171+
} else if topic.eq(&Sync::SIGNATURE_HASH) {
172+
handle_sync(log, db, config, &mut cache).await;
173+
count_syncs += 1;
174+
} else if topic.eq(&Transfer::SIGNATURE_HASH) {
175+
handle_transfer(
161176
log,
177+
block_timestamp,
162178
db,
163-
config,
164-
&mut factory,
165-
&mut bundle,
179+
&mut cache,
166180
)
167181
.await;
168-
count_syncs += 1;
169-
} else if topic
170-
== Transfer::SIGNATURE_HASH.to_string()
171-
{
172-
handle_transfer(log, block_timestamp, db)
173-
.await;
174182
count_transfers += 1;
175183
}
176184
}
177185
None => continue,
178186
}
179187
}
180188

181-
tokio::join!(
182-
db.update_bundle(&bundle),
183-
db.update_factory(&factory)
184-
);
189+
cache.store().await;
185190

186191
info!("Procesed {} mints {} burns {} swaps {} sync and {} transfer events", count_mints, count_burns, count_swaps, count_syncs, count_transfers);
187192
}

docker-compose.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@ services:
2828
volumes:
2929
- ./data/db:/var/lib/postgresql/data
3030
restart: always
31-
ports:
32-
- 5432:5432
3331
env_file: "./.env"
3432

35-
indexer:
36-
container_name: indexer
33+
taya-indexer:
34+
container_name: taya-indexer
3735
build:
3836
context: .
3937
depends_on:

0 commit comments

Comments
 (0)