Payments Agent
This tutorial demonstrates how to create a research agent that receives SOL payments from users before executing tasks. The agent analyzes investment-related queries and retrieves data from Web3 tools.
Overview
The agent leverages:
gpt-4o
model from OpenAI for language processing.CodeAgent
to orchestrate a series of steps toward a final result.AgentRuntime
to connect clients to the agent and manage execution.- Clients:
SimpleMessageClient
for local testing with predefined messages.- (Optional)
TwitterMentionClient
for processing mentions on Twitter.
- Web3 Tools:
get_coin_price
(CoinGecko) to fetch real-time cryptocurrency prices.get_token_profile
(DexScreener) to retrieve token profiles.
- A Pricing Mechanism using Solana (SOL) payments. The agent will validate payment before executing the task.
Payment & Transaction Requirements
To use the agent, a client (user) must provide a task with either:
- A link to a Solana transaction (e.g., from Solscan)
- A transaction signature on the Solana blockchain
Setup
-
Local Environment & Galadriel Installation:
-
Agent Wallet Creation:
The agent needs a Solana wallet to receive payments. If you don’t have one, create a new one. The tutorial is described in Wallet Tutorial.
-
Environment Variables Configuration:
Rename
template.env
to.env
and populate it with your OpenAI API key, agent wallet address, and optional Twitter API credentials (if usingTwitterMentionClient
):- Make sure you have configured
SOLANA_KEY_PATH
in .agents.env file to point the path to your Solana wallet
- Make sure you have configured
-
Agent Code (
agent.py
):Create a file named
agent.py
with the following Python code. Remember to replace placeholders with your actual values.Code Explanation:
-
Imports: Imports necessary libraries and modules.
-
Environment Variables: Loads environment variables from a
.env
file. -
Model Setup: Initializes the
LiteLLMModel
with the specified model ID and API key. -
Tool Setup:
- Initializes
coingecko.get_coin_price
anddexscreener.get_token_profile
- Initializes
-
Agent Setup:
- Initializes the
CodeAgent
with the specified tools
- Initializes the
-
Client Setup:
- Initializes the
SimpleMessageClient
with a sample question and Solana transaction link.
- Initializes the
-
Runtime Setup:
- Initializes the
AgentRuntime
with the agent, pricing, input, and output.
- Initializes the
-
-
Run the agent:
Optional: Running the agent with Twitter Client
To use the Twitter client, you have to set TWITTER_CONSUMER_API_KEY
, TWITTER_CONSUMER_API_SECRET
, TWITTER_ACCESS_TOKEN
and TWITTER_ACCESS_TOKEN_SECRET
environment variables defined inside .env file.
-
Uncomment Twitter related code snippets:
TwitterMentionClient
import statementtwitter_client
initialization with the proper credentials- Pass
twitter_client
client to theAgentRuntime
arguments forinputs
andoutputs
-
Run the agent:
The agent will now monitor Twitter mentions, check the transaction signature for payment, and provide its analysis if payment validation is successful.