[Python] Tokens not swaped in Uniswap (web3 blockchain)

Discussão em 'Python' iniciado por Stack, Fevereiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    how are you? I'm trying to do a Swap Input Exact, and I'm generating the hash and spending gas, but the tokens are not being swapped.

    This is a hash that was generated, but no tokens were swapped: 0x29972068d98f7c5c1d8ab54c3b1f48cadc0f4000f94ba9dc01b6dc6192593346

    This is the function that generated it:

    def uniswap_swap_function(web3, contract_router, wallet_address_metamask, wallet_private_key_metamask, swap, slippage=0.5):

    print(swap)

    quantity_in = float(swap['quantity_in'])
    token_address_in = web3.to_checksum_address(swap['token_address_in'])
    token_address_out = web3.to_checksum_address(swap['token_address_out'])

    decimals_token0 = int(swap['decimals_in']) # Decimais do token de entrada
    decimals_token1 = int(swap['decimals_out']) # Decimais do token de saída

    # Definir valores da troca com base nos decimais do token de entrada
    amount_in_wei = int(float(swap['quantity_in']) * (10 ** decimals_token0))

    # Instanciando o contrato do Uniswap V3 Router
    uniswap_router = web3.eth.contract(
    address=web3.to_checksum_address("0xE592427A0AEce92De3Edee1F18E0157C05861564"),
    abi=[
    {
    "inputs": [
    {
    "components": [
    {"internalType": "address", "name": "tokenIn", "type": "address"},
    {"internalType": "address", "name": "tokenOut", "type": "address"},
    {"internalType": "uint24", "name": "fee", "type": "uint24"},
    {"internalType": "address", "name": "recipient", "type": "address"},
    {"internalType": "uint256", "name": "deadline", "type": "uint256"},
    {"internalType": "uint256", "name": "amountIn", "type": "uint256"},
    {"internalType": "uint256", "name": "amountOutMinimum", "type": "uint256"},
    {"internalType": "uint160", "name": "sqrtPriceLimitX96", "type": "uint160"}
    ],
    "internalType": "struct ISwapRouter.ExactInputSingleParams",
    "name": "params",
    "type": "tuple"
    }
    ],
    "name": "exactInputSingle",
    "outputs": [
    {"internalType": "uint256", "name": "amountOut", "type": "uint256"}
    ],
    "stateMutability": "payable",
    "type": "function"
    }
    ]
    )

    # Definição do pool de taxas da Uniswap V3 (3000 = 0.3%, 500 = 0.05%)
    pool_fee = 3000 # Ajuste conforme necessário

    # Calculando o valor mínimo esperado com base no slippage
    expected_out = float(swap['quantity_out']) * (10 ** decimals_token1)
    amount_out_min = int(expected_out * (1 - slippage / 100))

    # Definição do deadline (tempo limite para a transação)
    deadline = int(time.time()) + 60 # 1 minuto

    # Parâmetros do swap no Uniswap V3
    params = (
    token_address_in,
    token_address_out,
    pool_fee,
    wallet_address_metamask,
    deadline,
    amount_in_wei,
    amount_out_min,
    0
    )

    # Construção da transação
    txn = uniswap_router.functions.exactInputSingle(params).build_transaction({
    'from': wallet_address_metamask,
    'gas': 250000,
    'gasPrice': web3.eth.gas_price,
    'nonce': web3.eth.get_transaction_count(wallet_address_metamask),
    })

    # Assinar a transação
    signed_txn = web3.eth.account.sign_transaction(txn, wallet_private_key_metamask)

    # Enviar a transação
    txn_hash = web3.eth.send_raw_transaction(signed_txn.raw_transaction)

    # Esperar a transação ser confirmada
    txn_receipt = web3.eth.wait_for_transaction_receipt(txn_hash)

    print(f'Transação confirmada com hash: {txn_receipt.transactionHash.hex()}')

    return txn_receipt.transactionHash.hex()


    Does anyone have any insights on what could be wrong? I've been stuck for a while, and all tokens are approved.

    Continue reading...

Compartilhe esta Página