Skip to content

Runtime API modules

Contains modules of the Runtime API Specification.

Supported modules

Name Description
AccountNonceApi Provides logic to get an account's nonce.
AuraApi Manages block authoring AuRa consensus mechanism.
BabeApi Manages block authoring BABE consensus mechanism.
Benchmarking Provides functionality for benchmarking extrinsic calls and system hooks.
BlockBuilder Provides functionality for building and finalizing a block.
Core Provides functionality for initialising and executing a block.
GenesisBuilder Builds genesis configuration.
GrandpaApi Manages the GRANDPA block finalization.
Metadata Returns the metadata of the runtime
OffchainWorkerApi Provides functionality to start offchain worker operations.
SessionKeys Generates and decodes session keys
TaggedTransactionQueue Validates transactions in the transaction queue.
TransactionPaymentApi Queries the runtime for transaction fees.
TransactionPaymentCallApi Queries the runtime for transaction call fees.

Structure

Each module must have the following:

  • name
  • version
  • exported runtime functions
    • Each runtime-exported function takes care of reading from and writing to the WASM memory.
  • metadata definition
  • Explanatory inline documentation

Example

An example module, which takes care of returning account balances.

package example

// imports

// Module implements the Example Runtime API.
type Module struct {
    memUtils utils.WasmMemoryTranslator
    logger   log.Logger
}

func New(logger log.Logger) Module {
    return Module{
        memUtils: utils.NewMemoryTranslator(),
        logger:   logger,
    }
}

// Name returns the name of the api module.
func (m Module) Name() string {
    return "ExampleModule"
}

// Item returns the first 8 bytes of the Blake2b hash of the name and version of the api module.
func (m Module) Item() types.ApiItem {
    hash := hashing.MustBlake2b8([]byte(ApiModuleName))
    return types.NewApiItem(hash, apiVersion)
}

// AccountBalance returns the balance of given AccountId.
// It takes two arguments:
// - dataPtr: Pointer to the data in the Wasm memory.
// - dataLen: Length of the data.
// which represent the SCALE-encoded AccountId.
// Returns a pointer-size of the SCALE-encoded balance of the account.
// <Link to Specification if found>
func (m Module) AccountBalance(dataPtr int32, dataLen int32) int64 {
    b := m.memUtils.GetWasmMemorySlice(dataPtr, dataLen)
    buffer := bytes.NewBuffer(b)

    accountId, err := types.DecodeAccountId(buffer)
    if err != nil {
        m.logger.Critical(err.Error())
    }

    // Logic to get account balance

    return m.memUtils.BytesToOffsetAndSize(accountBalance)
}

// Metadata returns the runtime api metadata of the module.
func (m Module) Metadata() types.RuntimeApiMetadata {
    // Metadata declaration of the module, which includes:
    // * Name of the module
    // * Definition of each runtime exported function (AccountBalance).
    methods := sc.Sequence[types.RuntimeApiMethodMetadata]{
        types.RuntimeApiMethodMetadata{
            Name: "account_balance",
            Inputs: sc.Sequence[types.RuntimeApiMethodParamMetadata]{
                types.RuntimeApiMethodParamMetadata{
                    Name: "account",
                    Type: sc.ToCompact(metadata.TypesAddress32),
                },
            },
            Output: sc.ToCompact(metadata.PrimitiveTypesU128),
            Docs:   sc.Sequence[sc.Str]{" Get current account balance of given `AccountId`."},
        },
    }

    return types.RuntimeApiMetadata{
        Name:    "ExampleModule",
        Methods: methods,
        Docs:    sc.Sequence[sc.Str]{" The Example API to query account balances."},
    }
}