This project contains a simple JavaScript script to swap Solana SPL tokens using the Solana Web3.js and SPL Token libraries. The example provided demonstrates swapping SOL with another SPL token.
Before running the script, ensure you have the following installed:
- Node.js
- npm (comes with Node.js)
-
Clone the Repository
git clone https://github.com/your-repo/solana-token-swap.git cd solana-token-swap
-
Install Dependencies
npm install @solana/web3.js @solana/spl-token
-
Update Constants
In the script file
swap.js
, update the mint addresses for the tokens you want to swap if they are different:const SOL_MINT_ADDRESS = "So11111111111111111111111111111111111111112"; const DOGWIFTHAT_MINT_ADDRESS = "EKpQGSJtjMFqKZ9KQanSqYXRcF8fBopzLHYxdM65zcjm";
-
Run the Script
To execute the script, use the following command:
node swap.js
The script will:
- Connect to the Solana mainnet.
- Generate a new keypair for both the sending and receiving wallets.
- Request an airdrop of SOL to the sending wallet for testing purposes.
- Create associated token accounts for both the SOL and the other SPL token.
- Transfer a small amount of SOL to the receiving wallet.
- Security: This script uses newly generated keypairs for demonstration purposes. In a real application, you would use securely managed wallets and private keys.
- Error Handling: This script lacks comprehensive error handling. Ensure you implement proper error checking for production use.
- Customization: Modify the token mint addresses, transfer amounts, and other logic as needed for your specific use case.
This project is licensed under the MIT License. See the LICENSE file for details.