stratus/eth/
codegen.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
//! Auto-generated code.

use std::borrow::Cow;

use crate::eth::primitives::Address;
use crate::infra::metrics;

include!(concat!(env!("OUT_DIR"), "/contracts.rs"));
include!(concat!(env!("OUT_DIR"), "/signatures.rs"));

pub type SoliditySignature = &'static str;

pub type ContractName = &'static str;

/// Returns the contract name to be used in observability tasks.
pub fn contract_name(address: &Option<Address>) -> ContractName {
    let Some(address) = address else { return metrics::LABEL_MISSING };
    match CONTRACTS.get(address.as_bytes()) {
        Some(contract_name) => contract_name,
        None => metrics::LABEL_UNKNOWN,
    }
}

/// Returns the function name to be used in observability tasks.
pub fn function_sig(bytes: impl AsRef<[u8]>) -> SoliditySignature {
    match function_sig_opt(bytes) {
        Some(signature) => signature,
        None => metrics::LABEL_UNKNOWN,
    }
}

/// Returns the function name to be used in observability tasks.
pub fn function_sig_opt(bytes: impl AsRef<[u8]>) -> Option<SoliditySignature> {
    let Some(id) = bytes.as_ref().get(..4) else {
        return Some(metrics::LABEL_MISSING);
    };
    SIGNATURES_4_BYTES.get(id).copied()
}

/// Returns the error name or string.
pub fn error_sig_opt(bytes: impl AsRef<[u8]>) -> Option<Cow<'static, str>> {
    bytes
        .as_ref()
        .get(..4)
        .and_then(|id| SIGNATURES_4_BYTES.get(id))
        .copied()
        .map(Cow::Borrowed)
        .or_else(|| {
            bytes.as_ref().get(4..).and_then(|bytes| {
                ethabi::decode(&[ethabi::ParamType::String], bytes)
                    .ok()
                    .and_then(|res| res.first().cloned())
                    .and_then(|token| token.into_string())
                    .map(Cow::Owned)
            })
        })
}