|
| 1 | +# AGENTS Guidelines for This Repository |
| 2 | + |
| 3 | +This repository contains a TypeScript example for performing token swaps on Solana using the Raydium SDK. When working on the project interactively with an agent (e.g. the Codex CLI) please follow the guidelines below for safe development and testing. |
| 4 | + |
| 5 | +## 1. Use Simulation Mode for Testing |
| 6 | + |
| 7 | +* **Always start with simulation** (`executeSwap: false` in swapConfig). |
| 8 | +* **Test swaps thoroughly** before executing real transactions. |
| 9 | +* **Do _not_ execute real swaps** during agent development sessions. |
| 10 | +* **Monitor gas costs** even in simulation mode. |
| 11 | + |
| 12 | +## 2. Keep Dependencies in Sync |
| 13 | + |
| 14 | +If you update dependencies: |
| 15 | + |
| 16 | +1. Use yarn to manage packages: `yarn add <package>`. |
| 17 | +2. Update all packages: `yarn upgrade`. |
| 18 | +3. Verify compatibility with Raydium SDK. |
| 19 | +4. Test with latest Solana web3.js versions. |
| 20 | + |
| 21 | +## 3. Environment Configuration |
| 22 | + |
| 23 | +Create a `.env` file with required settings: |
| 24 | + |
| 25 | +```env |
| 26 | +RPC_URL=YOUR_CHAINSTACK_RPC_URL |
| 27 | +WALLET_PRIVATE_KEY=YOUR_TEST_WALLET_PRIVATE_KEY |
| 28 | +``` |
| 29 | + |
| 30 | +Never commit `.env` to version control. |
| 31 | + |
| 32 | +## 4. Development Workflow |
| 33 | + |
| 34 | +Follow this sequence for safe development: |
| 35 | + |
| 36 | +1. **Download liquidity data**: Get latest pool information |
| 37 | + ```bash |
| 38 | + wget https://api.raydium.io/v2/sdk/liquidity/mainnet.json |
| 39 | + ``` |
| 40 | + |
| 41 | +2. **Configure swap**: Edit `src/swapConfig.ts` |
| 42 | + ```typescript |
| 43 | + executeSwap: false // Always false for testing |
| 44 | + ``` |
| 45 | + |
| 46 | +3. **Trim liquidity data**: Optimize for your token pair |
| 47 | + ```bash |
| 48 | + ts-node src/trimMainnet.ts |
| 49 | + ``` |
| 50 | + |
| 51 | +4. **Test swap**: Run simulation first |
| 52 | + ```bash |
| 53 | + yarn swap |
| 54 | + ``` |
| 55 | + |
| 56 | +## 5. Swap Configuration |
| 57 | + |
| 58 | +Key parameters in `src/swapConfig.ts`: |
| 59 | + |
| 60 | +| Parameter | Purpose | Safe Default | |
| 61 | +| --------- | ------- | ------------ | |
| 62 | +| `executeSwap` | Execute vs simulate | `false` | |
| 63 | +| `tokenAAmount` | Amount to swap | `0.01` | |
| 64 | +| `maxLamports` | Max fee allowed | `1000000` | |
| 65 | +| `maxRetries` | Retry attempts | `10` | |
| 66 | + |
| 67 | +## 6. Data Management |
| 68 | + |
| 69 | +### Liquidity File Optimization |
| 70 | +The full `mainnet.json` is ~500MB. Always trim it: |
| 71 | + |
| 72 | +```bash |
| 73 | +# Download fresh data |
| 74 | +wget https://api.raydium.io/v2/sdk/liquidity/mainnet.json |
| 75 | + |
| 76 | +# Trim to your tokens |
| 77 | +ts-node src/trimMainnet.ts |
| 78 | + |
| 79 | +# Use trimmed file (< 1MB) |
| 80 | +``` |
| 81 | + |
| 82 | +## 7. Token Address Management |
| 83 | + |
| 84 | +Common token addresses: |
| 85 | +* **SOL**: `So11111111111111111111111111111111111111112` |
| 86 | +* **USDC**: `EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v` |
| 87 | +* **USDT**: `Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB` |
| 88 | + |
| 89 | +Always verify addresses before swapping. |
| 90 | + |
| 91 | +## 8. Testing Checklist |
| 92 | + |
| 93 | +Before executing real swaps: |
| 94 | + |
| 95 | +- [ ] Simulation successful |
| 96 | +- [ ] Correct token addresses |
| 97 | +- [ ] Appropriate slippage settings |
| 98 | +- [ ] Sufficient SOL for fees |
| 99 | +- [ ] Liquidity data is current |
| 100 | +- [ ] RPC endpoint responsive |
| 101 | +- [ ] Private key secure |
| 102 | + |
| 103 | +## 9. Error Handling |
| 104 | + |
| 105 | +Common issues and solutions: |
| 106 | + |
| 107 | +**"Liquidity pool not found"** |
| 108 | +- Update mainnet.json |
| 109 | +- Verify token pair exists |
| 110 | +- Check trimmed file contains pair |
| 111 | + |
| 112 | +**"Transaction simulation failed"** |
| 113 | +- Check wallet balance |
| 114 | +- Verify token amounts |
| 115 | +- Increase maxLamports |
| 116 | + |
| 117 | +**"RPC request failed"** |
| 118 | +- Check RPC endpoint |
| 119 | +- Verify rate limits |
| 120 | +- Use different RPC if needed |
| 121 | + |
| 122 | +## 10. Performance Optimization |
| 123 | + |
| 124 | +* Use trimmed liquidity files (loads in < 1s vs minutes). |
| 125 | +* Cache pool information when possible. |
| 126 | +* Implement retry logic for RPC calls. |
| 127 | +* Use versioned transactions for efficiency. |
| 128 | + |
| 129 | +## 11. Security Best Practices |
| 130 | + |
| 131 | +* **Never commit private keys**. |
| 132 | +* **Use test wallets** for development. |
| 133 | +* **Start with small amounts** (0.01 SOL). |
| 134 | +* **Always simulate first**. |
| 135 | +* **Verify token addresses** from official sources. |
| 136 | +* **Monitor slippage** to avoid MEV attacks. |
| 137 | + |
| 138 | +## 12. Common Development Tasks |
| 139 | + |
| 140 | +### Add New Token Pair |
| 141 | +1. Find official token addresses |
| 142 | +2. Update `swapConfig.ts` |
| 143 | +3. Download fresh liquidity data |
| 144 | +4. Trim for new pair |
| 145 | +5. Test with simulation |
| 146 | + |
| 147 | +### Debug Failed Swap |
| 148 | +1. Check simulation output |
| 149 | +2. Verify liquidity exists |
| 150 | +3. Confirm wallet balances |
| 151 | +4. Review transaction logs |
| 152 | +5. Adjust maxLamports if needed |
| 153 | + |
| 154 | +## 13. Useful Commands Recap |
| 155 | + |
| 156 | +| Command | Purpose | |
| 157 | +| ------- | ------- | |
| 158 | +| `yarn` | Install dependencies | |
| 159 | +| `yarn swap` | Run swap (simulation or real) | |
| 160 | +| `ts-node src/trimMainnet.ts` | Trim liquidity data | |
| 161 | +| `wget [liquidity URL]` | Download pool data | |
| 162 | + |
| 163 | +## 14. RPC Configuration |
| 164 | + |
| 165 | +For Chainstack nodes: |
| 166 | +1. Sign up at https://console.chainstack.com |
| 167 | +2. Deploy Solana node |
| 168 | +3. Use HTTP endpoint for swaps |
| 169 | +4. Monitor rate limits |
| 170 | + |
| 171 | +## 15. Troubleshooting |
| 172 | + |
| 173 | +### Common Issues |
| 174 | + |
| 175 | +**"Module not found"** |
| 176 | +```bash |
| 177 | +yarn |
| 178 | +``` |
| 179 | + |
| 180 | +**"Large file warning"** |
| 181 | +- Never commit mainnet.json |
| 182 | +- Always use trimmed version |
| 183 | +- Add to .gitignore |
| 184 | + |
| 185 | +**"Insufficient lamports"** |
| 186 | +- Top up SOL for fees |
| 187 | +- Reduce swap amount |
| 188 | +- Increase maxLamports |
| 189 | + |
| 190 | +--- |
| 191 | + |
| 192 | +Following these practices ensures safe token swap development, prevents loss of funds, and maintains efficient execution. Always simulate before executing real swaps and use test amounts during development. |
0 commit comments