@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

bash
npm install @hydra-sdk/transaction

Key Classes

TxBuilder

The main class for building and managing Cardano transactions.

Constructor

typescript
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:

typescript
txBuilder.txOut('addr_test...', [{ unit: 'lovelace', quantity: '1000000' }])

txIn(txHash, outputIndex, amount, address) Add an input to the transaction:

typescript
txBuilder.txIn('txHash...', 0, [{ unit: 'lovelace', quantity: '1000000' }], 'addr_test...')

txInScript(scriptCbor) Add a script input:

typescript
txBuilder.txInScript('scriptCbor...')

txInDatumValue(datum) Add a datum value to the input:

typescript
txBuilder.txInDatumValue(datum)

txInRedeemerValue(redeemer) Add a redeemer value to the input:

typescript
txBuilder.txInRedeemerValue(redeemer)

txInCollateral(txHash, outputIndex, amount, address) Add a collateral input:

typescript
txBuilder.txInCollateral('txHash...', 0, [{ unit: 'lovelace', quantity: '1000000' }], 'addr_test...')

mint(quantity, policyId, assetName) Mint a new asset:

typescript
txBuilder.mint('1000', 'policyId...', 'assetName...')

mintingScript(scriptCbor) Add a minting script:

typescript
txBuilder.mintingScript('scriptCbor...')

mintRedeemerValue(redeemer) Add a redeemer value for minting:

typescript
txBuilder.mintRedeemerValue(redeemer)

changeAddress(address) Set the change address:

typescript
txBuilder.changeAddress('addr_test...')

selectUtxosFrom(utxos) Select UTXOs for the transaction:

typescript
txBuilder.selectUtxosFrom(utxos)

requiredSignerHash(pubKeyHash) Add a required signer:

typescript
txBuilder.requiredSignerHash('pubKeyHash...')

complete() Complete the transaction:

typescript
const tx = await txBuilder.complete()

Key Interfaces

TxBuilderOptions

Options for initializing the TxBuilder:

typescript
interface TxBuilderOptions {
  fetcher?: IFetcher
  submitter?: ISubmitter
  isHydra?: boolean
  params?: Partial<Protocol>
  verbose?: boolean
}

MintAsset

Interface for minting assets:

typescript
interface MintAsset {
  assetName: string
  quantity: string
  policyId: string
  policyScript?: ScriptRef
  redeemer?: Redeemer
}

Certificate

Interface for certificates:

typescript
interface Certificate {
  type: CertificateType
  stakeKeyHash?: string
  poolKeyHash?: string
  rewardAddress?: string
  poolParams?: any
  epoch?: number
}

Withdrawal

Interface for withdrawals:

typescript
interface Withdrawal {
  rewardAddress: string
  amount: string
}

ValidityRange

Interface for transaction validity range:

typescript
interface ValidityRange {
  invalidBefore?: number
  invalidAfter?: number
}

Utility Functions

Coin Selection Strategies

Available strategies for coin selection:

typescript
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:

typescript
type PlutusVersion = 'V1' | 'V2' | 'V3'

Datum and Redeemer Types

Types for datum and redeemer:

typescript
type Datum = CardanoWASM.PlutusData
type Redeemer = CardanoWASM.Redeemer

Script Reference

Interface for script references:

typescript
interface ScriptRef {
  scriptCbor: string
  version: PlutusVersion
}