@@ -369,7 +369,7 @@ def swap_erc20_tokens(self, token_in: str, token_out: str, amount_in: float):
369369
370370
371371 def swap_erc20_tokens_tx (self , user : UserInfo , token_in : str , token_out : str , amount_in : float ):
372-
372+ slippage = 0.05
373373 amount_in = self .w3 .to_wei (amount_in , unit = "ether" )
374374 universal_router_address = "0x8a1E35F5c98C4E85B36B7B253222eE17773b2781" # Replace with Flare's Universal Router if different
375375
@@ -513,6 +513,8 @@ def swap_erc20_tokens_tx(self, user: UserInfo, token_in: str, token_out: str, am
513513 token_in_abi = ERC20_ABI
514514 token_out_abi = ERC20_ABI
515515
516+ base_fee = self .w3 .eth .get_block ('latest' )['baseFeePerGas' ]
517+ priority_fee = self .w3 .eth .max_priority_fee
516518
517519 universal_router = self .w3 .eth .contract (address = universal_router_address , abi = SWAP_ROUTER_ABI )
518520 contract_in = self .w3 .eth .contract (address = token_in_address , abi = token_in_abi )
@@ -522,33 +524,43 @@ def swap_erc20_tokens_tx(self, user: UserInfo, token_in: str, token_out: str, am
522524 amount_out_min = 0 # Fetch dynamically for slippage protection
523525 deadline = self .w3 .eth .get_block ("latest" )["timestamp" ] + 300 # 5 minutes from now
524526
527+ # ---- Step 0.5: calculate amount_out_min
528+ params = (
529+ token_in_address , # tokenIn
530+ token_out_address , # tokenOut
531+ fee_tier , # fee (e.g., 500 = 0.05%)
532+ self .wallet_store .get_address (user ), # recipient (your address)
533+ int (self .w3 .eth .get_block ("latest" )["timestamp" ]) + 300 , # deadline (5 min)
534+ amount_in , # amountIn
535+ 0 , # amountOutMinimum (set to 0 for estimation)
536+ 0 # sqrtPriceLimitX96 (no limit)
537+ )
538+
539+ amount_out_wei = 0
540+ try :
541+ # Static call to estimate amountOut
542+ amount_out_wei = universal_router .functions .exactInputSingle (params ).call ()
543+ amount_out = self .w3 .from_wei (amount_out_wei , "ether" ) # Adjust decimals if needed
544+ except Exception as e :
545+ self .logger .error (e )
546+
547+ amount_out_min = amount_out_wei - amount_out_wei * slippage
548+
525549 # --- Step 1: Approve Universal Router to Spend wFLR ---
526550 approval_tx = contract_in .functions .approve (universal_router_address , amount_in ).build_transaction ({
527551 'from' : self .wallet_store .get_address (user ),
528552 'nonce' : self .get_nonce (),
529- "maxFeePerGas" : self . w3 . eth . gas_price ,
530- "maxPriorityFeePerGas" : self . w3 . eth . max_priority_fee ,
553+ "maxFeePerGas" : base_fee ,
554+ "maxPriorityFeePerGas" : base_fee + priority_fee ,
531555 'chainId' : self .w3 .eth .chain_id ,
532556 "type" : 2 ,
533557 })
534558
535559 self .logger .debug (f"Approval transaction: { approval_tx } " )
536560
537- params = (
538- token_in_address , # Token In
539- token_out_address , # Token Out
540- fee_tier , # Pool Fee Tier (0.05%)
541- self .wallet_store .get_address (user ), # Recipient
542- deadline , # Deadline (5 min)
543- amount_in , # Amount In (exact wFLR amount)
544- amount_out_min , # Minimum amount of JOULE expected
545- 0 # sqrtPriceLimitX96 (0 = no limit)
546- )
547-
548- base_fee = self .w3 .eth .get_block ('latest' )['baseFeePerGas' ]
549- priority_fee = self .w3 .eth .max_priority_fee
550-
551561 # --- Step 3: Execute the swap ---
562+
563+
552564 swap_tx = universal_router .functions .exactInputSingle (params ).build_transaction ({
553565 'from' : self .wallet_store .get_address (user ),
554566 'nonce' : self .get_nonce (),
0 commit comments