Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events support #27

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
testImplementation 'org.testcontainers:testcontainers:1.17.2'
testImplementation 'org.testcontainers:junit-jupiter:1.17.2'
testImplementation 'org.awaitility:awaitility:4.2.0'
testImplementation 'org.hamcrest:hamcrest:2.2'

testAnnotationProcessor project(':pallet:pallet-codegen')
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.strategyobject.substrateclient.api.pallet.balances.AccountData;
import com.strategyobject.substrateclient.api.pallet.balances.AccountDataReader;
import com.strategyobject.substrateclient.api.pallet.system.System;
import com.strategyobject.substrateclient.common.types.Lambda;
import com.strategyobject.substrateclient.crypto.ss58.SS58AddressFormat;
import com.strategyobject.substrateclient.pallet.GeneratedPalletFactory;
import com.strategyobject.substrateclient.pallet.PalletFactory;
import com.strategyobject.substrateclient.pallet.events.EventDescriptor;
import com.strategyobject.substrateclient.pallet.events.EventDescriptorReader;
import com.strategyobject.substrateclient.pallet.events.EventRegistry;
import com.strategyobject.substrateclient.rpc.GeneratedRpcSectionFactory;
import com.strategyobject.substrateclient.rpc.RpcSectionFactory;
import com.strategyobject.substrateclient.rpc.api.section.State;
Expand Down Expand Up @@ -35,12 +42,12 @@ public class DefaultModule extends AbstractModule {

private final @NonNull ProviderInterface providerInterface;

private Consumer<ScaleReaderRegistry> configureScaleReaderRegistry = x -> x.registerAnnotatedFrom(PREFIX);
private Consumer<ScaleWriterRegistry> configureScaleWriterRegistry = x -> x.registerAnnotatedFrom(PREFIX);
private BiConsumer<RpcDecoderRegistry, RpcDecoderContextFactory> configureRpcDecoderRegistry =
(registry, contextFactory) -> registry.registerAnnotatedFrom(contextFactory, PREFIX);
private BiConsumer<RpcEncoderRegistry, RpcEncoderContextFactory> configureRpcEncoderRegistry =
(registry, contextFactory) -> registry.registerAnnotatedFrom(contextFactory, PREFIX);
private Consumer<ScaleReaderRegistry> configureScaleReaderRegistry = Lambda::noop;
private Consumer<ScaleWriterRegistry> configureScaleWriterRegistry = Lambda::noop;
private BiConsumer<RpcDecoderRegistry, RpcDecoderContextFactory> configureRpcDecoderRegistry = Lambda::noop;
private BiConsumer<RpcEncoderRegistry, RpcEncoderContextFactory> configureRpcEncoderRegistry = Lambda::noop;
private Consumer<EventRegistry> configureEventRegistry = Lambda::noop;


public DefaultModule configureScaleReaderRegistry(Consumer<ScaleReaderRegistry> configure) {
configureScaleReaderRegistry = configureScaleReaderRegistry.andThen(configure);
Expand All @@ -62,6 +69,11 @@ public DefaultModule configureRpcEncoderRegistry(BiConsumer<RpcEncoderRegistry,
return this;
}

public DefaultModule configureEventRegistry(Consumer<EventRegistry> configure) {
configureEventRegistry = configureEventRegistry.andThen(configure);
return this;
}

@Override
protected void configure() {
try {
Expand Down Expand Up @@ -97,8 +109,13 @@ protected void configure() {

@Provides
@Singleton
public ScaleReaderRegistry provideScaleReaderRegistry() {
public ScaleReaderRegistry provideScaleReaderRegistry(MetadataProvider metadataProvider,
EventRegistry eventRegistry) {
val registry = new ScaleReaderRegistry();
registry.registerAnnotatedFrom(PREFIX);
registry.register(new AccountDataReader(registry), System.AccountData.class, AccountData.class);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be registered automatically?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, unless write a custom scale reader.

registry.register(new EventDescriptorReader(registry, metadataProvider, eventRegistry), EventDescriptor.class);

configureScaleReaderRegistry.accept(registry);
return registry;
}
Expand All @@ -107,6 +124,8 @@ public ScaleReaderRegistry provideScaleReaderRegistry() {
@Singleton
public ScaleWriterRegistry provideScaleWriterRegistry() {
val registry = new ScaleWriterRegistry();
registry.registerAnnotatedFrom(PREFIX);

configureScaleWriterRegistry.accept(registry);
return registry;
}
Expand All @@ -120,6 +139,7 @@ public RpcDecoderRegistry provideRpcDecoderRegistry(MetadataProvider metadataPro
metadataProvider,
registry,
scaleReaderRegistry);
registry.registerAnnotatedFrom(() -> context, PREFIX);

configureRpcDecoderRegistry.accept(registry, () -> context);
return registry;
Expand All @@ -134,11 +154,22 @@ public RpcEncoderRegistry provideRpcEncoderRegistry(MetadataProvider metadataPro
metadataProvider,
registry,
scaleWriterRegistry);
registry.registerAnnotatedFrom(() -> context, PREFIX);

configureRpcEncoderRegistry.accept(registry, () -> context);
return registry;
}

@Provides
@Singleton
public EventRegistry provideEventRegistry() {
val registry = new EventRegistry();
registry.registerAnnotatedFrom(PREFIX);

configureEventRegistry.accept(registry);
return registry;
}

@Provides
@Singleton
public MetadataProvider provideMetadata() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.strategyobject.substrateclient.api.pallet.balances;

import com.strategyobject.substrateclient.api.pallet.system.System;
import com.strategyobject.substrateclient.scale.ScaleType;
import com.strategyobject.substrateclient.scale.annotation.Scale;
import com.strategyobject.substrateclient.scale.annotation.ScaleReader;
import lombok.Getter;
import lombok.Setter;

import java.math.BigInteger;

@ScaleReader
@Getter
@Setter
public class AccountData implements System.AccountData {
@Scale(ScaleType.U128.class)
private BigInteger free;

@Scale(ScaleType.U128.class)
private BigInteger reserved;

@Scale(ScaleType.U128.class)
private BigInteger miscFrozen;

@Scale(ScaleType.U128.class)
private BigInteger feeFrozen;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.strategyobject.substrateclient.api.pallet.balances;

import com.strategyobject.substrateclient.pallet.annotation.Event;
import com.strategyobject.substrateclient.pallet.annotation.Pallet;
import com.strategyobject.substrateclient.rpc.api.AccountId;
import com.strategyobject.substrateclient.rpc.api.BalanceStatus;
import com.strategyobject.substrateclient.rpc.api.primitives.Balance;
import com.strategyobject.substrateclient.scale.annotation.ScaleReader;
import lombok.Getter;
import lombok.Setter;

@Pallet("Balances")
public interface Balances {

/**
* An account was created with some free balance. \[account, free_balance\]
*/
@Event(index = 0)
@Getter
@Setter
@ScaleReader
class Endowed {
private AccountId account;
private Balance freeBalance;
}

/**
* An account was removed whose balance was non-zero but below ExistentialDeposit,
* resulting in an outright loss. \[account, balance\]
*/
@Event(index = 1)
@Getter
@Setter
@ScaleReader
class DustLost {
private AccountId account;
private Balance balance;
}

/**
* Transfer succeeded. \[from, to, value\]
*/
@Event(index = 2)
@Getter
@Setter
@ScaleReader
class Transfer {
private AccountId from;
private AccountId to;
private Balance value;
}

/**
* A balance was set by root. \[who, free, reserved\]
*/
@Event(index = 3)
@Getter
@Setter
@ScaleReader
class BalanceSet {
private AccountId account;
private Balance free;
private Balance reserved;
}

/**
* Some amount was deposited (e.g. for transaction fees). \[who, deposit\]
*/
@Event(index = 4)
@Getter
@Setter
@ScaleReader
class Deposit {
private AccountId account;
private Balance value;
}

/**
* Some balance was reserved (moved from free to reserved). \[who, value\]
*/
@Event(index = 5)
@Getter
@Setter
@ScaleReader
class Reserved {
private AccountId account;
private Balance value;
}

/**
* Some balance was unreserved (moved from reserved to free). \[who, value\]
*/
@Event(index = 6)
@Getter
@Setter
@ScaleReader
class Unreserved {
private AccountId account;
private Balance value;
}

/**
* Some balance was moved from the reserve of the first account to the second account.
* Final argument indicates the destination balance type.
* \[from, to, balance, destination_status\]
*/
@Event(index = 7)
@Getter
@Setter
@ScaleReader
class ReserveRepatriated {
private AccountId from;
private AccountId to;
private Balance value;
private BalanceStatus destinationStatus;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.strategyobject.substrateclient.api.pallet.grandpa;

import com.strategyobject.substrateclient.crypto.PublicKey;
import com.strategyobject.substrateclient.scale.ScaleType;
import com.strategyobject.substrateclient.scale.annotation.Scale;
import com.strategyobject.substrateclient.scale.annotation.ScaleReader;
import lombok.Getter;
import lombok.Setter;

import java.math.BigInteger;

@ScaleReader
@Getter
@Setter
public class Authority {
private PublicKey authorityId;

@Scale(ScaleType.U64.class)
private BigInteger authorityWeight;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.strategyobject.substrateclient.api.pallet.grandpa;

import com.strategyobject.substrateclient.pallet.annotation.Event;
import com.strategyobject.substrateclient.pallet.annotation.Pallet;
import com.strategyobject.substrateclient.scale.annotation.ScaleReader;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Pallet("Grandpa")
public interface Grandpa {

/**
* New authority set has been applied. \[authority_set\]
*/
@Event(index = 0)
@Getter
@Setter
@ScaleReader
class NewAuthorities {
private List<Authority> authorityList;
}

/**
* Current authority set has been paused.
*/
@Event(index = 1)
@ScaleReader
class Paused {
}

/**
* Current authority set has been resumed.
*/
@Event(index = 2)
@ScaleReader
class Resumed {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.strategyobject.substrateclient.api.pallet.indices;

import com.strategyobject.substrateclient.pallet.annotation.Event;
import com.strategyobject.substrateclient.pallet.annotation.Pallet;
import com.strategyobject.substrateclient.rpc.api.AccountId;
import com.strategyobject.substrateclient.rpc.api.primitives.AccountIndex;
import com.strategyobject.substrateclient.scale.annotation.ScaleReader;
import lombok.Getter;
import lombok.Setter;

@Pallet("Indices")
public interface Indices {

/**
* An account index was assigned. \[index, who\]
*/
@Event(index = 0)
@Getter
@Setter
@ScaleReader
class IndexAssigned {
private AccountId account;
private AccountIndex index;
}

/**
* An account index has been freed up (unassigned). \[index\]
*/
@Event(index = 1)
@Getter
@Setter
@ScaleReader
class IndexFreed {
private AccountIndex accountIndex;
}

/**
* An account index has been frozen to its current account ID. \[index, who\]
*/
@Event(index = 2)
@Getter
@Setter
@ScaleReader
class IndexFrozen {
private AccountIndex index;
private AccountId account;
}
}
Loading