Skip to content

Commit

Permalink
Add swapper.supported_chains method
Browse files Browse the repository at this point in the history
  • Loading branch information
gemcoder21 committed Nov 13, 2024
1 parent 61e3c22 commit 1b287fd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
16 changes: 13 additions & 3 deletions gemstone/src/swapper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ mod uniswap;

use models::*;
use primitives::Chain;
use std::collections::HashSet;

#[async_trait]
pub trait GemSwapProvider: Send + Sync + Debug {
fn provider(&self) -> SwapProvider;
async fn supported_chains(&self) -> Result<Vec<Chain>, SwapperError>;
fn supported_chains(&self) -> Vec<Chain>;
async fn fetch_quote(&self, request: &SwapQuoteRequest, provider: Arc<dyn AlienProvider>) -> Result<SwapQuote, SwapperError>;
async fn fetch_quote_data(&self, quote: &SwapQuote, provider: Arc<dyn AlienProvider>, data: FetchQuoteData) -> Result<SwapQuoteData, SwapperError>;
async fn get_transaction_status(&self, chain: Chain, transaction_hash: &str, provider: Arc<dyn AlienProvider>) -> Result<bool, SwapperError>;
Expand All @@ -39,8 +40,17 @@ impl GemSwapper {
}
}

fn get_providers(&self) -> Vec<String> {
self.swappers.iter().map(|x| x.provider().name().to_string()).collect()
fn supported_chains(&self) -> Vec<Chain> {
self.swappers
.iter()
.flat_map(|x| x.supported_chains())
.collect::<HashSet<_>>()
.into_iter()
.collect()
}

fn get_providers(&self) -> Vec<SwapProvider> {
self.swappers.iter().map(|x| x.provider()).collect()
}

async fn fetch_quote(&self, request: SwapQuoteRequest) -> Result<Vec<SwapQuote>, SwapperError> {
Expand Down
2 changes: 1 addition & 1 deletion gemstone/src/swapper/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub struct SwapQuoteRequest {
pub struct GemSwapOptions {
pub slippage_bps: u32,
pub fee: Option<SwapReferralFees>,
pub preferred_providers: Vec<String>,
pub preferred_providers: Vec<SwapProvider>,
}

impl Default for GemSwapOptions {
Expand Down
7 changes: 3 additions & 4 deletions gemstone/src/swapper/thorchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ impl GemSwapProvider for ThorChain {
SwapProvider::Thorchain
}

async fn supported_chains(&self) -> Result<Vec<Chain>, SwapperError> {
let chains: Vec<Chain> = Chain::all()
fn supported_chains(&self) -> Vec<Chain> {
Chain::all()
.into_iter()
.filter_map(|chain| THORChainName::from_chain(&chain).map(|name| name.chain()))
.collect();
Ok(chains)
.collect()
}

async fn fetch_quote(&self, request: &SwapQuoteRequest, provider: Arc<dyn AlienProvider>) -> Result<SwapQuote, SwapperError> {
Expand Down
4 changes: 2 additions & 2 deletions gemstone/src/swapper/uniswap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ impl GemSwapProvider for UniswapV3 {
SwapProvider::UniswapV3
}

async fn supported_chains(&self) -> Result<Vec<Chain>, SwapperError> {
Ok(Chain::all().iter().filter(|x| self.support_chain(x)).cloned().collect())
fn supported_chains(&self) -> Vec<Chain> {
Chain::all().iter().filter(|x| self.support_chain(x)).cloned().collect()
}

async fn fetch_quote(&self, request: &SwapQuoteRequest, provider: Arc<dyn AlienProvider>) -> Result<SwapQuote, SwapperError> {
Expand Down

0 comments on commit 1b287fd

Please sign in to comment.