Skip to content
Ishinka edited this page Aug 10, 2022 · 7 revisions

This component provides tools which make possible proxies to be generated to the blockchain's pallet.

The annotation processor will generate proper implementations for the interfaces defined with @Pallet. This annotation indicates the pallet which represents a proxy to the blockchain's pallet. Where value() of the annotation is the name of the pallet.

Define a Pallet

The example below defines the pallet "System" that has the storage "BlockHash" and 2 events "ExtrinsicSuccess" and "ExtrinsicFailed".

@Pallet("System")
public interface SystemPallet {
    @Storage(
            value = "BlockHash",
            keys = {
                    @StorageKey(
                            type = @Scale(Integer.class),
                            hasher = StorageHasher.TwoX64Concat
                    )
            })
    StorageNMap<BlockHash> blockHash();

    @Event(index = 0)
    @Getter
    @Setter
    @ScaleReader
    class ExtrinsicSuccess {
        private DispatchInfo dispatchInfo;
    }

    @Event(index = 1)
    @Getter
    @Setter
    @ScaleReader
    class ExtrinsicFailed {
        private DispatchError dispatchError;
        private DispatchInfo dispatchInfo;
    }
}

Storage

The Storage annotation indicates the storage of the pallet. The annotation is applicable to a method that returns StorageNMap<> parametrized by the class of the value. The value() of the annotation is the name of the storage. If the storage item is of a type different from StorageValue (StorageValue is used to store any single value and has no keys) in the node, then the keys parameter must be set by an array of StorageKey. The number of keys must be equal to the number of keys in the storage structure in the node (e.g. a single key for StorageMap, two keys for StorageDoubleMap, N keys for StorageNMap).

The StorageKey annotation describes the SCALE-codec and the hash algorithm that are used for this key.

@Retention(RetentionPolicy.SOURCE)
public @interface StorageKey {

    Scale type() default @Scale();

    ScaleGeneric generic() default @ScaleGeneric(template = "", types = {});

    StorageHasher hasher();
}

Where:

  • type()returns the SCALE representation of the key in case it is non-generic;
  • generic() returns the SCALE representation of the key in case it is generic;
  • hasher() returns the hash algorithm of the key which is used to generate the map's key.

StorageHasher may have one of the following values:

  • Blake2B128Concat - the Blake2 128 Concat hash algorithm;
  • TwoX64Concat - the TwoX 64 Concat hash algorithm;
  • Identity - the Identity hash algorithm.

More information can be found on the Storage page.

Event

The Event annotation indicates the event of the pallet. The annotation is applicable to a class representing a pallet's event DTO. The class has to be SCALE readable, meaning that a corresponding SCALE reader has to be registered either manually or automatically with @ScaleReader or @AutoRegister. Events in turn also have to be registered in the EventRegistry. More information on the registrations can be found on the HowTo-Examples page.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Event {
    int index();
    
    String pallet() default "";
}

Where:

  • index() returns an index of the event in a pallet;
  • pallet() returns the name of a pallet. If the annotated class is nested within an interface annotated with @Pallet, this parameter can be omitted. A name specified in the @Pallet annotation will be used in this case;

Create an Instance of a Pallet

Normally, the instance of the pallet is being resolved via the Api.pallet method.

Clone this wiki locally