Smart Contract Development
Everything you need to build production-grade on-chain programs — the Venus DSL in depth, workflow patterns, security, testing, and the REX runtime.
Venus DSL deep-dive
The rialo! macro is Rialo's embedded DSL for writing workflow programs — on-chain programs that can pause, react to events, and resume without off-chain infrastructure. It compiles to SVM-compatible instructions.
Every Venus program has two sections:
rialo! {
// 1. Persistent state — survives across all async operations
state {
field: Type,
// ...
}
// 2. Program logic — functions + async operations
program {
fn my_function(ctx: Context) { ... }
EVERY 1h => recurring_task();
AFTER 7d => one_shot_task();
ON price_feed::below("BTC/USD", 50_000) => on_crash();
SEND other_fn(args);
START SubWorkflow { fields };
}
}
Workflow state
State declared in the state { } block is persistent — it lives on-chain and survives across transactions, timers, and events. The Venus compiler handles account allocation, serialization, and rent exemption automatically.
Unlike Solana's raw account model, you never size accounts, derive PDAs, or manage rent manually. Declare your fields, and the runtime handles the rest.
Supported state field types:
| Type | Notes |
|---|---|
u8, u16, u32, u64, u128 | Unsigned integers |
i8, i16, i32, i64, i128 | Signed integers |
bool | Boolean |
Pubkey | 32-byte on-chain address |
String | UTF-8 string, dynamically sized |
Vec<T> | Dynamic array of any serializable type |
Option<T> | Optional value |
ScopedWallet | SCALE agent wallet (from rialo_cdk::scale) |
Async operations
Venus adds five async keywords to your program block:
AFTER — one-shot delay
// Run once, 30 days from now
AFTER 30d => release_funds();
// Duration units: s, m, h, d
AFTER 3600s => task(); // 1 hour
AFTER 12h => task(); // 12 hoursEVERY — recurring timer
// Run repeatedly forever (until explicitly cancelled)
EVERY 1h => hourly_check();
EVERY 30d => monthly_billing();ON — event trigger
// React to price feed
ON price_feed::below("ETH/USD", 2_000) => margin_call();
ON price_feed::above("BTC/USD", 100_000) => take_profit();
// React to another program's event
ON other_program::SettlementComplete => process(event.amount);
// React to an HTTPS response matching a condition
ON https::poll("https://api.example/status").json_eq("status", "ready")
=> handle_ready();SEND — non-blocking async message
// Non-blocking — execution continues immediately
SEND notify_user(user_pubkey);
SEND execute_trade(amount);START — spawn workflow instance
// Each call spawns a fresh, isolated workflow instance
START EscrowWorkflow {
buyer,
seller,
amount,
deadline,
};Events
Events are typed structs decorated with #[event]. They are committed on-chain and can be listened to by other programs using ON.
#[event]
pub struct OrderFilled {
pub order_id: u64,
pub amount: u64,
pub price: u64,
pub buyer: Pubkey,
pub seller: Pubkey,
}
// Emit from a function
emit!(OrderFilled {
order_id: state.order_id,
amount, price, buyer, seller,
});Error handling
#[error_code]
pub enum MyError {
#[msg("You are not authorized")]
Unauthorized,
#[msg("Insufficient balance — need {} lamports")]
InsufficientFunds,
#[msg("Deadline has passed")]
DeadlinePassed,
}
// Use require! as a guard
fn withdraw(ctx: Context, amount: u64) {
require!(ctx.signer == state.owner, MyError::Unauthorized);
require!(state.balance >= amount, MyError::InsufficientFunds);
require!(
Clock::get().unix_timestamp < state.deadline,
MyError::DeadlinePassed
);
// ...
}Security patterns
Access control
// Always validate the signer for privileged operations
require!(ctx.signer == state.owner, Error::Unauthorized);
// Multi-sig via threshold MPC
#[require_multisig(threshold = 2, signers = state.admins)]
fn admin_action(ctx: Context) { ... }Re-entrancy prevention
// Checks-effects-interactions pattern
fn withdraw(ctx: Context, amount: u64) {
// 1. Checks
require!(state.balance >= amount, Error::InsufficientFunds);
// 2. Effects — update state BEFORE external call
state.balance -= amount;
// 3. Interaction — external call last
token::transfer(ctx.accounts.vault, ctx.signer, amount).unwrap();
}Integer overflow
# Cargo.toml — always enable overflow checks
[profile.release]
overflow-checks = trueTesting
Rialo programs can be unit-tested in pure Rust and integration-tested against a local node:
use rialo_cdk::testing::*;
#[tokio::test]
async fn test_counter_increment() {
// Spin up an in-process local validator
let mut ctx = TestContext::new().await;
// Deploy the program
let program_id = ctx.deploy("../target/deploy/counter.so").await;
// Create a test keypair
let user = ctx.new_keypair();
ctx.airdrop(user.pubkey(), 2_000_000_000).await;
// Call initialize
ctx.call(program_id, "initialize", &user, &[]).await.unwrap();
// Call increment 3 times
for _ in 0..3 {
ctx.call(program_id, "increment", &user, &[]).await.unwrap();
}
// Read and assert state
let state = ctx.get_state::(program_id).await;
assert_eq!(state.count, 3);
} cargo test # unit tests
rialo test --url localhost # integration tests vs local nodeREX runtime
The Rialo Extended Execution Runtime (REX) is built on RISC-V. Key properties:
- ZK-friendly — RISC-V's simple ISA enables zero-knowledge proofs of execution
- TEE integration — programs can opt into confidential execution via the
#[confidential]attribute - Native randomness — generated in the enclave, verifiably unbiasable
- Stable gas — execution costs are deterministic and don't fluctuate with network load
- SVM compatible — the SVM layer runs on top of REX, so Solana programs work natively
Solana / Anchor compatibility
If you have an existing Anchor project, swap the dependency:
# Before (Anchor)
anchor-lang = "0.30"
# After (Rialo — drop-in replacement)
rialo-sol-lang = "0.9"
rialo-s-program = "0.9"After porting, your program immediately has access to native HTTPS calls, price feeds, timers, messaging, and randomness — without adding any other dependencies.