swapConfig.ts
for easy adjustments (e.g., token amounts, slippage, versioned transaction usage).npm install --global yarn
in your terminal.src
directory, which holds all the executable scripts and necessary configurations for token swapping. Additionally, you’ll need a .env
file in the project’s root directory. This file is crucial as it holds sensitive information that enables the project to interact with the Solana blockchain and perform operations securely.
.env
file at the root of your project. This file should include your Solana RPC URL and your wallet’s private key. These variables are essential for connecting to the Solana network and executing transactions. Follow these steps:
.env
in the root directory of the project.
.env
file in a text editor and add the following lines, replacing YOUR_CHAINSTACK_SOLANA_NODE
and YOUR_PRIVATE_KEY
with your actual Solana RPC URL and wallet’s private key:
.env.example
file, which you can rename to .env
, and update with your details accordingly.
src/swapConfig.ts
.src/swapConfig.ts
, looks for the respective liquidity pair in mainnet.json
and produces trimmed_mainnet.json
, which contains only the data pertaining to the tokens that you need.RaydiumSwap.ts
fileRaydiumSwap.ts
defines the RaydiumSwap
class that encapsulates the functionality for performing token swaps using Raydium on the Solana blockchain.
This file includes functions to:
Connection
with a commitment level to confirmed
for transaction finality.Wallet
instance using a provided private key to sign transactions and interact with the blockchain.loadPoolKeys
findPoolInfoForTokens
loadPoolKeys
.
getSwapTransaction
useVersionedTransaction
flag, it can create either a legacy Transaction
or a VersionedTransaction
.Liquidity.makeSwapInstructionSimple
.sendLegacyTransaction
and sendVersionedTransaction
simulateLegacyTransaction
and simulateVersionedTransaction
calcAmountOut
RaydiumSwap
class is exported, making it available for import in other application parts.
This file is key for abstracting the complexities of swap operations. The Raydium SDK communicates directly with the Solana blockchain, preparing and executing transactions that swap tokens within the specified liquidity pools.
index.ts
fileindex.ts
is the entry point script that combines the functionality implemented in RaydiumSwap.ts
and the configuration specified in swapConfig.ts
to perform or simulate a token swap. Below is the functionality provided by each segment of the script:
RaydiumSwap
is created to establish a connection to the Solana blockchain and prepare the wallet for transaction signing.
loadPoolKeys
from the RaydiumSwap
instance to retrieve the necessary information from Raydium’s liquidity pools.
findPoolInfoForTokens
method, which uses the token addresses from the swapConfig
.
getSwapTransaction
method, passing in parameters such as the destination token address, the amount to swap, the retrieved pool information, and additional configuration like the maximum lamports for transaction fees and whether to use a versioned transaction.
swapConfig
, the script executes the swap on the blockchain or simulates it to forecast the outcome without sending the transaction.
sendVersionedTransaction
or sendLegacyTransaction
). It logs the transaction ID, which can be used to track the transaction on a Solana blockchain explorer like Solscan.simulateVersionedTransaction
or simulateLegacyTransaction
) and logs the results, which can help debug or test before live execution.swap
function is called to initiate the swap process.
This script is a high-level workflow that dictates the swap process from start to finish, abstracting the lower-level details and ensuring that the user-facing process is streamlined and straightforward.
swapConfig.ts
fileswapConfig.ts
file holds the configuration settings for a token swap using the Raydium SDK on the Solana blockchain. This is where you set the parameters used throughout the swap process. Let’s detail the contents and role of each configuration option.
This configuration file exports an object swapConfig
containing key-value pairs that define the parameters of the swap operation:
executeSwap
true
) or to simulate it (false
). This is useful for testing the swap process without committing real funds.
useVersionedTransaction
VersionedTransaction
format. This new transaction format can include additional metadata and is meant to be more future-proof.
tokenAAmount
0.01
indicates that the swap will attempt to exchange 0.01 SOL.
tokenAAddress
tokenBAddress
maxLamports
direction
in
means that the tokenAAmount
is the exact amount you will send, while out
means it’s the exact amount you wish to receive.
maxRetries
liquidityFile
RaydiumSwap
class to load pool keys and find the right pool for the swap.
src/swapConfig.ts
in your favorite code editor.
swapConfig
object to set up your desired swap parameters:
executeSwap
: Set this to false
if you wish to simulate the transaction before executing or true
to execute the swap immediately. It’s set on the simulation by default.useVersionedTransaction
: Determine whether to use a versioned transaction (true
) or a legacy transaction (false
). Versioned transactions are the new standard and are recommended.tokenAAmount
: Specify the amount of the source token you wish to swap. For example, 0.01
if you’re swapping 0.01 SOL.tokenAAddress
: Enter the source token’s mint address. By default, it’s set to SOL’s mint address.tokenBAddress
: Enter the mint address of the destination token. The default is set to USDC’s mint address.maxLamports
: Define the maximum fee you will pay for the transaction, which is measured in lamports.direction
: Choose 'in'
to fix the amount of the source token or 'out'
to fix the amount of the destination token.liquidityFile
: This should point to the Raydium liquidity pool information file. It’s preset to the Raydium liquidity pools.yarn swap
, the program will:
swapConfig
to identify the appropriate liquidity pool.
tokenAAmount
of tokenAAddress
for tokenBAddress
.
executeSwap
configuration, the program will either:
swapConfig.ts
empowers you to fine-tune swap parameters, while RaydiumSwap.ts
and index.ts
facilitate the execution and simulation of swaps, complete with transaction tracking capabilities.
By the end of this tutorial, you’ve gained the ability to conduct token swaps confidently, making the most of Solana’s rapid and cost-effective ecosystem. This foundation sets you up for continued growth and exploration in the vibrant landscape of decentralized finance.