Skip to main content
This tutorial guides you through building a trading agent that fetches trending coins, gets market data, performs analysis, and executes token swaps using the Galadriel framework.

Prerequisites

  • Python >=3.10
  • Git
  • Familiarity with blockchain concepts and Solana development (optional)

Setup

  1. Clone the Galadriel repository:
    Replace <repository_url> with the actual URL of the Galadriel repository.
  2. Create a virtual environment:
  3. Install the Galadriel framework:
  4. Configure environment variables:
    • Rename template.env to .env and add your OpenAI API and Coingecko API keys:
    • Rename template.agents.env to agents.env and add your Solana keypair path:
      If you don’t have a Solana keypair, you can create one and get funded using the Galadriel CLI (see Wallet Setup):
      And then:

Agent Implementation

  1. Create the agent.py file: Create a new file named agent.py in the examples/trading directory and add the following code:

Code Explanation

Imports

The code imports necessary modules from the Galadriel framework and standard Python libraries.

Configuration

The code loads environment variables from .env and agents.env files, initializes the LiteLLM model with your OpenAI API key, and defines the trading interval.

Tools

The code defines the tools that the agent will use:
  • market_data_devnet.fetch_mock_market_data: Fetches mock market data for development purposes.
  • raydium_cpmm.BuyTokenWithSolTool(): Tool for buying tokens with SOL using Raydium CPMM.
  • solana_common.GetAdminWalletAddressTool(): Tool to get the admin wallet address.
  • solana_common.GetUserBalanceTool(): Tool to get the user’s balance for a specific token.

Agent Initialization

The code initializes the CodeAgent with the trading prompt, LiteLLM model, and the defined tools. It also enables additional authorized imports such as json that might be required by the tools. Setting max_steps makes the trading agent more reliable by increasing the number of steps it can take to complete the task

Runtime Configuration

The code configures the AgentRuntime with a Cron input to schedule agent execution, no outputs (as the agent executes trades directly), and the initialized trading agent.

Start the Agent

The code runs the agent runtime, starting the autonomous trading loop.

Running the Agent

  1. Make sure the solana keypair defined in .agents.env has some funds.
  2. Run the agent:
The agent will then start fetching trending coins, getting market data, performing analysis, and executing token swaps every 5 minutes.