Skip to content
This repository was archived by the owner on Jun 1, 2026. It is now read-only.
2 changes: 1 addition & 1 deletion crates/gem_hypercore/src/provider/transaction_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<C: Client> HyperCoreClient<C> {
pub async fn transaction_state(&self, request: TransactionStateRequest) -> Result<TransactionUpdate, Box<dyn Error + Sync + Send>> {
match request.id.parse::<u64>() {
Ok(oid) => {
let start_time = (request.created_at - 5) * 1000;
let start_time = request.created_at - 5_000;
let fills = self.get_user_fills_by_time(&request.sender_address, start_time).await?;
Ok(transaction_state_mapper::map_transaction_state_order(fills, oid, request.id))
}
Expand Down
21 changes: 16 additions & 5 deletions crates/primitives/src/job_configuration.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
use serde::{Deserialize, Serialize};
use typeshare::typeshare;
use crate::Chain;

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[typeshare(swift = "Sendable")]
#[serde(rename_all = "camelCase")]
const INITIAL_CAP_MS: u32 = 5_000;
const MAX_INTERVAL_MS: u32 = 15_000;
const STEP_FACTOR: f32 = 1.1;

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct JobConfiguration {
pub initial_interval_ms: u32,
pub max_interval_ms: u32,
pub step_factor: f32,
}

impl JobConfiguration {
pub fn transaction_state(chain: Chain) -> Self {
Self {
initial_interval_ms: chain.block_time().clamp(1, INITIAL_CAP_MS),
max_interval_ms: MAX_INTERVAL_MS,
step_factor: STEP_FACTOR,
}
}
}
23 changes: 23 additions & 0 deletions crates/primitives/src/transaction_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,26 @@ pub enum TransactionState {
Failed,
Reverted,
}

impl TransactionState {
pub fn is_completed(&self) -> bool {
match self {
Self::Confirmed | Self::Failed | Self::Reverted => true,
Self::Pending | Self::InTransit => false,
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_is_completed() {
assert!(TransactionState::Confirmed.is_completed());
assert!(TransactionState::Failed.is_completed());
assert!(TransactionState::Reverted.is_completed());
assert!(!TransactionState::Pending.is_completed());
assert!(!TransactionState::InTransit.is_completed());
}
}
3 changes: 2 additions & 1 deletion crates/primitives/src/transaction_update.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::transaction_metadata_types::TransactionPerpetualMetadata;
use crate::transaction_metadata_types::{TransactionPerpetualMetadata, TransactionSwapMetadata};
use crate::transaction_state::TransactionState;
use num_bigint::BigInt;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -30,6 +30,7 @@ pub enum TransactionChange {
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum TransactionMetadata {
Perpetual(TransactionPerpetualMetadata),
Swap(TransactionSwapMetadata),
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
Expand Down
103 changes: 103 additions & 0 deletions gemstone/src/gateway/chain_factory.rs
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())))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using unwrap() here is risky. If a new chain is added to the match arm but is not supported by BitcoinChain::from_chain, the application will panic at runtime. It's safer to handle the potential error by returning a GatewayError::PlatformError.

Suggested change
Ok(Arc::new(BitcoinClient::new(alien_client, BitcoinChain::from_chain(chain).unwrap())))
Ok(Arc::new(BitcoinClient::new(alien_client, BitcoinChain::from_chain(chain).ok_or(GatewayError::PlatformError { msg: format!("Unsupported bitcoin chain: {:?}", chain) })?)))

}
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())))),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The TronGridClient is initialized with an empty string for the API key. This will likely lead to rate limiting or authentication errors when interacting with TronGrid. Consider making the API key configurable via preferences or environment variables.

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()))),
}
}
}
10 changes: 10 additions & 0 deletions gemstone/src/gateway/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::alien::AlienError;
use crate::transaction_state::TransactionStatusError;
use gem_jsonrpc::types::{ERROR_CLIENT_ERROR, JsonRpcError};
use std::{error::Error, fmt::Display};

Expand Down Expand Up @@ -29,6 +30,15 @@ impl From<uniffi::UnexpectedUniFFICallbackError> for GatewayError {
}
}

impl From<TransactionStatusError> for GatewayError {
fn from(err: TransactionStatusError) -> Self {
match err {
TransactionStatusError::NetworkError(msg) => Self::NetworkError { msg },
TransactionStatusError::PlatformError(msg) => Self::PlatformError { msg },
}
}
}

pub(crate) fn map_network_error(error: Box<dyn Error + Send + Sync>) -> GatewayError {
if let Some(jsonrpc_error) = error.downcast_ref::<JsonRpcError>()
&& jsonrpc_error.code == ERROR_CLIENT_ERROR
Expand Down
Loading
Loading