StxScript is a high-level language and transpiler for writing smart contracts on the Stacks blockchain. It provides a more familiar and expressive syntax compared to Clarity, while still compiling down to valid Clarity code.
[EXPERIMENTAL] : Under active development.
- Familiar syntax inspired by modern programming languages
- Static typing with type inference
- First-class support for Clarity concepts like traits, maps, and assets
- Enhanced readability and maintainability compared to raw Clarity code
- Comprehensive error messages and static analysis
- Support for complex data structures and operations
- Built-in support for common smart contract patterns
You can install StxScript using pip:
pip install stxscript
For development, we recommend using Poetry:
poetry add stxscript
After installation, you can use the stxscript
command to transpile StxScript files to Clarity:
stxscript input.stx output.clar
You can also use StxScript as a library in your Python projects:
from stxscript import StxScriptTranspiler
transpiler = StxScriptTranspiler()
clarity_code = transpiler.transpile(stxscript_code)
StxScript supports all Clarity types:
int
: Signed 128-bit integeruint
: Unsigned 128-bit integerbool
: Boolean (true or false)principal
: Stacks address or contract identifierstring
: UTF-8 stringbuffer
: Byte bufferlist<T>
: List of elements of type Toptional<T>
: Optional value of type TResponse<T, E>
: Response type with ok (T) and error (E) variantstuple
: Named fields of varying types
let myVariable: int = 42;
const MY_CONSTANT: uint = 100;
@public
function add(a: int, b: int): int {
return a + b;
}
if (condition) {
// do something
} else if (otherCondition) {
// do something else
} else {
// fallback
}
// List operations
let doubled = map([1, 2, 3], (x) => x * 2);
let evens = filter([1, 2, 3, 4], (x) => x % 2 == 0);
let sum = fold([1, 2, 3, 4], 0, (acc, x) => acc + x);
try {
// code that might throw an error
} catch (error) {
// handle error
}
throw "Custom error message";
// Traits
trait Token {
transfer(from: principal, to: principal, amount: uint): Response<bool, uint>;
getBalance(account: principal): Response<uint, uint>;
}
// Maps
@map({ key: principal, value: uint })
const balances = new Map<principal, uint>();
// Assets
@asset
class NFT {
id: uint;
owner: principal;
}
// Contract calls
let result = TokenContract.transfer(sender, recipient, amount);
- List comprehensions
- Type assertions and checks
- Async operations (where supported by Clarity)
- Import and export declarations for modular contract development
Here's a simple token contract written in StxScript:
@map({ key: principal, value: uint })
const balances = new Map<principal, uint>();
@public
function transfer(to: principal, amount: uint): Response<bool, uint> {
let sender = tx.sender;
let senderBalance = balances.get(sender) ?? 0;
if (senderBalance < amount) {
return err(1); // Insufficient balance
}
let recipientBalance = balances.get(to) ?? 0;
balances.set(sender, senderBalance - amount);
balances.set(to, recipientBalance + amount);
return ok(true);
}
@readable
function getBalance(account: principal): Response<uint, uint> {
return ok(balances.get(account) ?? 0);
}
-
Clone the repository:
git clone https://github.com/cryptuon/stxscript.git cd stxscript
-
Install Poetry if you haven't already:
curl -sSL https://install.python-poetry.org | python3 -
-
Install the project dependencies:
poetry install
-
Activate the virtual environment:
poetry shell
To run the test suite:
poetry run python -m unittest stxscript.test_transpiler
To run linting and type checking:
poetry run flake8 stxscript
poetry run mypy stxscript
We welcome contributions to StxScript! Please see our Contributing Guide for more details on how to get started.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.