Trade (Token Swap)
The Trade
tool, part of AgentiPy, provides functionalities for executing token swaps on the Solana blockchain using the Jupiter Aggregator. It allows users to swap one SPL token (or SOL) for another by fetching optimal routes and transaction details from Jupiter's API.
Functionality
This tool is implemented in the TradeManager
class.
Key Methods:
trade(agent: SolanaAgentKit, output_mint: Pubkey, input_amount: float, input_mint: Pubkey = TOKENS["USDC"], slippage_bps: int = DEFAULT_OPTIONS["SLIPPAGE_BPS"]) -> str
:Takes a
SolanaAgentKit
instance, theoutput_mint
(Pubkey of the token to receive),input_amount
(amount of the input token to swap, in UI units like SOL or USDC quantity),input_mint
(Pubkey of the token to send, defaults to USDC), andslippage_bps
(slippage tolerance in basis points, default is 300 = 3%).Fetches a swap quote from the Jupiter API (
/quote
) based on the input parameters. This quote provides the best route and expected output amount.Requests the serialized swap transaction from the Jupiter API (
/swap
) using the obtained quote and the agent's wallet address.Signs the received
VersionedTransaction
with the agent's wallet.Sends and confirms the transaction on the Solana network.
Returns the transaction signature as a string.
Important Note: This tool performs on-chain financial transactions. Using it with real assets carries risk, including potential loss of funds due to market volatility, slippage, or smart contract interactions. Always test thoroughly on Devnet with small amounts if possible, and understand the tokens and protocols you are interacting with.
Source Code
You can find the source code for this tool on GitHub: https://github.com/niceberginc/agentipy/blob/main/agentipy/tools/trade.py
Last updated