anvil/eth/otterscan/
api.rs

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
use crate::eth::{
    error::{BlockchainError, Result},
    macros::node_info,
    EthApi,
};
use alloy_consensus::Transaction as TransactionTrait;
use alloy_network::{
    AnyHeader, AnyRpcBlock, AnyRpcHeader, AnyRpcTransaction, AnyTxEnvelope, BlockResponse,
    TransactionResponse,
};
use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_rpc_types::{
    trace::{
        otterscan::{
            BlockDetails, ContractCreator, InternalOperation, OtsBlock, OtsBlockTransactions,
            OtsReceipt, OtsSlimBlock, OtsTransactionReceipt, TraceEntry, TransactionsWithReceipts,
        },
        parity::{
            Action, CallAction, CallType, CreateAction, CreateOutput, LocalizedTransactionTrace,
            RewardAction, TraceOutput,
        },
    },
    Block, BlockId, BlockNumberOrTag as BlockNumber, BlockTransactions,
};
use itertools::Itertools;

use futures::future::join_all;

pub fn mentions_address(trace: LocalizedTransactionTrace, address: Address) -> Option<B256> {
    match (trace.trace.action, trace.trace.result) {
        (Action::Call(CallAction { from, to, .. }), _) if from == address || to == address => {
            trace.transaction_hash
        }
        (_, Some(TraceOutput::Create(CreateOutput { address: created_address, .. })))
            if created_address == address =>
        {
            trace.transaction_hash
        }
        (Action::Create(CreateAction { from, .. }), _) if from == address => trace.transaction_hash,
        (Action::Reward(RewardAction { author, .. }), _) if author == address => {
            trace.transaction_hash
        }
        _ => None,
    }
}

/// Converts the list of traces for a transaction into the expected Otterscan format.
///
/// Follows format specified in the [`ots_traceTransaction`](https://github.com/otterscan/otterscan/blob/develop/docs/custom-jsonrpc.md#ots_tracetransaction) spec.
pub fn batch_build_ots_traces(traces: Vec<LocalizedTransactionTrace>) -> Vec<TraceEntry> {
    traces
        .into_iter()
        .filter_map(|trace| {
            let output = trace
                .trace
                .result
                .map(|r| match r {
                    TraceOutput::Call(output) => output.output,
                    TraceOutput::Create(output) => output.code,
                })
                .unwrap_or_default();
            match trace.trace.action {
                Action::Call(call) => Some(TraceEntry {
                    r#type: match call.call_type {
                        CallType::Call => "CALL",
                        CallType::CallCode => "CALLCODE",
                        CallType::DelegateCall => "DELEGATECALL",
                        CallType::StaticCall => "STATICCALL",
                        CallType::AuthCall => "AUTHCALL",
                        CallType::None => "NONE",
                    }
                    .to_string(),
                    depth: trace.trace.trace_address.len() as u32,
                    from: call.from,
                    to: call.to,
                    value: call.value,
                    input: call.input,
                    output,
                }),
                Action::Create(_) | Action::Selfdestruct(_) | Action::Reward(_) => None,
            }
        })
        .collect()
}

impl EthApi {
    /// Otterscan currently requires this endpoint, even though it's not part of the `ots_*`.
    /// Ref: <https://github.com/otterscan/otterscan/blob/071d8c55202badf01804f6f8d53ef9311d4a9e47/src/useProvider.ts#L71>
    ///
    /// As a faster alternative to `eth_getBlockByNumber` (by excluding uncle block
    /// information), which is not relevant in the context of an anvil node
    pub async fn erigon_get_header_by_number(
        &self,
        number: BlockNumber,
    ) -> Result<Option<AnyRpcBlock>> {
        node_info!("ots_getApiLevel");

        self.backend.block_by_number(number).await
    }

    /// As per the latest Otterscan source code, at least version 8 is needed.
    /// Ref: <https://github.com/otterscan/otterscan/blob/071d8c55202badf01804f6f8d53ef9311d4a9e47/src/params.ts#L1C2-L1C2>
    pub async fn ots_get_api_level(&self) -> Result<u64> {
        node_info!("ots_getApiLevel");

        // as required by current otterscan's source code
        Ok(8)
    }

    /// Trace internal ETH transfers, contracts creation (CREATE/CREATE2) and self-destructs for a
    /// certain transaction.
    pub async fn ots_get_internal_operations(&self, hash: B256) -> Result<Vec<InternalOperation>> {
        node_info!("ots_getInternalOperations");

        self.backend
            .mined_transaction(hash)
            .map(|tx| tx.ots_internal_operations())
            .ok_or_else(|| BlockchainError::DataUnavailable)
    }

