-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add crypto service to access several chains
- Loading branch information
1 parent
03e6488
commit ec20db5
Showing
15 changed files
with
190 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,36 @@ | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
#[allow(dead_code)] | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
pub enum Chain { | ||
EthereumMainnet, | ||
Optimism, | ||
Arbitrum, | ||
Goerli, | ||
Kovan, | ||
Rinkeby, | ||
EthereumSepolia, | ||
OptimismMainnet, | ||
OptimismSepolia, | ||
ArbitrumMainnet, | ||
ArbitrumSepolia, | ||
} | ||
|
||
impl Chain { | ||
#[allow(dead_code)] | ||
pub fn get_display_name(&self) -> &str { | ||
match self { | ||
Chain::EthereumMainnet => "Ethereum Mainnet", | ||
Chain::Optimism => "Optimism", | ||
Chain::Arbitrum => "Arbitrum", | ||
Chain::Goerli => "Goerli Testnet", | ||
Chain::Kovan => "Kovan Testnet", | ||
Chain::Rinkeby => "Rinkeby Testnet", | ||
Chain::EthereumSepolia => "Ethereum Sepolia", | ||
Chain::OptimismMainnet => "Optimism Mainnet", | ||
Chain::OptimismSepolia => "Optimism Sepolia", | ||
Chain::ArbitrumMainnet => "Arbitrum Mainnet", | ||
Chain::ArbitrumSepolia => "Arbitrum Sepolia", | ||
} | ||
} | ||
|
||
pub fn is_test_network(&self) -> bool { | ||
match self { | ||
Chain::EthereumMainnet => false, | ||
Chain::Optimism => false, | ||
Chain::Arbitrum => false, | ||
Chain::Goerli => true, | ||
Chain::Kovan => true, | ||
Chain::Rinkeby => true, | ||
Chain::EthereumSepolia => true, | ||
Chain::OptimismMainnet => false, | ||
Chain::OptimismSepolia => true, | ||
Chain::ArbitrumMainnet => false, | ||
Chain::ArbitrumSepolia => true, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::core::{chain::Chain, provider::{Provider, Balance}}; | ||
|
||
#[derive(Clone)] | ||
pub struct Crypto { | ||
endpoint_url: String, | ||
providers: HashMap<Chain, Provider>, | ||
} | ||
|
||
impl Crypto { | ||
pub fn new(endpoint_url: &str) -> Self { | ||
Self { | ||
endpoint_url: endpoint_url.to_string(), | ||
providers: HashMap::new() | ||
} | ||
} | ||
|
||
pub fn add_chain(&mut self, chain: Chain) -> anyhow::Result<bool> { | ||
if self.providers.contains_key(&chain) { | ||
return Ok(false); | ||
} | ||
let provider = Provider::new(&self.endpoint_url, chain.clone())?; | ||
self.providers.insert(chain, provider); | ||
Ok(true) | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn set_chains(&mut self, chains: Vec<Chain>) -> anyhow::Result<()> { | ||
let old_chains = self.providers.keys().cloned().collect::<Vec<_>>(); | ||
for chain in &old_chains { | ||
if !chains.contains(&chain) { | ||
self.providers.remove(&chain); | ||
} | ||
} | ||
|
||
for chain in &chains { | ||
if !old_chains.contains(chain) { | ||
self.add_chain(chain.clone())?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn get_active_chains(&self) -> Vec<Chain> { | ||
self.providers.keys().cloned().collect() | ||
} | ||
|
||
pub async fn get_eth_balance(&self, account: web3::types::Address) -> anyhow::Result<Balance> { | ||
let mut summary= Balance::new(0.0, 0.0, "ETH", false); | ||
for provider in self.providers.values() { | ||
if let Ok(balance) = provider.get_eth_balance(account).await { | ||
summary.value += balance.value; | ||
summary.usd_value += balance.usd_value; | ||
summary.from_test_network = summary.from_test_network || balance.from_test_network; | ||
} | ||
} | ||
Ok(summary) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use crate::core::chain::Chain; | ||
use super::super::crypto::Crypto; | ||
|
||
#[test] | ||
fn test_crypto_chains() -> anyhow::Result<()> { | ||
let mut crypto = Crypto::new("http://localhost:8545"); | ||
assert_eq!(crypto.get_active_chains().len(), 0); | ||
|
||
crypto.add_chain(Chain::EthereumMainnet)?; | ||
assert_eq!(crypto.get_active_chains().len(), 1); | ||
|
||
crypto.set_chains(vec![Chain::EthereumMainnet, Chain::ArbitrumMainnet])?; | ||
assert_eq!(crypto.get_active_chains().len(), 2); | ||
|
||
assert!(crypto.get_active_chains().contains(&Chain::EthereumMainnet)); | ||
assert!(crypto.get_active_chains().contains(&Chain::ArbitrumMainnet)); | ||
assert!(!crypto.get_active_chains().contains(&Chain::OptimismSepolia)); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
pub mod session; | ||
mod session_test; | ||
pub mod crypto; | ||
mod crypto_test; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.