Skip to content

Commit

Permalink
chore: improve crypto value converter readabilty
Browse files Browse the repository at this point in the history
  • Loading branch information
kuchmenko committed Jan 17, 2025
1 parent 25fed9a commit 9a4605e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,66 +1,65 @@
use num_bigint::BigInt;
use std::str::FromStr;

use num_bigint::BigInt;

pub mod converter {
use super::*;
pub struct CryptoValueConverter {}

impl CryptoValueConverter {
pub fn value_from(value: String, decimals: i32) -> BigInt {
let decimals = decimals - 8;
if decimals > 0 {
BigInt::from_str(value.as_str()).unwrap() / BigInt::from(10).pow(decimals as u32)
BigInt::from_str(&value).unwrap() / BigInt::from(10).pow(decimals as u32)
} else {
BigInt::from_str(value.as_str()).unwrap() * BigInt::from(10).pow(decimals.unsigned_abs())
BigInt::from_str(&value).unwrap() * BigInt::from(10).pow(decimals.unsigned_abs())
}
}

pub fn value_to(value: String, decimals: i32) -> BigInt {
let decimals = decimals - 8;
if decimals > 0 {
BigInt::from_str(value.as_str()).unwrap() * BigInt::from(10).pow((decimals).unsigned_abs())
BigInt::from_str(&value).unwrap() * BigInt::from(10).pow((decimals).unsigned_abs())
} else {
BigInt::from_str(value.as_str()).unwrap() / BigInt::from(10).pow((decimals).unsigned_abs())
BigInt::from_str(&value).unwrap() / BigInt::from(10).pow((decimals).unsigned_abs())
}
}
}

#[cfg(test)]
mod tests {
use super::converter;
use super::*;
use num_bigint::BigInt;
use std::str::FromStr;

#[test]
fn test_value_from() {
let value = "1000000000".to_string();

let result = converter::value_from(value.clone(), 18);
let result = CryptoValueConverter::value_from(value.clone(), 18);
assert_eq!(result, BigInt::from_str("0").unwrap());

let result = converter::value_from(value.clone(), 10);
let result = CryptoValueConverter::value_from(value.clone(), 10);
assert_eq!(result, BigInt::from_str("10000000").unwrap());

let result = converter::value_from(value.clone(), 6);
let result = CryptoValueConverter::value_from(value.clone(), 6);
assert_eq!(result, BigInt::from_str("100000000000").unwrap());

let result = converter::value_from(value.clone(), 8);
let result = CryptoValueConverter::value_from(value.clone(), 8);
assert_eq!(result, BigInt::from(1000000000));
}

#[test]
fn test_value_to() {
let value = "10000000".to_string();

let result = converter::value_to(value.clone(), 18);
let result = CryptoValueConverter::value_to(value.clone(), 18);
assert_eq!(result, BigInt::from_str("100000000000000000").unwrap());

let result = converter::value_to(value.clone(), 10);
let result = CryptoValueConverter::value_to(value.clone(), 10);
assert_eq!(result, BigInt::from(1000000000));

let result = converter::value_to(value.clone(), 6);
let result = CryptoValueConverter::value_to(value.clone(), 6);
assert_eq!(result, BigInt::from(100000));

let result = converter::value_to(value.clone(), 8);
let result = CryptoValueConverter::value_to(value.clone(), 8);
assert_eq!(result, BigInt::from(10000000));
}
}
2 changes: 2 additions & 0 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub use self::price::{Price, PriceFull};
pub mod asset;
pub mod config;
pub use self::config::{ConfigResponse, ConfigVersions, Release};
pub mod crypto_value_converter;
pub use self::crypto_value_converter::CryptoValueConverter;
pub mod currency;
pub use self::asset::Asset;
pub mod asset_id;
Expand Down
1 change: 0 additions & 1 deletion gemstone/src/swapper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mod chainlink;
mod custom_types;
mod eth_rpc;
mod permit2_data;
mod utils;
mod weth_address;

pub mod across;
Expand Down
10 changes: 5 additions & 5 deletions gemstone/src/swapper/stargate/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use std::sync::Arc;
use alloy_primitives::{hex, Address};
use async_trait::async_trait;
use gem_evm::stargate::contract::{MessagingFee, SendParam};
use primitives::Chain;
use primitives::{Chain, CryptoValueConverter};
use serde::{Deserialize, Serialize};

use crate::{
network::AlienProvider,
swapper::{
approval::check_approval_erc20, slippage::apply_slippage_in_bp, utils::converter, ApprovalType, FetchQuoteData, GemSwapProvider, SwapChainAsset,
SwapProvider, SwapProviderData, SwapQuote, SwapQuoteData, SwapQuoteRequest, SwapRoute, SwapperError,
approval::check_approval_erc20, slippage::apply_slippage_in_bp, ApprovalType, FetchQuoteData, GemSwapProvider, SwapChainAsset, SwapProvider,
SwapProviderData, SwapQuote, SwapQuoteData, SwapQuoteRequest, SwapRoute, SwapperError,
},
};

Expand Down Expand Up @@ -103,8 +103,8 @@ impl GemSwapProvider for Stargate {

let from_decimals = self.client.get_decimals_by_asset_id(&request.from_asset)?;
let to_decimals = self.client.get_decimals_by_asset_id(&request.to_asset)?;
let mut to_value = converter::value_from(oft_quote.receipt.amountReceivedLD.to_string(), from_decimals);
to_value = converter::value_to(to_value.to_string(), to_decimals);
let mut to_value = CryptoValueConverter::value_from(oft_quote.receipt.amountReceivedLD.to_string(), from_decimals);
to_value = CryptoValueConverter::value_to(to_value.to_string(), to_decimals);

Ok(SwapQuote {
from_value: request.value.to_string(),
Expand Down
7 changes: 3 additions & 4 deletions gemstone/src/swapper/thorchain/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ use crate::swapper::asset::{
AVALANCHE_USDC, AVALANCHE_USDT, BASE_CBBTC, BASE_USDC, ETHEREUM_DAI, ETHEREUM_USDC, ETHEREUM_USDT, ETHEREUM_WBTC, SMARTCHAIN_USDC, SMARTCHAIN_USDT,
};
use crate::swapper::thorchain::client::ThorChainSwapClient;
use crate::swapper::utils::converter;
use crate::swapper::{ApprovalType, FetchQuoteData, SwapProvider, SwapProviderData, SwapQuote, SwapQuoteData, SwapQuoteRequest, SwapRoute, SwapperError};
use crate::swapper::{GemSwapProvider, SwapChainAsset};
use alloy_core::sol_types::SolCall;
use alloy_primitives::Address;
use alloy_primitives::U256;
use async_trait::async_trait;
use gem_evm::thorchain::contracts::RouterInterface;
use primitives::Chain;
use primitives::{Chain, CryptoValueConverter};
use std::str::FromStr;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
Expand Down Expand Up @@ -58,7 +57,7 @@ impl GemSwapProvider for ThorChain {
let from_asset = THORChainAsset::from_asset_id(request.clone().from_asset).ok_or(SwapperError::NotSupportedAsset)?;
let to_asset = THORChainAsset::from_asset_id(request.clone().to_asset).ok_or(SwapperError::NotSupportedAsset)?;

let value = converter::value_from(request.clone().value, from_asset.decimals as i32);
let value = CryptoValueConverter::value_from(request.clone().value, from_asset.decimals as i32);
let fee = request.options.clone().fee.unwrap_or_default().thorchain;

let quote = client
Expand All @@ -74,7 +73,7 @@ impl GemSwapProvider for ThorChain {
)
.await?;

let to_value = converter::value_to(quote.expected_amount_out, to_asset.decimals as i32);
let to_value = CryptoValueConverter::value_to(quote.expected_amount_out, to_asset.decimals as i32);

let approval: ApprovalType = {
if from_asset.use_evm_router() {
Expand Down

0 comments on commit 9a4605e

Please sign in to comment.