    /// Check if an ETH address contains code at a certain block number.
    pub async fn ots_has_code(&self, address: Address, block_number: BlockNumber) -> Result<bool> {
        node_info!("ots_hasCode");
        let block_id = Some(BlockId::Number(block_number));
        Ok(self.get_code(address, block_id).await?.len() > 0)
    }

    /// Trace a transaction and generate a trace call tree.
    pub async fn ots_trace_transaction(&self, hash: B256) -> Result<Vec<TraceEntry>> {
        node_info!("ots_traceTransaction");

        Ok(batch_build_ots_traces(self.backend.trace_transaction(hash).await?))
    }

    /// Given a transaction hash, returns its raw revert reason.
    pub async fn ots_get_transaction_error(&self, hash: B256) -> Result<Bytes> {
        node_info!("ots_getTransactionError");

        if let Some(receipt) = self.backend.mined_transaction_receipt(hash) {
            if !receipt.inner.inner.as_receipt_with_bloom().receipt.status.coerce_status() {
                return Ok(receipt.out.map(|b| b.0.into()).unwrap_or(Bytes::default()));
            }
        }

        Ok(Bytes::default())
    }

    /// For simplicity purposes, we return the entire block instead of emptying the values that
    /// Otterscan doesn't want. This is the original purpose of the endpoint (to save bandwidth),
    /// but it doesn't seem necessary in the context of an anvil node
    pub async fn ots_get_block_details(
        &self,
        number: BlockNumber,
    ) -> Result<BlockDetails<AnyRpcHeader>> {
        node_info!("ots_getBlockDetails");

        if let Some(block) = self.backend.block_by_number(number).await? {
            let ots_block = self.build_ots_block_details(block).await?;
            Ok(ots_block)
        } else {
            Err(BlockchainError::BlockNotFound)
        }
    }

    /// For simplicity purposes, we return the entire block instead of emptying the values that
    /// Otterscan doesn't want. This is the original purpose of the endpoint (to save bandwidth),
    /// but it doesn't seem necessary in the context of an anvil node
    pub async fn ots_get_block_details_by_hash(
        &self,
        hash: B256,
    ) -> Result<BlockDetails<AnyRpcHeader>> {
        node_info!("ots_getBlockDetailsByHash");

        if let Some(block) = self.backend.block_by_hash(hash).await? {
            let ots_block = self.build_ots_block_details(block).await?;
            Ok(ots_block)
        } else {
            Err(BlockchainError::BlockNotFound)
        }
    }

    /// Gets paginated transaction data for a certain block. Return data is similar to
    /// eth_getBlockBy* + eth_getTransactionReceipt.
    pub async fn ots_get_block_transactions(
        &self,
        number: u64,
        page: usize,
        page_size: usize,
    ) -> Result<OtsBlockTransactions<AnyRpcTransaction, AnyRpcHeader>> {
        node_info!("ots_getBlockTransactions");

        match self.backend.block_by_number_full(number.into()).await? {
            Some(block) => self.build_ots_block_tx(block, page, page_size).await,
            None => Err(BlockchainError::BlockNotFound),
        }
    }

    /// Address history navigation. searches backwards from certain point in time.
    pub async fn ots_search_transactions_before(
        &self,
        address: Address,
        block_number: u64,
        page_size: usize,
    ) -> Result<TransactionsWithReceipts<alloy_rpc_types::Transaction<AnyTxEnvelope>>> {
        node_info!("ots_searchTransactionsBefore");

        let best = self.backend.best_number();
        // we go from given block (defaulting to best) down to first block
        // considering only post-fork
        let from = if block_number == 0 { best } else { block_number - 1 };
        let to = self.get_fork().map(|f| f.block_number() + 1).unwrap_or(1);

        let first_page = from >= best;
        let mut last_page = false;

        let mut res: Vec<_> = vec![];

        for n in (to..=from).rev() {
            if let Some(traces) = self.backend.mined_parity_trace_block(n) {
                let hashes = traces
                    .into_iter()
                    .rev()
                    .filter_map(|trace| mentions_address(trace, address))
                    .unique();

                if res.len() >= page_size {
                    break;
                }

                res.extend(hashes);
            }

            if n == to {
                last_page = true;
            }
        }

        self.build_ots_search_transactions(res, first_page, last_page).await
    }

