ABI Builder for NEAR Python Smart Contracts
Project description
NEAR ABI Python
ABI Builder for NEAR Python Smart Contracts - generate standardized interface definitions from your Python smart contracts.
🔑 Key Features
- Generate ABI schemas from Python NEAR smart contracts
- Support for multi-file projects and directory scanning
- Validate existing ABI definitions against the official schema
- Command-line interface for easy integration into build processes
- Usable as a library in your Python projects
- Support for NEP-compatible contract analysis
- Rich output formatting for better readability
📦 Installation
# Install with pip
pip install near-abi-py
# Install with uv
uv pip install near-abi-py
# Or use directly without installing (recommended)
uvx near-abi-py generate example.py
🚀 Quick Start
CLI Usage
# Generate ABI from a single contract file
near-abi-py generate example.py
# Generate ABI from a project directory
near-abi-py generate ./my_contract_project/
# Scan and list Python files in a directory
near-abi-py scan ./my_contract_project/
# Generate and save to a specific output file
near-abi-py generate example.py -o example.abi.json
# Validate an existing ABI file
near-abi-py validate contract.abi.json
Library Usage
from near_abi_py import generate_abi, generate_abi_from_files, validate_abi, find_python_files
# Generate ABI from a single contract file
abi = generate_abi("path/to/contract.py")
# Or generate ABI from multiple files
python_files = find_python_files("path/to/project_dir")
abi = generate_abi_from_files(python_files, "path/to/project_dir")
# Validate the generated ABI
validation_messages = validate_abi(abi)
if not validation_messages:
print("ABI is valid!")
else:
print("ABI has issues:", validation_messages)
📘 How It Works
NEAR ABI Python analyzes Python smart contract files by:
- Parsing the contract's AST (Abstract Syntax Tree)
- Identifying functions marked with NEAR decorators (
@view,@call,@init) - Extracting parameter types and return types
- Generating a standardized ABI schema following NEAR's specifications
- Validating the schema against the official metaschema
For multi-file projects:
- The tool scans the entire directory for Python files
- It analyzes each file for contract functions
- Functions from all files are combined into a single comprehensive ABI
The resulting ABI can be used by developer tools, IDEs, and frontends to:
- Understand contract interfaces
- Generate client-side bindings
- Type-check interactions with the contract
- Document the contract's capabilities
🔧 CLI Reference
near-abi-py [COMMAND] [OPTIONS]
Commands:
generate Generate ABI from a contract file or directory
validate Validate an existing ABI file
scan List Python files in a directory that would be scanned
Options for 'generate':
--output, -o Output file path (default: stdout)
--validate, -v Validate the generated ABI before output
--recursive/--no-recursive Scan subdirectories recursively (default: recursive)
--respect-gitignore/--ignore-gitignore
Respect .gitignore patterns (default: respect)
Options for 'scan':
--recursive/--no-recursive Scan subdirectories recursively (default: recursive)
--respect-gitignore/--ignore-gitignore
Respect .gitignore patterns (default: respect)
General options:
--help Show this help message
--version Show version information
📚 Library API
generate_abi(contract_file: str, package_path: Optional[str] = None) -> Dict[str, Any]
Analyzes a single Python contract file and generates the corresponding ABI.
Parameters:
contract_file: Path to the Python contract filepackage_path: Optional path to the package information file (e.g., pyproject.toml)
Returns:
- ABI definition as a dictionary
generate_abi_from_files(file_paths: List[str], project_dir: str) -> Dict[str, Any]
Generates an ABI from multiple Python files in a project directory.
Parameters:
file_paths: List of Python file paths to analyzeproject_dir: Root directory of the project
Returns:
- ABI definition as a dictionary
find_python_files(directory: str, recursive: bool = True, respect_gitignore: bool = True) -> List[str]
Finds all Python files in a directory, optionally respecting .gitignore patterns.
Parameters:
directory: Path to the directory to scanrecursive: Whether to scan subdirectories recursivelyrespect_gitignore: Whether to respect .gitignore patterns
Returns:
- List of paths to Python files
validate_abi(abi: Dict[str, Any]) -> List[str]
Validates an ABI against the official schema.
Parameters:
abi: The ABI to validate (dictionary)
Returns:
- List of validation error messages (empty if valid)
🧠 Understanding NEAR ABI Format
The generated ABI follows NEAR's ABI specification and includes:
- Contract metadata (name, version, authors)
- Function definitions (name, parameters, return types)
- Function modifiers (view, call, init, etc.)
- Type information in JSON Schema format
Example of a generated ABI:
{
"schema_version": "0.4.0",
"metadata": {
"name": "greeting",
"version": "0.1.0",
"authors": ["Example Author"],
"build": {
"compiler": "python 3.11.5",
"builder": "near-sdk-py 0.3.0"
}
},
"body": {
"functions": [
{
"name": "get_greeting",
"kind": "view",
"params": {
"serialization_type": "json",
"args": []
},
"result": {
"serialization_type": "json",
"type_schema": {
"type": "string"
}
}
},
{
"name": "set_greeting",
"kind": "call",
"params": {
"serialization_type": "json",
"args": [
{
"name": "message",
"type_schema": {
"type": "string"
}
}
]
}
}
],
"root_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "NEAR Contract Schema",
"definitions": {
"AccountId": {
"type": "string",
"description": "NEAR account identifier"
}
// Additional NEAR types...
}
}
}
}
🧠 Working with Multi-file Projects
NEAR ABI Python now supports analyzing multi-file NEAR contract projects. When a directory is provided instead of a single file, the tool:
- Scans for all Python files in the directory (recursively by default)
- Respects .gitignore patterns (if pathspec is installed)
- Analyzes each file for NEAR contract functions
- Combines all discovered functions into a single comprehensive ABI
- Automatically detects project metadata from pyproject.toml if available
This is especially useful for larger contracts split across multiple files or when your contract depends on local modules.
Example Multi-file Project Structure
my_near_contract/
├── pyproject.toml
├── contract.py # Main contract entry points
├── models/
│ ├── __init__.py
│ ├── account.py # Account-related functions
│ └── token.py # Token-related functions
└── utils/
├── __init__.py
└── helpers.py # Helper functions
When running:
near-abi-py generate my_near_contract/
The tool will scan all Python files in the project and combine all NEAR-decorated functions (@view, @call, @init) into a single ABI.
🔄 Integration with NEAR Tools
NEAR ABI Python complements the following tools in the NEAR Python ecosystem:
- near-sdk-py: The Python SDK for writing NEAR smart contracts
- nearc: The Python-to-WebAssembly compiler for NEAR smart contracts
While the SDK provides the framework for writing contracts and the compiler turns them into WebAssembly, NEAR ABI Python creates the interface definitions needed for external tools to interact with your contract.
💼 Use Cases
- Generate and share contract interfaces with frontend developers
- Enable IDE autocompletion and type checking for contract interactions
- Document your contract API in a standard format
- Validate contract implementations against interface specifications
- Support cross-language contract development and interaction
- Analyze complex, multi-file NEAR contract projects
🧩 Examples
Example Contract
from near_sdk_py import view, call, init, Context, Storage, Log
class GreetingContract:
@init
def new(self, owner_id=None):
"""Initialize the contract with optional owner"""
owner = owner_id or Context.predecessor_account_id()
Storage.set("owner", owner)
Log.info(f"Contract initialized by {owner}")
return True
@call
def set_greeting(self, message):
"""Store a greeting message (requires gas)"""
Storage.set("greeting", message)
return f"Greeting updated to: {message}"
@view
def get_greeting(self):
"""Retrieve the greeting message (free, no gas needed)"""
return Storage.get_string("greeting") or "Hello, NEAR world!"
# Export the contract methods
contract = GreetingContract()
# These exports make functions available to the NEAR runtime
new = contract.new
set_greeting = contract.set_greeting
get_greeting = contract.get_greeting
Generate ABI in a Build Process
#!/bin/bash
# build.sh
# Compile the contract
nearc contract.py
# Generate the ABI (works with directories too)
near-abi-py generate ./src/ -o contract.abi.json
# Deploy the contract (example)
near deploy myaccount.testnet contract.wasm
📝 Contributing
Contributions are welcome! Feel free to submit a Pull Request with bug fixes, improvements, or new features.
📜 License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file near_abi_py-0.3.0.tar.gz.
File metadata
- Download URL: near_abi_py-0.3.0.tar.gz
- Upload date:
- Size: 60.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a87fcb4cb9d31bd893968929fea44158e3990cd8a3053d8b986db58b0bf07d4
|
|
| MD5 |
d936c9af627443f13a909820f1d766cb
|
|
| BLAKE2b-256 |
ff634852e072a0a06fbfdaa3dcb782c077b4a5535a3ae9c1be7dacb4898e3096
|
File details
Details for the file near_abi_py-0.3.0-py3-none-any.whl.
File metadata
- Download URL: near_abi_py-0.3.0-py3-none-any.whl
- Upload date:
- Size: 23.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a877d9caa2de4e1f9e4005d483d55d6005681ce90d492fe7960a9ea7b24ba09
|
|
| MD5 |
545b4f4d9d494c53a88d153e82db7f67
|
|
| BLAKE2b-256 |
a3c4d3fa57da465555d96e1f7d8e22fdda5bad9496e36270fda50f6954633dba
|