Skip to content

Commit bba6bcc

Browse files
zanniszannis
andauthored
fix: reuse correct serialization function in top up (#49)
* fix: reuse correct serialization function in top up * fmt * fix: [rust-sdk] pass the api key in the correct header * fmt --------- Co-authored-by: zannis <[email protected]>
1 parent 106c9ec commit bba6bcc

File tree

5 files changed

+31
-45
lines changed

5 files changed

+31
-45
lines changed

crates/core/src/background_tasks/automatic_top_up_task.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,10 @@ impl AutomaticTopUpTask {
340340
Ok(balance) => {
341341
if balance < token_config.min_balance {
342342
info!(
343-
"Address {} ERC-20 balance ({}) is below minimum ({}) for token {}, needs top-up",
343+
"Relayer {} ERC-20 balance ({}) is below minimum ({}) for token {}, needs top-up",
344344
address,
345-
format_token_amount(&balance),
346-
format_token_amount(&token_config.min_balance),
345+
format_token_amount(&balance, token_config.decimals),
346+
format_token_amount(&token_config.min_balance, token_config.decimals),
347347
token_config.address
348348
);
349349
addresses_needing_top_up.push(*address);
@@ -410,7 +410,7 @@ impl AutomaticTopUpTask {
410410
info!(
411411
"Topped up address {} with {} tokens ({}). Transaction: {}",
412412
address,
413-
format_token_amount(&token_config.top_up_amount),
413+
format_token_amount(&token_config.top_up_amount, token_config.decimals),
414414
token_config.address,
415415
tx_hash
416416
);
@@ -769,7 +769,7 @@ impl AutomaticTopUpTask {
769769
info!(
770770
"From address {} has ERC-20 token balance: {} for token {}",
771771
from_address,
772-
format_token_amount(&balance),
772+
format_token_amount(&balance, token_config.decimals),
773773
token_config.address
774774
);
775775

@@ -780,16 +780,16 @@ impl AutomaticTopUpTask {
780780
info!(
781781
"From address {} requires {} tokens for token {}",
782782
from_address,
783-
format_token_amount(&min_required_balance),
783+
format_token_amount(&min_required_balance, token_config.decimals),
784784
token_config.address
785785
);
786786

787787
if balance < min_required_balance {
788788
warn!(
789789
"From address {} token balance ({}) is insufficient for top-up transaction. Required: {} for token {}",
790790
from_address,
791-
format_token_amount(&balance),
792-
format_token_amount(&min_required_balance),
791+
format_token_amount(&balance, token_config.decimals),
792+
format_token_amount(&min_required_balance, token_config.decimals),
793793
token_config.address
794794
);
795795
return Ok(false);
@@ -838,7 +838,7 @@ impl AutomaticTopUpTask {
838838
"Sending ERC-20 top-up transaction: {} -> {} ({} tokens of {}){}",
839839
from_address,
840840
relayer_address,
841-
format_token_amount(&token_config.top_up_amount),
841+
format_token_amount(&token_config.top_up_amount, token_config.decimals),
842842
token_config.address,
843843
if config.via_safe() { " via Safe" } else { "" }
844844
);

crates/core/src/shared/utils.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,24 @@ pub fn convert_blob_strings_to_blobs(
4444
}
4545

4646
pub fn format_wei_to_eth(wei: &U256) -> String {
47-
let eth_divisor = U256::from(10u64.pow(18));
48-
let whole_eth = wei / eth_divisor;
49-
let remainder = wei % eth_divisor;
47+
format_token_amount(wei, 18)
48+
}
49+
50+
/// Formats a token amount to a human-readable string.
51+
pub fn format_token_amount(amount: &U256, decimals: u8) -> String {
52+
let unit_divisor = U256::from(10u64.pow(decimals.into()));
53+
let whole_part = amount / unit_divisor;
54+
let remainder = amount % unit_divisor;
5055

5156
if remainder.is_zero() {
52-
format!("{}", whole_eth)
57+
format!("{}", whole_part)
5358
} else {
54-
let decimal_str = format!("{:018}", remainder);
59+
let decimal_str = format!("{:0width$}", remainder, width = decimals as usize);
5560
let decimal_trimmed = decimal_str.trim_end_matches('0');
56-
format!("{}.{}", whole_eth, decimal_trimmed)
61+
format!("{}.{}", whole_part, decimal_trimmed)
5762
}
5863
}
5964

60-
/// Formats a token amount to a human-readable string.
61-
pub fn format_token_amount(amount: &U256) -> String {
62-
// For now, use the same formatting as ETH (18 decimals)
63-
// This can be enhanced to support different token decimals
64-
format_wei_to_eth(amount)
65-
}
66-
6765
pub async fn get_chain_id(provider_url: &str) -> Result<ChainId, String> {
6866
let provider = create_retry_client(provider_url)
6967
.await

crates/core/src/yaml.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::gas::{
1313
TenderlyGasProviderSetupConfig,
1414
};
1515
use crate::network::{ChainId, Network};
16+
use crate::shared::utils::format_token_amount;
1617
use crate::transaction::types::TransactionSpeed;
1718
use crate::{rrelayer_error, shared::common_types::EvmAddress};
1819

@@ -692,11 +693,11 @@ impl Serialize for NativeTokenConfig {
692693
let mut state = serializer.serialize_struct("NativeTokenConfig", 4)?;
693694
state.serialize_field(
694695
"min_balance",
695-
&serialize_amount_with_decimals(&self.min_balance, self.decimals),
696+
&format_token_amount(&self.min_balance, self.decimals),
696697
)?;
697698
state.serialize_field(
698699
"top_up_amount",
699-
&serialize_amount_with_decimals(&self.top_up_amount, self.decimals),
700+
&format_token_amount(&self.top_up_amount, self.decimals),
700701
)?;
701702
state.serialize_field("decimals", &self.decimals)?;
702703
state.end()
@@ -747,11 +748,11 @@ impl Serialize for Erc20TokenConfig {
747748
state.serialize_field("address", &self.address)?;
748749
state.serialize_field(
749750
"min_balance",
750-
&serialize_amount_with_decimals(&self.min_balance, self.decimals),
751+
&format_token_amount(&self.min_balance, self.decimals),
751752
)?;
752753
state.serialize_field(
753754
"top_up_amount",
754-
&serialize_amount_with_decimals(&self.top_up_amount, self.decimals),
755+
&format_token_amount(&self.top_up_amount, self.decimals),
755756
)?;
756757
state.serialize_field("decimals", &self.decimals)?;
757758
state.end()
@@ -823,20 +824,6 @@ fn default_decimals() -> u8 {
823824
18
824825
}
825826

826-
fn serialize_amount_with_decimals(amount: &U256, decimals: u8) -> String {
827-
let divisor = U256::from(10u64.pow(decimals as u32));
828-
let whole_part = amount / divisor;
829-
let remainder = amount % divisor;
830-
831-
if remainder.is_zero() {
832-
format!("{}", whole_part)
833-
} else {
834-
let decimal_str = format!("{:0width$}", remainder, width = decimals as usize);
835-
let decimal_trimmed = decimal_str.trim_end_matches('0');
836-
format!("{}.{}", whole_part, decimal_trimmed)
837-
}
838-
}
839-
840827
#[derive(Debug, Clone)]
841828
pub enum AllOrNetworks {
842829
All,

crates/sdk/src/api/http.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use base64::{Engine as _, engine::general_purpose};
22
use reqwest::{
33
Client,
4-
header::{AUTHORIZATION, CONTENT_TYPE, HeaderMap, HeaderValue},
4+
header::{AUTHORIZATION, CONTENT_TYPE, HeaderMap, HeaderName, HeaderValue},
55
};
66
use serde::{Serialize, de::DeserializeOwned};
77

@@ -40,10 +40,8 @@ impl HttpClient {
4040
);
4141
}
4242
AuthConfig::ApiKey { api_key } => {
43-
headers.insert(
44-
AUTHORIZATION,
45-
HeaderValue::from_str(&format!("Bearer {}", api_key)).unwrap(),
46-
);
43+
let header_name = HeaderName::from_static("x-rrelayer-api-key");
44+
headers.insert(header_name, HeaderValue::from_str(api_key).unwrap());
4745
}
4846
}
4947

documentation/rrelayer/docs/pages/changelog.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
### Bug fixes
1212

13+
- fix: use correct serialization function for ERC20 tokens in topup flow
14+
- fix: Rust SDK now passes API key in `x-rrelayer-api-key` header instead of `Authorization` header
15+
1316
---
1417

1518
### Breaking changes

0 commit comments

Comments
 (0)