1
1
package com .strategyobject .substrateclient .api ;
2
2
3
+ import com .strategyobject .substrateclient .api .rpc .State ;
3
4
import com .strategyobject .substrateclient .pallet .GeneratedPalletResolver ;
4
- import com .strategyobject .substrateclient .rpc . Rpc ;
5
- import com .strategyobject .substrateclient .rpc .RpcImpl ;
5
+ import com .strategyobject .substrateclient .pallet . PalletResolver ;
6
+ import com .strategyobject .substrateclient .rpc .RpcGeneratedSectionFactory ;
6
7
import com .strategyobject .substrateclient .transport .ProviderInterface ;
8
+ import lombok .NonNull ;
7
9
import lombok .val ;
8
10
11
+ import java .util .Map ;
12
+ import java .util .concurrent .ConcurrentHashMap ;
13
+
9
14
/**
10
15
* Provides the ability to query a node and interact with the Polkadot or Substrate chains.
11
16
* It allows interacting with blockchain in various ways: using RPC's queries directly or
12
17
* accessing Pallets and its APIs, such as storages, transactions, etc.
13
18
*/
14
- public interface Api {
15
- static DefaultApi with (ProviderInterface provider ) {
16
- val rpc = RpcImpl .with (provider );
19
+ public class Api implements AutoCloseable {
20
+ private final @ NonNull ProviderInterface providerInterface ;
21
+ private final @ NonNull PalletResolver palletResolver ;
22
+ private final Map <Class <?>, Object > resolvedCache = new ConcurrentHashMap <>();
23
+
24
+ private Api (@ NonNull ProviderInterface providerInterface ) {
25
+ this .providerInterface = providerInterface ;
17
26
18
- return DefaultApi .with (rpc , GeneratedPalletResolver .with (rpc ));
27
+ val stateProvider = RpcGeneratedSectionFactory .create (State .class , providerInterface );
28
+ this .palletResolver = GeneratedPalletResolver .with (stateProvider );
19
29
}
20
30
21
31
/**
22
- * @return the instance that provides a proper API for querying the RPC's methods.
32
+ * Resolves the instance of a rpc by its definition.
33
+ *
34
+ * @param clazz the class of the rpc
35
+ * @param <T> the type of the rpc
36
+ * @return appropriate instance of the rpc
23
37
*/
24
- Rpc rpc ();
38
+ public <T > T rpc (Class <T > clazz ) {
39
+ return clazz .cast (resolvedCache
40
+ .computeIfAbsent (clazz , x -> RpcGeneratedSectionFactory .create (x , providerInterface )));
41
+ }
25
42
26
43
/**
27
44
* Resolves the instance of a pallet by its definition.
45
+ *
28
46
* @param clazz the class of the pallet
29
- * @param <T> the type of the pallet
47
+ * @param <T> the type of the pallet
30
48
* @return appropriate instance of the pallet
31
49
*/
32
- <T > T pallet (Class <T > clazz );
33
- }
50
+ public <T > T pallet (@ NonNull Class <T > clazz ) {
51
+ return clazz .cast (resolvedCache
52
+ .computeIfAbsent (clazz , palletResolver ::resolve ));
53
+ }
54
+
55
+ public static Api with (ProviderInterface providerInterface ) {
56
+ return new Api (providerInterface );
57
+ }
58
+
59
+ @ Override
60
+ public void close () throws Exception {
61
+ if (providerInterface instanceof AutoCloseable ) {
62
+ ((AutoCloseable ) providerInterface ).close ();
63
+ }
64
+ }
65
+ }
0 commit comments