    /// Address history navigation. searches forward from certain point in time.
    pub async fn ots_search_transactions_after(
        &self,
        address: Address,
        block_number: u64,
        page_size: usize,
    ) -> Result<TransactionsWithReceipts<alloy_rpc_types::Transaction<AnyTxEnvelope>>> {
        node_info!("ots_searchTransactionsAfter");

        let best = self.backend.best_number();
        // we go from the first post-fork block, up to the tip
        let first_block = self.get_fork().map(|f| f.block_number() + 1).unwrap_or(1);
        let from = if block_number == 0 { first_block } else { block_number + 1 };
        let to = best;

        let mut first_page = from >= best;
        let mut last_page = false;

        let mut res: Vec<_> = vec![];

        for n in from..=to {
            if n == first_block {
                last_page = true;
            }

            if let Some(traces) = self.backend.mined_parity_trace_block(n) {
                let hashes = traces
                    .into_iter()
                    .rev()
                    .filter_map(|trace| mentions_address(trace, address))
                    .unique();

                if res.len() >= page_size {
                    break;
                }

                res.extend(hashes);
            }

            if n == to {
                first_page = true;
            }
        }

        // Results are always sent in reverse chronological order, according to the Otterscan spec
        res.reverse();
        self.build_ots_search_transactions(res, first_page, last_page).await
    }

    /// Given a sender address and a nonce, returns the tx hash or null if not found. It returns
    /// only the tx hash on success, you can use the standard eth_getTransactionByHash after that to
    /// get the full transaction data.
    pub async fn ots_get_transaction_by_sender_and_nonce(
        &self,
        address: Address,
        nonce: U256,
    ) -> Result<Option<B256>> {
        node_info!("ots_getTransactionBySenderAndNonce");

        let from = self.get_fork().map(|f| f.block_number() + 1).unwrap_or_default();
        let to = self.backend.best_number();

        for n in (from..=to).rev() {
            if let Some(txs) = self.backend.mined_transactions_by_block_number(n.into()).await {
                for tx in txs {
                    if U256::from(tx.nonce()) == nonce && tx.from == address {
                        return Ok(Some(tx.tx_hash()));
                    }
                }
            }
        }

        Ok(None)
    }

    /// Given an ETH contract address, returns the tx hash and the direct address who created the
    /// contract.
    pub async fn ots_get_contract_creator(&self, addr: Address) -> Result<Option<ContractCreator>> {
        node_info!("ots_getContractCreator");

        let from = self.get_fork().map(|f| f.block_number()).unwrap_or_default();
        let to = self.backend.best_number();

        // loop in reverse, since we want the latest deploy to the address
        for n in (from..=to).rev() {
            if let Some(traces) = self.backend.mined_parity_trace_block(n) {
                for trace in traces.into_iter().rev() {
                    match (trace.trace.action, trace.trace.result) {
                        (
                            Action::Create(CreateAction { from, .. }),
                            Some(TraceOutput::Create(CreateOutput { address, .. })),
                        ) if address == addr => {
                            return Ok(Some(ContractCreator {
                                hash: trace.transaction_hash.unwrap(),
                                creator: from,
                            }));
                        }
                        _ => {}
                    }
                }
            }
        }

        Ok(None)
    }
    /// The response for ots_getBlockDetails includes an `issuance` object that requires computing
    /// the total gas spent in a given block.
    ///
    /// The only way to do this with the existing API is to explicitly fetch all receipts, to get
    /// their `gas_used`. This would be extremely inefficient in a real blockchain RPC, but we can
    /// get away with that in this context.
    ///
    /// The [original spec](https://github.com/otterscan/otterscan/blob/develop/docs/custom-jsonrpc.md#ots_getblockdetails)
    /// also mentions we can hardcode `transactions` and `logsBloom` to an empty array to save
    /// bandwidth, because fields weren't intended to be used in the Otterscan UI at this point.
    ///
    /// This has two problems though:
    ///   - It makes the endpoint too specific to Otterscan's implementation
    ///   - It breaks the abstraction built in `OtsBlock<TX>` which computes `transaction_count`
    ///     based on the existing list.
    ///
    /// Therefore we keep it simple by keeping the data in the response
    pub async fn build_ots_block_details(
        &self,
        block: AnyRpcBlock,
    ) -> Result<BlockDetails<alloy_rpc_types::Header<AnyHeader>>> {
        if block.transactions.is_uncle() {
            return Err(BlockchainError::DataUnavailable);
        }
        let receipts_futs = block
            .transactions
            .hashes()
            .map(|hash| async move { self.transaction_receipt(hash).await });

        // fetch all receipts
        let receipts = join_all(receipts_futs)
            .await
            .into_iter()
            .map(|r| match r {
                Ok(Some(r)) => Ok(r),
                _ => Err(BlockchainError::DataUnavailable),
            })
            .collect::<Result<Vec<_>>>()?;

        let total_fees = receipts
            .iter()
            .fold(0, |acc, receipt| acc + receipt.gas_used * receipt.effective_gas_price);

        let Block { header, uncles, transactions, withdrawals } = block.inner;

        let block =
            OtsSlimBlock { header, uncles, transaction_count: transactions.len(), withdrawals };

        Ok(BlockDetails {
            block,
            total_fees: U256::from(total_fees),
            // issuance has no meaningful value in anvil's backend. just default to 0
            issuance: Default::default(),
        })
    }

