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
- First Contract Transaction β Lock and spend from a script address
- 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