SOLANA Swaps
This guide demonstrates how to execute a token swap on solana network using the Compass API. The example covers the complete workflow from connecting a wallet to monitoring transaction status.
Overview
The implementation follows these key steps:
Wallet Connection
Quote Retrieval
Transaction Creation
Step-by-Step Execution
Status Monitoring
Implementation Details
Prerequisites
Phantom or any solana compatible wallet
Integrator ID from Compass
Solana Web3.js library
API Endpoints
The example uses the following endpoints:
/quotes
This endpoint fetches quotes from available liquidity sources and responds with quotes that are sorted by best output amount by default. The first item in the quotes array is the recommended quote.
Request Parameters
Example Request
Example Response
/createTx
This endpoint fetches the transaction steps for the selected quote. A solana transaction cn have either swap step or bridge step . Since this is s solana swap example, the transaction step includes swap. The response includes steps that need to be executed.The Step object includes a step ID and step type (swap).
Request Parameters
Example Request
Example Response
View full createTx API response
/nextTx
This endpoint fetches the transaction data that needs to be executed for the selected step. The transaction data includes the transaction parameters for the step. Since this is an Solana swap example, the transaction data includes the txnSol object field in the transaction data.
Note: The transaction data is specific to the step type. For example, if the step type is swap, the transaction data includes the swap parameters. If the step type is token approval, the transaction data includes the token approval parameters. Moreover, the next step data will be available only if the previous step is executed successfully.
Request Parameters
Example Request
Example Response
/status
This endpoint fetches the transaction status for the selected step once the transaction step is submitted to the blockchain. The transaction status includes: the status of the transaction, the source transaction hash, the source transaction URL, the destination transaction hash, the destination transaction URL, the output amount received, and the output token details.
View full status API documentation
Transaction Status Codes
in-progress: Transaction is pendingsuccess: Transaction completed successfullypartial-success: If output amount token is different from the destination token, the transaction is partial success.failed: Transaction failed
Request Parameters
Example Request
Example Response
Code Example
In the code example below, we use plain JavaScript for simplicity, but the code is framework-agnostic and can be easily adapted to React, Next.js, Vite, or any other JavaScript framework. Simply modify the syntax and component structure to match your chosen framework's conventions.
NodeJS Implementation
Browser Implementation with Phantom Wallet
Common Implementation
Usage Notes
For browser implementation:
Make sure Phantom wallet extension is installed
Handle wallet connection states and user interactions appropriately
For NodeJS implementation:
Ensure you have the required environment variables (SOLANA_PRIVATE_KEY, RPC_URL)
Handle errors and cleanup appropriately
Common considerations:
Replace 'your-integrator-id' with your actual integrator ID
Implement proper error handling and loading states
Add appropriate UI feedback for transaction status
Last updated
Was this helpful?