    /// Fetches all receipts for the blocks's transactions, as required by the
    /// [`ots_getBlockTransactions`] endpoint spec, and returns the final response object.
    ///
    /// [`ots_getBlockTransactions`]: https://github.com/otterscan/otterscan/blob/develop/docs/custom-jsonrpc.md#ots_getblockdetails
    pub async fn build_ots_block_tx(
        &self,
        mut block: AnyRpcBlock,
        page: usize,
        page_size: usize,
    ) -> Result<OtsBlockTransactions<AnyRpcTransaction, AnyRpcHeader>> {
        if block.transactions.is_uncle() {
            return Err(BlockchainError::DataUnavailable);
        }

        block.transactions = match block.transactions() {
            BlockTransactions::Full(txs) => BlockTransactions::Full(
                txs.iter().skip(page * page_size).take(page_size).cloned().collect(),
            ),
            BlockTransactions::Hashes(txs) => BlockTransactions::Hashes(
                txs.iter().skip(page * page_size).take(page_size).cloned().collect(),
            ),
            BlockTransactions::Uncle => unreachable!(),
        };

        let receipt_futs = block.transactions.hashes().map(|hash| self.transaction_receipt(hash));

        let receipts = join_all(receipt_futs.map(|r| async {
            if let Ok(Some(r)) = r.await {
                let block = self.block_by_number(r.block_number.unwrap().into()).await?;
                let timestamp = block.ok_or(BlockchainError::BlockNotFound)?.header.timestamp;
                let receipt = r.map_inner(OtsReceipt::from);
                Ok(OtsTransactionReceipt { receipt, timestamp: Some(timestamp) })
            } else {
                Err(BlockchainError::BlockNotFound)
            }
        }))
        .await
        .into_iter()
        .collect::<Result<Vec<_>>>()?;

        let transaction_count = block.transactions().len();
        let fullblock = OtsBlock { block: block.inner, transaction_count };

        let ots_block_txs = OtsBlockTransactions { fullblock, receipts };

        Ok(ots_block_txs)
    }

    pub async fn build_ots_search_transactions(
        &self,
        hashes: Vec<B256>,
        first_page: bool,
        last_page: bool,
    ) -> Result<TransactionsWithReceipts<alloy_rpc_types::Transaction<AnyTxEnvelope>>> {
        let txs_futs = hashes.iter().map(|hash| async { self.transaction_by_hash(*hash).await });

        let txs = join_all(txs_futs)
            .await
            .into_iter()
            .map(|t| match t {
                Ok(Some(t)) => Ok(t.inner),
                _ => Err(BlockchainError::DataUnavailable),
            })
            .collect::<Result<Vec<_>>>()?;

        let receipt_futs = hashes.iter().map(|hash| self.transaction_receipt(*hash));

        let receipts = join_all(receipt_futs.map(|r| async {
            if let Ok(Some(r)) = r.await {
                let block = self.block_by_number(r.block_number.unwrap().into()).await?;
                let timestamp = block.ok_or(BlockchainError::BlockNotFound)?.header.timestamp;
                let receipt = r.map_inner(OtsReceipt::from);
                Ok(OtsTransactionReceipt { receipt, timestamp: Some(timestamp) })
            } else {
                Err(BlockchainError::BlockNotFound)
            }
        }))
        .await
        .into_iter()
        .collect::<Result<Vec<_>>>()?;

        Ok(TransactionsWithReceipts { txs, receipts, first_page, last_page })
    }
}