This repository was archived by the owner on Jun 1, 2026. It is now read-only.
Repository navigation
Transaction state #1104
Merged
Merged
Transaction state #1104
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e67c887
Added chain_factory
0xSou 0eea08e
Add JobConfiguration struct and export
0xSou c660f94
Add TransactionStatusClient with timeout-fail policy
0xSou 7b79707
Support swap InTransit status and status provider
0xSou 2a167cb
Undo InTransit support for now
0xSou c9d209d
Update status_provider.rs
0xSou 3430604
Add is_completed and use it in status provider
0xSou 6647585
Use milliseconds end-to-end for transaction status timeout
0xSou 8c81f2b
Replace JobConfiguration with GemJobConfiguration
0xSou a33120c
Merge branch 'main' into trans-state
0xSou 449e5e2
Centralize job configuration in primitives
0xSou 138dc21
Merge branch 'main' into trans-state
0xSou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,11 @@ | ||
| use serde::{Deserialize, Serialize}; | ||
| use typeshare::typeshare; | ||
|
|
||
| #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] | ||
| #[typeshare(swift = "Sendable")] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct JobConfiguration { | ||
| pub initial_interval_ms: u32, | ||
| pub max_interval_ms: u32, | ||
| pub step_factor: f32, | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,103 @@ | ||
| use super::{GatewayError, GemPreferences, PreferencesWrapper}; | ||
| use crate::alien::{AlienProvider, new_alien_client}; | ||
| use crate::network::JsonRpcClient; | ||
| use chain_traits::ChainTraits; | ||
| use gem_algorand::rpc::AlgorandClientIndexer; | ||
| use gem_algorand::rpc::client::AlgorandClient; | ||
| use gem_aptos::rpc::client::AptosClient; | ||
| use gem_bitcoin::rpc::client::BitcoinClient; | ||
| use gem_cardano::rpc::client::CardanoClient; | ||
| use gem_cosmos::rpc::client::CosmosClient; | ||
| use gem_evm::rpc::EthereumClient; | ||
| use gem_hypercore::rpc::client::HyperCoreClient; | ||
| use gem_near::rpc::client::NearClient; | ||
| use gem_polkadot::rpc::client::PolkadotClient; | ||
| use gem_solana::rpc::client::SolanaClient; | ||
| use gem_stellar::rpc::client::StellarClient; | ||
| use gem_sui::rpc::client::SuiClient; | ||
| use gem_ton::rpc::client::TonClient; | ||
| use gem_tron::rpc::{client::TronClient, trongrid::client::TronGridClient}; | ||
| use gem_xrp::rpc::client::XRPClient; | ||
| use primitives::{BitcoinChain, Chain, EVMChain, chain_cosmos::CosmosChain}; | ||
| use std::sync::Arc; | ||
|
|
||
| pub struct ChainClientFactory { | ||
| alien: Arc<dyn AlienProvider>, | ||
| preferences: Arc<dyn GemPreferences>, | ||
| secure_preferences: Arc<dyn GemPreferences>, | ||
| } | ||
|
|
||
| impl ChainClientFactory { | ||
| pub fn new(alien: Arc<dyn AlienProvider>, preferences: Arc<dyn GemPreferences>, secure_preferences: Arc<dyn GemPreferences>) -> Self { | ||
| Self { | ||
| alien, | ||
| preferences, | ||
| secure_preferences, | ||
| } | ||
| } | ||
|
|
||
| pub async fn create(&self, chain: Chain) -> Result<Arc<dyn ChainTraits>, GatewayError> { | ||
| let url = self.alien.get_endpoint(chain).map_err(|e| GatewayError::PlatformError { msg: e.to_string() })?; | ||
| self.create_with_url(chain, url).await | ||
| } | ||
|
|
||
| pub async fn create_with_url(&self, chain: Chain, url: String) -> Result<Arc<dyn ChainTraits>, GatewayError> { | ||
| let alien_client = new_alien_client(url, self.alien.clone()); | ||
| match chain { | ||
| Chain::HyperCore => { | ||
| let preferences = Arc::new(PreferencesWrapper { | ||
| preferences: self.preferences.clone(), | ||
| }); | ||
| let secure_preferences = Arc::new(PreferencesWrapper { | ||
| preferences: self.secure_preferences.clone(), | ||
| }); | ||
| Ok(Arc::new(HyperCoreClient::new_with_preferences(alien_client, preferences, secure_preferences))) | ||
| } | ||
| Chain::Bitcoin | Chain::BitcoinCash | Chain::Litecoin | Chain::Doge | Chain::Zcash => { | ||
| Ok(Arc::new(BitcoinClient::new(alien_client, BitcoinChain::from_chain(chain).unwrap()))) | ||
| } | ||
| Chain::Cardano => Ok(Arc::new(CardanoClient::new(alien_client))), | ||
| Chain::Stellar => Ok(Arc::new(StellarClient::new(alien_client))), | ||
| Chain::Sui => Ok(Arc::new(SuiClient::new(JsonRpcClient::new(alien_client.clone())))), | ||
| Chain::Xrp => Ok(Arc::new(XRPClient::new(JsonRpcClient::new(alien_client.clone())))), | ||
| Chain::Algorand => Ok(Arc::new(AlgorandClient::new(alien_client.clone(), AlgorandClientIndexer::new(alien_client.clone())))), | ||
| Chain::Near => Ok(Arc::new(NearClient::new(JsonRpcClient::new(alien_client.clone())))), | ||
| Chain::Aptos => Ok(Arc::new(AptosClient::new(alien_client))), | ||
| Chain::Cosmos | Chain::Osmosis | Chain::Celestia | Chain::Thorchain | Chain::Injective | Chain::Sei | Chain::Noble => { | ||
| Ok(Arc::new(CosmosClient::new(CosmosChain::from_chain(chain).unwrap(), alien_client))) | ||
| } | ||
| Chain::Ton => Ok(Arc::new(TonClient::new(alien_client))), | ||
| Chain::Tron => Ok(Arc::new(TronClient::new(alien_client.clone(), TronGridClient::new(alien_client.clone(), String::new())))), | ||
|
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. |
||
| Chain::Polkadot => Ok(Arc::new(PolkadotClient::new(alien_client))), | ||
| Chain::Solana => Ok(Arc::new(SolanaClient::new(JsonRpcClient::new(alien_client.clone())))), | ||
| Chain::Ethereum | ||
| | Chain::Arbitrum | ||
| | Chain::SmartChain | ||
| | Chain::Polygon | ||
| | Chain::Optimism | ||
| | Chain::Base | ||
| | Chain::AvalancheC | ||
| | Chain::OpBNB | ||
| | Chain::Fantom | ||
| | Chain::Gnosis | ||
| | Chain::Manta | ||
| | Chain::Blast | ||
| | Chain::ZkSync | ||
| | Chain::Linea | ||
| | Chain::Mantle | ||
| | Chain::Celo | ||
| | Chain::World | ||
| | Chain::Sonic | ||
| | Chain::SeiEvm | ||
| | Chain::Abstract | ||
| | Chain::Berachain | ||
| | Chain::Ink | ||
| | Chain::Unichain | ||
| | Chain::Hyperliquid | ||
| | Chain::Plasma | ||
| | Chain::Monad | ||
| | Chain::XLayer | ||
| | Chain::Stable => Ok(Arc::new(EthereumClient::new(JsonRpcClient::new(alien_client), EVMChain::from_chain(chain).unwrap()))), | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,39 +1,26 @@ | ||
| mod chain_factory; | ||
| mod error; | ||
| mod preferences; | ||
|
|
||
| pub use chain_factory::ChainClientFactory; | ||
| pub use error::GatewayError; | ||
| use error::map_network_error; | ||
| #[cfg(test)] | ||
| pub use preferences::EmptyPreferences; | ||
| pub use preferences::GemPreferences; | ||
| use preferences::PreferencesWrapper; | ||
| pub(crate) use preferences::PreferencesWrapper; | ||
|
|
||
| use crate::alien::{AlienProvider, AlienProviderWrapper, new_alien_client}; | ||
| use crate::alien::{AlienProvider, AlienProviderWrapper}; | ||
| use crate::api_client::GemApiClient; | ||
| use crate::models::*; | ||
| use crate::network::JsonRpcClient; | ||
| use crate::transaction_state::StatusProvider; | ||
| use chain_traits::ChainTraits; | ||
| use gem_algorand::rpc::AlgorandClientIndexer; | ||
| use gem_algorand::rpc::client::AlgorandClient; | ||
| use gem_aptos::rpc::client::AptosClient; | ||
| use gem_bitcoin::rpc::client::BitcoinClient; | ||
| use gem_cardano::rpc::client::CardanoClient; | ||
| use gem_cosmos::rpc::client::CosmosClient; | ||
| use gem_evm::rpc::EthereumClient; | ||
| use gem_hypercore::rpc::client::HyperCoreClient; | ||
| use gem_near::rpc::client::NearClient; | ||
| use gem_polkadot::rpc::client::PolkadotClient; | ||
| use gem_solana::rpc::client::SolanaClient; | ||
| use gem_stellar::rpc::client::StellarClient; | ||
| use gem_sui::rpc::client::SuiClient; | ||
| use gem_ton::rpc::client::TonClient; | ||
| use gem_tron::rpc::{client::TronClient, trongrid::client::TronGridClient}; | ||
| use gem_xrp::rpc::client::XRPClient; | ||
| use swapper::swapper::GemSwapper as Swapper; | ||
| use std::future::Future; | ||
| use std::sync::Arc; | ||
| use yielder::Yielder; | ||
|
|
||
| use primitives::{AssetId, BitcoinChain, Chain, ChartPeriod, EVMChain, ScanAddressTarget, ScanTransactionPayload, TransactionPreloadInput, chain_cosmos::CosmosChain}; | ||
| use primitives::{AssetId, Chain, ChartPeriod, ScanAddressTarget, ScanTransactionPayload, TransactionPreloadInput}; | ||
|
|
||
| #[uniffi::export(with_foreign)] | ||
| #[async_trait::async_trait] | ||
|
|
@@ -44,21 +31,15 @@ pub trait GemGatewayEstimateFee: Send + Sync { | |
|
|
||
| #[derive(uniffi::Object)] | ||
| pub struct GemGateway { | ||
| pub provider: Arc<dyn AlienProvider>, | ||
| pub preferences: Arc<dyn GemPreferences>, | ||
| pub secure_preferences: Arc<dyn GemPreferences>, | ||
| pub api_client: GemApiClient, | ||
| chain_factory: Arc<ChainClientFactory>, | ||
| yielder: Yielder, | ||
| status_provider: StatusProvider, | ||
|
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. can we move status provider out of this? |
||
| } | ||
|
|
||
| impl std::fmt::Debug for GemGateway { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| f.debug_struct("GemGateway") | ||
| .field("provider", &"<AlienProvider>") | ||
| .field("preferences", &"<GemPreferences>") | ||
| .field("secure_preferences", &"<GemPreferences>") | ||
| .field("api_client", &self.api_client) | ||
| .finish() | ||
| f.debug_struct("GemGateway").field("api_client", &self.api_client).finish() | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -68,74 +49,9 @@ impl GemGateway { | |
| F: FnOnce(Arc<dyn ChainTraits>) -> Fut, | ||
| Fut: Future<Output = Result<T, Box<dyn std::error::Error + Send + Sync>>>, | ||
| { | ||
| let provider = self.provider(chain).await?; | ||
| let provider = self.chain_factory.create(chain).await?; | ||
| call(provider).await.map_err(|e| GatewayError::NetworkError { msg: e.to_string() }) | ||
| } | ||
|
|
||
| pub async fn provider(&self, chain: Chain) -> Result<Arc<dyn ChainTraits>, GatewayError> { | ||
| let url = self.provider.get_endpoint(chain).unwrap(); | ||
| self.provider_with_url(chain, url).await | ||
| } | ||
|
|
||
| pub async fn provider_with_url(&self, chain: Chain, url: String) -> Result<Arc<dyn ChainTraits>, GatewayError> { | ||
| let alien_client = new_alien_client(url.clone(), self.provider.clone()); | ||
| match chain { | ||
| Chain::HyperCore => { | ||
| let preferences = Arc::new(PreferencesWrapper { | ||
| preferences: self.preferences.clone(), | ||
| }); | ||
| let secure_preferences = Arc::new(PreferencesWrapper { | ||
| preferences: self.secure_preferences.clone(), | ||
| }); | ||
| Ok(Arc::new(HyperCoreClient::new_with_preferences(alien_client, preferences, secure_preferences))) | ||
| } | ||
| Chain::Bitcoin | Chain::BitcoinCash | Chain::Litecoin | Chain::Doge | Chain::Zcash => { | ||
| Ok(Arc::new(BitcoinClient::new(alien_client, BitcoinChain::from_chain(chain).unwrap()))) | ||
| } | ||
| Chain::Cardano => Ok(Arc::new(CardanoClient::new(alien_client))), | ||
| Chain::Stellar => Ok(Arc::new(StellarClient::new(alien_client))), | ||
| Chain::Sui => Ok(Arc::new(SuiClient::new(JsonRpcClient::new(alien_client.clone())))), | ||
| Chain::Xrp => Ok(Arc::new(XRPClient::new(JsonRpcClient::new(alien_client.clone())))), | ||
| Chain::Algorand => Ok(Arc::new(AlgorandClient::new(alien_client.clone(), AlgorandClientIndexer::new(alien_client.clone())))), | ||
| Chain::Near => Ok(Arc::new(NearClient::new(JsonRpcClient::new(alien_client.clone())))), | ||
| Chain::Aptos => Ok(Arc::new(AptosClient::new(alien_client))), | ||
| Chain::Cosmos | Chain::Osmosis | Chain::Celestia | Chain::Thorchain | Chain::Injective | Chain::Sei | Chain::Noble => { | ||
| Ok(Arc::new(CosmosClient::new(CosmosChain::from_chain(chain).unwrap(), alien_client))) | ||
| } | ||
| Chain::Ton => Ok(Arc::new(TonClient::new(alien_client))), | ||
| Chain::Tron => Ok(Arc::new(TronClient::new(alien_client.clone(), TronGridClient::new(alien_client.clone(), String::new())))), | ||
| Chain::Polkadot => Ok(Arc::new(PolkadotClient::new(alien_client))), | ||
| Chain::Solana => Ok(Arc::new(SolanaClient::new(JsonRpcClient::new(alien_client.clone())))), | ||
| Chain::Ethereum | ||
| | Chain::Arbitrum | ||
| | Chain::SmartChain | ||
| | Chain::Polygon | ||
| | Chain::Optimism | ||
| | Chain::Base | ||
| | Chain::AvalancheC | ||
| | Chain::OpBNB | ||
| | Chain::Fantom | ||
| | Chain::Gnosis | ||
| | Chain::Manta | ||
| | Chain::Blast | ||
| | Chain::ZkSync | ||
| | Chain::Linea | ||
| | Chain::Mantle | ||
| | Chain::Celo | ||
| | Chain::World | ||
| | Chain::Sonic | ||
| | Chain::SeiEvm | ||
| | Chain::Abstract | ||
| | Chain::Berachain | ||
| | Chain::Ink | ||
| | Chain::Unichain | ||
| | Chain::Hyperliquid | ||
| | Chain::Plasma | ||
| | Chain::Monad | ||
| | Chain::XLayer | ||
| | Chain::Stable => Ok(Arc::new(EthereumClient::new(JsonRpcClient::new(alien_client), EVMChain::from_chain(chain).unwrap()))), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait::async_trait] | ||
|
|
@@ -154,13 +70,16 @@ impl GemGateway { | |
| #[uniffi::constructor] | ||
| pub fn new(provider: Arc<dyn AlienProvider>, preferences: Arc<dyn GemPreferences>, secure_preferences: Arc<dyn GemPreferences>, api_url: String) -> Self { | ||
| let api_client = GemApiClient::new(api_url, provider.clone()); | ||
| let yielder = Yielder::new(Arc::new(AlienProviderWrapper::new(provider.clone()))); | ||
| let chain_factory = Arc::new(ChainClientFactory::new(provider.clone(), preferences, secure_preferences)); | ||
| let alien_wrapper = Arc::new(AlienProviderWrapper::new(provider)); | ||
| let yielder = Yielder::new(alien_wrapper.clone()); | ||
| let swapper = Swapper::new(alien_wrapper); | ||
| let status_provider = StatusProvider::new(chain_factory.clone(), swapper); | ||
| Self { | ||
| provider, | ||
| preferences, | ||
| secure_preferences, | ||
| api_client, | ||
| chain_factory, | ||
| yielder, | ||
| status_provider, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -191,8 +110,7 @@ impl GemGateway { | |
| } | ||
|
|
||
| pub async fn get_transaction_status(&self, chain: Chain, request: GemTransactionStateRequest) -> Result<GemTransactionUpdate, GatewayError> { | ||
| self.with_provider(chain, |provider| async move { provider.get_transaction_status(request.into()).await }) | ||
| .await | ||
| Ok(self.status_provider.get(chain, request).await?) | ||
| } | ||
|
|
||
| pub async fn get_chain_id(&self, chain: Chain) -> Result<String, GatewayError> { | ||
|
|
@@ -301,7 +219,7 @@ impl GemGateway { | |
| } | ||
|
|
||
| pub async fn get_is_token_address(&self, chain: Chain, token_id: String) -> Result<bool, GatewayError> { | ||
| Ok(self.provider(chain).await?.get_is_token_address(&token_id)) | ||
| Ok(self.chain_factory.create(chain).await?.get_is_token_address(&token_id)) | ||
| } | ||
|
|
||
| pub async fn get_balance_earn(&self, chain: Chain, address: String, token_ids: Vec<String>) -> Result<Vec<GemAssetBalance>, GatewayError> { | ||
|
|
@@ -325,7 +243,7 @@ impl GemGateway { | |
|
|
||
| pub async fn get_node_status(&self, chain: Chain, url: &str) -> Result<GemNodeStatus, GatewayError> { | ||
| let start_time = std::time::Instant::now(); | ||
| let provider = self.provider_with_url(chain, url.to_string()).await?; | ||
| let provider = self.chain_factory.create_with_url(chain, url.to_string()).await?; | ||
|
|
||
| let (chain_id, latest_block_number) = futures::try_join!(provider.get_chain_id(), provider.get_block_latest_number()).map_err(map_network_error)?; | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Using
unwrap()here is risky. If a new chain is added to the match arm but is not supported byBitcoinChain::from_chain, the application will panic at runtime. It's safer to handle the potential error by returning aGatewayError::PlatformError.