1+ use std:: cmp;
12use std:: collections:: HashMap ;
23use std:: sync:: Arc ;
34
@@ -15,11 +16,11 @@ use tokio::sync::Mutex;
1516
1617use config:: { Config , TokenAddress } ;
1718
18- use crate :: { blockchain, BridgeResult , BridgeResultVecWrapper , Route } ;
1919use crate :: blockchain:: erc20:: IERC20 :: IERC20Instance ;
2020use crate :: source:: { EthereumTransaction , RequiredApprovalDetails , RouteSource } ;
21+ use crate :: token_price:: utils:: { get_token_amount_from_value_in_usd, Errors } ;
2122use crate :: token_price:: TokenPriceProvider ;
22- use crate :: token_price :: utils :: { Errors , get_token_amount_from_value_in_usd } ;
23+ use crate :: { blockchain , BridgeResult , BridgeResultVecWrapper } ;
2324
2425pub struct SettlementEngine < Source : RouteSource , PriceProvider : TokenPriceProvider > {
2526 source : Source ,
@@ -100,13 +101,28 @@ impl<Source: RouteSource, PriceProvider: TokenPriceProvider>
100101 . await
101102 . map_err ( |err| SettlementEngineErrors :: GetTokenAmountFromValueInUsdError ( err) ) ?;
102103
103- info ! ( "Token amount: {:?} for route {}" , token_amount, route) ;
104+ let max_user_balance = & route. source_amount
105+ * f64:: powi (
106+ 10.0 ,
107+ route
108+ . route
109+ . from_token
110+ . by_chain
111+ . get ( & route. route . from_chain . id )
112+ . unwrap ( )
113+ . decimals
114+ . into ( ) ,
115+ ) ;
116+ let max_user_balance = max_user_balance. floor ( ) . to_string ( ) . parse ( ) ?;
117+ let user_balance: Uint < 256 , 4 > = cmp:: min ( max_user_balance, token_amount) ;
118+
119+ info ! ( "Token amount: {:?} for route {}" , user_balance, route) ;
104120
105121 let ( ethereum_transactions, required_approval_details) = self
106122 . source
107123 . generate_route_transactions (
108124 & route. route ,
109- & token_amount ,
125+ & user_balance ,
110126 & route. from_address ,
111127 & route. to_address ,
112128 )
@@ -126,7 +142,7 @@ impl<Source: RouteSource, PriceProvider: TokenPriceProvider>
126142 from_address : route. from_address . clone ( ) ,
127143 to_address : route. to_address . clone ( ) ,
128144 token : route. route . from_token . symbol . clone ( ) ,
129- token_amount : token_amount . clone ( ) ,
145+ token_amount : user_balance . clone ( ) ,
130146 } ,
131147 t,
132148 )
@@ -224,7 +240,7 @@ impl<Source: RouteSource, PriceProvider: TokenPriceProvider>
224240 return Ok ( None ) ;
225241 }
226242
227- let required_approval = required_approval_details. amount - current_approval ;
243+ let required_approval = required_approval_details. amount ;
228244 info ! (
229245 "Required Approval: {:?} against requirement: {:?}" ,
230246 required_approval, required_approval_details
@@ -353,6 +369,9 @@ pub enum SettlementEngineErrors<Source: RouteSource, PriceProvider: TokenPricePr
353369
354370 #[ error( "Error while calling ERC20 Contract: {0}" ) ]
355371 AlloyError ( #[ from] alloy:: contract:: Error ) ,
372+
373+ #[ error( "Error while parsing U256: {0}" ) ]
374+ ParseError ( #[ from] ruint:: ParseError ) ,
356375}
357376
358377pub fn generate_erc20_instance_map (
@@ -440,14 +459,14 @@ mod tests {
440459 use thiserror:: Error ;
441460 use tokio:: sync:: Mutex ;
442461
443- use config:: { Config , get_sample_config } ;
462+ use config:: { get_sample_config , Config } ;
444463 use storage:: { KeyValueStore , RedisClientError } ;
445464
446- use crate :: { BridgeResult , BungeeClient , CoingeckoClient } ;
447465 use crate :: settlement_engine:: {
448466 generate_erc20_instance_map, SettlementEngine , SettlementEngineErrors , TransactionType ,
449467 } ;
450468 use crate :: source:: { EthereumTransaction , RequiredApprovalDetails } ;
469+ use crate :: { BridgeResult , BungeeClient , CoingeckoClient } ;
451470
452471 #[ derive( Error , Debug , Display ) ]
453472 struct Err ;
@@ -768,6 +787,7 @@ mod tests {
768787 & "USDC" . to_string ( ) ,
769788 & "USDC" . to_string ( ) ,
770789 false ,
790+ 200.0 ,
771791 1000.0 ,
772792 TEST_OWNER_WALLET . to_string ( ) ,
773793 TEST_OWNER_WALLET . to_string ( ) ,
@@ -805,6 +825,7 @@ mod tests {
805825 & "USDC" . to_string ( ) ,
806826 & "USDT" . to_string ( ) ,
807827 false ,
828+ 200.0 ,
808829 1000.0 ,
809830 TEST_OWNER_WALLET . to_string ( ) ,
810831 TEST_OWNER_WALLET . to_string ( ) ,
0 commit comments