Skip to main content

ABI Builder for NEAR Python Smart Contracts

Project description

NEAR ABI Python

Python Version License: MIT

A lightweight tool for generating Application Binary Interface (ABI) definitions for NEAR smart contracts written in Python.

🔑 Key Features

  • Simple: Minimalist design with a focus on usability
  • 🔍 Auto-detection: Automatically detects NEAR contract functions and their types
  • 📊 Beautiful CLI: Rich text interface with color-coded output
  • 📄 Validation: Built-in ABI validation against the official schema
  • 📂 Multi-file support: Analyze entire directories of Python contract files

📦 Installation

# Install with pip
pip install near-abi-py

# Install with uv
uv pip install near-abi-py

# Or use directly without installing
python -m 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/

# 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

# Generate ABI from a single contract file
abi = generate_abi("path/to/contract.py")

# Or generate ABI from multiple files
python_files = ["path/to/file1.py", "path/to/file2.py"]
abi = generate_abi_from_files(python_files, "path/to/project_dir")

# Validate the generated ABI
from near_abi_py.utils import validate_abi
is_valid, messages = validate_abi(abi)

if is_valid:
    print("ABI is valid!")
else:
    print("ABI has issues:", messages)

📘 How It Works

NEAR ABI Python analyzes Python smart contract files by:

  1. Identifying functions marked with NEAR decorators (@view, @call, @init, etc.)
  2. Extracting parameter types and return types using Python type hints
  3. Generating JSON Schema representations of each parameter and return type
  4. Creating a standardized ABI schema following NEAR's specifications
  5. Validating the output against the official ABI schema

🔧 CLI Reference

Usage: near-abi-py [OPTIONS] COMMAND [ARGS]...

  NEAR Python ABI Builder

Options:
  --version  Show the version and exit.
  --help     Show this message and exit.

Commands:
  generate  Generate ABI from a contract file or directory
  validate  Validate an existing ABI file against the schema

Generate Command

Usage: near-abi-py generate [OPTIONS] SOURCE_PATH

  Generate ABI from a Python contract file or directory.

Options:
  -o, --output FILE              Output file path (default: stdout)
  --recursive / --no-recursive   Scan subdirectories recursively (default: True)
  --validate / --no-validate     Validate the generated ABI (default: True)
  --help                         Show this message and exit.

Validate Command

Usage: near-abi-py validate [OPTIONS] ABI_FILE

  Validate an existing ABI file against the schema.

Options:
  --help      Show this message and exit.

🧩 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()
new = contract.new
set_greeting = contract.set_greeting
get_greeting = contract.get_greeting

🧠 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

📦 Dependencies

  • Python 3.12+
  • pydantic >= 2.10.6
  • click >= 8.1.8
  • jsonschema >= 4.23.0
  • rich >= 13.9.4

📜 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

near_abi_py-0.5.0.tar.gz (47.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

near_abi_py-0.5.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file near_abi_py-0.5.0.tar.gz.

File metadata

  • Download URL: near_abi_py-0.5.0.tar.gz
  • Upload date:
  • Size: 47.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.6.8

File hashes

Hashes for near_abi_py-0.5.0.tar.gz
Algorithm Hash digest
SHA256 feab62f9acb99ea547aed4e7008c5beb06b9adb68ab63ce3d2250b3d79181f79
MD5 807760f624d627a9d1b943d019849ef6
BLAKE2b-256 c9b23fb32174abcef704dd84ad7d075713ec4b9c820a610cb5d5927256f9e4dc

See more details on using hashes here.

File details

Details for the file near_abi_py-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for near_abi_py-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 571be721a292d90218a3acf555f60d26e5fc75fd8ccfd633cef0823834c3a1ca
MD5 dfff1c508a537c579f96d116c6579b23
BLAKE2b-256 b3c56eaeb60eb0bdea76ab2ca8154c2cf07e5ad885e9a50d1cb839b58433347a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page