PyBitrum
Smart Contracts For Agents
The problem PyBitrum solves
PyBitrum solves the critical challenge of making smart contract development accessible to Python developers while ensuring production-grade security and autonomy. Here's what it enables:
1. Democratizing Smart Contract Development
- Write Once, Deploy Everywhere: Developers can write smart contracts in Python (a language 10M+ developers know) and simultaneously compile to both Solidity and Vyper, eliminating the steep learning curve of specialized smart contract languages.
- Lower Barrier to Entry: Python's readability and familiar syntax reduce cognitive overhead compared to learning Solidity from scratch.
2. Autonomous Agent Deployment
- 24/7 Agent Execution: PyBitrum enables developers to deploy
AgentWalletentities - autonomous agents with their own EOAs (Externally Owned Accounts), private keys, and on-chain identities that execute missions without human intervention. - Mission Board Economy: Post bounties in ETH, let agents claim, execute, and settle transactions autonomously - creating a decentralized task execution marketplace.
- Swarm Orchestration: Coordinate fleets of agents using concurrent Python threads with shared state broadcasting, enabling complex multi-agent scenarios like market making, arbitrage, and liquidation bots.
3. Industrial-Grade Safety & Security
- Compile-Time Security Guardrails: 14 dangerous patterns (exec, eval, os, subprocess, sockets, etc.) are blocked at the security check stage - before any code transformation.
- Dual-Language Validation: Code is validated for both Vyper and Solidity compatibility, ensuring maximum deployment flexibility and catching language-specific bugs early.
- Automatic Reentrancy Protection: The
@agent_actiondecorator auto-injects@nonreentrantandmsg.senderverification, preventing common attack vectors without manual intervention.
4. Cloud-Native Development Experience
- Browser-Based IDE: The Playground provides a Monaco Editor with syntax highlighting, real-time compilation feedback, GitHub OAuth, and one-click MetaMask deployment.
- Instant Feedback Loop: Developers see compilation errors/warnings and generated Solidity/Vyper code in real-time, accelerating iteration.
- Full-Stack Tooling: Includes a Mission Board UI for agent interaction, comprehensive documentation hub, and a pip-installable SDK for local development.
5. Reduces Smart Contract Bugs & Costs
- AST-Based Transformation: Uses Abstract Syntax Trees to intelligently map Python constructs to EVM semantics, handling edge cases like memory layout and type conversions automatically.
- Consistent Code Generation: The dual-compiler pipeline ensures generated contracts are idiomatic in both Solidity and Vyper, reducing the risk of subtle compiler-specific bugs.
- Lower Deployment Costs: Vyper code often generates smaller bytecode than Solidity, reducing gas costs for on-chain deployments.
Challenges I ran into
Challenge 1: Dual-Compiler AST Parity
Problem: Python's AST has fundamentally different semantics than Solidity/Vyper. For example, Python's dynamic typing, runtime method resolution, and duck typing don't map cleanly to EVM's static types and fixed memory layout.
Specific Issues:
- Python
dict→ VyperHashMaprequires knowing the key/value types upfront (which Python doesn't enforce). - Python
list→ VyperDynArraywith a compile-time size limit (Vyper limits arrays to 256 elements by default). - Python's
selfcontext needed to be inferred as either@internalor@externalvisibility in Vyper/Solidity.
How I Solved It:
- Type Inference Engine: Built a multi-pass type analyzer that infers types from variable assignments, function return statements, and decorator hints.
- Decorator Annotations: Introduced
@contract,@action,@readdecorators that let developers explicitly annotate visibility and mutability where inference fails. - AST Transformation Layers: Created separate
PythonToVyperTransformer(36KB) andPythonToSolidityTransformer(24KB) classes, each handling language-specific quirks (e.g., Vyper's strictStringsizing vs Solidity's dynamicstring).
Challenge 2: Security Without False Positives
Problem: Blocking dangerous patterns like exec() and eval() is straightforward, but legitimate Python patterns get caught. For example:
- A function named
execute()shouldn't be blocked. - A variable containing
"os"as a string shouldn't trigger an "os import" warning. - Developers were frustrated with overly strict validation.
Specific Issues:
- Regex patterns for
os.*andsys.*produced false positives on identifiers likecustom_os_pathorsystem_config. - The first version blocked all
importstatements, making it impossible to usefrom pybitrum import Contract.
How I Solved It:
- AST-Based Pattern Matching: Replaced string regex with proper Python AST analysis. Now only
import osandimport sysare blocked, not arbitrary string matches. - Whitelisted Imports: Allowed safe imports like
from pybitrum import Contract,from typing import Dict, etc. - Multi-Pass Validation: Split validation into a
SecurityChecker(14 hard-coded patterns) andCodeValidator(AST walks), allowing different severity levels. - Detailed Error Messages: When validation fails, the compiler now returns specific line numbers and context (e.g., "exec() at line 42 is not allowed"), not just "bad code."
Challenge 3: TypeScript ↔ Python Serialization
Problem: The frontend (TypeScript/Next.js) sends Python code to the Flask compiler service and receives back Vyper/Solidity code + AST JSON. Serializing complex Python objects across HTTP required careful schema design.
Specific Issues:
- Python's
ast.ASTnodes aren't JSON-serializable by default. - Type mismatches between TypeScript interfaces and Python dictionaries caused runtime errors on the frontend.
- Large ASTs (100+ KB for complex contracts) bloated JSON payloads, causing slow compilation responses.
How I Solved It:
- Custom JSON Encoder: Built a
PyBitrumJSONEncoderthat convertsast.ASTnodes, code objects, and other non-serializable types to dictionaries. - Zod Schemas: On the backend (Express), used Zod to validate all compiler responses against strict TypeScript interfaces before returning to the frontend.
- AST Pruning: Limited the AST response to only essential fields (function names, arguments, return types) rather than the full parse tree, reducing payload size by ~70%.
- HTTP Compression: Enabled gzip compression on the Flask compiler service to further reduce response times.
Challenge 4: Vyper vs Solidity Bytecode Divergence
Problem: The same Python contract compiled to both Vyper and Solidity could produce different EVM bytecode and gas costs. For example:
- Vyper's
HashMapgenerates different storage layout than Solidity'smapping. - Vyper's built-in overflow checks add runtime gas overhead that Solidity 0.8.x includes at the language level.
- Compiler optimizations differ-solcx defaults to different optimization runs than vyper.
Specific Issues:
- Agents couldn't trust both compiled versions; they'd pick one and ignore the other.
- The Hive Registry contract expected a specific ABI, but Vyper and Solidity generated slightly different ABIs for the same logic.
- Gas estimates were inconsistent, making it hard to price agent missions on the Mission Board.
How I Solved It:
- Dual-ABI Registration: Modified the Hive Registry to accept both Vyper and Solidity ABIs, storing them alongside the contract address.
- Deterministic Compilation: Locked compiler versions (solcx 0.8.20, vyper 0.4.3) and fixed optimization settings to ensure reproducible bytecode.
Cheer Project
Cheering for a project means supporting a project you like with as little as 0.0025 ETH. Right now, you can Cheer using ETH on Arbitrum, Optimism and Base.
