Getting Started with StxScript¶
This guide will help you install StxScript and create your first smart contract.
Requirements¶
- Python 3.10 or higher
- pip or uv package manager
Installation¶
From PyPI¶
From Source (Development)¶
git clone https://github.com/cryptuon/stxscript.git
cd stxscript
uv venv && uv pip install -e ".[dev]"
Verify Installation¶
Your First Contract¶
1. Create a Project¶
This creates:
2. Write Your Contract¶
Edit src/main.stx:
// Simple counter contract
const MAX_COUNT: uint = 1000u;
let counter: uint = 0u;
@public
function increment(): Response<uint, uint> {
if (counter >= MAX_COUNT) {
return err(1u);
}
counter = counter + 1u;
return ok(counter);
}
@public
function decrement(): Response<uint, uint> {
if (counter == 0u) {
return err(2u);
}
counter = counter - 1u;
return ok(counter);
}
@readonly
function get_counter(): uint {
return counter;
}
3. Transpile to Clarity¶
View the output:
4. Development Workflow¶
Start watch mode for automatic rebuilding:
Format your code:
Lint for issues:
Key Concepts¶
Variables vs Constants¶
// Constants (immutable)
const MAX_SUPPLY: uint = 1000000u;
// Variables (mutable state)
let total_supply: uint = 0u;
Function Decorators¶
// Public functions (callable from outside)
@public
function transfer(to: principal, amount: uint): Response<bool, uint> {
// ...
}
// Read-only functions (no state changes)
@readonly
function get_balance(account: principal): uint {
// ...
}
// Private functions (internal only)
function validate(amount: uint): bool {
// ...
}
Type System¶
// Primitive types
let count: uint = 42u; // Unsigned integer
let value: int = -42; // Signed integer
let name: string = "Token"; // String
let active: bool = true; // Boolean
let owner: principal = tx-sender; // Principal (address)
// Complex types
let numbers: List<uint> = [1u, 2u, 3u];
let user: { name: string, balance: uint } = { name: "Alice", balance: 100u };
let maybe: Optional<uint> = some(42u);
let result: Response<bool, uint> = ok(true);
// Maps
map balances<principal, uint>;
Type Aliases¶
Next Steps¶
- Language Overview - Complete language reference
- CLI Reference - All CLI commands
- Testing - Write contract tests
- IDE Setup - Configure VS Code