Add a custom transaction

Custom transactions

Transactions are an essential part of blockchain applications that are created using the Lisk SDK.

The Lisk SDK provides a class BaseTransaction from which developers can inherit and extend from, to create custom transaction types.

The application-specific business logic for custom transaction types is defined according to an abstract interface that is common across all transaction types.

All of the default transaction types of the Lisk SDK transactions implement the abstract interface of the base transaction, and therefore the base transaction can be used as a model for custom transactions. It is also possible to inherit one of the default transaction types, in order to extend or modify them.

Register a custom transaction

Add your custom transaction type to your blockchain application by registering it to the application instance as shown below:

const { Application, genesisBlockDevnet } = require('lisk-sdk');

const MyTransaction = require('./my_transaction');

const app = new Application(genesisBlockDevnet);

app.registerTransaction(MyTransaction); (1)

app
    .run()
    .then(() => app.logger.info('App started...'))
    .catch(error => {
        console.error('Faced error in application', error);
        process.exit(1);
    });
1 Register the custom transaction.

For information on creating your own custom transaction, please follow the tutorials.

The BaseTransaction interface

The BaseTransaction class is the interface that all other Transaction Types need to inherit from, in order to be compatible with the Lisk SDK.

Required properties

TYPE

static TYPE: number

The hallmark of a transaction. Set this constant to any number, except 0-9, which are reserved for the default transactions.

Required methods

All of the abstract methods and properties on the base transaction’s interface are required to be implemented. These are all listed below:

prepare

prepare(store: StateStorePrepare): Promise<void>

validateAsset

validateAsset(): ReadonlyArray<TransactionError>

Before a transaction reaches the apply step it is validated. Check the transaction’s asset correctness from the schema perspective, (no access to StateStore here). Invalidate the transaction by pushing an error into the result array. Prepare the relevant information about the accounts, which will be accessible in the later steps during the apply and undo steps.

applyAsset

applyAsset(store: StateStore): ReadonlyArray<TransactionError>

The business use case of a transaction is implemented in the applyAsset method. It applies all of the necessary changes from the received transaction to the affected account(s), by calling store.set. Calling store.get will acquire all of the relevant data. The transaction that is currently processing is the function’s context, (e.g. this.amount). This transaction can be invalidated by pushing an error into the result array.

undoAsset

undoAsset(store: StateStore): ReadonlyArray<TransactionError>

The inversion of the applyAsset method. Undoes all of the changes to the accounts applied by the applyAsset step.

Additional methods

To increase your application’s performance, the following functions should be overidden: verifyAgainstTransactions, assetFromSync, fromSync.

The BaseTransaction provides the default implementation of the methods revolving around the signatures. As your application matures it is possible to implement custom methods of how your transaction’s signature is derived: sign, getBytes, assetToBytes.

Default transaction types

The first 10 transaction types are reserved for the Lisk protocol. Do not use these to register custom transactions.

Each default transaction type implements a different use case of the Lisk network. For example:

  1. Balance transfer, (type 0).

  2. Second signature registration, (type 1).

  3. Delegate registration, (type 2).

  4. Delegate vote, (type 3).

  5. Multi-signature account registration, (type 4).

For a complete list of all default transaction types, please see the section Lisk Transactions of the Lisk Protocol.

Furthermore, the Lisk SDK tutorials include simple code examples of custom transaction types.

What is the lifecycle of a transaction?

The lifecycle of a general transaction using the Lisk SDK can be summarized in 7 steps:

  1. A transaction is created and signed, (off-chain). The script to execute this is as follows: src/create_and_sign.ts.

  2. The transaction is sent to a network. This can be done by a third party tool, (such as curl or Postman). However this can also be achieved by using Lisk Commander, Lisk Desktop or Mobile. All of the tools need to be authorized to access an HTTP API of a network node.

  3. A network node receives the transaction and after a lightweight schema validation, adds it to a transaction pool.

  4. In the transaction pool, the transactions are firstly validated. In this step, only static checks are performed, which include schema validation and signature validation.

  5. Validated transactions go to the prepare step, as defined in the transaction class, which to limit the I/O database operations, prepares all the information relevant to properly apply or undo the transaction. The store with the prepared data is a parameter of the afore-mentioned methods.

  6. Delegates forge the valid transactions into blocks and broadcasts the blocks to the network. Each network node performs the apply and applyAsset steps, after the successful completion of the validate step.

  7. Shortly after a block is applied, it is possible that a node performs the undo step; (due to decentralized network conditions). If this occurs, then the block containing all of the included transactions is reverted in favor of a competing block.

While implementing a custom transaction, it is necessary to complete some of these steps. Often, a base transaction implements a default behavior. With experience, you may decide to override some of these base transaction methods, resulting in an implementation that is well-tailored and provides the best possible performance for your use case.