@hydra-sdk/transaction
The transaction package provides advanced tools for building, managing, and submitting Cardano transactions, including support for Plutus scripts, multi-asset transactions, and more.
Installation
npm install @hydra-sdk/transaction
Key Classes
TxBuilder
The main class for building and managing Cardano transactions.
Constructor
import TxBuilder from '@hydra-sdk/transaction'
const txBuilder = new TxBuilder({
fetcher: customFetcher, // Optional custom fetcher
submitter: customSubmitter, // Optional custom submitter
isHydra: false, // Optional Hydra mode
params: {}, // Optional protocol parameters
verbose: true // Optional verbose mode
})
Methods
txOut(address, amount)
Add an output to the transaction:
txBuilder.txOut('addr_test...', [{ unit: 'lovelace', quantity: '1000000' }])
txIn(txHash, outputIndex, amount, address)
Add an input to the transaction:
txBuilder.txIn('txHash...', 0, [{ unit: 'lovelace', quantity: '1000000' }], 'addr_test...')
txInScript(scriptCbor)
Add a script input:
txBuilder.txInScript('scriptCbor...')
txInDatumValue(datum)
Add a datum value to the input:
txBuilder.txInDatumValue(datum)
txInRedeemerValue(redeemer)
Add a redeemer value to the input:
txBuilder.txInRedeemerValue(redeemer)
txInCollateral(txHash, outputIndex, amount, address)
Add a collateral input:
txBuilder.txInCollateral('txHash...', 0, [{ unit: 'lovelace', quantity: '1000000' }], 'addr_test...')
mint(quantity, policyId, assetName)
Mint a new asset:
txBuilder.mint('1000', 'policyId...', 'assetName...')
mintingScript(scriptCbor)
Add a minting script:
txBuilder.mintingScript('scriptCbor...')
mintRedeemerValue(redeemer)
Add a redeemer value for minting:
txBuilder.mintRedeemerValue(redeemer)
changeAddress(address)
Set the change address:
txBuilder.changeAddress('addr_test...')
selectUtxosFrom(utxos)
Select UTXOs for the transaction:
txBuilder.selectUtxosFrom(utxos)
requiredSignerHash(pubKeyHash)
Add a required signer:
txBuilder.requiredSignerHash('pubKeyHash...')
complete()
Complete the transaction:
const tx = await txBuilder.complete()
Key Interfaces
TxBuilderOptions
Options for initializing the TxBuilder:
interface TxBuilderOptions {
fetcher?: IFetcher
submitter?: ISubmitter
isHydra?: boolean
params?: Partial<Protocol>
verbose?: boolean
}
MintAsset
Interface for minting assets:
interface MintAsset {
assetName: string
quantity: string
policyId: string
policyScript?: ScriptRef
redeemer?: Redeemer
}
Certificate
Interface for certificates:
interface Certificate {
type: CertificateType
stakeKeyHash?: string
poolKeyHash?: string
rewardAddress?: string
poolParams?: any
epoch?: number
}
Withdrawal
Interface for withdrawals:
interface Withdrawal {
rewardAddress: string
amount: string
}
ValidityRange
Interface for transaction validity range:
interface ValidityRange {
invalidBefore?: number
invalidAfter?: number
}
Utility Functions
Coin Selection Strategies
Available strategies for coin selection:
const COIN_SELECTION_STRATEGY = {
LargestFirst: CardanoWASM.CoinSelectionStrategyCIP2.LargestFirst,
RandomImprove: CardanoWASM.CoinSelectionStrategyCIP2.RandomImprove,
LargestFirstMultiAsset: CardanoWASM.CoinSelectionStrategyCIP2.LargestFirstMultiAsset,
RandomImproveMultiAsset: CardanoWASM.CoinSelectionStrategyCIP2.RandomImproveMultiAsset
}
Plutus Script Versions
Supported Plutus script versions:
type PlutusVersion = 'V1' | 'V2' | 'V3'
Datum and Redeemer Types
Types for datum and redeemer:
type Datum = CardanoWASM.PlutusData
type Redeemer = CardanoWASM.Redeemer
Script Reference
Interface for script references:
interface ScriptRef {
scriptCbor: string
version: PlutusVersion
}
