From 699a84e6c48069e086cec15335efef06c4da5f0a Mon Sep 17 00:00:00 2001
From: Patrice Tisserand
Date: Wed, 17 Apr 2024 10:18:21 +0200
Subject: [PATCH 1/3] feat(indexer): ethereum retrieve gas used for a given
transaction
---
apps/indexer/src/ethereum_indexer/client.rs | 23 ++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/apps/indexer/src/ethereum_indexer/client.rs b/apps/indexer/src/ethereum_indexer/client.rs
index e9d62351..e59b3371 100644
--- a/apps/indexer/src/ethereum_indexer/client.rs
+++ b/apps/indexer/src/ethereum_indexer/client.rs
@@ -201,10 +201,31 @@ impl EthereumClient {
Ok(logs)
}
+ /// Retrieve message status in StarknetCore messaging contract
pub async fn query_message_status(&self, msg_hash: [u8; 32]) -> Result {
let messaging =
StarknetMessaging::new(self.messaging_address, Arc::new(self.provider.clone()));
let status = messaging.l2_to_l1_messages(msg_hash).call().await?;
- Ok(status.try_into().unwrap())
+ match status.try_into() {
+ Ok(s) => Ok(s),
+ Err(e) => Err(anyhow!("Failed to retrieve message status: {:?}", e))
+ }
}
+
+ /// Retrieve gas used for a given transaction
+ pub async fn get_tx_fees(&self, transaction_hash: &str) -> Result {
+ let tx_hash: TxHash = H256::from_str(transaction_hash).unwrap();
+ if let Some(receipt) = self.provider.get_transaction_receipt(tx_hash).await? {
+ let effective_gas_price = receipt.effective_gas_price.unwrap();
+ let gas_used = receipt.gas_used.unwrap();
+ let total_fees = effective_gas_price * gas_used;
+ match total_fees.try_into() {
+ Ok(fees) => Ok(fees),
+ Err(e) => Err(anyhow!("{:?}", e))
+ }
+ } else {
+ Err(anyhow!("Failed to get receipt for {}", transaction_hash))
+ }
+ }
+
}
From 85cc68b10eb938b18d70a5a59638679e5073220c Mon Sep 17 00:00:00 2001
From: Patrice Tisserand
Date: Wed, 17 Apr 2024 16:28:07 +0200
Subject: [PATCH 2/3] indexer: add reqwest
---
apps/indexer/Cargo.lock | 517 ++++++++++++++++++++++++++++++++++++----
apps/indexer/Cargo.toml | 1 +
2 files changed, 466 insertions(+), 52 deletions(-)
diff --git a/apps/indexer/Cargo.lock b/apps/indexer/Cargo.lock
index 3c2471b5..ecc8e4fa 100644
--- a/apps/indexer/Cargo.lock
+++ b/apps/indexer/Cargo.lock
@@ -109,7 +109,7 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
dependencies = [
- "windows-sys",
+ "windows-sys 0.48.0",
]
[[package]]
@@ -119,7 +119,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188"
dependencies = [
"anstyle",
- "windows-sys",
+ "windows-sys 0.48.0",
]
[[package]]
@@ -259,9 +259,9 @@ dependencies = [
"bitflags 1.3.2",
"bytes",
"futures-util",
- "http",
- "http-body",
- "hyper",
+ "http 0.2.9",
+ "http-body 0.4.5",
+ "hyper 0.14.27",
"itoa",
"matchit",
"memchr",
@@ -289,8 +289,8 @@ dependencies = [
"async-trait",
"bytes",
"futures-util",
- "http",
- "http-body",
+ "http 0.2.9",
+ "http-body 0.4.5",
"mime",
"rustversion",
"tower-layer",
@@ -342,6 +342,12 @@ version = "0.21.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "604178f6c5c21f02dc555784810edfb88d34ac2c73b2eae109655649ee73ce3d"
+[[package]]
+name = "base64"
+version = "0.22.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9475866fec1451be56a3c2400fd081ff546538961565ccb5b7142cbd22bc7a51"
+
[[package]]
name = "base64ct"
version = "1.6.0"
@@ -763,11 +769,21 @@ dependencies = [
"unicode-segmentation",
]
+[[package]]
+name = "core-foundation"
+version = "0.9.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f"
+dependencies = [
+ "core-foundation-sys",
+ "libc",
+]
+
[[package]]
name = "core-foundation-sys"
-version = "0.8.4"
+version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
+checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
[[package]]
name = "cpufeatures"
@@ -1163,7 +1179,7 @@ checksum = "6b30f669a7961ef1631673d2766cc92f52d64f7ef354d4fe0ddfd30ed52f0f4f"
dependencies = [
"errno-dragonfly",
"libc",
- "windows-sys",
+ "windows-sys 0.48.0",
]
[[package]]
@@ -1310,7 +1326,7 @@ dependencies = [
"proc-macro2",
"quote",
"regex",
- "reqwest",
+ "reqwest 0.11.18",
"serde",
"serde_json",
"syn 2.0.28",
@@ -1371,7 +1387,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22b3a8269d3df0ed6364bc05b4735b95f4bf830ce3aef87d5e760fb0e93e5b91"
dependencies = [
"ethers-core",
- "reqwest",
+ "reqwest 0.11.18",
"semver 1.0.18",
"serde",
"serde_json",
@@ -1396,7 +1412,7 @@ dependencies = [
"futures-locks",
"futures-util",
"instant",
- "reqwest",
+ "reqwest 0.11.18",
"serde",
"serde_json",
"thiserror",
@@ -1423,11 +1439,11 @@ dependencies = [
"futures-util",
"hashers",
"hex",
- "http",
+ "http 0.2.9",
"instant",
"once_cell",
"pin-project",
- "reqwest",
+ "reqwest 0.11.18",
"serde",
"serde_json",
"thiserror",
@@ -1552,6 +1568,21 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
+[[package]]
+name = "foreign-types"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
+dependencies = [
+ "foreign-types-shared",
+]
+
+[[package]]
+name = "foreign-types-shared"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
+
[[package]]
name = "form_urlencoded"
version = "1.2.0"
@@ -1765,7 +1796,7 @@ dependencies = [
"futures-core",
"futures-sink",
"futures-util",
- "http",
+ "http 0.2.9",
"indexmap 1.9.3",
"slab",
"tokio",
@@ -1773,6 +1804,25 @@ dependencies = [
"tracing",
]
+[[package]]
+name = "h2"
+version = "0.4.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "816ec7294445779408f36fe57bc5b7fc1cf59664059096c65f905c1c61f58069"
+dependencies = [
+ "bytes",
+ "fnv",
+ "futures-core",
+ "futures-sink",
+ "futures-util",
+ "http 1.1.0",
+ "indexmap 2.0.0",
+ "slab",
+ "tokio",
+ "tokio-util",
+ "tracing",
+]
+
[[package]]
name = "hashbrown"
version = "0.12.3"
@@ -1833,7 +1883,7 @@ version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb"
dependencies = [
- "windows-sys",
+ "windows-sys 0.48.0",
]
[[package]]
@@ -1858,6 +1908,17 @@ dependencies = [
"itoa",
]
+[[package]]
+name = "http"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258"
+dependencies = [
+ "bytes",
+ "fnv",
+ "itoa",
+]
+
[[package]]
name = "http-body"
version = "0.4.5"
@@ -1865,7 +1926,30 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1"
dependencies = [
"bytes",
- "http",
+ "http 0.2.9",
+ "pin-project-lite",
+]
+
+[[package]]
+name = "http-body"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643"
+dependencies = [
+ "bytes",
+ "http 1.1.0",
+]
+
+[[package]]
+name = "http-body-util"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0475f8b2ac86659c21b64320d5d653f9efe42acd2a4e560073ec61a155a34f1d"
+dependencies = [
+ "bytes",
+ "futures-core",
+ "http 1.1.0",
+ "http-body 1.0.0",
"pin-project-lite",
]
@@ -1897,9 +1981,9 @@ dependencies = [
"futures-channel",
"futures-core",
"futures-util",
- "h2",
- "http",
- "http-body",
+ "h2 0.3.20",
+ "http 0.2.9",
+ "http-body 0.4.5",
"httparse",
"httpdate",
"itoa",
@@ -1911,6 +1995,26 @@ dependencies = [
"want",
]
+[[package]]
+name = "hyper"
+version = "1.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fe575dd17d0862a9a33781c8c4696a55c320909004a67a00fb286ba8b1bc496d"
+dependencies = [
+ "bytes",
+ "futures-channel",
+ "futures-util",
+ "h2 0.4.4",
+ "http 1.1.0",
+ "http-body 1.0.0",
+ "httparse",
+ "itoa",
+ "pin-project-lite",
+ "smallvec",
+ "tokio",
+ "want",
+]
+
[[package]]
name = "hyper-rustls"
version = "0.24.1"
@@ -1918,13 +2022,49 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d78e1e73ec14cf7375674f74d7dde185c8206fd9dea6fb6295e8a98098aaa97"
dependencies = [
"futures-util",
- "http",
- "hyper",
+ "http 0.2.9",
+ "hyper 0.14.27",
"rustls 0.21.5",
"tokio",
"tokio-rustls 0.24.1",
]
+[[package]]
+name = "hyper-tls"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0"
+dependencies = [
+ "bytes",
+ "http-body-util",
+ "hyper 1.3.1",
+ "hyper-util",
+ "native-tls",
+ "tokio",
+ "tokio-native-tls",
+ "tower-service",
+]
+
+[[package]]
+name = "hyper-util"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ca38ef113da30126bbff9cd1705f9273e15d45498615d138b0c20279ac7a76aa"
+dependencies = [
+ "bytes",
+ "futures-channel",
+ "futures-util",
+ "http 1.1.0",
+ "http-body 1.0.0",
+ "hyper 1.3.1",
+ "pin-project-lite",
+ "socket2 0.5.3",
+ "tokio",
+ "tower",
+ "tower-service",
+ "tracing",
+]
+
[[package]]
name = "iana-time-zone"
version = "0.1.57"
@@ -2066,7 +2206,7 @@ checksum = "b58db92f96b720de98181bbbe63c831e87005ab460c1bf306eb2622b4707997f"
dependencies = [
"socket2 0.5.3",
"widestring",
- "windows-sys",
+ "windows-sys 0.48.0",
"winreg 0.50.0",
]
@@ -2084,7 +2224,7 @@ checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b"
dependencies = [
"hermit-abi",
"rustix",
- "windows-sys",
+ "windows-sys 0.48.0",
]
[[package]]
@@ -2302,7 +2442,7 @@ checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2"
dependencies = [
"libc",
"wasi",
- "windows-sys",
+ "windows-sys 0.48.0",
]
[[package]]
@@ -2331,7 +2471,7 @@ dependencies = [
"rand",
"rustc_version_runtime",
"rustls 0.20.8",
- "rustls-pemfile",
+ "rustls-pemfile 1.0.3",
"serde",
"serde_bytes",
"serde_with 1.14.0",
@@ -2352,6 +2492,24 @@ dependencies = [
"webpki-roots 0.22.6",
]
+[[package]]
+name = "native-tls"
+version = "0.2.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e"
+dependencies = [
+ "lazy_static",
+ "libc",
+ "log",
+ "openssl",
+ "openssl-probe",
+ "openssl-sys",
+ "schannel",
+ "security-framework",
+ "security-framework-sys",
+ "tempfile",
+]
+
[[package]]
name = "new_debug_unreachable"
version = "1.0.4"
@@ -2475,6 +2633,50 @@ dependencies = [
"syn 1.0.109",
]
+[[package]]
+name = "openssl"
+version = "0.10.64"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "95a0481286a310808298130d22dd1fef0fa571e05a8f44ec801801e84b216b1f"
+dependencies = [
+ "bitflags 2.3.3",
+ "cfg-if",
+ "foreign-types",
+ "libc",
+ "once_cell",
+ "openssl-macros",
+ "openssl-sys",
+]
+
+[[package]]
+name = "openssl-macros"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.28",
+]
+
+[[package]]
+name = "openssl-probe"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
+
+[[package]]
+name = "openssl-sys"
+version = "0.9.102"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c597637d56fbc83893a35eb0dd04b2b8e7a50c91e64e9493e398b5df4fb45fa2"
+dependencies = [
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
[[package]]
name = "ordered-multimap"
version = "0.6.0"
@@ -2531,7 +2733,7 @@ dependencies = [
"libc",
"redox_syscall 0.3.5",
"smallvec",
- "windows-targets",
+ "windows-targets 0.48.1",
]
[[package]]
@@ -2982,10 +3184,10 @@ dependencies = [
"encoding_rs",
"futures-core",
"futures-util",
- "h2",
- "http",
- "http-body",
- "hyper",
+ "h2 0.3.20",
+ "http 0.2.9",
+ "http-body 0.4.5",
+ "hyper 0.14.27",
"hyper-rustls",
"ipnet",
"js-sys",
@@ -2995,7 +3197,7 @@ dependencies = [
"percent-encoding",
"pin-project-lite",
"rustls 0.21.5",
- "rustls-pemfile",
+ "rustls-pemfile 1.0.3",
"serde",
"serde_json",
"serde_urlencoded",
@@ -3010,6 +3212,48 @@ dependencies = [
"winreg 0.10.1",
]
+[[package]]
+name = "reqwest"
+version = "0.12.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3e6cc1e89e689536eb5aeede61520e874df5a4707df811cd5da4aa5fbb2aae19"
+dependencies = [
+ "base64 0.22.0",
+ "bytes",
+ "encoding_rs",
+ "futures-core",
+ "futures-util",
+ "h2 0.4.4",
+ "http 1.1.0",
+ "http-body 1.0.0",
+ "http-body-util",
+ "hyper 1.3.1",
+ "hyper-tls",
+ "hyper-util",
+ "ipnet",
+ "js-sys",
+ "log",
+ "mime",
+ "native-tls",
+ "once_cell",
+ "percent-encoding",
+ "pin-project-lite",
+ "rustls-pemfile 2.1.2",
+ "serde",
+ "serde_json",
+ "serde_urlencoded",
+ "sync_wrapper",
+ "system-configuration",
+ "tokio",
+ "tokio-native-tls",
+ "tower-service",
+ "url",
+ "wasm-bindgen",
+ "wasm-bindgen-futures",
+ "web-sys",
+ "winreg 0.52.0",
+]
+
[[package]]
name = "resolv-conf"
version = "0.7.0"
@@ -3148,7 +3392,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys",
- "windows-sys",
+ "windows-sys 0.48.0",
]
[[package]]
@@ -3184,6 +3428,22 @@ dependencies = [
"base64 0.21.2",
]
+[[package]]
+name = "rustls-pemfile"
+version = "2.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "29993a25686778eb88d4189742cd713c9bce943bc54251a33509dc63cbacf73d"
+dependencies = [
+ "base64 0.22.0",
+ "rustls-pki-types",
+]
+
+[[package]]
+name = "rustls-pki-types"
+version = "1.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ecd36cc4259e3e4514335c4a138c6b43171a8d61d8f5c9348f9fc7529416f247"
+
[[package]]
name = "rustls-webpki"
version = "0.100.1"
@@ -3258,6 +3518,15 @@ dependencies = [
"syn 1.0.109",
]
+[[package]]
+name = "schannel"
+version = "0.1.23"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534"
+dependencies = [
+ "windows-sys 0.52.0",
+]
+
[[package]]
name = "scopeguard"
version = "1.2.0"
@@ -3300,6 +3569,29 @@ dependencies = [
"zeroize",
]
+[[package]]
+name = "security-framework"
+version = "2.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "770452e37cad93e0a50d5abc3990d2bc351c36d0328f86cefec2f2fb206eaef6"
+dependencies = [
+ "bitflags 1.3.2",
+ "core-foundation",
+ "core-foundation-sys",
+ "libc",
+ "security-framework-sys",
+]
+
+[[package]]
+name = "security-framework-sys"
+version = "2.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "41f3cc463c0ef97e11c3461a9d3787412d30e8e7eb907c79180c4a57bf7c04ef"
+dependencies = [
+ "core-foundation-sys",
+ "libc",
+]
+
[[package]]
name = "semver"
version = "0.9.0"
@@ -3561,9 +3853,9 @@ dependencies = [
[[package]]
name = "smallvec"
-version = "1.11.0"
+version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9"
+checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "socket2"
@@ -3582,7 +3874,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2538b18701741680e0322a2302176d3253a35388e2e62f172f64f4f16605f877"
dependencies = [
"libc",
- "windows-sys",
+ "windows-sys 0.48.0",
]
[[package]]
@@ -3631,6 +3923,7 @@ dependencies = [
"mongodb",
"num-bigint",
"regex",
+ "reqwest 0.12.3",
"serde",
"serde_json",
"sha3",
@@ -3777,7 +4070,7 @@ dependencies = [
"ethereum-types",
"flate2",
"log",
- "reqwest",
+ "reqwest 0.11.18",
"serde",
"serde_json",
"serde_with 2.3.3",
@@ -3875,7 +4168,7 @@ dependencies = [
"hex",
"home",
"once_cell",
- "reqwest",
+ "reqwest 0.11.18",
"semver 1.0.18",
"serde",
"serde_json",
@@ -3913,6 +4206,27 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
+[[package]]
+name = "system-configuration"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7"
+dependencies = [
+ "bitflags 1.3.2",
+ "core-foundation",
+ "system-configuration-sys",
+]
+
+[[package]]
+name = "system-configuration-sys"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9"
+dependencies = [
+ "core-foundation-sys",
+ "libc",
+]
+
[[package]]
name = "take_mut"
version = "0.2.2"
@@ -3935,7 +4249,7 @@ dependencies = [
"fastrand",
"redox_syscall 0.3.5",
"rustix",
- "windows-sys",
+ "windows-sys 0.48.0",
]
[[package]]
@@ -4047,7 +4361,7 @@ dependencies = [
"signal-hook-registry",
"socket2 0.4.9",
"tokio-macros",
- "windows-sys",
+ "windows-sys 0.48.0",
]
[[package]]
@@ -4061,6 +4375,16 @@ dependencies = [
"syn 2.0.28",
]
+[[package]]
+name = "tokio-native-tls"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2"
+dependencies = [
+ "native-tls",
+ "tokio",
+]
+
[[package]]
name = "tokio-rustls"
version = "0.23.4"
@@ -4302,7 +4626,7 @@ dependencies = [
"byteorder",
"bytes",
"data-encoding",
- "http",
+ "http 0.2.9",
"httparse",
"log",
"rand",
@@ -4431,6 +4755,12 @@ dependencies = [
"serde",
]
+[[package]]
+name = "vcpkg"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
+
[[package]]
name = "version_check"
version = "0.9.4"
@@ -4609,7 +4939,7 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f"
dependencies = [
- "windows-targets",
+ "windows-targets 0.48.1",
]
[[package]]
@@ -4618,7 +4948,16 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
- "windows-targets",
+ "windows-targets 0.48.1",
+]
+
+[[package]]
+name = "windows-sys"
+version = "0.52.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
+dependencies = [
+ "windows-targets 0.52.5",
]
[[package]]
@@ -4627,13 +4966,29 @@ version = "0.48.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05d4b17490f70499f20b9e791dcf6a299785ce8af4d709018206dc5b4953e95f"
dependencies = [
- "windows_aarch64_gnullvm",
- "windows_aarch64_msvc",
- "windows_i686_gnu",
- "windows_i686_msvc",
- "windows_x86_64_gnu",
- "windows_x86_64_gnullvm",
- "windows_x86_64_msvc",
+ "windows_aarch64_gnullvm 0.48.0",
+ "windows_aarch64_msvc 0.48.0",
+ "windows_i686_gnu 0.48.0",
+ "windows_i686_msvc 0.48.0",
+ "windows_x86_64_gnu 0.48.0",
+ "windows_x86_64_gnullvm 0.48.0",
+ "windows_x86_64_msvc 0.48.0",
+]
+
+[[package]]
+name = "windows-targets"
+version = "0.52.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb"
+dependencies = [
+ "windows_aarch64_gnullvm 0.52.5",
+ "windows_aarch64_msvc 0.52.5",
+ "windows_i686_gnu 0.52.5",
+ "windows_i686_gnullvm",
+ "windows_i686_msvc 0.52.5",
+ "windows_x86_64_gnu 0.52.5",
+ "windows_x86_64_gnullvm 0.52.5",
+ "windows_x86_64_msvc 0.52.5",
]
[[package]]
@@ -4642,42 +4997,90 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc"
+[[package]]
+name = "windows_aarch64_gnullvm"
+version = "0.52.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263"
+
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3"
+[[package]]
+name = "windows_aarch64_msvc"
+version = "0.52.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6"
+
[[package]]
name = "windows_i686_gnu"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241"
+[[package]]
+name = "windows_i686_gnu"
+version = "0.52.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670"
+
+[[package]]
+name = "windows_i686_gnullvm"
+version = "0.52.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9"
+
[[package]]
name = "windows_i686_msvc"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00"
+[[package]]
+name = "windows_i686_msvc"
+version = "0.52.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf"
+
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1"
+[[package]]
+name = "windows_x86_64_gnu"
+version = "0.52.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9"
+
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953"
+[[package]]
+name = "windows_x86_64_gnullvm"
+version = "0.52.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596"
+
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a"
+[[package]]
+name = "windows_x86_64_msvc"
+version = "0.52.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0"
+
[[package]]
name = "winnow"
version = "0.5.2"
@@ -4703,7 +5106,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1"
dependencies = [
"cfg-if",
- "windows-sys",
+ "windows-sys 0.48.0",
+]
+
+[[package]]
+name = "winreg"
+version = "0.52.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a277a57398d4bfa075df44f501a17cfdf8542d224f0d36095a2adc7aee4ef0a5"
+dependencies = [
+ "cfg-if",
+ "windows-sys 0.48.0",
]
[[package]]
diff --git a/apps/indexer/Cargo.toml b/apps/indexer/Cargo.toml
index 0e9c4d7a..7b50d2be 100644
--- a/apps/indexer/Cargo.toml
+++ b/apps/indexer/Cargo.toml
@@ -24,3 +24,4 @@ log = "0.4.17"
num-bigint = "0.4.4"
config = { version = "0.14.0", features = ["json"] }
sha3 = "0.10.8"
+reqwest = { version = "0.12.3", features = ["json"] }
From 36dd031c7fc85516ee87d9b367c45f0226b20eb5 Mon Sep 17 00:00:00 2001
From: Patrice Tisserand
Date: Wed, 17 Apr 2024 16:37:13 +0200
Subject: [PATCH 3/3] feat(indexer): retrieve L1 deposit transaction price
Ethereum price is provided by Moralis with WETH uniswap v3
---
apps/indexer/src/ethereum_indexer/client.rs | 7 +--
apps/indexer/src/ethereum_indexer/events.rs | 1 +
apps/indexer/src/ethereum_indexer/indexer.rs | 34 ++++++++++-
apps/indexer/src/main.rs | 1 +
apps/indexer/src/price/mod.rs | 1 +
apps/indexer/src/price/moralis.rs | 61 ++++++++++++++++++++
apps/indexer/src/starknet_indexer/events.rs | 1 +
apps/indexer/src/storage/mod.rs | 8 +++
8 files changed, 108 insertions(+), 6 deletions(-)
create mode 100644 apps/indexer/src/price/mod.rs
create mode 100644 apps/indexer/src/price/moralis.rs
diff --git a/apps/indexer/src/ethereum_indexer/client.rs b/apps/indexer/src/ethereum_indexer/client.rs
index e59b3371..36b15d28 100644
--- a/apps/indexer/src/ethereum_indexer/client.rs
+++ b/apps/indexer/src/ethereum_indexer/client.rs
@@ -208,12 +208,12 @@ impl EthereumClient {
let status = messaging.l2_to_l1_messages(msg_hash).call().await?;
match status.try_into() {
Ok(s) => Ok(s),
- Err(e) => Err(anyhow!("Failed to retrieve message status: {:?}", e))
+ Err(e) => Err(anyhow!("Failed to retrieve message status: {:?}", e)),
}
}
/// Retrieve gas used for a given transaction
- pub async fn get_tx_fees(&self, transaction_hash: &str) -> Result {
+ pub async fn get_tx_fees(&self, transaction_hash: &str) -> Result {
let tx_hash: TxHash = H256::from_str(transaction_hash).unwrap();
if let Some(receipt) = self.provider.get_transaction_receipt(tx_hash).await? {
let effective_gas_price = receipt.effective_gas_price.unwrap();
@@ -221,11 +221,10 @@ impl EthereumClient {
let total_fees = effective_gas_price * gas_used;
match total_fees.try_into() {
Ok(fees) => Ok(fees),
- Err(e) => Err(anyhow!("{:?}", e))
+ Err(e) => Err(anyhow!("{:?}", e)),
}
} else {
Err(anyhow!("Failed to get receipt for {}", transaction_hash))
}
}
-
}
diff --git a/apps/indexer/src/ethereum_indexer/events.rs b/apps/indexer/src/ethereum_indexer/events.rs
index c3be194e..aec23971 100644
--- a/apps/indexer/src/ethereum_indexer/events.rs
+++ b/apps/indexer/src/ethereum_indexer/events.rs
@@ -55,6 +55,7 @@ pub fn get_store_data(log: Log) -> Result<(Option, Option, Optio
block_timestamp: 0,
block_number: log.block_number.unwrap().try_into().unwrap(),
tx_hash: format!("{:#x}", log.transaction_hash.unwrap()),
+ price: None,
};
// TODO: not a fan of the mut here and for event, but as the type of data can change,
diff --git a/apps/indexer/src/ethereum_indexer/indexer.rs b/apps/indexer/src/ethereum_indexer/indexer.rs
index 239442f2..0fef5d4d 100644
--- a/apps/indexer/src/ethereum_indexer/indexer.rs
+++ b/apps/indexer/src/ethereum_indexer/indexer.rs
@@ -1,9 +1,10 @@
use super::client::EthereumClient;
use super::events;
use crate::config::{ChainConfig, XchainTxConfig};
+use crate::price::moralis::MoralisPrice;
use crate::storage::{
store::{BlockStore, CrossChainTxStore, EventStore, PendingWithdrawStore, RequestStore},
- BlockIndex, BridgeChain, CrossChainTxKind, Event, EventLabel,
+ BlockIndex, BridgeChain, CrossChainTxKind, Event, EventLabel, EventPrice,
};
use crate::utils;
use crate::ChainsBlocks;
@@ -23,6 +24,7 @@ pub struct EthereumIndexer<
store: Arc,
chains_blocks: Arc>,
xchain_txor_config: XchainTxConfig,
+ pricer: MoralisPrice,
}
impl EthereumIndexer
@@ -37,12 +39,15 @@ where
xchain_txor_config: XchainTxConfig,
) -> Result> {
let client = EthereumClient::new(config.clone()).await?;
+ /// TODO: should we add moralis api key to configuration file?
+ let pricer = MoralisPrice::new(None);
Ok(EthereumIndexer {
client,
config,
store,
chains_blocks,
xchain_txor_config,
+ pricer,
})
}
@@ -238,8 +243,18 @@ where
let l_sig = l.topics[0];
match events::get_store_data(l)? {
- (Some(r), Some(e), xchain_tx) => {
+ (Some(r), Some(mut e), xchain_tx) => {
log::debug!("Request/Event/Tx\n{:?}\n{:?}\n{:?}", r, e, xchain_tx);
+ if e.label == EventLabel::DepositInitiatedL1 {
+ match self.compute_event_price(&e).await {
+ Ok(price) => {
+ log::debug!("Price: {:?}", price);
+ e.price = Some(price);
+ }
+ Err(e) => log::warn!("Failed to compute event price: {:?}", e),
+ }
+ }
+
self.store.insert_event(e.clone()).await?;
if self.store.req_by_hash(&r.hash).await?.is_none() {
@@ -313,4 +328,19 @@ where
}
Ok(())
}
+
+ async fn compute_event_price(&self, e: &Event) -> Result {
+ let gas = self.client.get_tx_fees(&e.tx_hash).await?;
+ let eth_price = self
+ .pricer
+ .get_price("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", None)
+ .await?;
+ let mut usd_price = (gas as f64) * eth_price.parse::()?;
+ usd_price = usd_price / (10_u64.pow(18) as f64);
+
+ Ok(EventPrice {
+ gas,
+ usd_price: format!("{}", usd_price),
+ })
+ }
}
diff --git a/apps/indexer/src/main.rs b/apps/indexer/src/main.rs
index e5c3d842..77dfb636 100644
--- a/apps/indexer/src/main.rs
+++ b/apps/indexer/src/main.rs
@@ -15,6 +15,7 @@ use tokio::sync::RwLock as AsyncRwLock;
pub mod config;
pub mod ethereum_indexer;
pub mod handlers;
+pub mod price;
pub mod starknet_indexer;
pub mod storage;
pub mod utils;
diff --git a/apps/indexer/src/price/mod.rs b/apps/indexer/src/price/mod.rs
new file mode 100644
index 00000000..4c2c258a
--- /dev/null
+++ b/apps/indexer/src/price/mod.rs
@@ -0,0 +1 @@
+pub mod moralis;
diff --git a/apps/indexer/src/price/moralis.rs b/apps/indexer/src/price/moralis.rs
new file mode 100644
index 00000000..981033a3
--- /dev/null
+++ b/apps/indexer/src/price/moralis.rs
@@ -0,0 +1,61 @@
+use std::env;
+
+use reqwest::{
+ self,
+ header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE},
+};
+
+use serde::Deserialize;
+
+use anyhow::{anyhow, Result};
+
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase")]
+struct APIResponse {
+ usd_price_formatted: String,
+}
+
+pub struct MoralisPrice {
+ client: reqwest::Client,
+ headers: HeaderMap,
+}
+
+impl MoralisPrice {
+ pub fn new(api_key: Option<&str>) -> MoralisPrice {
+ let api_key = if api_key.is_none() {
+ env::var("MORALIS_API_KEY").expect("MORALIS_API_KEY environment variable")
+ } else {
+ api_key.unwrap().to_owned()
+ };
+ let client = reqwest::Client::new();
+ let mut headers = HeaderMap::new();
+ headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
+ headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
+ headers.insert("X-API-KEY", HeaderValue::from_str(&api_key).unwrap());
+ MoralisPrice { client, headers }
+ }
+
+ pub async fn get_price(&self, token: &str, block: Option) -> Result {
+ let base_url = "https://deep-index.moralis.io/api/v2.2/erc20";
+ let url = if block.is_some() {
+ let block = block.unwrap();
+ format!("{base_url}/{token}/price?chain=eth&to_block={block}")
+ } else {
+ format!("{base_url}/{token}/price?chain=eth")
+ };
+ let response = self
+ .client
+ .get(url)
+ .headers(self.headers.clone())
+ .send()
+ .await?;
+ if response.status().is_success() {
+ match response.json::().await {
+ Ok(parsed) => Ok(parsed.usd_price_formatted),
+ Err(_) => Err(anyhow!("Failed to parse response")),
+ }
+ } else {
+ Err(anyhow!("{:?}", response.error_for_status()))
+ }
+ }
+}
diff --git a/apps/indexer/src/starknet_indexer/events.rs b/apps/indexer/src/starknet_indexer/events.rs
index cc4352e5..1b232925 100644
--- a/apps/indexer/src/starknet_indexer/events.rs
+++ b/apps/indexer/src/starknet_indexer/events.rs
@@ -35,6 +35,7 @@ pub fn get_store_data(
block_timestamp: block_timestamp.try_into()?,
block_number: event.block_number,
tx_hash: felt_to_hex(&event.transaction_hash),
+ price: None,
};
let tx;
diff --git a/apps/indexer/src/storage/mod.rs b/apps/indexer/src/storage/mod.rs
index 7fa3e07d..871c5322 100644
--- a/apps/indexer/src/storage/mod.rs
+++ b/apps/indexer/src/storage/mod.rs
@@ -94,6 +94,12 @@ pub struct Request {
pub content: String,
}
+#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
+pub struct EventPrice {
+ pub gas: u64,
+ pub usd_price: String,
+}
+
/// Records event associated to requests.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Event {
@@ -107,6 +113,8 @@ pub struct Event {
pub block_number: u64,
// Transaction hash of the transaction which triggered the event.
pub tx_hash: String,
+ // Transaction price
+ pub price: Option,
}
#[derive(Debug, Serialize, Deserialize, Clone)]