@@ -541,34 +541,6 @@ def swap_erc20_tokens_tx(self, user: UserInfo, token_in: str, token_out: str, am
541541 deadline = self .w3 .eth .get_block ("latest" )["timestamp" ] + 300 # 5 minutes from now
542542
543543
544- # ---- Step 0.5: calculate amount_out_min
545- params = (
546- token_in_address , # tokenIn
547- token_out_address , # tokenOut
548- fee_tier , # fee (e.g., 500 = 0.05%)
549- self .wallet_store .get_address (user ), # recipient (your address)
550- int (self .w3 .eth .get_block ("latest" )["timestamp" ]) + 300 , # deadline (5 min)
551- amount_in_wei , # amountIn
552- 1 , # amountOutMinimum (set to 0 for estimation)
553- 0 # sqrtPriceLimitX96 (no limit)
554- )
555-
556- amount_out_wei = 1
557- try :
558- amount_out_wei = universal_router .functions .exactInputSingle (params ).call ()
559- except Exception as e :
560- self .logger .error (f"Failed to estimate amount out: { str (e )} " , extra = {"params" : params })
561- raise
562-
563- amount_out = amount_out_wei / (10 ** token_out_decimals )
564- amount_out_min = int (amount_out_wei * (1 - slippage )) # Keep in wei units
565- self .logger .debug ("Estimated swap output" , extra = {
566- "amount_in_wei" : amount_in_wei , "token_in" : token_in ,
567- "amount_out" : amount_out , "token_out" : token_out ,
568- "amount_out_min" : amount_out_min
569- })
570-
571-
572544
573545
574546
@@ -598,6 +570,33 @@ def swap_erc20_tokens_tx(self, user: UserInfo, token_in: str, token_out: str, am
598570
599571
600572
573+ # ---- Step 0.5: calculate amount_out_min
574+ params = (
575+ token_in_address , # tokenIn
576+ token_out_address , # tokenOut
577+ fee_tier , # fee (e.g., 500 = 0.05%)
578+ self .wallet_store .get_address (user ), # recipient (your address)
579+ int (self .w3 .eth .get_block ("latest" )["timestamp" ]) + 300 , # deadline (5 min)
580+ amount_in_wei , # amountIn
581+ 1 , # amountOutMinimum (set to 0 for estimation)
582+ 0 # sqrtPriceLimitX96 (no limit)
583+ )
584+
585+ amount_out_wei = 1
586+ try :
587+ amount_out_wei = universal_router .functions .exactInputSingle (params ).call ()
588+ except Exception as e :
589+ self .logger .error (f"Failed to estimate amount out: { str (e )} " , extra = {"params" : params })
590+ raise
591+
592+ amount_out = amount_out_wei / (10 ** token_out_decimals )
593+ amount_out_min = int (amount_out_wei * (1 - slippage )) # Keep in wei units
594+ self .logger .debug ("Estimated swap output" , extra = {
595+ "amount_in_wei" : amount_in_wei , "token_in" : token_in ,
596+ "amount_out" : amount_out , "token_out" : token_out ,
597+ "amount_out_min" : amount_out_min
598+ })
599+
601600 # --- Step 1: Approve Universal Router to Spend wFLR ---
602601 approval_tx = contract_in .functions .approve (universal_router_address , amount_in_wei ).build_transaction ({
603602 'from' : self .wallet_store .get_address (user ),
0 commit comments