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
Run
button 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
.env
file):CHAIN_ID
FROM_TOKEN_ADDRESS
TO_TOKEN_ADDRESS
USER_WALLET_ADDRESS
PRIVATE_KEY
(Wallet Private Key)OKX_API_KEY
OKX_SECRET_KEY
OKX_PASSPHRASE
- Set up your environment and initialize Web3 connection.
- Check token allowances using the
getAllowance
function. - If needed, approve tokens using the
sendApproveTx
function. - Get a quote for your swap using the
getQuote
function. - Execute the swap using the
sendSwapTx
function.
- 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.