Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Crypto-Price-Alert-Agent/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 🪙 AI Crypto Price Alert Agent

This agent uses the **Fetch.ai uAgents library** to monitor cryptocurrency prices in real-time. It is designed to help traders or enthusiasts get automated alerts when a specific coin hits a target price.

## Features
- **Real-time Monitoring:** Fetches data every 60 seconds using the CoinGecko API.
- **Automated Logic:** Compares current price against a user-defined threshold.
- **Fetch.ai Native:** Built using the `uAgents` framework for decentralized communication.

## How to Run
1. Install dependencies:
```bash
pip install -r requirements.txt
33 changes: 33 additions & 0 deletions Crypto-Price-Alert-Agent/agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from uagents import Agent, Context
import requests

# 1. Define your Agent
# Replace "your_seed_phrase" with any random text
crypto_agent = Agent(name="crypto_watcher", seed="Cripto_Watcher_24")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Replace "your_seed_phrase" with any random text
# In production, load from os.environ["AGENT_SEED_PHRASE"]
crypto_agent = Agent(name="crypto_watcher", seed="Cripto_Watcher_24")

# 2. Configuration
CRYPTO_ID = "bitcoin" # You can change this to 'ethereum' etc.
THRESHOLD_PRICE = 60000 # Alert if price goes below this

def get_price(coin_id):
url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd"
response = requests.get(url)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The asynchronous check_price function uses a blocking requests.get() call without a timeout, which will freeze the agent and make it unresponsive during the API request.
Severity: CRITICAL

Suggested Fix

Replace the synchronous requests.get() call with an asynchronous HTTP client library like aiohttp. Alternatively, if requests must be used, wrap the blocking call in asyncio.to_thread() to run it in a separate thread without blocking the main event loop. In either case, a reasonable timeout should be added to all network requests to prevent indefinite hangs.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: Crypto-Price-Alert-Agent/agent.py#L14

Potential issue: The `check_price` function is an asynchronous interval handler that
uses the synchronous `requests.get()` library to make an HTTP call. This is a blocking
I/O operation that will freeze the agent's entire asyncio event loop while it waits for
the network response. During this time, the agent will be unable to process any other
tasks, such as incoming messages or other scheduled intervals. Furthermore, the
`requests.get()` call does not have a `timeout` parameter, creating a risk that the
agent could hang indefinitely if the external API is slow or unresponsive, causing it to
miss subsequent checks.

Did we get this right? 👍 / 👎 to inform future reviews.

data = response.json()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data = response.json()
def get_price(coin_id):
url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd"
response = requests.get(url)
response.raise_for_status() # Raise HTTPError for bad responses
data = response.json()
return data[coin_id]['usd']

return data[coin_id]['usd']

@crypto_agent.on_interval(period=60.0)
async def check_price(ctx: Context):
try:
current_price = get_price(CRYPTO_ID)
ctx.logger.info(f"Current {CRYPTO_ID} price: ${current_price}")

if current_price < THRESHOLD_PRICE:
ctx.logger.info(f"🚨 ALERT: {CRYPTO_ID} is below ${THRESHOLD_PRICE}! Time to buy?")
else:
ctx.logger.info(f"Price is stable above ${THRESHOLD_PRICE}.")

except Exception as e:
ctx.logger.error(f"Failed to fetch price: {e}")

if __name__ == "__main__":
crypto_agent.run()
2 changes: 2 additions & 0 deletions Crypto-Price-Alert-Agent/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
uagents
requests