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

Commit 38e2184

Browse files
committed
Add cache_headers
1 parent 32adf87 commit 38e2184

5 files changed

Lines changed: 28 additions & 17 deletions

File tree

‎crates/gem_jsonrpc/src/rpc.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use std::{
1010
sync::Arc,
1111
};
1212

13-
pub const X_CACHE_TTL: &str = "x-gem-cache-ttl";
14-
1513
pub type RpcResponse = Response;
1614

1715
pub trait RpcClientError: Error + Send + Sync + 'static + Display + Sized {
@@ -52,7 +50,7 @@ impl Target {
5250
self.headers = Some(HashMap::new());
5351
}
5452
if let Some(headers) = self.headers.as_mut() {
55-
headers.insert(X_CACHE_TTL.into(), ttl.to_string());
53+
headers.insert(gem_client::X_CACHE_TTL.into(), ttl.to_string());
5654
}
5755
self
5856
}

‎crates/swapper/src/cache.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ use std::collections::HashMap;
33

44
pub(crate) const STATIC_READ_CACHE_TTL_SECONDS: u64 = 30 * primitives::duration::DAY.as_secs();
55

6+
pub(crate) fn cache_headers(ttl_seconds: u64) -> HashMap<String, String> {
7+
HashMap::from([(X_CACHE_TTL.to_string(), ttl_seconds.to_string())])
8+
}
9+
610
pub(crate) fn static_read_cache_headers() -> HashMap<String, String> {
7-
HashMap::from([(X_CACHE_TTL.to_string(), STATIC_READ_CACHE_TTL_SECONDS.to_string())])
11+
cache_headers(STATIC_READ_CACHE_TTL_SECONDS)
812
}

‎crates/swapper/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub mod uniswap;
3535

3636
use number_formatter::BigNumberFormatter;
3737

38-
pub(crate) use cache::{STATIC_READ_CACHE_TTL_SECONDS, static_read_cache_headers};
38+
pub(crate) use cache::{STATIC_READ_CACHE_TTL_SECONDS, cache_headers, static_read_cache_headers};
3939

4040
/// Converts a human-readable amount string to base units value.
4141
pub fn amount_to_value(token: &str, decimals: u32) -> Option<String> {

‎crates/swapper/src/stonfi/client.rs‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use gem_ton::{
1010
use num_bigint::BigUint;
1111
use num_traits::{Num, ToPrimitive};
1212
use primitives::Address as PrimitiveAddress;
13-
use std::{collections::HashMap, fmt::Debug, str::FromStr};
13+
use std::{fmt::Debug, str::FromStr};
1414

1515
const GET_WALLET_ADDRESS_METHOD: &str = "get_wallet_address";
1616
const GET_POOL_ADDRESS_METHOD: &str = "get_pool_address";
@@ -84,17 +84,21 @@ where
8484
}
8585

8686
async fn run_get_method(&self, address: &str, method: &str, stack: Vec<StackArg>) -> Result<RunGetMethodResult, SwapperError> {
87-
self.run_get_method_with_headers(address, method, stack, HashMap::new()).await
87+
let result = self
88+
.ton_client
89+
.run_get_method(address, method, stack)
90+
.await
91+
.map_err(|err| SwapperError::ComputeQuoteError(err.to_string()))?;
92+
if result.exit_code != 0 {
93+
return Err(SwapperError::ComputeQuoteError(format!("TON get-method {method} exit code {}", result.exit_code)));
94+
}
95+
Ok(result)
8896
}
8997

9098
async fn run_static_get_method(&self, address: &str, method: &str, stack: Vec<StackArg>) -> Result<RunGetMethodResult, SwapperError> {
91-
self.run_get_method_with_headers(address, method, stack, static_read_cache_headers()).await
92-
}
93-
94-
async fn run_get_method_with_headers(&self, address: &str, method: &str, stack: Vec<StackArg>, headers: HashMap<String, String>) -> Result<RunGetMethodResult, SwapperError> {
9599
let result = self
96100
.ton_client
97-
.run_get_method_with_headers(address, method, stack, headers)
101+
.run_get_method_with_headers(address, method, stack, static_read_cache_headers())
98102
.await
99103
.map_err(|err| SwapperError::ComputeQuoteError(err.to_string()))?;
100104
if result.exit_code != 0 {

‎crates/swapper/src/thorchain/client.rs‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ use super::{
22
asset::THORChainAsset,
33
model::{AsgardVault, InboundAddress, QuoteSwapRequest, QuoteSwapResponse, TransactionStatus},
44
};
5-
use crate::SwapperError;
6-
use gem_client::{Client, ClientExt, X_CACHE_TTL};
5+
use crate::{SwapperError, cache_headers};
6+
use gem_client::{Client, ClientExt};
7+
use primitives::duration::MINUTE;
78
use serde_urlencoded;
8-
use std::{collections::HashMap, fmt::Debug};
9+
use std::fmt::Debug;
10+
11+
const INBOUND_ADDRESS_CACHE_TTL_SECONDS: u64 = 10 * MINUTE.as_secs();
912

1013
#[derive(Clone, Debug)]
1114
pub struct ThorChainSwapClient<C>
@@ -48,8 +51,10 @@ where
4851
}
4952

5053
pub async fn get_inbound_addresses(&self) -> Result<Vec<InboundAddress>, SwapperError> {
51-
let headers = HashMap::from([(X_CACHE_TTL.to_string(), "600".to_string())]);
52-
self.client.get_with_headers("/thorchain/inbound_addresses", headers).await.map_err(SwapperError::from)
54+
self.client
55+
.get_with_headers("/thorchain/inbound_addresses", cache_headers(INBOUND_ADDRESS_CACHE_TTL_SECONDS))
56+
.await
57+
.map_err(SwapperError::from)
5358
}
5459

5560
pub async fn get_asgard_vaults(&self) -> Result<Vec<AsgardVault>, SwapperError> {

0 commit comments

Comments
 (0)