openclaw-algorand-plugin 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +112 -0
- package/index.ts +361 -0
- package/lib/mcp-servers.ts +14 -0
- package/lib/x402-fetch.ts +213 -0
- package/memory/algorand-plugin.md +82 -0
- package/openclaw.plugin.json +30 -0
- package/package.json +38 -0
- package/setup.ts +80 -0
- package/skills/algorand-development/SKILL.md +90 -0
- package/skills/algorand-development/references/build-smart-contracts-reference.md +79 -0
- package/skills/algorand-development/references/build-smart-contracts.md +52 -0
- package/skills/algorand-development/references/create-project-reference.md +86 -0
- package/skills/algorand-development/references/create-project.md +89 -0
- package/skills/algorand-development/references/implement-arc-standards-arc32-arc56.md +396 -0
- package/skills/algorand-development/references/implement-arc-standards-arc4.md +265 -0
- package/skills/algorand-development/references/implement-arc-standards.md +92 -0
- package/skills/algorand-development/references/search-algorand-examples-reference.md +119 -0
- package/skills/algorand-development/references/search-algorand-examples.md +89 -0
- package/skills/algorand-development/references/troubleshoot-errors-contract.md +373 -0
- package/skills/algorand-development/references/troubleshoot-errors-transaction.md +599 -0
- package/skills/algorand-development/references/troubleshoot-errors.md +105 -0
- package/skills/algorand-development/references/use-algokit-cli-reference.md +228 -0
- package/skills/algorand-development/references/use-algokit-cli.md +64 -0
- package/skills/algorand-interaction/SKILL.md +223 -0
- package/skills/algorand-interaction/references/algorand-mcp.md +743 -0
- package/skills/algorand-interaction/references/examples-algorand-mcp.md +647 -0
- package/skills/algorand-python/SKILL.md +95 -0
- package/skills/algorand-python/references/build-smart-contracts-decorators.md +413 -0
- package/skills/algorand-python/references/build-smart-contracts-reference.md +55 -0
- package/skills/algorand-python/references/build-smart-contracts-storage.md +452 -0
- package/skills/algorand-python/references/build-smart-contracts-transactions.md +445 -0
- package/skills/algorand-python/references/build-smart-contracts-types.md +438 -0
- package/skills/algorand-python/references/build-smart-contracts.md +82 -0
- package/skills/algorand-python/references/create-project-reference.md +55 -0
- package/skills/algorand-python/references/create-project.md +75 -0
- package/skills/algorand-python/references/implement-arc-standards-arc32-arc56.md +101 -0
- package/skills/algorand-python/references/implement-arc-standards-arc4.md +154 -0
- package/skills/algorand-python/references/implement-arc-standards.md +39 -0
- package/skills/algorand-python/references/troubleshoot-errors-contract.md +355 -0
- package/skills/algorand-python/references/troubleshoot-errors-transaction.md +430 -0
- package/skills/algorand-python/references/troubleshoot-errors.md +46 -0
- package/skills/algorand-python/references/use-algokit-utils-reference.md +350 -0
- package/skills/algorand-python/references/use-algokit-utils.md +76 -0
- package/skills/algorand-typescript/SKILL.md +131 -0
- package/skills/algorand-typescript/references/algorand-ts-migration-from-beta.md +448 -0
- package/skills/algorand-typescript/references/algorand-ts-migration-from-tealscript.md +487 -0
- package/skills/algorand-typescript/references/algorand-ts-migration.md +102 -0
- package/skills/algorand-typescript/references/algorand-typescript-syntax-methods-and-abi.md +134 -0
- package/skills/algorand-typescript/references/algorand-typescript-syntax-reference.md +58 -0
- package/skills/algorand-typescript/references/algorand-typescript-syntax-storage.md +154 -0
- package/skills/algorand-typescript/references/algorand-typescript-syntax-transactions.md +187 -0
- package/skills/algorand-typescript/references/algorand-typescript-syntax-types-and-values.md +150 -0
- package/skills/algorand-typescript/references/algorand-typescript-syntax.md +84 -0
- package/skills/algorand-typescript/references/build-smart-contracts-reference.md +52 -0
- package/skills/algorand-typescript/references/build-smart-contracts.md +74 -0
- package/skills/algorand-typescript/references/call-smart-contracts-reference.md +237 -0
- package/skills/algorand-typescript/references/call-smart-contracts.md +183 -0
- package/skills/algorand-typescript/references/create-project-reference.md +53 -0
- package/skills/algorand-typescript/references/create-project.md +86 -0
- package/skills/algorand-typescript/references/deploy-react-frontend-examples.md +527 -0
- package/skills/algorand-typescript/references/deploy-react-frontend-reference.md +412 -0
- package/skills/algorand-typescript/references/deploy-react-frontend.md +239 -0
- package/skills/algorand-typescript/references/implement-arc-standards-arc32-arc56.md +73 -0
- package/skills/algorand-typescript/references/implement-arc-standards-arc4.md +126 -0
- package/skills/algorand-typescript/references/implement-arc-standards.md +44 -0
- package/skills/algorand-typescript/references/test-smart-contracts-examples.md +245 -0
- package/skills/algorand-typescript/references/test-smart-contracts-unit-tests.md +147 -0
- package/skills/algorand-typescript/references/test-smart-contracts.md +127 -0
- package/skills/algorand-typescript/references/troubleshoot-errors-contract.md +296 -0
- package/skills/algorand-typescript/references/troubleshoot-errors-transaction.md +438 -0
- package/skills/algorand-typescript/references/troubleshoot-errors.md +56 -0
- package/skills/algorand-typescript/references/use-algokit-utils-reference.md +342 -0
- package/skills/algorand-typescript/references/use-algokit-utils.md +74 -0
- package/skills/algorand-x402-python/SKILL.md +113 -0
- package/skills/algorand-x402-python/references/create-python-x402-client-examples.md +469 -0
- package/skills/algorand-x402-python/references/create-python-x402-client-reference.md +313 -0
- package/skills/algorand-x402-python/references/create-python-x402-client.md +207 -0
- package/skills/algorand-x402-python/references/create-python-x402-facilitator-examples.md +924 -0
- package/skills/algorand-x402-python/references/create-python-x402-facilitator-reference.md +629 -0
- package/skills/algorand-x402-python/references/create-python-x402-facilitator.md +408 -0
- package/skills/algorand-x402-python/references/create-python-x402-server-examples.md +703 -0
- package/skills/algorand-x402-python/references/create-python-x402-server-reference.md +303 -0
- package/skills/algorand-x402-python/references/create-python-x402-server.md +221 -0
- package/skills/algorand-x402-python/references/explain-algorand-x402-python-examples.md +605 -0
- package/skills/algorand-x402-python/references/explain-algorand-x402-python-reference.md +315 -0
- package/skills/algorand-x402-python/references/explain-algorand-x402-python.md +167 -0
- package/skills/algorand-x402-python/references/use-python-x402-core-avm-examples.md +554 -0
- package/skills/algorand-x402-python/references/use-python-x402-core-avm-reference.md +278 -0
- package/skills/algorand-x402-python/references/use-python-x402-core-avm.md +166 -0
- package/skills/algorand-x402-typescript/SKILL.md +129 -0
- package/skills/algorand-x402-typescript/references/create-typescript-x402-client-examples.md +879 -0
- package/skills/algorand-x402-typescript/references/create-typescript-x402-client-reference.md +371 -0
- package/skills/algorand-x402-typescript/references/create-typescript-x402-client.md +236 -0
- package/skills/algorand-x402-typescript/references/create-typescript-x402-facilitator-examples.md +875 -0
- package/skills/algorand-x402-typescript/references/create-typescript-x402-facilitator-reference.md +461 -0
- package/skills/algorand-x402-typescript/references/create-typescript-x402-facilitator.md +270 -0
- package/skills/algorand-x402-typescript/references/create-typescript-x402-nextjs-examples.md +1181 -0
- package/skills/algorand-x402-typescript/references/create-typescript-x402-nextjs-reference.md +360 -0
- package/skills/algorand-x402-typescript/references/create-typescript-x402-nextjs.md +251 -0
- package/skills/algorand-x402-typescript/references/create-typescript-x402-paywall-examples.md +870 -0
- package/skills/algorand-x402-typescript/references/create-typescript-x402-paywall-reference.md +323 -0
- package/skills/algorand-x402-typescript/references/create-typescript-x402-paywall.md +281 -0
- package/skills/algorand-x402-typescript/references/create-typescript-x402-server-examples.md +1135 -0
- package/skills/algorand-x402-typescript/references/create-typescript-x402-server-reference.md +382 -0
- package/skills/algorand-x402-typescript/references/create-typescript-x402-server.md +216 -0
- package/skills/algorand-x402-typescript/references/explain-algorand-x402-typescript-examples.md +616 -0
- package/skills/algorand-x402-typescript/references/explain-algorand-x402-typescript-reference.md +323 -0
- package/skills/algorand-x402-typescript/references/explain-algorand-x402-typescript.md +232 -0
- package/skills/algorand-x402-typescript/references/use-typescript-x402-core-avm-examples.md +1417 -0
- package/skills/algorand-x402-typescript/references/use-typescript-x402-core-avm-reference.md +504 -0
- package/skills/algorand-x402-typescript/references/use-typescript-x402-core-avm.md +158 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: algorand-python
|
|
3
|
+
description: Comprehensive guide for Algorand Python (PuyaPy) smart contract development — contract building with decorators, storage, transactions, types, AlgoKit Utils, ARC standards, and troubleshooting. This is the parent skill for all Python-specific Algorand content. Use when writing Python contract code, working with algopy decorators, understanding Algorand Python storage patterns, using AlgoKit Utils in Python, or troubleshooting Python-specific errors. Strong triggers include "Algorand Python", "PuyaPy", "algopy", "ARC4Contract", "@arc4.abimethod", "GlobalState Python", "BoxMap Python", "algokit_utils Python", "pip install algokit-utils", "Python contract", "Python smart contract".
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Algorand Python
|
|
7
|
+
|
|
8
|
+
This is the aggregated parent skill for all Python-specific Algorand development. Use the reference files below to find detailed guidance for each topic.
|
|
9
|
+
|
|
10
|
+
## Quick Start
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
# Create Python project
|
|
14
|
+
algokit init -n my-project -t python --answer preset_name production --defaults
|
|
15
|
+
|
|
16
|
+
# Install AlgoKit Utils
|
|
17
|
+
pip install algokit-utils
|
|
18
|
+
|
|
19
|
+
# Development cycle
|
|
20
|
+
algokit project run build # Compile Python contracts
|
|
21
|
+
algokit project run test # Run tests
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Key Python Pattern
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from algopy import ARC4Contract, arc4
|
|
28
|
+
|
|
29
|
+
class MyContract(ARC4Contract):
|
|
30
|
+
@arc4.abimethod
|
|
31
|
+
def hello(self, name: arc4.String) -> arc4.String:
|
|
32
|
+
return arc4.String("Hello, " + name.native)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Reference Guide
|
|
36
|
+
|
|
37
|
+
Navigate to the appropriate reference based on your task. Each topic has one or more files.
|
|
38
|
+
|
|
39
|
+
### Creating Python Projects
|
|
40
|
+
|
|
41
|
+
Initialize Python AlgoKit projects with proper templates and presets.
|
|
42
|
+
|
|
43
|
+
- [create-project.md](./references/create-project.md) — Python project creation
|
|
44
|
+
- [create-project-reference.md](./references/create-project-reference.md) — Python presets and templates
|
|
45
|
+
|
|
46
|
+
### Building Smart Contracts (Python)
|
|
47
|
+
|
|
48
|
+
Python-specific contract building with PuyaPy, including deep-dive references for decorators, storage, transactions, and types.
|
|
49
|
+
|
|
50
|
+
- [build-smart-contracts.md](./references/build-smart-contracts.md) — Python contract building guide
|
|
51
|
+
- [build-smart-contracts-reference.md](./references/build-smart-contracts-reference.md) — Python repos and patterns
|
|
52
|
+
- [build-smart-contracts-decorators.md](./references/build-smart-contracts-decorators.md) — `@arc4.abimethod`, `@arc4.baremethod`, `@subroutine`
|
|
53
|
+
- [build-smart-contracts-storage.md](./references/build-smart-contracts-storage.md) — GlobalState, LocalState, Box, BoxMap, BoxRef
|
|
54
|
+
- [build-smart-contracts-transactions.md](./references/build-smart-contracts-transactions.md) — Inner transactions, group transactions, fee pooling
|
|
55
|
+
- [build-smart-contracts-types.md](./references/build-smart-contracts-types.md) — AVM types, ARC-4 types, reference types
|
|
56
|
+
|
|
57
|
+
### AlgoKit Utils (Python)
|
|
58
|
+
|
|
59
|
+
Interact with Algorand from Python applications.
|
|
60
|
+
|
|
61
|
+
- [use-algokit-utils.md](./references/use-algokit-utils.md) — Python AlgoKit Utils overview
|
|
62
|
+
- [use-algokit-utils-reference.md](./references/use-algokit-utils-reference.md) — AlgorandClient Python API reference
|
|
63
|
+
|
|
64
|
+
### Implementing ARC Standards (Python)
|
|
65
|
+
|
|
66
|
+
Python implementations of ARC-4, ARC-32, and ARC-56.
|
|
67
|
+
|
|
68
|
+
- [implement-arc-standards.md](./references/implement-arc-standards.md) — Python ARC overview
|
|
69
|
+
- [implement-arc-standards-arc4.md](./references/implement-arc-standards-arc4.md) — Python ARC-4 implementations
|
|
70
|
+
- [implement-arc-standards-arc32-arc56.md](./references/implement-arc-standards-arc32-arc56.md) — Python typed client usage
|
|
71
|
+
|
|
72
|
+
### Troubleshooting Errors (Python)
|
|
73
|
+
|
|
74
|
+
Python-specific error diagnosis and fixes.
|
|
75
|
+
|
|
76
|
+
- [troubleshoot-errors.md](./references/troubleshoot-errors.md) — Python error overview
|
|
77
|
+
- [troubleshoot-errors-contract.md](./references/troubleshoot-errors-contract.md) — Contract errors with Python fixes
|
|
78
|
+
- [troubleshoot-errors-transaction.md](./references/troubleshoot-errors-transaction.md) — Transaction errors with Python fixes
|
|
79
|
+
|
|
80
|
+
## Topic Quick Reference
|
|
81
|
+
|
|
82
|
+
| Topic | Files | Description |
|
|
83
|
+
| ----- | ----- | ----------- |
|
|
84
|
+
| Create Project | 2 | Python template initialization |
|
|
85
|
+
| Build Contracts | 6 | PuyaPy contracts, decorators, storage, transactions, types |
|
|
86
|
+
| AlgoKit Utils | 2 | AlgorandClient Python API |
|
|
87
|
+
| ARC Standards | 3 | Python ARC implementations |
|
|
88
|
+
| Troubleshoot | 3 | Python error fixes |
|
|
89
|
+
|
|
90
|
+
## How to Use This Skill
|
|
91
|
+
|
|
92
|
+
1. **Start here** to understand which reference you need
|
|
93
|
+
2. **Read the topic `.md`** file for step-by-step guidance
|
|
94
|
+
3. **Consult detail files** for specific subtopics (decorators, storage, types, etc.)
|
|
95
|
+
4. **For language-agnostic content** (CLI, ARC specs, general workflows), see the `algorand-development` parent skill
|
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
# Algorand Python Decorators
|
|
2
|
+
|
|
3
|
+
Define smart contract methods and their behavior using decorators in Algorand Python.
|
|
4
|
+
|
|
5
|
+
## When to use this reference
|
|
6
|
+
|
|
7
|
+
Use this reference when:
|
|
8
|
+
|
|
9
|
+
- Creating ABI methods that external callers can invoke
|
|
10
|
+
- Defining bare methods for application lifecycle operations
|
|
11
|
+
- Writing internal subroutines for code reuse
|
|
12
|
+
- Controlling method visibility and call permissions
|
|
13
|
+
- Handling application create, update, delete, and opt-in actions
|
|
14
|
+
|
|
15
|
+
## Contract Base Classes
|
|
16
|
+
|
|
17
|
+
### ARC4Contract (Recommended)
|
|
18
|
+
|
|
19
|
+
Use `ARC4Contract` for contracts that expose ABI methods.
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from algopy import ARC4Contract, arc4
|
|
23
|
+
|
|
24
|
+
class MyContract(ARC4Contract):
|
|
25
|
+
@arc4.abimethod
|
|
26
|
+
def hello(self, name: arc4.String) -> arc4.String:
|
|
27
|
+
return "Hello, " + name
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Contract (Low-Level)
|
|
31
|
+
|
|
32
|
+
Use `Contract` for raw approval/clear program control.
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from algopy import Contract, UInt64
|
|
36
|
+
|
|
37
|
+
class MyContract(Contract):
|
|
38
|
+
def approval_program(self) -> UInt64:
|
|
39
|
+
return UInt64(1) # Approve
|
|
40
|
+
|
|
41
|
+
def clear_state_program(self) -> UInt64:
|
|
42
|
+
return UInt64(1) # Approve
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## @arc4.abimethod
|
|
46
|
+
|
|
47
|
+
Marks a method as an ABI method callable by external transactions.
|
|
48
|
+
|
|
49
|
+
### Basic Usage
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from algopy import ARC4Contract, arc4, UInt64
|
|
53
|
+
|
|
54
|
+
class MyContract(ARC4Contract):
|
|
55
|
+
@arc4.abimethod
|
|
56
|
+
def add(self, a: arc4.UInt64, b: arc4.UInt64) -> arc4.UInt64:
|
|
57
|
+
return arc4.UInt64(a.native + b.native)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Parameters
|
|
61
|
+
|
|
62
|
+
| Parameter | Type | Default | Description |
|
|
63
|
+
|-----------|------|---------|-------------|
|
|
64
|
+
| `name` | `str` | Method name | Override method name in ABI |
|
|
65
|
+
| `create` | `"allow"`, `"require"`, `"disallow"` | `"disallow"` | Application creation behavior |
|
|
66
|
+
| `allow_actions` | `list[str]` | `["NoOp"]` | Allowed OnComplete actions |
|
|
67
|
+
| `readonly` | `bool` | `False` | Mark as read-only (no state changes) |
|
|
68
|
+
| `default_args` | `dict` | `{}` | Default argument sources |
|
|
69
|
+
|
|
70
|
+
### Application Creation
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from algopy import ARC4Contract, arc4, String
|
|
74
|
+
|
|
75
|
+
class MyContract(ARC4Contract):
|
|
76
|
+
def __init__(self) -> None:
|
|
77
|
+
self.name = String()
|
|
78
|
+
|
|
79
|
+
# CORRECT - Method that creates the application
|
|
80
|
+
@arc4.abimethod(create="require")
|
|
81
|
+
def create(self, name: arc4.String) -> None:
|
|
82
|
+
self.name = name.native
|
|
83
|
+
|
|
84
|
+
# CORRECT - Method that can optionally create
|
|
85
|
+
@arc4.abimethod(create="allow")
|
|
86
|
+
def initialize(self, name: arc4.String) -> None:
|
|
87
|
+
self.name = name.native
|
|
88
|
+
|
|
89
|
+
# CORRECT - Method that cannot create (default)
|
|
90
|
+
@arc4.abimethod # create="disallow" is default
|
|
91
|
+
def update_name(self, name: arc4.String) -> None:
|
|
92
|
+
self.name = name.native
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### OnComplete Actions
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from algopy import ARC4Contract, arc4, Txn
|
|
99
|
+
|
|
100
|
+
class MyContract(ARC4Contract):
|
|
101
|
+
# CORRECT - Allow opt-in calls
|
|
102
|
+
@arc4.abimethod(allow_actions=["OptIn"])
|
|
103
|
+
def opt_in(self) -> None:
|
|
104
|
+
pass
|
|
105
|
+
|
|
106
|
+
# CORRECT - Allow close-out calls
|
|
107
|
+
@arc4.abimethod(allow_actions=["CloseOut"])
|
|
108
|
+
def close_out(self) -> None:
|
|
109
|
+
pass
|
|
110
|
+
|
|
111
|
+
# CORRECT - Allow update calls (requires UpdateApplication)
|
|
112
|
+
@arc4.abimethod(allow_actions=["UpdateApplication"])
|
|
113
|
+
def update(self) -> None:
|
|
114
|
+
assert Txn.sender == self.creator
|
|
115
|
+
|
|
116
|
+
# CORRECT - Allow delete calls
|
|
117
|
+
@arc4.abimethod(allow_actions=["DeleteApplication"])
|
|
118
|
+
def delete(self) -> None:
|
|
119
|
+
assert Txn.sender == self.creator
|
|
120
|
+
|
|
121
|
+
# CORRECT - Allow multiple actions
|
|
122
|
+
@arc4.abimethod(allow_actions=["NoOp", "OptIn"])
|
|
123
|
+
def register(self) -> None:
|
|
124
|
+
pass
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Read-Only Methods
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
from algopy import ARC4Contract, arc4, UInt64
|
|
131
|
+
|
|
132
|
+
class MyContract(ARC4Contract):
|
|
133
|
+
def __init__(self) -> None:
|
|
134
|
+
self.counter = UInt64(0)
|
|
135
|
+
|
|
136
|
+
# CORRECT - Mark as read-only for simulation
|
|
137
|
+
@arc4.abimethod(readonly=True)
|
|
138
|
+
def get_counter(self) -> arc4.UInt64:
|
|
139
|
+
return arc4.UInt64(self.counter)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Custom Method Names
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
from algopy import ARC4Contract, arc4
|
|
146
|
+
|
|
147
|
+
class MyContract(ARC4Contract):
|
|
148
|
+
# CORRECT - Override ABI method name
|
|
149
|
+
@arc4.abimethod(name="getBalance")
|
|
150
|
+
def get_balance(self) -> arc4.UInt64:
|
|
151
|
+
return arc4.UInt64(0)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Default Arguments
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
from algopy import ARC4Contract, arc4, Asset, Global
|
|
158
|
+
|
|
159
|
+
class MyContract(ARC4Contract):
|
|
160
|
+
def __init__(self) -> None:
|
|
161
|
+
self.asset = Asset()
|
|
162
|
+
|
|
163
|
+
# CORRECT - Default arg from state or constant
|
|
164
|
+
@arc4.abimethod(
|
|
165
|
+
default_args={
|
|
166
|
+
"asset": "asset", # From self.asset
|
|
167
|
+
"sender": "global.creator_address" # From Global
|
|
168
|
+
}
|
|
169
|
+
)
|
|
170
|
+
def transfer(self, asset: Asset, sender: arc4.Address) -> None:
|
|
171
|
+
pass
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## @arc4.baremethod
|
|
175
|
+
|
|
176
|
+
Marks a method as a bare method (no ABI selector, no arguments/return).
|
|
177
|
+
|
|
178
|
+
### Basic Usage
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
from algopy import ARC4Contract, arc4
|
|
182
|
+
|
|
183
|
+
class MyContract(ARC4Contract):
|
|
184
|
+
# CORRECT - Bare method for creation
|
|
185
|
+
@arc4.baremethod(create="require")
|
|
186
|
+
def create(self) -> None:
|
|
187
|
+
pass
|
|
188
|
+
|
|
189
|
+
# CORRECT - Bare method for opt-in
|
|
190
|
+
@arc4.baremethod(allow_actions=["OptIn"])
|
|
191
|
+
def opt_in(self) -> None:
|
|
192
|
+
pass
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Parameters
|
|
196
|
+
|
|
197
|
+
| Parameter | Type | Default | Description |
|
|
198
|
+
|-----------|------|---------|-------------|
|
|
199
|
+
| `create` | `"allow"`, `"require"`, `"disallow"` | `"disallow"` | Application creation behavior |
|
|
200
|
+
| `allow_actions` | `list[str]` | `["NoOp"]` | Allowed OnComplete actions |
|
|
201
|
+
|
|
202
|
+
### Common Bare Method Patterns
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
from algopy import ARC4Contract, arc4, Txn
|
|
206
|
+
|
|
207
|
+
class MyContract(ARC4Contract):
|
|
208
|
+
# CORRECT - Bare create (no arguments needed)
|
|
209
|
+
@arc4.baremethod(create="require")
|
|
210
|
+
def create(self) -> None:
|
|
211
|
+
pass
|
|
212
|
+
|
|
213
|
+
# CORRECT - Bare update
|
|
214
|
+
@arc4.baremethod(allow_actions=["UpdateApplication"])
|
|
215
|
+
def update(self) -> None:
|
|
216
|
+
assert Txn.sender == self.creator
|
|
217
|
+
|
|
218
|
+
# CORRECT - Bare delete
|
|
219
|
+
@arc4.baremethod(allow_actions=["DeleteApplication"])
|
|
220
|
+
def delete(self) -> None:
|
|
221
|
+
assert Txn.sender == self.creator
|
|
222
|
+
|
|
223
|
+
# CORRECT - Bare opt-in
|
|
224
|
+
@arc4.baremethod(allow_actions=["OptIn"])
|
|
225
|
+
def opt_in(self) -> None:
|
|
226
|
+
pass
|
|
227
|
+
|
|
228
|
+
# CORRECT - Bare close-out
|
|
229
|
+
@arc4.baremethod(allow_actions=["CloseOut"])
|
|
230
|
+
def close_out(self) -> None:
|
|
231
|
+
pass
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
## @subroutine
|
|
235
|
+
|
|
236
|
+
Marks a function as a reusable subroutine (internal, not callable externally).
|
|
237
|
+
|
|
238
|
+
### Basic Usage
|
|
239
|
+
|
|
240
|
+
```python
|
|
241
|
+
from algopy import ARC4Contract, arc4, UInt64, subroutine
|
|
242
|
+
|
|
243
|
+
class MyContract(ARC4Contract):
|
|
244
|
+
@arc4.abimethod
|
|
245
|
+
def calculate(self, value: arc4.UInt64) -> arc4.UInt64:
|
|
246
|
+
result = self._double(value.native)
|
|
247
|
+
return arc4.UInt64(result)
|
|
248
|
+
|
|
249
|
+
# CORRECT - Private subroutine
|
|
250
|
+
@subroutine
|
|
251
|
+
def _double(self, value: UInt64) -> UInt64:
|
|
252
|
+
return value * UInt64(2)
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Module-Level Subroutines
|
|
256
|
+
|
|
257
|
+
```python
|
|
258
|
+
from algopy import ARC4Contract, arc4, UInt64, subroutine
|
|
259
|
+
|
|
260
|
+
# CORRECT - Module-level subroutine
|
|
261
|
+
@subroutine
|
|
262
|
+
def calculate_fee(amount: UInt64) -> UInt64:
|
|
263
|
+
return amount * UInt64(3) // UInt64(100)
|
|
264
|
+
|
|
265
|
+
class MyContract(ARC4Contract):
|
|
266
|
+
@arc4.abimethod
|
|
267
|
+
def process(self, amount: arc4.UInt64) -> arc4.UInt64:
|
|
268
|
+
fee = calculate_fee(amount.native)
|
|
269
|
+
return arc4.UInt64(fee)
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
### Inline Parameter
|
|
273
|
+
|
|
274
|
+
Control whether the subroutine is inlined at call sites.
|
|
275
|
+
|
|
276
|
+
```python
|
|
277
|
+
from algopy import subroutine, UInt64
|
|
278
|
+
|
|
279
|
+
# CORRECT - Force inlining (small, frequently called)
|
|
280
|
+
@subroutine(inline=True)
|
|
281
|
+
def is_valid(value: UInt64) -> bool:
|
|
282
|
+
return value > UInt64(0)
|
|
283
|
+
|
|
284
|
+
# CORRECT - Prevent inlining (large, called multiple times)
|
|
285
|
+
@subroutine(inline=False)
|
|
286
|
+
def complex_calculation(a: UInt64, b: UInt64, c: UInt64) -> UInt64:
|
|
287
|
+
# Complex logic here
|
|
288
|
+
return a * b + c
|
|
289
|
+
|
|
290
|
+
# CORRECT - Let compiler decide (default)
|
|
291
|
+
@subroutine # inline="auto" is default
|
|
292
|
+
def helper(value: UInt64) -> UInt64:
|
|
293
|
+
return value + UInt64(1)
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
## Lifecycle Methods
|
|
297
|
+
|
|
298
|
+
### __init__ (State Initialization)
|
|
299
|
+
|
|
300
|
+
```python
|
|
301
|
+
from algopy import ARC4Contract, UInt64, String
|
|
302
|
+
|
|
303
|
+
class MyContract(ARC4Contract):
|
|
304
|
+
def __init__(self) -> None:
|
|
305
|
+
# Initialize state - runs on application creation
|
|
306
|
+
self.counter = UInt64(0)
|
|
307
|
+
self.name = String("default")
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### clear_state_program (Clear State)
|
|
311
|
+
|
|
312
|
+
```python
|
|
313
|
+
from algopy import ARC4Contract, UInt64
|
|
314
|
+
|
|
315
|
+
class MyContract(ARC4Contract):
|
|
316
|
+
# CORRECT - Custom clear state logic
|
|
317
|
+
def clear_state_program(self) -> UInt64:
|
|
318
|
+
# Return 1 to approve, 0 to reject
|
|
319
|
+
return UInt64(1)
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
## Method Visibility Summary
|
|
323
|
+
|
|
324
|
+
| Decorator | Externally Callable | Has ABI Selector | Can Have Args/Return |
|
|
325
|
+
|-----------|---------------------|------------------|----------------------|
|
|
326
|
+
| `@arc4.abimethod` | Yes | Yes | Yes |
|
|
327
|
+
| `@arc4.baremethod` | Yes | No | No |
|
|
328
|
+
| `@subroutine` | No | No | Yes |
|
|
329
|
+
| (no decorator) | No | No | Yes (internal only) |
|
|
330
|
+
|
|
331
|
+
## Common Mistakes
|
|
332
|
+
|
|
333
|
+
### Using abimethod for Internal Logic
|
|
334
|
+
|
|
335
|
+
```python
|
|
336
|
+
# INCORRECT - Internal logic exposed as ABI method
|
|
337
|
+
@arc4.abimethod
|
|
338
|
+
def _calculate_fee(self, amount: arc4.UInt64) -> arc4.UInt64:
|
|
339
|
+
return arc4.UInt64(amount.native * UInt64(3) // UInt64(100))
|
|
340
|
+
|
|
341
|
+
# CORRECT - Use subroutine for internal logic
|
|
342
|
+
@subroutine
|
|
343
|
+
def _calculate_fee(self, amount: UInt64) -> UInt64:
|
|
344
|
+
return amount * UInt64(3) // UInt64(100)
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
### Forgetting create Parameter for Creation Methods
|
|
348
|
+
|
|
349
|
+
```python
|
|
350
|
+
# INCORRECT - Cannot call this during creation
|
|
351
|
+
@arc4.abimethod
|
|
352
|
+
def create_app(self, name: arc4.String) -> None:
|
|
353
|
+
self.name = name.native
|
|
354
|
+
|
|
355
|
+
# CORRECT - Explicitly allow or require creation
|
|
356
|
+
@arc4.abimethod(create="require")
|
|
357
|
+
def create_app(self, name: arc4.String) -> None:
|
|
358
|
+
self.name = name.native
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
### Arguments on Bare Methods
|
|
362
|
+
|
|
363
|
+
```python
|
|
364
|
+
# INCORRECT - Bare methods cannot have arguments
|
|
365
|
+
@arc4.baremethod
|
|
366
|
+
def opt_in(self, user_name: arc4.String) -> None: # Error!
|
|
367
|
+
pass
|
|
368
|
+
|
|
369
|
+
# CORRECT - Use abimethod if you need arguments
|
|
370
|
+
@arc4.abimethod(allow_actions=["OptIn"])
|
|
371
|
+
def opt_in(self, user_name: arc4.String) -> None:
|
|
372
|
+
pass
|
|
373
|
+
|
|
374
|
+
# CORRECT - Bare method without arguments
|
|
375
|
+
@arc4.baremethod(allow_actions=["OptIn"])
|
|
376
|
+
def opt_in(self) -> None:
|
|
377
|
+
pass
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
### Missing Return Type on Subroutines
|
|
381
|
+
|
|
382
|
+
```python
|
|
383
|
+
# INCORRECT - Missing return type annotation
|
|
384
|
+
@subroutine
|
|
385
|
+
def calculate(value: UInt64):
|
|
386
|
+
return value * UInt64(2)
|
|
387
|
+
|
|
388
|
+
# CORRECT - Include return type
|
|
389
|
+
@subroutine
|
|
390
|
+
def calculate(value: UInt64) -> UInt64:
|
|
391
|
+
return value * UInt64(2)
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
### Using Native Types in ABI Method Signatures
|
|
395
|
+
|
|
396
|
+
```python
|
|
397
|
+
# INCORRECT - ABI methods should use arc4 types for args/return
|
|
398
|
+
@arc4.abimethod
|
|
399
|
+
def add(self, a: UInt64, b: UInt64) -> UInt64:
|
|
400
|
+
return a + b
|
|
401
|
+
|
|
402
|
+
# CORRECT - Use arc4 types in signature
|
|
403
|
+
@arc4.abimethod
|
|
404
|
+
def add(self, a: arc4.UInt64, b: arc4.UInt64) -> arc4.UInt64:
|
|
405
|
+
return arc4.UInt64(a.native + b.native)
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
## References
|
|
409
|
+
|
|
410
|
+
- [Algorand Python Contract Structure](https://dev.algorand.co/algokit/languages/python/lg-contract-structure/)
|
|
411
|
+
- [ARC-4 ABI Methods](https://dev.algorand.co/algokit/languages/python/lg-arc4/)
|
|
412
|
+
- [algopy API Reference](https://dev.algorand.co/reference/algorand-python/api/api-algopy/)
|
|
413
|
+
- [algopy.arc4 Reference](https://dev.algorand.co/reference/algorand-python/api/api-algopy-arc4/)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Python Smart Contract Repository and Pattern Reference
|
|
2
|
+
|
|
3
|
+
Python-specific repositories, paths, and patterns for building Algorand smart contracts with PuyaPy.
|
|
4
|
+
|
|
5
|
+
## Priority Repositories
|
|
6
|
+
|
|
7
|
+
### Priority 1: DevPortal Code Examples (Python)
|
|
8
|
+
**Repository:** `algorandfoundation/devportal-code-examples`
|
|
9
|
+
**Path:** `projects/python-examples/contracts/`
|
|
10
|
+
|
|
11
|
+
Common contract patterns available:
|
|
12
|
+
- State management examples (GlobalState, LocalState)
|
|
13
|
+
- ARC-4 method examples
|
|
14
|
+
- Inner transaction patterns
|
|
15
|
+
- Box storage patterns
|
|
16
|
+
|
|
17
|
+
Always retrieve corresponding test files alongside contracts.
|
|
18
|
+
|
|
19
|
+
### Priority 2: Puya Compiler Examples
|
|
20
|
+
**Repository:** `algorandfoundation/puya`
|
|
21
|
+
**Path:** `examples/`
|
|
22
|
+
|
|
23
|
+
Key examples:
|
|
24
|
+
- `hello_world_arc4/` -- Basic ARC-4 contract structure
|
|
25
|
+
- `voting/` -- State management, BoxMap patterns, complex logic
|
|
26
|
+
- `amm/` -- Advanced patterns (multi-asset, inner transactions, BoxMap)
|
|
27
|
+
- Search for `itxn` patterns for inner transaction examples
|
|
28
|
+
|
|
29
|
+
### Priority 3: AlgoKit Python Template
|
|
30
|
+
**Repository:** `algorandfoundation/algokit-python-template`
|
|
31
|
+
|
|
32
|
+
Provides:
|
|
33
|
+
- Project structure and scaffolding
|
|
34
|
+
- Build and test configuration
|
|
35
|
+
- Integration test patterns with generated clients
|
|
36
|
+
|
|
37
|
+
## Pattern-Specific Lookups
|
|
38
|
+
|
|
39
|
+
| Pattern | Where to Look |
|
|
40
|
+
|---------|--------------|
|
|
41
|
+
| Box storage / BoxMap | `puya/examples/voting/`, `puya/examples/amm/` |
|
|
42
|
+
| Inner transactions | `puya/examples/` (search for itxn usage) |
|
|
43
|
+
| ARC-4 methods | `puya/examples/hello_world_arc4/` |
|
|
44
|
+
| State management | `devportal-code-examples/projects/python-examples/contracts/` |
|
|
45
|
+
| AMM / DeFi | `puya/examples/amm/` |
|
|
46
|
+
| Voting | `puya/examples/voting/` |
|
|
47
|
+
| Decorators | See [build-smart-contracts-decorators.md](./build-smart-contracts-decorators.md) |
|
|
48
|
+
| Storage types | See [build-smart-contracts-storage.md](./build-smart-contracts-storage.md) |
|
|
49
|
+
| Transactions | See [build-smart-contracts-transactions.md](./build-smart-contracts-transactions.md) |
|
|
50
|
+
| Types | See [build-smart-contracts-types.md](./build-smart-contracts-types.md) |
|
|
51
|
+
|
|
52
|
+
## Supporting Repositories
|
|
53
|
+
|
|
54
|
+
- `algorandfoundation/algokit-cli` -- CLI tool reference
|
|
55
|
+
- `algorandfoundation/algokit-utils-ts` -- Utility functions for deployment and testing
|