Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions contract-py/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
dist/
wheels/
*.egg-info

# Virtual environments
venv/
env/
ENV/
.venv

# Testing
.pytest_cache/
.coverage
htmlcov/
.tox/

# NEAR
*.wasm
.near/
neardev/
out/

1 change: 1 addition & 0 deletions contract-py/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.13
127 changes: 127 additions & 0 deletions contract-py/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Count on NEAR Contract (Python)

The smart contract exposes methods to interact with a counter stored in the NEAR network.


# Quickstart

1. Make sure you have installed [Python](https://www.python.org/downloads/) >= 3.11
2. Install [uv](https://docs.astral.sh/uv/) for package management
3. Install the [NEAR CLI](https://github.com/near/near-cli#setup)

<br />

## 1. Build and Deploy the Contract

### Setup Virtual Environment

Creates a virtual environment with all required dependencies for building the contract.

```bash
uvx nearc contract.py --create-venv
```

### Build the Contract

Compile the contract to WebAssembly:

```bash
uvx nearc contract.py
```

This will create a `contract.wasm` file in the project root.

### Deploy to Testnet

```bash
near deploy <your-account-id>.testnet contract.wasm
```

### Initialize the Contract

After deployment, initialize the contract:

```bash
near call <your-account-id>.testnet initialize '{}' --accountId <your-account-id>.testnet
```

<br />

## 2. Get the Counter

`get_num` is a read-only method (aka `view` method).

`View` methods can be called for **free** by anyone, even people **without a NEAR account**!

```bash
near view <your-account-id>.testnet get_num
```

<br />

## 3. Modify the Counter

`increment`, `decrement` and `reset` change the contract's state, for which they are `call` methods.

`Call` methods can only be invoked using a NEAR account, since the account needs to pay GAS for the transaction.

```bash
# Increment by 1 (default)
near call <your-account-id>.testnet increment '{}' --accountId <your-account-id>.testnet

# Increment by specific number
near call <your-account-id>.testnet increment '{"number": 5}' --accountId <your-account-id>.testnet

# Decrement by 1 (default)
near call <your-account-id>.testnet decrement '{}' --accountId <your-account-id>.testnet

# Decrement by specific number
near call <your-account-id>.testnet decrement '{"number": 3}' --accountId <your-account-id>.testnet

# Reset counter to zero
near call <your-account-id>.testnet reset '{}' --accountId <your-account-id>.testnet
```

<br />

## 4. Run Tests

The contract includes comprehensive tests using the [near-pytest](https://github.com/r-near/near-pytest) framework:

```bash
# Install test dependencies
uv sync

# Run tests
uv run pytest
```

<br />

## Project Structure

```
contract-py/
├── contract.py # Main contract code
├── tests/
│ └── test_mod.py # Contract tests
├── pyproject.toml # Project configuration
├── uv.lock # Dependency lock file
├── .python-version # Python version specification
├── .gitignore # Git ignore rules
└── README.md
```

<br />

## Useful Links

- [near-sdk-py](https://github.com/r-near/near-sdk-py) - NEAR smart contract development SDK for Python
- [near-pytest](https://github.com/r-near/near-pytest) - Testing framework for NEAR smart contracts
- [NEAR CLI-rs](https://near.cli.rs) - Interact with NEAR blockchain from command line
- [NEAR Python Documentation](https://docs.near.org/tools/near-api)
- [NEAR Documentation](https://docs.near.org)
- [NEAR StackOverflow](https://stackoverflow.com/questions/tagged/nearprotocol)
- [NEAR Discord](https://near.chat)
- [NEAR Telegram Developers Community Group](https://t.me/neardev)
- NEAR DevHub: [Telegram](https://t.me/neardevhub), [Twitter](https://twitter.com/neardevhub)
52 changes: 52 additions & 0 deletions contract-py/contract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from near_sdk_py import Contract, call, view, init


class Counter(Contract):
"""
A simple counter smart contract that stores an integer value
and provides methods to increment, decrement, reset, and view it.
"""
@init
def initialize(self):
"""Initialize the contract with a counter value of 0."""
self.storage["val"] = 0

@view
def get_num(self) -> int:
"""
Public read-only method: Returns the counter value.

Returns:
int: The current counter value
"""
return self.storage.get("val", 0)

@call
def increment(self, number: int = 1):
"""
Public method: Increment the counter.

Args:
number (int): The amount to increment by (default: 1)
"""
current_val = self.storage.get("val", 0)
self.storage["val"] = current_val + number
self.log_info(f"Increased number to {self.storage['val']}")

@call
def decrement(self, number: int = 1):
"""
Public method: Decrement the counter.

Args:
number (int): The amount to decrement by (default: 1)
"""
current_val = self.storage.get("val", 0)
self.storage["val"] = current_val - number
self.log_info(f"Decreased number to {self.storage['val']}")

@call
def reset(self):
"""Public method: Reset the counter to zero."""
self.storage["val"] = 0
self.log_info("Reset counter to zero")
10 changes: 10 additions & 0 deletions contract-py/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[project]
name = "counter-py"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"near-pytest>=0.7.1",
"near-sdk-py>=0.7.3",
]
Loading
Loading