Skip to content
This repository was archived by the owner on Jun 1, 2026. It is now read-only.

Commit 4d14fc4

Browse files
authored
Transaction state (#1104)
1 parent 6420fa4 commit 4d14fc4

13 files changed

Lines changed: 334 additions & 111 deletions

File tree

‎crates/gem_hypercore/src/provider/transaction_state.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<C: Client> HyperCoreClient<C> {
1818
pub async fn transaction_state(&self, request: TransactionStateRequest) -> Result<TransactionUpdate, Box<dyn Error + Sync + Send>> {
1919
match request.id.parse::<u64>() {
2020
Ok(oid) => {
21-
let start_time = (request.created_at - 5) * 1000;
21+
let start_time = request.created_at - 5_000;
2222
let fills = self.get_user_fills_by_time(&request.sender_address, start_time).await?;
2323
Ok(transaction_state_mapper::map_transaction_state_order(fills, oid, request.id))
2424
}
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
use serde::{Deserialize, Serialize};
2-
use typeshare::typeshare;
1+
use crate::Chain;
32

4-
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
5-
#[typeshare(swift = "Sendable")]
6-
#[serde(rename_all = "camelCase")]
3+
const INITIAL_CAP_MS: u32 = 5_000;
4+
const MAX_INTERVAL_MS: u32 = 15_000;
5+
const STEP_FACTOR: f32 = 1.1;
6+
7+
#[derive(Debug, Clone, Copy, PartialEq)]
78
pub struct JobConfiguration {
89
pub initial_interval_ms: u32,
910
pub max_interval_ms: u32,
1011
pub step_factor: f32,
1112
}
13+
14+
impl JobConfiguration {
15+
pub fn transaction_state(chain: Chain) -> Self {
16+
Self {
17+
initial_interval_ms: chain.block_time().clamp(1, INITIAL_CAP_MS),
18+
max_interval_ms: MAX_INTERVAL_MS,
19+
step_factor: STEP_FACTOR,
20+
}
21+
}
22+
}

‎crates/primitives/src/transaction_state.rs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,26 @@ pub enum TransactionState {
1313
Failed,
1414
Reverted,
1515
}
16+
17+
impl TransactionState {
18+
pub fn is_completed(&self) -> bool {
19+
match self {
20+
Self::Confirmed | Self::Failed | Self::Reverted => true,
21+
Self::Pending | Self::InTransit => false,
22+
}
23+
}
24+
}
25+
26+
#[cfg(test)]
27+
mod tests {
28+
use super::*;
29+
30+
#[test]
31+
fn test_is_completed() {
32+
assert!(TransactionState::Confirmed.is_completed());
33+
assert!(TransactionState::Failed.is_completed());
34+
assert!(TransactionState::Reverted.is_completed());
35+
assert!(!TransactionState::Pending.is_completed());
36+
assert!(!TransactionState::InTransit.is_completed());
37+
}
38+
}

‎crates/primitives/src/transaction_update.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::transaction_metadata_types::TransactionPerpetualMetadata;
1+
use crate::transaction_metadata_types::{TransactionPerpetualMetadata, TransactionSwapMetadata};
22
use crate::transaction_state::TransactionState;
33
use num_bigint::BigInt;
44
use serde::{Deserialize, Serialize};
@@ -30,6 +30,7 @@ pub enum TransactionChange {
3030
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
3131
pub enum TransactionMetadata {
3232
Perpetual(TransactionPerpetualMetadata),
33+
Swap(TransactionSwapMetadata),
3334
}
3435

3536
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
use super::{GatewayError, GemPreferences, PreferencesWrapper};
2+
use crate::alien::{AlienProvider, new_alien_client};
3+
use crate::network::JsonRpcClient;
4+
use chain_traits::ChainTraits;
5+
use gem_algorand::rpc::AlgorandClientIndexer;
6+
use gem_algorand::rpc::client::AlgorandClient;
7+
use gem_aptos::rpc::client::AptosClient;
8+
use gem_bitcoin::rpc::client::BitcoinClient;
9+
use gem_cardano::rpc::client::CardanoClient;
10+
use gem_cosmos::rpc::client::CosmosClient;
11+
use gem_evm::rpc::EthereumClient;
12+
use gem_hypercore::rpc::client::HyperCoreClient;
13+
use gem_near::rpc::client::NearClient;
14+
use gem_polkadot::rpc::client::PolkadotClient;
15+
use gem_solana::rpc::client::SolanaClient;
16+
use gem_stellar::rpc::client::StellarClient;
17+
use gem_sui::rpc::client::SuiClient;
18+
use gem_ton::rpc::client::TonClient;
19+
use gem_tron::rpc::{client::TronClient, trongrid::client::TronGridClient};
20+
use gem_xrp::rpc::client::XRPClient;
21+
use primitives::{BitcoinChain, Chain, EVMChain, chain_cosmos::CosmosChain};
22+
use std::sync::Arc;
23+
24+
pub struct ChainClientFactory {
25+
alien: Arc<dyn AlienProvider>,
26+
preferences: Arc<dyn GemPreferences>,
27+
secure_preferences: Arc<dyn GemPreferences>,
28+
}
29+
30+
impl ChainClientFactory {
31+
pub fn new(alien: Arc<dyn AlienProvider>, preferences: Arc<dyn GemPreferences>, secure_preferences: Arc<dyn GemPreferences>) -> Self {
32+
Self {
33+
alien,
34+
preferences,
35+
secure_preferences,
36+
}
37+
}
38+
39+
pub async fn create(&self, chain: Chain) -> Result<Arc<dyn ChainTraits>, GatewayError> {
40+
let url = self.alien.get_endpoint(chain).map_err(|e| GatewayError::PlatformError { msg: e.to_string() })?;
41+
self.create_with_url(chain, url).await
42+
}
43+
44+
pub async fn create_with_url(&self, chain: Chain, url: String) -> Result<Arc<dyn ChainTraits>, GatewayError> {
45+
let alien_client = new_alien_client(url, self.alien.clone());
46+
match chain {
47+
Chain::HyperCore => {
48+
let preferences = Arc::new(PreferencesWrapper {
49+
preferences: self.preferences.clone(),
50+
});
51+
let secure_preferences = Arc::new(PreferencesWrapper {
52+
preferences: self.secure_preferences.clone(),
53+
});
54+
Ok(Arc::new(HyperCoreClient::new_with_preferences(alien_client, preferences, secure_preferences)))
55+
}
56+
Chain::Bitcoin | Chain::BitcoinCash | Chain::Litecoin | Chain::Doge | Chain::Zcash => {
57+
Ok(Arc::new(BitcoinClient::new(alien_client, BitcoinChain::from_chain(chain).unwrap())))
58+
}
59+
Chain::Cardano => Ok(Arc::new(CardanoClient::new(alien_client))),
60+
Chain::Stellar => Ok(Arc::new(StellarClient::new(alien_client))),
61+
Chain::Sui => Ok(Arc::new(SuiClient::new(JsonRpcClient::new(alien_client.clone())))),
62+
Chain::Xrp => Ok(Arc::new(XRPClient::new(JsonRpcClient::new(alien_client.clone())))),
63+
Chain::Algorand => Ok(Arc::new(AlgorandClient::new(alien_client.clone(), AlgorandClientIndexer::new(alien_client.clone())))),
64+
Chain::Near => Ok(Arc::new(NearClient::new(JsonRpcClient::new(alien_client.clone())))),
65+
Chain::Aptos => Ok(Arc::new(AptosClient::new(alien_client))),
66+
Chain::Cosmos | Chain::Osmosis | Chain::Celestia | Chain::Thorchain | Chain::Injective | Chain::Sei | Chain::Noble => {
67+
Ok(Arc::new(CosmosClient::new(CosmosChain::from_chain(chain).unwrap(), alien_client)))
68+
}
69+
Chain::Ton => Ok(Arc::new(TonClient::new(alien_client))),
70+
Chain::Tron => Ok(Arc::new(TronClient::new(alien_client.clone(), TronGridClient::new(alien_client.clone(), String::new())))),
71+
Chain::Polkadot => Ok(Arc::new(PolkadotClient::new(alien_client))),
72+
Chain::Solana => Ok(Arc::new(SolanaClient::new(JsonRpcClient::new(alien_client.clone())))),
73+
Chain::Ethereum
74+
| Chain::Arbitrum
75+
| Chain::SmartChain
76+
| Chain::Polygon
77+
| Chain::Optimism
78+
| Chain::Base
79+
| Chain::AvalancheC
80+
| Chain::OpBNB
81+
| Chain::Fantom
82+
| Chain::Gnosis
83+
| Chain::Manta
84+
| Chain::Blast
85+
| Chain::ZkSync
86+
| Chain::Linea
87+
| Chain::Mantle
88+
| Chain::Celo
89+
| Chain::World
90+
| Chain::Sonic
91+
| Chain::SeiEvm
92+
| Chain::Abstract
93+
| Chain::Berachain
94+
| Chain::Ink
95+
| Chain::Unichain
96+
| Chain::Hyperliquid
97+
| Chain::Plasma
98+
| Chain::Monad
99+
| Chain::XLayer
100+
| Chain::Stable => Ok(Arc::new(EthereumClient::new(JsonRpcClient::new(alien_client), EVMChain::from_chain(chain).unwrap()))),
101+
}
102+
}
103+
}

‎gemstone/src/gateway/error.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::alien::AlienError;
2+
use crate::transaction_state::TransactionStatusError;
23
use gem_jsonrpc::types::{ERROR_CLIENT_ERROR, JsonRpcError};
34
use std::{error::Error, fmt::Display};
45

@@ -29,6 +30,15 @@ impl From<uniffi::UnexpectedUniFFICallbackError> for GatewayError {
2930
}
3031
}
3132

33+
impl From<TransactionStatusError> for GatewayError {
34+
fn from(err: TransactionStatusError) -> Self {
35+
match err {
36+
TransactionStatusError::NetworkError(msg) => Self::NetworkError { msg },
37+
TransactionStatusError::PlatformError(msg) => Self::PlatformError { msg },
38+
}
39+
}
40+
}
41+
3242
pub(crate) fn map_network_error(error: Box<dyn Error + Send + Sync>) -> GatewayError {
3343
if let Some(jsonrpc_error) = error.downcast_ref::<JsonRpcError>()
3444
&& jsonrpc_error.code == ERROR_CLIENT_ERROR

0 commit comments

Comments
 (0)