Skip to content

Commit 32d528d

Browse files
Several fixes
1 parent d85a027 commit 32d528d

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

src/flare_ai_defai/blockchain/sparkdex.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,12 @@ def swap_erc20_tokens_tx(self, user: UserInfo, token_in: str, token_out: str, am
430430
],
431431
"stateMutability": "view",
432432
"type": "function"
433+
},{
434+
"inputs": [],
435+
"name": "decimals",
436+
"outputs": [{"internalType": "uint8", "name": "", "type": "uint8"}],
437+
"stateMutability": "view",
438+
"type": "function"
433439
}
434440

435441

@@ -520,6 +526,17 @@ def swap_erc20_tokens_tx(self, user: UserInfo, token_in: str, token_out: str, am
520526
contract_in = self.w3.eth.contract(address=token_in_address, abi=token_in_abi)
521527
contract_out = self.w3.eth.contract(address=token_out_address, abi=token_out_abi)
522528

529+
token_in_decimals = contract_in.functions.decimals().call()
530+
token_out_decimals = contract_out.functions.decimals().call()
531+
self.logger.debug("Token decimals", extra={
532+
"token_in": token_in, "decimals_in": token_in_decimals,
533+
"token_out": token_out, "decimals_out": token_out_decimals
534+
})
535+
536+
amount_in_wei = int(amount_in * (10 ** token_in_decimals))
537+
if amount_in_wei <= 0:
538+
raise ValueError(f"Invalid amount_in: {amount_in} for {token_in}")
539+
523540
fee_tier = 500 # Assuming 0.05% pool fee
524541
amount_out_min = 0 # Fetch dynamically for slippage protection
525542
deadline = self.w3.eth.get_block("latest")["timestamp"] + 300 # 5 minutes from now
@@ -532,27 +549,32 @@ def swap_erc20_tokens_tx(self, user: UserInfo, token_in: str, token_out: str, am
532549
fee_tier, # fee (e.g., 500 = 0.05%)
533550
self.wallet_store.get_address(user), # recipient (your address)
534551
int(self.w3.eth.get_block("latest")["timestamp"]) + 300, # deadline (5 min)
535-
amount_in, # amountIn
536-
0, # amountOutMinimum (set to 0 for estimation)
552+
amount_in_wei, # amountIn
553+
1, # amountOutMinimum (set to 0 for estimation)
537554
0 # sqrtPriceLimitX96 (no limit)
538555
)
539556

540-
amount_out_wei = 0
557+
amount_out_wei = 1
541558
try:
542-
# Static call to estimate amountOut
543559
amount_out_wei = universal_router.functions.exactInputSingle(params).call()
544-
amount_out = self.w3.from_wei(amount_out_wei, "ether") # Adjust decimals if needed
545560
except Exception as e:
546-
self.logger.error(e)
561+
self.logger.error(f"Failed to estimate amount out: {str(e)}", extra={"params": params})
562+
raise
547563

548-
amount_out_min = amount_out_wei - int(amount_out_wei*slippage)
564+
amount_out = amount_out_wei / (10 ** token_out_decimals)
565+
amount_out_min = int(amount_out_wei * (1 - slippage)) # Keep in wei units
566+
self.logger.debug("Estimated swap output", extra={
567+
"amount_in": amount_in, "token_in": token_in,
568+
"amount_out": amount_out, "token_out": token_out,
569+
"amount_out_min": amount_out_min
570+
})
549571

550572
# --- Step 1: Approve Universal Router to Spend wFLR ---
551573
approval_tx = contract_in.functions.approve(universal_router_address, amount_in).build_transaction({
552574
'from': self.wallet_store.get_address(user),
553575
'nonce': self.get_nonce(),
554-
"maxFeePerGas": base_fee,
555-
"maxPriorityFeePerGas": base_fee + priority_fee,
576+
"maxFeePerGas": base_fee + priority_fee,
577+
"maxPriorityFeePerGas": priority_fee,
556578
'chainId': self.w3.eth.chain_id,
557579
"type": 2,
558580
})
@@ -565,12 +587,13 @@ def swap_erc20_tokens_tx(self, user: UserInfo, token_in: str, token_out: str, am
565587
token_out_address, # tokenOut
566588
fee_tier, # fee (e.g., 500 = 0.05%)
567589
self.wallet_store.get_address(user), # recipient (your address)
568-
int(self.w3.eth.get_block("latest")["timestamp"]) + 300, # deadline (5 min)
569-
amount_in, # amountIn
590+
deadline, # deadline (5 min)
591+
amount_in_wei, # amountIn
570592
amount_out_min, # amountOutMinimum (set to 0 for estimation)
571593
0 # sqrtPriceLimitX96 (no limit)
572594
)
573595

596+
574597
swap_tx = universal_router.functions.exactInputSingle(params).build_transaction({
575598
'from': self.wallet_store.get_address(user),
576599
'nonce': self.get_nonce(),
@@ -580,6 +603,8 @@ def swap_erc20_tokens_tx(self, user: UserInfo, token_in: str, token_out: str, am
580603
"type": 2,
581604
})
582605

606+
self.logger.debug(f"Approval transaction: {approval_tx}")
607+
self.logger.debug(f"Swap transaction: {swap_tx}")
583608
# --- Step 4: Check JOULE Balance ---
584609
# joule_balance = joule_contract.functions.balanceOf(self.wallet_store.get_address(user)).call()
585610
# print(f"New JOULE balance: {joule_balance / 10**6}")

0 commit comments

Comments
 (0)