AgentOracle / app.py
SofiTesfay2010's picture
Upload app.py with huggingface_hub
6da08bc verified
from fastapi import FastAPI, Header, Response
from pydantic import BaseModel
from typing import Dict, Optional
import random
import time
app = FastAPI(
title="AgentOracle x402",
description="Real-time sentiment and price data for agent-native tokens. Pay $0.05 per query."
)
# Config
RECEIVER_WALLET = "0x2d44fc27a616606b42448309F4d8e3F423d93267"
PRICE_USDC = 0.05
PRICE_ATOMIC = int(PRICE_USDC * 1_000_000)
class OracleResponse(BaseModel):
token: str
price_usd: float
sentiment: str
trend: str
timestamp: str
@app.get("/")
async def root():
return {"message": "AgentOracle is ONLINE. POST /query to get market intel."}
@app.post("/query", response_model=OracleResponse)
async def get_intel(
token_name: str,
response: Response,
x_payment: Optional[str] = Header(None)
):
# x402 Payment Gate
if not x_payment or not x_payment.startswith("pay_"):
response.status_code = 402
response.headers["WWW-Authenticate"] = f'Token realm="x402", as_id="{RECEIVER_WALLET}", min_amount="{PRICE_ATOMIC}", currency="USDC", network="base"'
return {
"error": "Payment Required",
"message": f"Send {PRICE_USDC} USDC to {RECEIVER_WALLET} on Base to unlock this oracle data.",
"payment_details": {
"address": RECEIVER_WALLET,
"amount": PRICE_ATOMIC,
"chain": "base"
}
}
# Market Intel Logic (Simulated for MVP)
prices = {"VIRTUAL": 1.24, "PROMPT": 0.85, "MOLT": 0.12, "SIM": 0.05}
sentiments = ["BULLISH", "BEARISH", "NEUTRAL", "EXTREME GREED"]
return OracleResponse(
token=token_name.upper(),
price_usd=prices.get(token_name.upper(), random.uniform(0.1, 2.0)),
sentiment=random.choice(sentiments),
trend="UP" if random.random() > 0.5 else "DOWN",
timestamp=str(int(time.time()))
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)