This project demonstrates how to build a token swap application on the EVM network using the OKX DEX API. The example shows how to swap ETH for USDC on the Ethereum network.
- Check token allowances
- Approve tokens for swapping
- Get quotes for token swaps
- Execute token swaps
- Node.js and npm installed
- An Ethereum wallet with private key
- OKX API credentials (API Key, Secret Key, and Passphrase)
- Fork the repository:
- Click "Use Template" to fork this repl.
- Install the dependencies
In Replit, open the shell terminal and run the following command:
npm install- Run the project:
- Use the
Runbutton provided by Replit to start the application. - Once the server starts, it will provide a webview window with the application running live.
- Set up your environment variables (consider using a
.envfile):CHAIN_IDFROM_TOKEN_ADDRESSTO_TOKEN_ADDRESSUSER_WALLET_ADDRESSPRIVATE_KEY(Wallet Private Key)OKX_API_KEYOKX_SECRET_KEYOKX_PASSPHRASE
- Set up your environment and initialize Web3 connection.
- Check token allowances using the
getAllowancefunction. - If needed, approve tokens using the
sendApproveTxfunction. - Get a quote for your swap using the
getQuotefunction. - Execute the swap using the
sendSwapTxfunction.
- Always ensure you have sufficient gas for transactions.
- Be cautious with private keys and API credentials. Never share them publicly.
- This example uses a 3% slippage tolerance. Adjust as needed.
Contributions, issues, and feature requests are welcome. Feel free to check issues page if you want to contribute.