Data Expressions
This guide covers how to work with expressions in Tx3.
Overview
Expressions in Tx3 are used to:
- Compute values
- Access properties
- Perform operations
- Construct data
Data Expressions
Literals
// Integer literals123-4560
// Boolean literalstruefalse
// String literals"hello""world"
// Bytes literals0xDEADBEEF0x1234
Constructors
// Record constructionState { field1: value1, field2: value2,}
// Variant constructionResult::Success(42)Result::Error("failed")
Binary Operations
// Arithmetica + ba - b
// Comparisona == ba != ba < ba > ba <= ba >= b
// Logicala && ba || b!a
Property Access
// Record field accessrecord.field
// Variant field accessvariant.field
Asset Expressions
Asset Constructors
// ADA constructorAda(1000000)
// Custom asset constructorMyToken(100)
// NFT constructorNFT(policy_id, asset_name)
Asset Operations
// Additionasset1 + asset2
// Subtractionasset1 - asset2
// Property accessasset.amount
Common Patterns
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
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
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
Order of Operations
- Parentheses
- Property access
- Unary operations
- Binary operations
- Constructors
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
-
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
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
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
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)), }}