Transaction Basics

Working with transactions and its various options

The MeshTxBuilder is a powerful low-level APIs that allows you to build and sign transactions.

The MeshTxBuilder is a powerful interface where the higher level Transaction class is indeed a pre-built combination of the MeshTxBuilder APIs. With these lower level APIs, it builds the object to be passing to the serialization libraries like cardano-sdk and Whisky SDK to construct transactions.

In this page, we will cover how to initialize the MeshTxBuilder and the basic operations of building a transaction.

Initialize Tx Builder

To start building an customized transaction, you need to first initialize MeshTxBuilder:

The MeshTxBuilder instance has the following signature:

There are 6 optional fields to pass in to initialized the lower level APIs instance:

  1. serializer: The default serializer is CSLSerializer. You can pass in your own serializer instance.
  2. fetcher: When you build the transaction without sufficient fields as required by the serialization library, we would index the blockchain to fill the information for you. Affected APIs are txIn, txInCollateral, spendingTxInReference.
  3. submitter: It is used if you would like to use the submitter submitTx API directly from the instance.
  4. evaluator: It would perform redeemer execution unit optimization, returning error message in case of invalid transaction.
  5. isHydra: Use another set of default protocol parameters for building transactions.
  6. params: You can pass in the protocol parameters directly.

Below provides some examples of transaction building. Complete working examples can be found in mesh-lower-level-api-demo

Common Function(s)

The function(s) that we use in most of these examples.

Get tx builder

This function creates a new instance of the MeshTxBuilder with the blockchain provider.

Send Value

Sending values to a recipient is a common operation in blockchain transactions. The Mesh SDK provides a simple way to build a transaction to send values to a recipient.

The following shows a simple example of building a transaction to send values to a recipient:

Send Value

Send assets to a recipient.

Connect wallet to run this demo

No wallets installed

Multi-signature Transaction

The main idea of a multi-signature transaction is to have multiple signatures to authorize a transaction.

In the above code snippet, we are signing the transaction with the user wallet and then signing the transaction with the minting wallet. ThesignTx function is used to sign the transaction. The second argument is a boolean value that indicates whether the transaction is a multi-signature transaction.

Multi-signature Transaction

Create a multi-signature transaction. In this demo, we will create a transaction with two signatures, where one signature is from the user wallet and the other is from a minting wallet.

Connect wallet to run this demo

No wallets installed

Build with Object

One alternative to use the lower level APIs is to build the transaction with an object.

The following shows a simple example of building a transaction to send values to a recipient:

Send Lovelace

Send lovelace to a recipient

Connect wallet to run this demo

No wallets installed

Set Metadata

Add messages/comments/memos as transaction metadata. This is useful for attaching additional information to a transaction.

Transaction message

Add messages/comments/memos as transaction metadata

Connect wallet to run this demo

No wallets installed

Transaction message

Add messages/comments/memos as transaction metadata. This is useful for attaching additional information to a transaction.

The specification for the individual strings follow the general design specification for JSON metadata, which is already implemented and in operation on the cardano blockchain. The used metadatum label is 674:, this number was choosen because it is the T9 encoding of the stringmsg. The message content has the key msg: and consists of an array of individual message-strings. The number of theses message-strings must be at least one for a single message, more for multiple messages/lines. Each of theses individual message-strings array entries must be at most 64 bytes when UTF-8 encoded.

Transaction message

Add messages/comments/memos as transaction metadata

Connect wallet to run this demo

No wallets installed

Set Collateral

Specify the UTXOs that you want to use as collateral.

Set Required Signers

Sets the required signers for the transaction. This is useful when you want to include multiple signers, such as in a multi-signature transaction or smart contracts.

Set Start and Expire Time

We can define the time-to-live (TTL) for the transaction. TTL is the time limit for our transaction to be included in a blockchain, if it is not in a blockchain by then the transaction will be cancelled. This time limit is defined as slot.

In order to get the slot of the time you wish the transaction would expire, you can use resolveSlotNo. For example, if you would like the transaction to expire in 5 minutes, you can get the slot in the following way:

Next, we set the TTL with invalidHereafter and providing the slot, this means that if the transaction is submitted after after slot will not be valid.

Likewise, we can set a "validity start interval" for the transaction, where it is the time the transaction will be valid. We can define the start time with invalidBefore and providing the slot: