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.

How GlueX Router Works

The GlueX Router simplifies the token zapping process by:
  1. Fetching a price quote for zapping tokens into liquidity positions.
  2. Generating the appropriate calldata for the zap.
  3. Executing the transaction on-chain to zap tokens and receive LP tokens.

Script Overview

1. Setup and Configuration

The following script initializes the necessary configurations:
from web3 import Web3
import requests
import json
import os
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.co/your_rpc_url"
PRIVATE_KEY = "your_private_key"

# Token Information
TOKEN_IN = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"  # Input token address
LP_TOKEN_OUT = "0x0d4a11d5EEaaC28EC3F61d100daF4d40471f1852"  # LP token address
INPUT_AMOUNT = 1000000000000000000  # Example: 1 token in Wei

# 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

2. Fetching a Zap Quote

Before executing the zap, obtain a quote from the GlueX Router. This ensures that you get the best rate available:
def fetch_quote(body, headers):
    """Fetch a quote from the GlueX Router."""
    response = requests.post(QUOTE_ENDPOINT, json=body, headers=headers)
    return response.json()
This function sends a request to the GlueX Router to retrieve a quote for zapping tokens.

3. Executing the Zap Transaction

Once the quote is retrieved, execute the zap transaction by sending the appropriate calldata:
def execute_transaction(calldata, router_address, account, value=0):
    """Execute transaction on the 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),
        "value": value,
        "chainId": 1
    }
    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
This function constructs and signs the transaction, ensuring it is sent to the Ethereum blockchain for processing.

4. End-to-End Zap Execution

The main() function ties everything together by fetching a quote and executing the transaction:
def main():
    headers = {"x-api-key": API_KEY}
    body = {
        "chainID": "ethereum",  # Ethereum Mainnet
        "userAddress": account.address,
        "outputReceiver": account.address,
        "uniquePID": UNIQUE_PID,
        "inputToken": TOKEN_IN,
        "outputToken": LP_TOKEN_OUT,
        "inputAmount": INPUT_AMOUNT,
        "isPermit2": False
    }
    
    print("============================================================================")
    print("Fetching zap quote...")
    print(body)
    quote_data = fetch_quote(body, headers)
    
    if quote_data.get('statusCode') != 200:
        print("Error fetching quote:", quote_data)
        return
    
    print("Quote received successfully:")
    print(quote_data)
    
    router_address = quote_data["result"]["router"]
    calldata = quote_data["result"]["calldata"]
    
    print("Executing transaction...")
    execute_txn = execute_transaction(calldata, router_address, account, value=0)
    
    receipt = web3.eth.wait_for_transaction_receipt(execute_txn)
    print("Transaction confirmed. Receipt:")
    print(receipt)
    print("")

if __name__ == "__main__":
    main()

5. Complete Code Implementation

6. Running the Script

To execute the zap, run the script as follows:
python zap_token.py