Data Expressions
This guide covers how to work with expressions in Tx3.
Overview
Section titled “Overview”Expressions in Tx3 are used to:
- Compute values
- Access properties
- Perform operations
- Construct data
Data Expressions
Section titled “Data Expressions”Literals
Section titled “Literals”// Integer literals123-4560
// Boolean literalstruefalse
// String literals"hello""world"
// Bytes literals0xDEADBEEF0x1234
Constructors
Section titled “Constructors”// Record constructionState { field1: value1, field2: value2,}
// Variant constructionResult::Success(42)Result::Error("failed")
Binary Operations
Section titled “Binary Operations”// Arithmetica + ba - b
// Comparisona == ba != ba < ba > ba <= ba >= b
// Logicala && ba || b!a
Property Access
Section titled “Property Access”// Record field accessrecord.field
// Variant field accessvariant.field
Asset Expressions
Section titled “Asset Expressions”Asset Constructors
Section titled “Asset Constructors”// ADA constructorAda(1000000)
// Custom asset constructorMyToken(100)
// NFT constructorAnyAsset(policy_id, asset_name, 1)
Asset Operations
Section titled “Asset Operations”// Additionasset1 + asset2
// Subtractionasset1 - asset2
// Property accessasset.amount
Common Patterns
Section titled “Common Patterns”Value Computation
Section titled “Value Computation”tx transfer(amount: Int) { input source { from: Sender, min_amount: Ada(amount), }
output { to: Receiver, amount: Ada(amount), }
output { to: Sender, amount: source - Ada(amount) - fees, }}
State Updates
Section titled “State Updates”tx update_state(new_value: Int) { input current { from: Contract, datum_is: State, }
output { to: Contract, amount: current.amount, datum: State { version: current.version + 1, value: new_value, timestamp: current.timestamp, } }}
Conditional Logic
Section titled “Conditional Logic”tx conditional_transfer( amount: Int, should_lock: Bool) { input source { from: Sender, min_amount: Ada(amount), }
output { to: should_lock ? TimeLock : Receiver, amount: Ada(amount), datum: should_lock ? LockData { amount } : None, }}
Expression Evaluation
Section titled “Expression Evaluation”Order of Operations
Section titled “Order of Operations”- Parentheses
- Property access
- Unary operations
- Binary operations
- Constructors
Examples
Section titled “Examples”// Complex expression(a + b) * (c - d)
// Property access with operationrecord.field + value
// Nested constructionState { value: (a + b) * c, timestamp: current.timestamp + 1,}
Best Practices
Section titled “Best Practices”-
Expression Clarity
- Use parentheses for clarity
- Break complex expressions
- Document assumptions
-
Type Safety
- Check operand types
- Handle edge cases
- Validate results
-
Performance
- Minimize computation
- Cache repeated values
- Optimize expressions
-
Error Prevention
- Check for null/undefined
- Validate ranges
- Handle edge cases
Common Use Cases
Section titled “Common Use Cases”Fee Calculation
Section titled “Fee Calculation”tx transfer_with_fee( amount: Int, fee_rate: Int) { input source { from: Sender, min_amount: Ada(amount + (amount * fee_rate / 100)), }
output { to: Receiver, amount: Ada(amount), }
output { to: FeeCollector, amount: Ada(amount * fee_rate / 100), }}
State Transitions
Section titled “State Transitions”tx transition( new_state: State) { input current { from: Contract, datum_is: State, }
output { to: Contract, amount: current.amount, datum: State { version: current.version + 1, state: new_state, timestamp: current.timestamp + 1, } }}
Asset Management
Section titled “Asset Management”tx manage_assets( amounts: [Int]) { input source { from: Manager, min_amount: amounts.fold(Ada(0), |acc, x| acc + Ada(x)), }
output { to: Pool, amount: amounts.fold(Ada(0), |acc, x| acc + Ada(x)), }}