-
Notifications
You must be signed in to change notification settings - Fork 0
Eth bridge indexer metrics + coinmarketcap API to fetch a price. #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: vmarkushin/test-succint
Are you sure you want to change the base?
Changes from 31 commits
fd8d6cd
c19fc7c
6128e21
69a4e47
61015fe
66a385f
34923a3
81b8b81
85ec404
ae4ec00
a588239
6776314
6498c6c
dc0d3f5
9aad530
94d54df
ce1b338
8bca713
abb649e
6fc42e8
d8b0850
403f755
7d026c1
a72ad4c
8f15f86
87d08a2
99c6e00
3f618d8
0565e9e
079f5c8
d46272f
a6ad620
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,18 +3,68 @@ use evm_indexer::{ | |
| configs::indexer_config::EVMIndexerConfig, db::db::Database, indexer, rpc::rpc::Rpc, | ||
| }; | ||
| use log::*; | ||
| use serde::Deserialize; | ||
| use simple_logger::SimpleLogger; | ||
| use std::time::Duration; | ||
| use tokio::time::sleep; | ||
|
|
||
| use std::collections::HashMap; | ||
| use reqwest::{Client, Url}; | ||
| use chrono::Utc; | ||
|
|
||
|
|
||
| async fn fetch_cryptocurrency_data(api_key: &str) -> Result<String, reqwest::Error> { | ||
| //price-performance-stats is not allowed for free plan | ||
| // let mut url = Url::parse("https://pro-api.coinmarketcap.com/v2/cryptocurrency/price-performance-stats/latest").unwrap(); | ||
| //only quotes/latest is allowed for free plan | ||
| let mut url = Url::parse("https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest").unwrap(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe, this should be configurable? not hardcodeded, in case coinmarketcap changes endpoint
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there are so many params for this indexer to set as a dependency. |
||
|
|
||
| let mut query_params = HashMap::new(); | ||
| query_params.insert("symbol", "ETH"); | ||
|
|
||
| url.set_query(Some(&query_params.iter().map(|(k, v)| format!("{}={}", k, v)).collect::<Vec<_>>().join("&"))); | ||
|
|
||
| let client = Client::new(); | ||
| let resp = client.get(url) | ||
| .header("X-CMC_PRO_API_KEY", api_key) | ||
| .header("Accepts", "application/json") | ||
| .send().await.expect("Failed to send request") | ||
| .text().await?; | ||
|
|
||
| Ok(resp) | ||
| } | ||
|
|
||
| async fn price_from_cmc_data(cmc_api_key : &str) -> Option<f64> { | ||
| let str_cmc_data = fetch_cryptocurrency_data(cmc_api_key).await.unwrap(); | ||
| let data: serde_json::Value = serde_json::from_str(&str_cmc_data).unwrap(); | ||
| data["data"]["ETH"][0]["quote"]["USD"]["price"].as_f64() | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| struct Cryptocurrency { | ||
| name: String, | ||
| price: f64, | ||
| last_timestamp: i64, | ||
| } | ||
|
|
||
| #[tokio::main()] | ||
| async fn main() { | ||
| dotenv().ok(); | ||
|
|
||
|
|
||
|
|
||
| let log = SimpleLogger::new().with_level(LevelFilter::Info); | ||
|
|
||
| let mut config = EVMIndexerConfig::new(); | ||
|
|
||
| let mut eth_price = Cryptocurrency { | ||
| name: "ETH".to_string(), | ||
| price: price_from_cmc_data(&config.coinmarketcap_api_key).await.unwrap(), | ||
| last_timestamp: Utc::now().timestamp(), | ||
| }; | ||
|
|
||
| println!("{:?}", eth_price); | ||
|
|
||
| if config.debug { | ||
| log.with_level(LevelFilter::Debug).init().unwrap(); | ||
| } else { | ||
|
|
@@ -46,7 +96,13 @@ async fn main() { | |
| None => rpc.get_last_block().await.unwrap(), | ||
| }; | ||
| loop { | ||
| indexer::sync_chain(&rpc, &db, &mut config, &mut indexed_blocks, from_block).await; | ||
| //check that timestamp is not older than 60 mins | ||
| if eth_price.last_timestamp < Utc::now().timestamp() - 60 * 60 { | ||
| eth_price.price = price_from_cmc_data(&config.coinmarketcap_api_key).await.unwrap(); | ||
| eth_price.last_timestamp = Utc::now().timestamp(); | ||
| println!("new eth price {:?}", eth_price); | ||
| } | ||
| indexer::sync_chain(&rpc, &db, &mut config, &mut indexed_blocks, from_block, eth_price.price).await; | ||
| sleep(Duration::from_millis(50)).await; | ||
| } | ||
| } else { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| CREATE TABLE relayertransactions ( | ||
| block_hash TEXT NOT NULL, | ||
| block_number BIGINT NOT NULL, | ||
| chain TEXT NOT NULL, | ||
| from_address TEXT NOT NULL, | ||
| gas TEXT NOT NULL, | ||
| gas_price TEXT NOT NULL, | ||
| fee_usd TEXT NOT NULL, | ||
| hash TEXT NOT NULL, | ||
| input TEXT NOT NULL, | ||
| max_fee_per_gas TEXT, | ||
| max_priority_fee_per_gas TEXT, | ||
| method TEXT NOT NULL, | ||
| call_batch_methods TEXT NOT NULL, | ||
| nonce TEXT NOT NULL, | ||
| timestamp TEXT NOT NULL, | ||
| to_address TEXT NOT NULL, | ||
| transaction_index BIGINT NOT NULL, | ||
| transaction_type BIGINT, | ||
| value TEXT NOT NULL, | ||
| CONSTRAINT relayertransactions_pkey PRIMARY KEY (hash) | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS relayertransactions_by_block_number ON relayertransactions (block_number DESC); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS relayertransactions_by_sender ON relayertransactions (from_address); --; -- STORING (to_address); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS relayertransactions_by_receiver ON relayertransactions (to_address); --; -- STORING (from_address); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS relayertransactions_by_chain ON relayertransactions (chain); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS relayertransactions_by_timestamp ON relayertransactions (timestamp DESC); | ||
|
|
||
| CREATE TABLE feecollectors ( | ||
| token_address TEXT NOT NULL, | ||
| transaction_hash TEXT NOT NULL, | ||
| address TEXT NOT NULL, | ||
| block_number BIGINT NOT NULL, | ||
| amount TEXT NOT NULL, | ||
| timestamp TEXT NOT NULL, | ||
| is_withdrawn TEXT NOT NULL, | ||
| CONSTRAINT feecollectors_pkey PRIMARY KEY (token_address, transaction_hash, address) | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS feecollectors_by_timestamp ON feecollectors (timestamp DESC); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS feecollectors_by_address ON feecollectors (address); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS feecollectors_by_receiver ON feecollectors (token_address); | ||
|
|
||
|
|
||
| CREATE TABLE sendpacketevents ( | ||
| transaction_hash TEXT NOT NULL, | ||
| sequence BIGINT NOT NULL, | ||
| source_port TEXT NOT NULL, | ||
| source_channel TEXT NOT NULL, | ||
| source_port_indexed TEXT NOT NULL, | ||
| source_channel_indexed TEXT NOT NULL, | ||
| timeout_revision_number BIGINT NOT NULL, | ||
| timeout_revision_height BIGINT NOT NULL, | ||
| timeout_timestamp BIGINT NOT NULL, | ||
| data TEXT NOT NULL, | ||
| amount TEXT NOT NULL, | ||
| denom TEXT NOT NULL, | ||
| receiver TEXT NOT NULL, | ||
| sender TEXT NOT NULL, | ||
| memo TEXT NOT NULL, | ||
| CONSTRAINT sendpacketevents_pkey PRIMARY KEY (transaction_hash) | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS sendpacketevents_by_sequence ON sendpacketevents (sequence); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS sendpacketevents_by_transaction_hash ON sendpacketevents (transaction_hash); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS sendpacketevents_by_receiver ON sendpacketevents (receiver); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS sendpacketevents_by_denom ON sendpacketevents (denom); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS sendpacketevents_by_sender ON sendpacketevents (sender); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS sendpacketevents_by_amount ON sendpacketevents (amount); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it a real api-key ?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
real key. without any funds. 10k monthly credits calls.
example.
repo is private. you can substitute with your key if you wish
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok