dexther_lib 0.0.261

Dexther tools for the platform.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
use ethers::types::{TransactionReceipt, H256, U256, U64};
use serde::Deserialize;
use serde::Serialize;
use std::collections::HashMap;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TokenInfo {
    pub address: String,
    pub name: String,
    pub symbol: String,
    pub decimals: String,
    pub owner: String,
    pub total_supply: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PairInfo {
    pub address: String,
    pub name: String,
    pub symbol: String,
    pub v3: Option<bool>,
    pub factory: String,
    pub tickSpacing: Option<i32>,
    pub fee: Option<i32>    
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PairInfoPub {
    pub token0: TokenInfo,
    pub token1: TokenInfo,
    pub pair: PairInfo,
    pub block_number: String,
    pub transaction_hash: String,
    pub log_index: String,
    pub _id: String,
    pub deployer: String,
    pub factory: String,
    pub v3: Option<bool>,
    pub tickSpacing: Option<i32>,
    pub fee: Option<i32>    
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PairCreated {
    pub token0: String,
    pub token1: String,
    pub pair: String,
    pub block_number: String,
    pub transaction_hash: String,
    pub transaction_index: String,
    pub timestamp: String,
    pub deployer: String,
    pub factory: String,
    pub v3: Option<bool>,
    pub tickSpacing: Option<i32>,
    pub fee: Option<i32>    
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MintTransaction {
    pub _id: String,
    pub event_type: String,
    pub sender: String,
    pub amount_0: String,
    pub amount_1: String,
    pub pair: String,
    pub block_number: String,
    pub block_hash: String,
    pub tx_hash: String,
    pub log_index: String,
    pub tx_from: String,
    pub tx_to: Option<String>,
    pub v3: Option<bool>,
    pub tick_lower: Option<String>,
    pub tick_upper: Option<String>,}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BurnTransaction {
    pub _id: String,
    pub event_type: String,
    pub sender: String,
    pub to: String,
    pub amount_0: String,
    pub amount_1: String,
    pub pair: String,
    pub block_number: String,
    pub block_hash: String,
    pub tx_hash: String,
    pub log_index: String,
    pub tx_from: String,
    pub tx_to: Option<String>,
    pub v3: Option<bool>,
    pub tick_lower: Option<String>,
    pub tick_upper: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SwapTransaction {
    pub _id: String,
    pub event_type: String,
    pub sender: String,
    pub to: String,
    pub amount_0_in: String,
    pub amount_1_in: String,
    pub amount_0_out: String,
    pub amount_1_out: String,
    pub block_hash: String,
    pub pair: String,
    pub block_number: String,
    pub tx_hash: String,
    pub timestamp: String,
    pub log_index: String,
    pub tx_from: String,
    pub tx_to: Option<String>,
    pub v3: Option<bool>,
    pub sqrt_price_x96: Option<String>,
    pub liquidity_v3: Option<String>,
    pub tick: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Block {
    pub _id: String,
    pub block_hash: String,
    pub number: String,
    pub timestamp: String,
    pub gas_used: String,
    pub gas_limit: String,
    pub size: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenTransfer {
    pub _id: String,
    pub event_type: String,
    pub from: String,
    pub to: String,
    pub value: String,
    pub address: String,
    pub block_number: String,
    pub block_hash: String,
    pub tx_hash: String,
    pub log_index: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenState {
    pub total_supply: String,
    pub circ_supply: String,
    pub decimals: i64,
    pub address: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenStatePub {
    pub total_supply: String,
    pub circ_supply: String,
    pub token_address: String,
    pub block_number: i64,
    pub chain_id: u8,
}

impl TokenState {
    pub fn to_pub(&mut self, chain_id: u8, block_number: i64, address: String) -> TokenStatePub {
        TokenStatePub {
            total_supply: self.total_supply.clone(),
            circ_supply: self.circ_supply.clone(),
            token_address: address,
            block_number: block_number,
            chain_id: chain_id,
        }
    }
}

impl Default for TokenState {
    fn default() -> Self {
        return TokenState {
            address: "".to_string(),
            decimals: 0,
            total_supply: "0".to_string(),
            circ_supply: "0".to_string(),
        };
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnrichedTransaction {
    pub tx_hash: String,
    pub log_index: String,
    pub block_number: String,
    pub chain_id: String,
    pub pair_address: String,
    pub amount0_in: String,
    pub amount1_in: String,
    pub amount0_out: String,
    pub amount1_out: String,
    pub event_type: String,
    pub token0_price_usd: String,
    pub token1_price_usd: String,
    pub transaction_price: String,
    pub token0_amount: String,
    pub token1_amount: String,
    pub transaction_type: Option<String>,
    pub from: String,
    pub to: Option<String>,
    pub maker: String,
    pub tx_from: String,
    pub tx_to: Option<String>,
    pub block_timestamp: String,
    pub v3: Option<bool>,
    pub sqrt_price_x96: Option<String>,
    pub liquidity_v3: Option<String>,
    pub tick: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PairStatePub {
    pub pair_address: String,
    pub chain_id: u8,
    pub block_number: u64,
    pub block_timestamp: String,
    pub total_volume: f64,
    pub total_liquidity: f64,
    pub pooled_token0: String,
    pub pooled_token1: String,
    pub token0_decimals: u8,
    pub token1_decimals: u8,
    pub token0_symbol: String,
    pub token1_symbol: String,
    pub token0_address: String,
    pub token1_address: String,
    pub token0_price_in_token1: f64,
    pub token1_price_in_token0: f64,
    pub total_buys: u64,
    pub total_sells: u64,
    pub total_txns: u64,
    pub token0_price_usd: f64,
    pub token1_price_usd: f64,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct BlockPub {
    pub number: U64,
    pub difficulty: Option<U256>,
    pub hash: H256,
    pub timestamp: U256,
    pub size: Option<U256>,
    pub gas_used: Option<U256>,
    pub gas_limit: Option<U256>,
    pub receipts: Vec<TransactionReceipt>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Lock {
    pub pair_address: String,
    pub user: Option<String>,
    pub amount: String,
    pub lock_date: Option<String>,
    pub unlock_date: String,
    pub lock_address: String,
    pub log_index: String,
    pub block_number: String,
    pub chain_id: String,
    pub tx_hash: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OwnershipTransferred {
    pub tx_hash: String,
    pub log_index: String,
    pub block_number: String,
    pub maker: String,
    pub previous_owner: String,
    pub new_owner: String,
    pub token_address: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WindowMetricsPub {
    pub block_number: u8,
    pub pair_address: String,
    pub chain_id: i32,
    pub metrics: (
        Option<WindowMetrics>,
        Option<WindowMetrics>,
        Option<WindowMetrics>,
        Option<WindowMetrics>,
    ),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WindowMetrics {
    // Token 0 price
    pub token0_price: f64,
    // Token 1 price
    pub token1_price: f64,
    // Number of transactions
    pub tx_count: i32,
    // Total volume in usd
    pub volume_usd: f64,
    // Number of buys
    pub tx_buys: i32,
    // Number of sells
    pub tx_sells: i32,
    // Number of unique wallets
    pub unique_wallets: i32,
    // Number of unique wallets that made buys
    pub unique_wallets_buys: i32,
    // Number of unique wallets that made sells
    pub unique_wallets_sells: i32,
    // Number of transactions superior to 100 usd
    pub tx_sup_100_usd: i32,
    // Number of wallets with more than 20 transactions
    pub wallet_sup_20_tx: i32,
}

impl Default for WindowMetrics {
    fn default() -> Self {
        Self {
            token0_price: 0.0f64,
            token1_price: 0.0f64,
            tx_count: 0,
            volume_usd: 0.0f64,
            tx_buys: 0,
            tx_sells: 0,
            unique_wallets: 0,
            unique_wallets_buys: 0,
            unique_wallets_sells: 0,
            tx_sup_100_usd: 0,
            wallet_sup_20_tx: 0,
        }
    }
}

impl WindowMetrics {
    pub fn has_changed(&self, other: &WindowMetrics) -> bool {
        self.tx_count != other.tx_count
            || (self.volume_usd - other.volume_usd).abs() > f64::EPSILON
            || self.tx_buys != other.tx_buys
            || self.tx_sells != other.tx_sells
            || self.unique_wallets != other.unique_wallets
            || self.unique_wallets_buys != other.unique_wallets_buys
            || self.unique_wallets_sells != other.unique_wallets_sells
            || self.tx_sup_100_usd != other.tx_sup_100_usd
            || self.wallet_sup_20_tx != other.wallet_sup_20_tx
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Transaction {
    Block(Block),
    Swap(SwapTransaction),
    Mint(MintTransaction),
    Burn(BurnTransaction),
    TokenTransfer(TokenTransfer),
    PairInfoPub(PairInfoPub),
    Lock(Lock),
    OwnershipTransferred(OwnershipTransferred),
}

// In token_balances.rs
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HolderBalanceChange {
    pub token_address: String,
    pub chain_id: String,
    pub holder: String,
    pub new_balance: String,  // IBig as string, or use IBig directly
    pub block_number: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HolderCountChange {
    pub token_address: String,
    pub chain_id: String,
    pub new_holder_count: i32,
    pub block_number: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenSupplyChange {
    pub token_address: String,
    pub chain_id: String,
    pub total_minted: String,      // Cumulative minted amount (only increases)
    pub total_burned: String,       // Cumulative burned amount (only increases)
    pub circulating_supply: String, // Calculated: total_minted - total_burned
    pub block_number: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenBalanceChanges {
    pub holder_balances: Vec<HolderBalanceChange>,
    pub holder_count_changes: Vec<HolderCountChange>,
    pub supply_changes: Vec<TokenSupplyChange>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum HolderPnlStatus {
    Flat,
    Profit,
    Loss,
    Exited,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum HolderPnlScope {
    Chain,
    Global,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum HolderPnlWindow {
    Block,
    Day,
    Week,
    Month,
    Custom,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HolderPnlChange {
    pub chain_id: String,
    pub token_address: Option<String>,
    pub pair_address: Option<String>,
    pub holder: String,
    pub position_size: String,
    pub average_entry_price: String,
    pub realized_pnl_usd: String,
    pub unrealized_pnl_usd: String,
    pub total_pnl_usd: String,
    pub status: HolderPnlStatus,
    pub scope: HolderPnlScope,
    pub window: HolderPnlWindow,
    pub block_number: u64,
    pub window_start_block: Option<u64>,
    pub window_end_block: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HolderPnlStatusBucket {
    pub status: HolderPnlStatus,
    pub holder_count: i64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HolderPnlStatusCount {
    pub chain_id: String,
    pub token_address: Option<String>,
    pub pair_address: Option<String>,
    pub scope: HolderPnlScope,
    pub window: HolderPnlWindow,
    pub block_number: u64,
    pub buckets: Vec<HolderPnlStatusBucket>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HolderPnlLeaderboardEntry {
    pub chain_id: String,
    pub token_address: Option<String>,
    pub pair_address: Option<String>,
    pub holder: String,
    pub position_size: String,
    pub average_entry_price: String,
    pub realized_pnl_usd: String,
    pub unrealized_pnl_usd: String,
    pub total_pnl_usd: String,
    pub rank: u32,
    pub scope: HolderPnlScope,
    pub window: HolderPnlWindow,
    pub block_number: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HolderPnlChanges {
    pub holder_changes: Vec<HolderPnlChange>,
    pub status_counts: Vec<HolderPnlStatusCount>,
    pub leaderboard_entries: Vec<HolderPnlLeaderboardEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TransactionPub {
    Transaction(Transaction),
    EnrichedTransaction(EnrichedTransaction),
    PairStatePub(PairStatePub),
    TokenStatePub(TokenStatePub),
    PairInfoPub(PairInfoPub),
    WindowMetricsPub(WindowMetricsPub),
    TokenBalanceChangePub(TokenBalanceChanges),
    HolderPnlChangePub(HolderPnlChanges),
}

pub type TokensCollection = HashMap<String, TokenState>;