Prerequisites

Before running the script, ensure you have the following installed: Python:
  • Python 3.10
  • Web3 (pip install web3)
  • Requests (pip install requests)
TypeScript:
  • Node.js 18+
  • TypeScript (npm install -g typescript)
  • Web3 (npm install web3)
  • Axios (npm install axios)
JavaScript:
  • Node.js 18+
  • Web3 (npm install web3)
  • Axios (npm install axios)
Additionally, you need access to an Ethereum compatible blockchain node, such as an Infura or Tenderly RPC endpoint.

Setup and Configuration

from web3 import Web3
import requests
import json
import time

# Configuration
API_KEY = "your_api_key"
UNIQUE_PID = "your_unique_pid"
QUOTE_ENDPOINT = "https://router.gluex.xyz/v1/quote"
RPC_URL = "https://mainnet.gateway.tenderly.co/your_rpc_url"
PRIVATE_KEY = "your_private_key"

# Token Addresses
USDC_ADDRESS = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"  # USDC on Ethereum
ETH_ADDRESS = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"    # ETH on Ethereum
INPUT_AMOUNT = 1000000  # 1 USDC (6 decimals)

# Initialize Web3
web3 = Web3(Web3.HTTPProvider(RPC_URL))
account = web3.eth.account.from_key(PRIVATE_KEY)
COMPUTATION_UNITS = 1000000
COMPUTATION_COST = web3.eth.gas_price

Fetching a Swap Quote

Before executing the transaction, fetch a quote from the GlueX Router
def fetch_quote():
    """Fetch a quote from the GlueX Router for $USDC to $ETH swap"""
    headers = {"x-api-key": API_KEY}
    body = {
        "chainID": "1",  # Ethereum Mainnet
        "userAddress": account.address,
        "outputReceiver": account.address,
        "uniquePID": UNIQUE_PID,
        "inputToken": USDC_ADDRESS,
        "outputToken": ETH_ADDRESS,
        "inputAmount": INPUT_AMOUNT,
        "isPermit2": False
    }

    response = requests.post(QUOTE_ENDPOINT, json=body, headers=headers)

    return response.json()

Approving the Router Contract

Before executing the swap, the router contract needs permission to spend the user’s USDC tokens
def approve_spender(spender, amount, token_address):
    """Approve the router contract to spend $USDC"""
    signature = "0x095ea7b3"
    padded_spender = Web3.to_checksum_address(spender)[2:].zfill(64)
    calldata = f"{signature}{padded_spender}{int(amount):064x}"

    txn = {
        "from": account.address,
        "to": Web3.to_checksum_address(token_address),
        "data": calldata,
        "gas": COMPUTATION_UNITS,
        "gasPrice": web3.eth.gas_price,
        "nonce": web3.eth.get_transaction_count(account.address),
        "chainId": 1,  # Ethereum mainnet
    }

    signed_txn = web3.eth.account.sign_transaction(txn, account.key)
    txn_hash = web3.eth.send_raw_transaction(signed_txn.raw_transaction)
    print(f"Approval Transaction Hash: {web3.to_hex(txn_hash)}")

    return txn_hash

Executing the Transaction

After approval, we execute the transaction using the calldata from the quote
def execute_transaction(calldata, router_address):
    """Execute the swap transaction on GlueX Router"""
    txn = {
        "from": account.address,
        "to": Web3.to_checksum_address(router_address),
        "data": calldata,
        "gas": COMPUTATION_UNITS,
        "gasPrice": web3.eth.gas_price,
        "nonce": web3.eth.get_transaction_count(account.address),
        "chainId": 1,  # Ethereum mainnet
    }

    signed_txn = web3.eth.account.sign_transaction(txn, account.key)

    try:
        txn_hash = web3.eth.send_raw_transaction(signed_txn.raw_transaction)
    except Web3RPCError as err:
        time.sleep(5)
        txn["nonce"] = web3.eth.get_transaction_count(account.address)
        signed_txn = web3.eth.account.sign_transaction(txn, account.key)
        txn_hash = web3.eth.send_raw_transaction(signed_txn.raw_transaction)

    print(f"Transaction Hash: {web3.to_hex(txn_hash)}")

    return txn_hash

Putting Everything Together

def main():
    # Fetch the quote
    quote_data = fetch_quote()
    if quote_data.get('statusCode') != 200:
        print("Error fetching quote:", quote_data)
        return

    print("Quote received successfully:", quote_data)
    router_address = quote_data["result"]["router"]
    calldata = quote_data["result"]["calldata"]

    # Approve the router contract
    print("Approving router contract to spend USDC...")
    txn_hash = approve_spender(router_address, INPUT_AMOUNT, USDC_ADDRESS)
    print("Waiting for approval transaction confirmation...")
    approval_receipt = web3.eth.wait_for_transaction_receipt(txn_hash)
    if approval_receipt.status != 1:
        print("Approval failed. Aborting.")
        return

    # Execute the transaction
    print("Executing transaction...")
    execute_txn = execute_transaction(calldata, router_address)

    receipt = web3.eth.wait_for_transaction_receipt(execute_txn)
    print("Transaction confirmed. Receipt:")
    print(receipt)


if __name__ == "__main__":
    main()

Complete Code Implementation

Running the Script

To execute the swap, run the script as follows:
python dex-transaction.py

Conclusion

By following this tutorial, users can seamlessly swap USDC for ETH using the GlueX Router. The script automates fetching quotes, handling approvals, and executing swaps securely. Users can modify this script to suit their specific needs, such as swapping different tokens, changing input amounts, or integrating it into larger applications.

Opportunities

🔹 Instant access to best swap rates across DEXs 🔹 Seamless token swaps with proper approval handling 🔹 Cost savings by reducing gas fees across multiple steps With GlueX, DeFi becomes as easy as sending an email 🚀