Skip to content

Commit 7922673

Browse files
committed
events support
1 parent 43d9f6b commit 7922673

File tree

111 files changed

+2113
-111
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+2113
-111
lines changed

api/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies {
1818
testImplementation 'org.testcontainers:testcontainers:1.17.2'
1919
testImplementation 'org.testcontainers:junit-jupiter:1.17.2'
2020
testImplementation 'org.awaitility:awaitility:4.2.0'
21+
testImplementation 'org.hamcrest:hamcrest:2.2'
2122

2223
testAnnotationProcessor project(':pallet:pallet-codegen')
2324
}

api/src/main/java/com/strategyobject/substrateclient/api/DefaultModule.java

+38-7
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33
import com.google.inject.AbstractModule;
44
import com.google.inject.Provides;
55
import com.google.inject.Singleton;
6+
import com.strategyobject.substrateclient.api.pallet.balances.AccountData;
7+
import com.strategyobject.substrateclient.api.pallet.balances.AccountDataReader;
8+
import com.strategyobject.substrateclient.api.pallet.system.System;
9+
import com.strategyobject.substrateclient.common.types.Lambda;
610
import com.strategyobject.substrateclient.crypto.ss58.SS58AddressFormat;
711
import com.strategyobject.substrateclient.pallet.GeneratedPalletFactory;
812
import com.strategyobject.substrateclient.pallet.PalletFactory;
13+
import com.strategyobject.substrateclient.pallet.events.EventDescriptor;
14+
import com.strategyobject.substrateclient.pallet.events.EventDescriptorReader;
15+
import com.strategyobject.substrateclient.pallet.events.EventRegistry;
916
import com.strategyobject.substrateclient.rpc.GeneratedRpcSectionFactory;
1017
import com.strategyobject.substrateclient.rpc.RpcSectionFactory;
1118
import com.strategyobject.substrateclient.rpc.api.section.State;
@@ -35,12 +42,12 @@ public class DefaultModule extends AbstractModule {
3542

3643
private final @NonNull ProviderInterface providerInterface;
3744

38-
private Consumer<ScaleReaderRegistry> configureScaleReaderRegistry = x -> x.registerAnnotatedFrom(PREFIX);
39-
private Consumer<ScaleWriterRegistry> configureScaleWriterRegistry = x -> x.registerAnnotatedFrom(PREFIX);
40-
private BiConsumer<RpcDecoderRegistry, RpcDecoderContextFactory> configureRpcDecoderRegistry =
41-
(registry, contextFactory) -> registry.registerAnnotatedFrom(contextFactory, PREFIX);
42-
private BiConsumer<RpcEncoderRegistry, RpcEncoderContextFactory> configureRpcEncoderRegistry =
43-
(registry, contextFactory) -> registry.registerAnnotatedFrom(contextFactory, PREFIX);
45+
private Consumer<ScaleReaderRegistry> configureScaleReaderRegistry = Lambda::noop;
46+
private Consumer<ScaleWriterRegistry> configureScaleWriterRegistry = Lambda::noop;
47+
private BiConsumer<RpcDecoderRegistry, RpcDecoderContextFactory> configureRpcDecoderRegistry = Lambda::noop;
48+
private BiConsumer<RpcEncoderRegistry, RpcEncoderContextFactory> configureRpcEncoderRegistry = Lambda::noop;
49+
private Consumer<EventRegistry> configureEventRegistry = Lambda::noop;
50+
4451

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

72+
public DefaultModule configureEventRegistry(Consumer<EventRegistry> configure) {
73+
configureEventRegistry = configureEventRegistry.andThen(configure);
74+
return this;
75+
}
76+
6577
@Override
6678
protected void configure() {
6779
try {
@@ -97,8 +109,13 @@ protected void configure() {
97109

98110
@Provides
99111
@Singleton
100-
public ScaleReaderRegistry provideScaleReaderRegistry() {
112+
public ScaleReaderRegistry provideScaleReaderRegistry(MetadataProvider metadataProvider,
113+
EventRegistry eventRegistry) {
101114
val registry = new ScaleReaderRegistry();
115+
registry.registerAnnotatedFrom(PREFIX);
116+
registry.register(new AccountDataReader(registry), System.AccountData.class, AccountData.class);
117+
registry.register(new EventDescriptorReader(registry, metadataProvider, eventRegistry), EventDescriptor.class);
118+
102119
configureScaleReaderRegistry.accept(registry);
103120
return registry;
104121
}
@@ -107,6 +124,8 @@ public ScaleReaderRegistry provideScaleReaderRegistry() {
107124
@Singleton
108125
public ScaleWriterRegistry provideScaleWriterRegistry() {
109126
val registry = new ScaleWriterRegistry();
127+
registry.registerAnnotatedFrom(PREFIX);
128+
110129
configureScaleWriterRegistry.accept(registry);
111130
return registry;
112131
}
@@ -120,6 +139,7 @@ public RpcDecoderRegistry provideRpcDecoderRegistry(MetadataProvider metadataPro
120139
metadataProvider,
121140
registry,
122141
scaleReaderRegistry);
142+
registry.registerAnnotatedFrom(() -> context, PREFIX);
123143

124144
configureRpcDecoderRegistry.accept(registry, () -> context);
125145
return registry;
@@ -134,11 +154,22 @@ public RpcEncoderRegistry provideRpcEncoderRegistry(MetadataProvider metadataPro
134154
metadataProvider,
135155
registry,
136156
scaleWriterRegistry);
157+
registry.registerAnnotatedFrom(() -> context, PREFIX);
137158

138159
configureRpcEncoderRegistry.accept(registry, () -> context);
139160
return registry;
140161
}
141162

163+
@Provides
164+
@Singleton
165+
public EventRegistry provideEventRegistry() {
166+
val registry = new EventRegistry();
167+
registry.registerAnnotatedFrom(PREFIX);
168+
169+
configureEventRegistry.accept(registry);
170+
return registry;
171+
}
172+
142173
@Provides
143174
@Singleton
144175
public MetadataProvider provideMetadata() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.strategyobject.substrateclient.api.pallet.balances;
2+
3+
import com.strategyobject.substrateclient.api.pallet.system.System;
4+
import com.strategyobject.substrateclient.scale.ScaleType;
5+
import com.strategyobject.substrateclient.scale.annotation.Scale;
6+
import com.strategyobject.substrateclient.scale.annotation.ScaleReader;
7+
import lombok.Getter;
8+
import lombok.Setter;
9+
10+
import java.math.BigInteger;
11+
12+
@ScaleReader
13+
@Getter
14+
@Setter
15+
public class AccountData implements System.AccountData {
16+
@Scale(ScaleType.U128.class)
17+
private BigInteger free;
18+
19+
@Scale(ScaleType.U128.class)
20+
private BigInteger reserved;
21+
22+
@Scale(ScaleType.U128.class)
23+
private BigInteger miscFrozen;
24+
25+
@Scale(ScaleType.U128.class)
26+
private BigInteger feeFrozen;
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.strategyobject.substrateclient.api.pallet.balances;
2+
3+
import com.strategyobject.substrateclient.pallet.annotation.Event;
4+
import com.strategyobject.substrateclient.pallet.annotation.Pallet;
5+
import com.strategyobject.substrateclient.rpc.api.AccountId;
6+
import com.strategyobject.substrateclient.rpc.api.BalanceStatus;
7+
import com.strategyobject.substrateclient.rpc.api.primitives.Balance;
8+
import com.strategyobject.substrateclient.scale.annotation.ScaleReader;
9+
import lombok.Getter;
10+
import lombok.Setter;
11+
12+
@Pallet("Balances")
13+
public interface Balances {
14+
15+
/**
16+
* An account was created with some free balance. \[account, free_balance\]
17+
*/
18+
@Event(index = 0)
19+
@Getter
20+
@Setter
21+
@ScaleReader
22+
class Endowed {
23+
private AccountId account;
24+
private Balance freeBalance;
25+
}
26+
27+
/**
28+
* An account was removed whose balance was non-zero but below ExistentialDeposit,
29+
* resulting in an outright loss. \[account, balance\]
30+
*/
31+
@Event(index = 1)
32+
@Getter
33+
@Setter
34+
@ScaleReader
35+
class DustLost {
36+
private AccountId account;
37+
private Balance balance;
38+
}
39+
40+
/**
41+
* Transfer succeeded. \[from, to, value\]
42+
*/
43+
@Event(index = 2)
44+
@Getter
45+
@Setter
46+
@ScaleReader
47+
class Transfer {
48+
private AccountId from;
49+
private AccountId to;
50+
private Balance value;
51+
}
52+
53+
/**
54+
* A balance was set by root. \[who, free, reserved\]
55+
*/
56+
@Event(index = 3)
57+
@Getter
58+
@Setter
59+
@ScaleReader
60+
class BalanceSet {
61+
private AccountId account;
62+
private Balance free;
63+
private Balance reserved;
64+
}
65+
66+
/**
67+
* Some amount was deposited (e.g. for transaction fees). \[who, deposit\]
68+
*/
69+
@Event(index = 4)
70+
@Getter
71+
@Setter
72+
@ScaleReader
73+
class Deposit {
74+
private AccountId account;
75+
private Balance value;
76+
}
77+
78+
/**
79+
* Some balance was reserved (moved from free to reserved). \[who, value\]
80+
*/
81+
@Event(index = 5)
82+
@Getter
83+
@Setter
84+
@ScaleReader
85+
class Reserved {
86+
private AccountId account;
87+
private Balance value;
88+
}
89+
90+
/**
91+
* Some balance was unreserved (moved from reserved to free). \[who, value\]
92+
*/
93+
@Event(index = 6)
94+
@Getter
95+
@Setter
96+
@ScaleReader
97+
class Unreserved {
98+
private AccountId account;
99+
private Balance value;
100+
}
101+
102+
/**
103+
* Some balance was moved from the reserve of the first account to the second account.
104+
* Final argument indicates the destination balance type.
105+
* \[from, to, balance, destination_status\]
106+
*/
107+
@Event(index = 7)
108+
@Getter
109+
@Setter
110+
@ScaleReader
111+
class ReserveRepatriated {
112+
private AccountId from;
113+
private AccountId to;
114+
private Balance value;
115+
private BalanceStatus destinationStatus;
116+
}
117+
118+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.strategyobject.substrateclient.api.pallet.grandpa;
2+
3+
import com.strategyobject.substrateclient.crypto.PublicKey;
4+
import com.strategyobject.substrateclient.scale.ScaleType;
5+
import com.strategyobject.substrateclient.scale.annotation.Scale;
6+
import com.strategyobject.substrateclient.scale.annotation.ScaleReader;
7+
import lombok.Getter;
8+
import lombok.Setter;
9+
10+
import java.math.BigInteger;
11+
12+
@ScaleReader
13+
@Getter
14+
@Setter
15+
public class Authority {
16+
private PublicKey authorityId;
17+
18+
@Scale(ScaleType.U64.class)
19+
private BigInteger authorityWeight;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.strategyobject.substrateclient.api.pallet.grandpa;
2+
3+
import com.strategyobject.substrateclient.pallet.annotation.Event;
4+
import com.strategyobject.substrateclient.pallet.annotation.Pallet;
5+
import com.strategyobject.substrateclient.scale.annotation.ScaleReader;
6+
import lombok.Getter;
7+
import lombok.Setter;
8+
9+
import java.util.List;
10+
11+
@Pallet("Grandpa")
12+
public interface Grandpa {
13+
14+
/**
15+
* New authority set has been applied. \[authority_set\]
16+
*/
17+
@Event(index = 0)
18+
@Getter
19+
@Setter
20+
@ScaleReader
21+
class NewAuthorities {
22+
private List<Authority> authorityList;
23+
}
24+
25+
/**
26+
* Current authority set has been paused.
27+
*/
28+
@Event(index = 1)
29+
@ScaleReader
30+
class Paused {
31+
}
32+
33+
/**
34+
* Current authority set has been resumed.
35+
*/
36+
@Event(index = 2)
37+
@ScaleReader
38+
class Resumed {
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.strategyobject.substrateclient.api.pallet.indices;
2+
3+
import com.strategyobject.substrateclient.pallet.annotation.Event;
4+
import com.strategyobject.substrateclient.pallet.annotation.Pallet;
5+
import com.strategyobject.substrateclient.rpc.api.AccountId;
6+
import com.strategyobject.substrateclient.rpc.api.primitives.AccountIndex;
7+
import com.strategyobject.substrateclient.scale.annotation.ScaleReader;
8+
import lombok.Getter;
9+
import lombok.Setter;
10+
11+
@Pallet("Indices")
12+
public interface Indices {
13+
14+
/**
15+
* An account index was assigned. \[index, who\]
16+
*/
17+
@Event(index = 0)
18+
@Getter
19+
@Setter
20+
@ScaleReader
21+
class IndexAssigned {
22+
private AccountId account;
23+
private AccountIndex index;
24+
}
25+
26+
/**
27+
* An account index has been freed up (unassigned). \[index\]
28+
*/
29+
@Event(index = 1)
30+
@Getter
31+
@Setter
32+
@ScaleReader
33+
class IndexFreed {
34+
private AccountIndex accountIndex;
35+
}
36+
37+
/**
38+
* An account index has been frozen to its current account ID. \[index, who\]
39+
*/
40+
@Event(index = 2)
41+
@Getter
42+
@Setter
43+
@ScaleReader
44+
class IndexFrozen {
45+
private AccountIndex index;
46+
private AccountId account;
47+
}
48+
}

0 commit comments

Comments
 (0)