This document describes the layout of the examples/ directory, the
conventions every example follows, and how to add new ones.
examples/
├── README.md # Top-level overview + scenario picker
├── STRUCTURE.md # This file
└── ccaas/ # All examples are CCAAS
├── README.md # CCAAS-specific notes
├── fabric-shim/ # Low-level fabric_shim examples
│ ├── README.md
│ ├── asset-transfer-basic/
│ │ ├── main.py # Chaincode implementation + entry point
│ │ ├── README.md # Packaging + deployment walkthrough
│ │ ├── connection.json # Where the peer finds the chaincode
│ │ ├── metadata.json # Package label + type ("ccaas")
│ │ ├── requirements.txt # Python deps for this example
│ │ └── Dockerfile # Optional container image
│ └── asset-transfer-sbe/
│ ├── main.py
│ ├── README.md
│ ├── connection.json
│ ├── metadata.json
│ ├── requirements.txt
│ └── Dockerfile
└── fabric-contract-api/ # High-level fabric_contract_api examples
├── README.md
├── asset-transfer-basic/
│ ├── main.py
│ ├── README.md
│ ├── connection.json
│ ├── metadata.json
│ ├── requirements.txt
│ └── Dockerfile
└── asset-transfer-sbe/
├── main.py
├── README.md
├── connection.json
├── metadata.json
├── requirements.txt
└── Dockerfile
| fabric-shim | fabric-contract-api | |
|---|---|---|
| Package | src.fabric_shim |
src.fabric_contract_api |
| Base class | Chaincode |
Contract |
| Dispatch | Manual if action == "..." in invoke |
Automatic via reflection on contract methods |
| Arguments | list[str] — parse yourself |
Type-annotated Python parameters (dataclasses OK) |
| Returns | pb.Response(status=..., payload=...) |
Plain Python value — serializer converts to bytes |
| Metadata | Not generated | Auto-generated, exposed via org.hyperledger.fabric:get_metadata |
| Read-only tag | Not supported | get_evaluate_transactions() returns the list |
| Code style | Imperative | Declarative |
Each scenario exists in both layers so you can compare:
asset-transfer-basic: InitLedger / CreateAsset / ReadAsset / UpdateAsset / DeleteAsset / GetAllAssets.asset-transfer-sbe: the above minusGetAllAssets, plusTransferAssetandAssetExists. On-chain JSON uses the Go SBE field names (ID/Value/Owner/OwnerOrg).
Every example follows these conventions so they're easy to compare and swap:
- Single self-contained file.
- Resolves the repository root at runtime by walking up the parent
chain looking for
src/fabric_chaincode_python/fabric_shim(orsrc/fabric_chaincode_python/fabric_contract_api). This makes the example runnable from any working directory. - Exposes a
build_chaincode()factory (used by tests) and amain()entry point. - Uses
from __future__ import annotationsso type hints work with PEP 563 string-form annotations.
{
"address": "127.0.0.1:9999",
"dial_timeout": "10s",
"tls_required": false
}{
"path": "",
"type": "ccaas",
"label": "<example_label>_1"
}The label is unique per example so multiple chaincodes can coexist on
the same peer.
Each example pins the subset of dependencies it actually uses. When
in doubt, install the parent repo's requirements.txt from the
repository root — it includes everything every example needs.
Built from the repository root as Docker context, so the entire
src/ tree is copied into the image. Example:
docker build -f examples/ccaas/<layer>/<scenario>/Dockerfile \
-t <scenario>-<layer>:latest .Each README follows this template:
- One-paragraph overview — what the example does and how it differs from its sibling in the other layer.
- Prerequisites — Fabric network, Python version, peer CLI.
- Quick Start — numbered steps: prepare venv, package, install, approve, commit, start server, invoke, query.
- Docker Deployment —
docker build+docker runcommands. - File Structure — what each file in the example directory is for.
- References — links to the sibling example in the other layer, the package documentation, and the official Fabric docs.
- Pick the layer (
ccaas/fabric-shim/orccaas/fabric-contract-api/). - Pick a scenario name (e.g.
asset-transfer-secured-auction). - Create the directory:
ccaas/<layer>/<scenario>/. - Copy the file set from the closest existing example.
- Implement the chaincode in
main.py. - Adjust
metadata.json'slabelso it's unique. - Write the
README.mdfollowing the template above. - Add a row to the table in
ccaas/<layer>/README.md. - Add a row to the table in
examples/README.md. - (Optional) Add a row to the table at the top of this file.
Each example:
- References the parent
src/fabric_chaincode_python/fabric_shim/and / orsrc/fabric_chaincode_python/fabric_contract_api/for the framework. - Uses the parent
requirements.txtor specifies its own subset. - Operates independently once deployed — the chaincode process does not
need the source tree at runtime as long as
PYTHONPATH=/appis set inside the Docker image (or the venv has the package installed).
✅ DO:
- Include clear, step-by-step README instructions.
- Use realistic asset management logic.
- Provide Docker support.
- Add error handling and validation.
- Test thoroughly before committing.
- Document all assumptions and prerequisites.
❌ DON'T:
- Skip documentation.
- Use hardcoded credentials or secrets.
- Create incomplete or non-working examples.
- Assume the user knows all Fabric concepts.
- Forget to update the layer README and
examples/README.md.