From b900ebf129b54d563e18b0f17b5a9dd408b93c1a Mon Sep 17 00:00:00 2001 From: Aman Raj <42104907+AmanRaj1608@users.noreply.github.com> Date: Wed, 17 Jul 2024 19:19:47 +0400 Subject: [PATCH] fix: token amount in approval (#44) * fix: token amount in approval * feat: Add source amount to BridgeResult struct and methods --------- Co-authored-by: Ankur Dubey --- crates/routing-engine/src/lib.rs | 4 ++ crates/routing-engine/src/routing_engine.rs | 26 +++++++------ .../routing-engine/src/settlement_engine.rs | 37 +++++++++++++++---- 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/crates/routing-engine/src/lib.rs b/crates/routing-engine/src/lib.rs index 4aed5b5..56bd89a 100644 --- a/crates/routing-engine/src/lib.rs +++ b/crates/routing-engine/src/lib.rs @@ -111,6 +111,7 @@ pub enum RouteError { #[derive(Debug)] pub struct BridgeResult { route: Route, + source_amount: f64, source_amount_in_usd: f64, from_address: String, to_address: String, @@ -147,6 +148,7 @@ impl BridgeResult { from_token_id: &String, to_token_id: &String, is_smart_contract_deposit: bool, + source_amount: f64, source_amount_in_usd: f64, from_address: String, to_address: String, @@ -160,6 +162,7 @@ impl BridgeResult { to_token_id, is_smart_contract_deposit, )?, + source_amount, source_amount_in_usd, from_address, to_address, @@ -200,6 +203,7 @@ mod test { &"USDT".to_string(), false, 100.0, + 200.0, "0x123".to_string(), "0x456".to_string(), ) diff --git a/crates/routing-engine/src/routing_engine.rs b/crates/routing-engine/src/routing_engine.rs index 663546c..d5db3a8 100644 --- a/crates/routing-engine/src/routing_engine.rs +++ b/crates/routing-engine/src/routing_engine.rs @@ -2,22 +2,21 @@ use std::cmp; use std::collections::HashMap; use std::sync::Arc; -use alloy::dyn_abi::abi::Token; use futures::stream::{self, StreamExt}; use log::{debug, error, info}; use thiserror::Error; use tokio::sync::{Mutex, RwLock}; use account_aggregation::{service::AccountAggregationService, types::TokenWithBalance}; -use config::{ChainConfig, Config, config::BucketConfig, SolverConfig, TokenConfig}; +use config::{config::BucketConfig, ChainConfig, Config, SolverConfig, TokenConfig}; use storage::{KeyValueStore, RedisClient, RedisClientError}; +use crate::token_price::utils::{get_token_price, Errors}; +use crate::token_price::TokenPriceProvider; use crate::{ - BridgeResult, - BridgeResultVecWrapper, estimator::{Estimator, LinearRegressionEstimator}, Route, + estimator::{Estimator, LinearRegressionEstimator}, + BridgeResult, BridgeResultVecWrapper, Route, }; -use crate::token_price::TokenPriceProvider; -use crate::token_price::utils::{Errors, get_token_price}; const FETCH_REDIS_KEYS_BATCH_SIZE: usize = 50; @@ -241,6 +240,7 @@ impl RoutingEngine { to_chain, &balance.token, to_token, + balance.amount, amount_to_take, false, &balance.address, @@ -321,6 +321,7 @@ impl RoutingEngine { to_chain_id: u32, from_token_id: &str, to_token_id: &str, + token_amount: f64, token_amount_in_usd: f64, is_smart_contract_deposit: bool, from_address: &str, @@ -344,6 +345,7 @@ impl RoutingEngine { Ok(BridgeResult { route: Route { from_chain, to_chain, from_token, to_token, is_smart_contract_deposit }, + source_amount: token_amount, source_amount_in_usd: token_amount_in_usd, from_address: from_address.to_string(), to_address: to_address.to_string(), @@ -366,20 +368,20 @@ mod tests { use account_aggregation::service::AccountAggregationService; use config::{ - BucketConfig, ChainConfig, get_sample_config, SolverConfig, TokenConfig, + get_sample_config, BucketConfig, ChainConfig, SolverConfig, TokenConfig, TokenConfigByChainConfigs, }; - use storage::{KeyValueStore, RedisClientError}; use storage::mongodb_client::MongoDBClient; + use storage::{KeyValueStore, RedisClientError}; + use crate::estimator::Estimator; + use crate::routing_engine::PathQuery; + use crate::token_price::TokenPriceProvider; use crate::{ - CoingeckoClient, estimator::{DataPoint, LinearRegressionEstimator}, routing_engine::{RoutingEngine, RoutingEngineError}, + CoingeckoClient, }; - use crate::estimator::Estimator; - use crate::routing_engine::PathQuery; - use crate::token_price::TokenPriceProvider; #[derive(Error, Debug, Display)] struct Err; diff --git a/crates/routing-engine/src/settlement_engine.rs b/crates/routing-engine/src/settlement_engine.rs index a94db56..2295023 100644 --- a/crates/routing-engine/src/settlement_engine.rs +++ b/crates/routing-engine/src/settlement_engine.rs @@ -1,3 +1,4 @@ +use std::cmp; use std::collections::HashMap; use std::sync::Arc; @@ -15,11 +16,11 @@ use tokio::sync::Mutex; use config::{Config, TokenAddress}; -use crate::{blockchain, BridgeResult, BridgeResultVecWrapper, Route}; use crate::blockchain::erc20::IERC20::IERC20Instance; use crate::source::{EthereumTransaction, RequiredApprovalDetails, RouteSource}; +use crate::token_price::utils::{get_token_amount_from_value_in_usd, Errors}; use crate::token_price::TokenPriceProvider; -use crate::token_price::utils::{Errors, get_token_amount_from_value_in_usd}; +use crate::{blockchain, BridgeResult, BridgeResultVecWrapper}; pub struct SettlementEngine { source: Source, @@ -100,13 +101,28 @@ impl .await .map_err(|err| SettlementEngineErrors::GetTokenAmountFromValueInUsdError(err))?; - info!("Token amount: {:?} for route {}", token_amount, route); + let max_user_balance = &route.source_amount + * f64::powi( + 10.0, + route + .route + .from_token + .by_chain + .get(&route.route.from_chain.id) + .unwrap() + .decimals + .into(), + ); + let max_user_balance = max_user_balance.floor().to_string().parse()?; + let user_balance: Uint<256, 4> = cmp::min(max_user_balance, token_amount); + + info!("Token amount: {:?} for route {}", user_balance, route); let (ethereum_transactions, required_approval_details) = self .source .generate_route_transactions( &route.route, - &token_amount, + &user_balance, &route.from_address, &route.to_address, ) @@ -126,7 +142,7 @@ impl from_address: route.from_address.clone(), to_address: route.to_address.clone(), token: route.route.from_token.symbol.clone(), - token_amount: token_amount.clone(), + token_amount: user_balance.clone(), }, t, ) @@ -224,7 +240,7 @@ impl return Ok(None); } - let required_approval = required_approval_details.amount - current_approval; + let required_approval = required_approval_details.amount; info!( "Required Approval: {:?} against requirement: {:?}", required_approval, required_approval_details @@ -353,6 +369,9 @@ pub enum SettlementEngineErrors