Credits Ledger Pack
The Credits Ledger Pack is the first concrete Xian solution pack.
It is also the first pack that has been deepened into a fuller reference application slice.
It shows how to use Xian as an application-controlled internal ledger for credits, balances, issuance, transfers, burns, and auditability.
When To Use It
This pattern fits well when you need:
- platform credits
- community or loyalty points
- API usage credits
- marketplace balances
- partner settlement units
The application keeps its own relational data off-chain and uses Xian as the authoritative shared credits ledger.
Why Xian Fits
This pack is intentionally close to the product thesis:
- simple contract logic
- strong Python integration through
xian-py - clear event and history model
- easy local and remote operator flows
- straightforward monitoring and recovery story
Reference-App Pattern
The deeper reference-app slice for this pack uses a three-part shape:
- the Xian contract remains the authoritative credits ledger
- a resumable projector rebuilds a local SQLite read model from indexed BDS events
- an API service combines authoritative chain reads and projected application reads
That is the core backend pattern Xian is meant to support:
- keep shared programmable state on-chain
- reshape it into application-oriented queries off-chain
- rebuild those read models deterministically from indexed chain events
Recommended Operator Paths
Use these templates:
- local development:
single-node-indexed - remote deployment:
embedded-backend
Those operator paths already give this pack the right posture:
- indexed reads through BDS
- dashboard and health visibility
- service-oriented integration defaults
Pack Assets
Contract Asset
The reusable credits-ledger contract lives in xian-configs:
solution-packs/credits-ledger/contracts/credits_ledger.s.py
The intended deployed contract name in the examples is:
con_credits_ledgerSDK Examples
The xian-py repo includes pack-specific examples under:
examples/credits_ledger/admin_job.pyexamples/credits_ledger/api_service.pyexamples/credits_ledger/event_worker.py
Those cover:
- contract bootstrap / admin flow
- API-service integration with projected activity and summary views
- resumable projection from indexed events into a local SQLite database
Local Walkthrough
1. Create And Start A Local Indexed Node
cd ~/xian/xian-cli
uv run xian network create credits-local --chain-id xian-credits-local-1 \
--template single-node-indexed --generate-validator-key --init-node
uv run xian node start validator-1
uv run xian node health validator-12. Bootstrap The Credits Ledger Contract
Use the admin job from xian-py:
cd ~/xian/xian-py
export XIAN_NODE_URL=http://127.0.0.1:26657
export XIAN_CHAIN_ID=xian-credits-local-1
export XIAN_WALLET_PRIVATE_KEY=<private-key-hex>
uv run python examples/credits_ledger/admin_job.pyThe admin job:
- connects to the node
- deploys
con_credits_ledgerif it does not exist yet - prints the current operator and total supply
- optionally issues credits when
XIAN_CREDITS_ISSUE_TOandXIAN_CREDITS_ISSUE_AMOUNTare set
3. Run The API Service
Install the optional app dependencies first:
uv sync --group dev --extra appuv run uvicorn examples.credits_ledger.api_service:app --reload --app-dir .The service exposes both authoritative and projected reads:
GET /healthGET /balances/{address}GET /accounts/{address}GET /accounts/{address}/activityGET /activity/recentGET /projection/healthGET /projection/summaryGET /events/transferPOST /issuePOST /burnPOST /transfer
The important split is:
balances/{address}reads the authoritative on-chain ledger stateprojection/*andaccounts/*/activityread the local application projection built from indexed events
4. Run The Projector Worker
uv run python examples/credits_ledger/projector_worker.pyThe projector:
- backfills from indexed BDS events on first start
- persists a local SQLite database
- maintains resumable cursors inside that projection database
- rebuilds:
- recent ledger activity
- per-account summary counters
- projected ledger supply totals
It consumes:
IssueTransferBurn
The default projection database path is:
.credits-ledger-projection.sqlite3Override it with:
export XIAN_CREDITS_PROJECTION_PATH=/path/to/credits-ledger.sqlite35. Query The Reference-App Views
Once the projector is running, you can query the richer application views:
curl http://127.0.0.1:8000/projection/summary
curl http://127.0.0.1:8000/activity/recent
curl http://127.0.0.1:8000/accounts/<address>
curl http://127.0.0.1:8000/accounts/<address>/activityThose routes demonstrate the intended division of labor:
- Xian stores and enforces the ledger
- the projector shapes indexed events into application reads
- the API service serves both authoritative and projected views
Remote Operator Story
For remote Linux hosts, use xian-deploy with the embedded-backend deployment posture.
The main remote commands are:
ansible-playbook playbooks/deploy.yml
ansible-playbook playbooks/health.yml
ansible-playbook playbooks/bootstrap-state-sync.yml
ansible-playbook playbooks/restore-state-snapshot.ymlUse recovery intentionally:
- prepared node-home archive:
push-home.ymlthendeploy.yml - application-state snapshot import:
restore-state-snapshot.yml - protocol state sync:
bootstrap-state-sync.yml
See Starting, Stopping & Monitoring for the concrete local and remote runbooks behind those paths.
What This Pack Proves
The Credits Ledger Pack proves that Xian can act as:
- a programmable ledger backend
- a service-friendly integration target for Python applications
- an event-fed projection source for local application read models
- an operationally manageable indexed node stack
It is the first pack because it exercises nearly the full golden path without needing domain-heavy workflow logic.