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 wrapping process by:
  1. Fetching a price quote for wrapping native tokens.
  2. Generating the appropriate calldata for the wrap.
  3. Executing the transaction on-chain to wrap the native token into its wrapped form (e.g., ETH to WETH).

Script Overview

1. Setup and Configuration

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
NATIVE_TOKEN_ADDRESS = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"  # Represents native ETH
TOKEN_IN = NATIVE_TOKEN_ADDRESS
TOKEN_OUT = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"  # WETH Contract Address
INPUT_AMOUNT = 1000000000000000000  # Example: 1 ETH 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 Wrap Quote

def fetch_quote(body, headers):
    """Fetch a quote from the GlueX Router."""
    response = requests.post(QUOTE_ENDPOINT, json=body, headers=headers)
    return response.json()

3. Executing the Wrap Transaction

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

4. End-to-End Wrap Execution

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": TOKEN_OUT,
        "inputAmount": INPUT_AMOUNT,
        "isPermit2": False
    }
    
    print("============================================================================")
    print("Fetching wrap 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=INPUT_AMOUNT)
    
    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 wrap, run the script as follows:
python wrap_token.py