Building Cardano Transactions
Scalus provides a fluent API for constructing Cardano transactions — payments, token minting, staking, governance, and smart contract interactions.
What You Can Build
- Payments — Send ADA and native tokens
- Spending UTxOs — Spending UTxOs from wallet and script addresses
- Token Minting — Create and burn native assets
- Staking — Delegate stake and withdraw rewards
- Governance — DRep registration and voting
- Smart Contract Interactions — Spend script UTxOs with Plutus validators
Transaction Builder
TransactionBuilder (TxBuilder) handles the complexity of Cardano transactions — UTxO selection, fee calculation, script evaluation, and balancing:
val tx = TxBuilder(env)
.payTo(recipient, Value.ada(10))
.complete(provider, myAddress)
.await()
.sign(signer)
.transactionSince validators are written in Scala, you can share code between on-chain and off-chain:
- Code Reuse — Share data types and validation logic
- Type Safety — Scala’s type system across your entire stack
- Integrated Testing — Test validators and transactions together
Quick Example
val tx = TxBuilder(env)
.spend(utxo) // Add UTxO as input
.payTo(recipientAddress, Value.ada(10)) // Send 10 ADA to recipient
.build(changeTo = changeAddress) // Finalize: calculate fees, handle change
.sign(signer) // Add signature
.transaction // Get the final transactionTxBuilder is purely functional — each method returns a new immutable instance. Only build() can throw while evaluating scripts and balancing.
Automatic Completion
The complete() method handles everything that can be determined programmatically:
TxBuilder(env)
.payTo(recipient, Value.ada(10))
.complete(provider, sponsorAddress) // Automatically complete using funds from sponsorAddressIt automatically:
- Selects UTxOs from the sponsor address to cover outputs and fees
- Adds collateral inputs for script execution
- Calculates fees based on transaction size and execution costs
- Creates change outputs and iteratively rebalances
Script Evaluation
TxBuilder evaluates Plutus scripts during build to catch errors before submission:
TxBuilder(env, evaluator)
.spend(scriptUtxo, redeemer, script)
.payTo(recipient, scriptUtxo.output.value)
.build(changeTo = changeAddress) // Scripts evaluated hereNext Steps
- First Transaction — Step-by-step guide
- Payment Methods — Send ADA and tokens
- Spending UTxOs — Input selection and script UTxOs
- Minting & Burning — Create and destroy native tokens
- Staking & Rewards — Delegation and reward withdrawal
- Governance — DRep registration and voting
Related
- Smart Contracts — Write validators to use in transactions
- Testing with Emulator — Test transactions locally
- DApp Starter Tutorial — Complete example with transactions
Last updated on