Use Raydium

The Use Raydium tool, part of AgentiPy, allows users to interact with Raydium's Automated Market Maker (AMM) liquidity pools on the Solana blockchain. It provides functionalities to buy tokens by swapping SOL, and sell tokens for SOL.

Functionality

This tool is implemented in the RaydiumManager class. It interacts directly with Raydium's on-chain programs by constructing and sending Solana transactions. Helper utilities from agentipy.utils.raydium are used for fetching pool information, calculating amounts, and creating swap instructions.

Key Methods:

  • buy_with_raydium(agent: SolanaAgentKit, pair_address: str, sol_in: float = 0.01, slippage: int = 5) -> bool:

    • Executes a buy order on a Raydium liquidity pool, swapping SOL for a target token.

    • Parameters:

      • agent: The SolanaAgentKit instance (provides wallet and connection).

      • pair_address: The string address of the Raydium AMM pool ID (not the token mint).

      • sol_in: The amount of SOL to spend (default 0.01 SOL).

      • slippage: Allowed slippage percentage (default 5%).

    • Fetches pool keys for the given pair_address.

    • Calculates the minimum amount of the target token to receive based on sol_in and slippage.

    • Manages creation of WSOL (Wrapped SOL) account and the target token's Associated Token Account (ATA) if they don't exist.

    • Constructs a transaction with instructions for creating/initializing WSOL account, swapping via Raydium, and closing the temporary WSOL account.

    • Signs and sends the transaction, then confirms it.

    • Returns True if confirmed, False otherwise.

  • sell_with_raydium(agent: SolanaAgentKit, pair_address: str, percentage: int = 100, slippage: int = 5) -> bool:

    • Executes a sell order on a Raydium liquidity pool, swapping a token for SOL.

    • Parameters:

      • agent: The SolanaAgentKit instance.

      • pair_address: The string address of the Raydium AMM pool ID.

      • percentage: The percentage of the user's token balance to sell (default 100%).

      • slippage: Allowed slippage percentage (default 5%).

    • Fetches pool keys and the user's balance for the token being sold.

    • Calculates the amount of tokens to sell based on percentage and the minimum SOL amount to receive based on slippage.

    • Manages creation of a temporary WSOL account to receive the SOL from the swap.

    • Constructs a transaction similar to buy_with_raydium but for selling tokens.

    • Optionally closes the token account being sold if 100% of its balance is sold.

    • Signs, sends, and confirms the transaction.

    • Returns True if confirmed, False otherwise.

Important Considerations:

  • Direct On-Chain Interaction: This tool interacts directly with the Solana blockchain and Raydium's smart contracts. It does not use a proxy service.

  • Financial Risk: Trading on DEXs like Raydium involves financial risks, including price impact, slippage, and potential smart contract vulnerabilities.

  • AMM Pool ID: You must use the correct Raydium AMM Liquidity Pool ID for the pair_address parameter, not a token mint address. These IDs can be found on Raydium's website or through DEX explorers.

  • WSOL Handling: The tool correctly wraps SOL into WSOL for trading against SPL tokens in Raydium pools and unwraps WSOL back to SOL when selling tokens for SOL. It manages temporary WSOL accounts.

  • Transaction Confirmation: The methods include a call to confirm_txn to wait for transaction finalization.

Source Code

You can find the source code for this tool on GitHub: https://github.com/niceberginc/agentipy/blob/main/agentipy/tools/use_raydium.py

Last updated