Prerequisites
Before running the script, ensure you have the following installed: Python:- Python 3.10
- Web3 (
pip install web3
) - Requests (
pip install requests
)
- Node.js 18+
- TypeScript (
npm install -g typescript
) - Web3 (
npm install web3
) - Axios (
npm install axios
)
- Node.js 18+
- Web3 (
npm install web3
) - Axios (
npm install axios
)
How GlueX Router Works
The GlueX Router simplifies the token swap process by:- Fetching a price quote for swapping native tokens.
- Generating the appropriate calldata for the swap.
- Executing the transaction on-chain to swap the native token for the desired output token.
Script Overview
1. Setup and Configuration
The following script initializes the necessary configurations:2. Fetching a Swap Quote
Before executing the swap, obtain a quote from the GlueX Router. This ensures that you get the best rate available:3. Executing the Swap Transaction
Once the quote is retrieved, execute the swap transaction by sending the appropriate calldata:4. End-to-End Swap Execution
Themain()
function ties everything together by fetching a quote and executing the transaction:
5. Complete Code Implementation
Complete Python Implementation
Complete Python Implementation
Complete TypeScript Implementation
Complete TypeScript Implementation
Complete JavaScript Implementation
Complete JavaScript Implementation
6. Running the Script
To execute the swap, run the script as follows:6. Understanding the Workflow
- User provides their private key and RPC URL.
- The script fetches a quote for the native token swap from the GlueX Router.
- The appropriate calldata is generated for the transaction.
- The transaction is executed on-chain, sending native tokens and receiving the desired token in return.
- The transaction hash is displayed, which users can verify on Etherscan or their preferred blockchain explorer.