Add BlockRun integration for x402 USDC micropayments#124
Add BlockRun integration for x402 USDC micropayments#1241bcMax wants to merge 6 commits intoPolymarket:mainfrom
Conversation
- Add blockrun.py connector with LangChain-compatible LLM provider - Update executor.py to support BlockRun as alternative to OpenAI - Add BLOCKRUN_ENABLED env var for easy switching - Support 31+ models including GPT-5, GPT-4, Claude, Gemini via x402 - Update README with BlockRun setup and usage docs - Update .env.example with BlockRun config options BlockRun enables agents to pay for LLM calls with USDC micropayments on Base, eliminating the need for API key management.
04af876 to
f0f3703
Compare
Fix for x402 Wallet IntegrationThe Cursor bot correctly identified that the current implementation doesn't actually handle x402 payments. Here's the fix: The ProblemThe current code passes a placeholder string The SolutionUse the
Updated CodeReplace ```python Provides LLM access via x402 USDC micropayments on Base network. SECURITY NOTE - Private Key Handling:Your private key NEVER leaves your machine. Here's what happens:
This is the same security model as:
The x402 protocol uses EIP-3009 (TransferWithAuthorization) which allows import os from langchain_core.callbacks import CallbackManagerForLLMRun Model mapping: common names -> BlockRun model IDsBLOCKRUN_MODEL_MAP: Dict[str, str] = { BLOCKRUN_MAX_TOKENS: Dict[str, int] = { def get_blockrun_model_name(model: str) -> str: class BlockRunChat(BaseChatModel): def create_blockrun_llm(model: str = "gpt-4o", temperature: float = 0.7, **kwargs) -> BlockRunChat: Testing Verified ✅I tested this code and it works:
Full test code and verified output available at: https://github.com/BlockRunAI/nano-banana-blockrun/tree/main/polymarket-fix |
Refactored the BlockRun connector to use the official blockrun-llm SDK instead of a custom x402 implementation. The SDK handles all payment flow automatically: - EIP-712 signing for USDC TransferWithAuthorization - 402 Payment Required response handling - Automatic retry with payment signature Added blockrun-llm>=0.2.0 to requirements.txt 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
.env.example
Outdated
| # Your agent pays for LLM calls with USDC on Base | ||
| # Learn more: https://blockrun.ai | ||
| BLOCKRUN_ENABLED=false | ||
| BLOCKRUN_API_URL="https://api.blockrun.ai/v1" |
There was a problem hiding this comment.
Inconsistent API URLs between documentation and code defaults
The .env.example and README specify https://api.blockrun.ai/v1 as the BLOCKRUN_API_URL, but the code in blockrun.py defaults to https://blockrun.ai/api. These are different endpoints. Users who don't set the env var get the code default, while those who copy from documentation get a different URL. This inconsistency will cause connection failures depending on which URL is correct.
Additional Locations (2)
.env.example
Outdated
| # Your agent pays for LLM calls with USDC on Base | ||
| # Learn more: https://blockrun.ai | ||
| BLOCKRUN_ENABLED=false | ||
| BLOCKRUN_API_URL="https://api.blockrun.ai/v1" |
There was a problem hiding this comment.
Required wallet key environment variable missing from configuration
The BlockRun connector requires a wallet private key for x402 payment signing, looking for BLOCKRUN_WALLET_KEY or BLOCKRUN_PRIVATE_KEY environment variables. However, neither variable is documented in .env.example. The PR description claims agents use "the same wallet for trading AND AI payments," but the code doesn't use POLYGON_WALLET_PRIVATE_KEY. Users following the setup guide will have no wallet key configured, causing payment authorization to fail.
Additional Locations (1)
- Added clear note that payments are USDC on Base network only - Emphasized that private keys never leave the machine - Only signatures are transmitted, not keys 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Added create_blockrun_llm() for LangChain compatibility with executor.py - Added get_blockrun_model_name() and get_blockrun_token_limit() functions - Added GPT-5 to model mappings - Fixed API URL in .env.example (https://blockrun.ai/api) - Added BLOCKRUN_WALLET_KEY to .env.example - Note: Uses POLYGON_WALLET_PRIVATE_KEY as fallback for wallet key All functions use the official blockrun-llm SDK internally. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
agents/connectors/blockrun.py
Outdated
| >>> client = create_blockrun_client(private_key="0x...") | ||
| >>> response = client.chat("gpt-4o", "What is 2+2?") | ||
| """ | ||
| pk = private_key or os.getenv("BLOCKRUN_WALLET_KEY") or os.getenv("BLOCKRUN_PRIVATE_KEY") |
There was a problem hiding this comment.
Inconsistent private key fallback between client functions
The create_blockrun_client function falls back to BLOCKRUN_PRIVATE_KEY environment variable, while BlockRunLLM falls back to POLYGON_WALLET_PRIVATE_KEY. The .env.example documents POLYGON_WALLET_PRIVATE_KEY as the fallback. This inconsistency means create_blockrun_client won't work for users who only set POLYGON_WALLET_PRIVATE_KEY as instructed in the documentation.
Additional Locations (1)
- Changed create_blockrun_client to use POLYGON_WALLET_PRIVATE_KEY as fallback instead of BLOCKRUN_PRIVATE_KEY for consistency with BlockRunLLM - Added error handling for empty choices array in BlockRunLLM.invoke() 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Added "Payment: USDC on Base network only" to BlockRunLLM class docstring - Added Base network note to create_blockrun_llm function - Updated .env.example with chain ID 8453 and clearer Base-only messaging 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
| role = 'user' | ||
|
|
||
| content = msg.content if hasattr(msg, 'content') else str(msg) | ||
| formatted_messages.append({"role": role, "content": content}) |
There was a problem hiding this comment.
String input to invoke iterates over characters breaking API
The BlockRunLLM.invoke() method iterates over its input with for msg in messages:, but several executor methods pass plain strings rather than message lists (e.g., get_superforecast, filter_events, source_best_trade). When a string is passed, Python iterates over each character, creating a separate API message per character. A 1000-character prompt becomes 1000 single-character messages. LangChain's ChatOpenAI.invoke() handles string inputs by converting them to a single user message, but BlockRunLLM lacks this check. This completely breaks multiple executor methods when BlockRun is enabled.
Summary
This PR adds BlockRun as an alternative LLM provider, enabling Polymarket agents to pay for AI inference with USDC micropayments via the x402 protocol on Base.
What is BlockRun?
BlockRun is a crypto-native AI gateway that provides:
Changes
agents/connectors/blockrun.py- New connector with LangChain-compatible LLM provideragents/application/executor.py- Updated to support BlockRun as alternative to OpenAI.env.example- Added BlockRun configuration optionsREADME.md- Added documentation for BlockRun setup and usageUsage
Why This Matters for Polymarket Agents
Testing
The integration uses the existing LangChain
ChatOpenAIclass with BlockRun's OpenAI-compatible API, ensuring compatibility with all existing code paths.Learn more: blockrun.ai | x402 Protocol
Note
Introduces BlockRun as an alternative LLM backend using x402 USDC micropayments and integrates it into the agent runtime.
agents/connectors/blockrun.py: BlockRun client and LangChain-compatibleBlockRunLLM, model mappings, and token limitsagents/application/executor.py: Addsuse_blockrunflag (orBLOCKRUN_ENABLED) to switch between OpenAI and BlockRun; sets token limits via BlockRun helpers.env.example: AddsBLOCKRUN_ENABLED,BLOCKRUN_WALLET_KEY, andBLOCKRUN_API_URLconfigurationREADME.md: Setup/usage docs for BlockRun, model list, and examplesrequirements.txt: Addsblockrun-llmdependencyWritten by Cursor Bugbot for commit 183441d. This will update automatically on new commits. Configure here.