Case

Suppose you hold $GALA and want to put it to work earning yield in a lending protocol, specifically by converting it into interest bearing $aETHWETH in a single atomic transaction. With GlueX’s unified Router API and smart contract integrations, you can swap one token and deposit the proceeds into a lending vault atomically, without juggling approvals or multiple onchain calls. In this guide, we’ll walk through exactly how to:
  1. Build a swap quote to convert $GALA into $WETH
  2. Bundle that quote into a Zap that deposits directly into the Aave vault to receive $aETHWETH
  3. Submit the transaction and verify the resulting vault deposit
We’ll use a real Ethereum mainnet transaction as our reference example
Transaction hash: 0xd0c48d25ac3e0ab111b3ab401903d4a357baa22ee9c9efb832e0b80278ba5ba3
0xd0c48d25ac3e0ab111b3ab401903d4a357baa22ee9c9efb832e0b80278ba5ba3 Example Txn

How It Works

  1. Transfer $GALA from the user’s wallet
  2. Swap $GALA for $WETH via Uniswap V3
  3. Receive $aETHWETH via Aave V3
  4. Start earning yield automatically

Why This Matters?

Moving from a regular token to a yield bearing asset usually requires multiple platforms, manual steps and higher fees. GlueX removes these barriers, bringing: ✅ Time efficiency - One transaction instead of many ✅ Cost savings - Lesser gas fees ✅ Simplicity - No manual interactions with multiple protocols This tutorial uses $GALA → $aETHWETH as an example, but GlueX allows you to swap any token to any yield generating token in a single transaction

Implementation

Prerequisites

Before running the script, ensure you have the following installed:
  • Python 3.10
  • Web3 (pip install web3)
  • Requests (pip install requests)
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"
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
GALA_ADDRESS = "0xd1d2Eb1B1e90B638588728b4130137D262C87cae"
AETHWETH_ADDRESS = "0x4d5F47FA6A74757f35C14fD3a6Ef8E3C9BC514E8"

# Input amount (in decimals)
INPUT_AMOUNT = 2000000000000  # 2,000,000 GALA (including 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 $GALA to $aETHWETH swap"""
    headers = {"x-api-key": API_KEY}
    body = {
        "chainID": "ethereum",
        "userAddress": account.address,
        "outputReceiver": account.address,
        "uniquePID": "your_unique_pid",
        "inputToken": GALA_ADDRESS,
        "outputToken": AETHWETH_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 GALA tokens
def approve_spender(spender, amount, token_address):
    """Approve the router contract to spend $GALA"""
    signature = "0x095ea7b3"
    encoded_arguments = Web3.solidity_keccak(['address', 'uint256'], [spender, amount]).hex()
    calldata = signature + encoded_arguments[2:]

    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),
    }

    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),
    }

    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 GALA...")
    approve_spender(router_address, INPUT_AMOUNT, GALA_ADDRESS)

    # 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()

Conclusion

With GlueX, moving from $GALA to a yield generating asset like $aETHWETH takes just one transaction. This tutorial demonstrates:
  • Fetching a swap quote
  • Approving the router contract to spend $GALA
  • Executing the transaction to swap and deposit smoothly
Depositing to a Lending Vault
from web3 import Web3
import requests
import json
import time

# Configuration
API_KEY = "your_api_key"
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
TOKEN_A = "0xd1d2Eb1B1e90B638588728b4130137D262C87cae"
TOKEN_B = "0x4d5F47FA6A74757f35C14fD3a6Ef8E3C9BC514E8"

# Input amount (in decimals)
INPUT_AMOUNT = 2000000000000  # 2,000,000 TOKEN_A (including 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


def fetch_quote():
    """Fetch a quote from the GlueX Router for $TOKEN_A to $TOKEN_B swap"""
    headers = {"x-api-key": API_KEY}
    body = {
        "chainID": "ethereum",
        "userAddress": account.address,
        "outputReceiver": account.address,
        "uniquePID": "your_unique_pid",
        "inputToken": TOKEN_A,
        "outputToken": TOKEN_B,
        "inputAmount": INPUT_AMOUNT,
        "isPermit2": False
    }

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

    return response.json()


def approve_spender(spender, amount, token_address):
    """Approve the router contract to spend $TOKEN_A"""
    signature = "0x095ea7b3"
    encoded_arguments = Web3.solidity_keccak(
        ['address', 'uint256'], [spender, amount]
    ).hex()
    calldata = signature + encoded_arguments[2:]

    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),
    }

    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


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),
    }

    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


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 TOKEN_A...")
    approve_spender(router_address, INPUT_AMOUNT, TOKEN_A)

    # 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()

Oppurtunities

🔹 Instant access to yield without manual interventions 🔹 Seamless diversification into yield generating assets 🔹 Cost savings by reducing gas fees across multiple steps With GlueX, DeFi becomes as easy as sending an email 🚀