Skip to content

Commit 2c19a49

Browse files
authored
Merge pull request #28 from strategyobject/release/v0.2.0
Release/v0.2.0
2 parents 01c5eb2 + e1df62e commit 2c19a49

File tree

565 files changed

+7476
-3630
lines changed

Some content is hidden

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

565 files changed

+7476
-3630
lines changed

.github/workflows/gradle-build.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ on:
99
jobs:
1010
build:
1111

12-
runs-on: ubuntu-20.04
12+
runs-on: ubuntu-22.04
1313
permissions:
1414
contents: read
1515
packages: write
1616

1717
steps:
18-
- uses: actions/checkout@v2
18+
- uses: actions/checkout@v3
1919
- name: Set up JDK 8
20-
uses: actions/setup-java@v2
20+
uses: actions/setup-java@v3
2121
with:
2222
java-version: '8'
23-
distribution: 'adopt'
23+
distribution: 'temurin'
2424

2525
- name: Build with Gradle
2626
run: gradle build

.github/workflows/gradle-publish.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ on:
1010
jobs:
1111
publish:
1212

13-
runs-on: ubuntu-20.04
13+
runs-on: ubuntu-22.04
1414
permissions:
1515
contents: read
1616
packages: write
1717

1818
steps:
19-
- uses: actions/checkout@v2
19+
- uses: actions/checkout@v3
2020
- name: Set up JDK 8
21-
uses: actions/setup-java@v2
21+
uses: actions/setup-java@v3
2222
with:
2323
java-version: '8'
24-
distribution: 'adopt'
24+
distribution: 'temurin'
2525

2626
- name: Build with Gradle
2727
run: gradle build

api/build.gradle

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
dependencies {
22
implementation project(':common')
3+
implementation project(':crypto')
34
implementation project(':pallet')
45
implementation project(':rpc')
5-
implementation project(':rpc:rpc-core')
6-
implementation project(':rpc:rpc-sections')
7-
implementation project(':rpc:rpc-types')
6+
implementation project(':rpc:rpc-api')
87
implementation project(':scale')
9-
implementation project(':storage')
108
implementation project(':transport')
11-
implementation project(':types')
9+
10+
api 'com.google.inject:guice:5.1.0'
11+
12+
annotationProcessor project(':pallet:pallet-codegen')
13+
annotationProcessor project(':rpc:rpc-codegen')
1214

1315
testImplementation project(':tests')
1416

15-
testImplementation 'org.testcontainers:testcontainers:1.17.1'
16-
testImplementation 'org.testcontainers:junit-jupiter:1.17.1'
17+
testImplementation 'ch.qos.logback:logback-classic:1.2.11'
18+
testImplementation 'org.testcontainers:testcontainers:1.17.3'
19+
testImplementation 'org.testcontainers:junit-jupiter:1.17.3'
20+
testImplementation 'org.awaitility:awaitility:4.2.0'
21+
testImplementation 'org.hamcrest:hamcrest:2.2'
1722

1823
testAnnotationProcessor project(':pallet:pallet-codegen')
19-
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,73 @@
11
package com.strategyobject.substrateclient.api;
22

3-
import com.strategyobject.substrateclient.pallet.GeneratedPalletResolver;
4-
import com.strategyobject.substrateclient.rpc.Rpc;
5-
import com.strategyobject.substrateclient.rpc.RpcImpl;
3+
import com.google.inject.Module;
4+
import com.strategyobject.substrateclient.pallet.PalletFactory;
5+
import com.strategyobject.substrateclient.rpc.RpcSectionFactory;
6+
import com.strategyobject.substrateclient.rpc.metadata.MetadataProvider;
67
import com.strategyobject.substrateclient.transport.ProviderInterface;
7-
import lombok.val;
8+
import lombok.NonNull;
9+
import lombok.RequiredArgsConstructor;
10+
11+
import java.util.Map;
12+
import java.util.concurrent.ConcurrentHashMap;
13+
import java.util.function.Supplier;
814

915
/**
1016
* Provides the ability to query a node and interact with the Polkadot or Substrate chains.
1117
* It allows interacting with blockchain in various ways: using RPC's queries directly or
1218
* accessing Pallets and its APIs, such as storages, transactions, etc.
1319
*/
14-
public interface Api {
15-
static DefaultApi with(ProviderInterface provider) {
16-
val rpc = RpcImpl.with(provider);
20+
@RequiredArgsConstructor
21+
public class Api implements AutoCloseable {
22+
private final @NonNull RpcSectionFactory rpcSectionFactory;
23+
private final @NonNull PalletFactory palletFactory;
24+
private final @NonNull MetadataProvider metadataProvider;
25+
private final Map<Class<?>, Object> resolvedCache = new ConcurrentHashMap<>();
1726

18-
return DefaultApi.with(rpc, GeneratedPalletResolver.with(rpc));
19-
}
2027

2128
/**
22-
* @return the instance that provides a proper API for querying the RPC's methods.
29+
* Resolves the instance of a rpc by its definition.
30+
*
31+
* @param clazz the class of the rpc
32+
* @param <T> the type of the rpc
33+
* @return appropriate instance of the rpc
2334
*/
24-
Rpc rpc();
35+
public <T> T rpc(@NonNull Class<T> clazz) {
36+
return clazz.cast(resolvedCache.computeIfAbsent(clazz, rpcSectionFactory::create));
37+
}
2538

2639
/**
2740
* Resolves the instance of a pallet by its definition.
41+
*
2842
* @param clazz the class of the pallet
29-
* @param <T> the type of the pallet
43+
* @param <T> the type of the pallet
3044
* @return appropriate instance of the pallet
3145
*/
32-
<T> T pallet(Class<T> clazz);
33-
}
46+
public <T> T pallet(@NonNull Class<T> clazz) {
47+
return clazz.cast(resolvedCache.computeIfAbsent(clazz, palletFactory::create));
48+
}
49+
50+
/**
51+
* Provides access to current version of metadata in use.
52+
*
53+
* @return MetadataProvider instance
54+
*/
55+
public MetadataProvider metadata() {
56+
return metadataProvider;
57+
}
58+
59+
@Override
60+
public void close() throws Exception {
61+
if (rpcSectionFactory instanceof AutoCloseable) {
62+
((AutoCloseable) rpcSectionFactory).close();
63+
}
64+
}
65+
66+
public static ApiBuilder<DefaultModule> with(@NonNull Supplier<ProviderInterface> providerInterface) {
67+
return with(new DefaultModule(providerInterface.get()));
68+
}
69+
70+
public static <M extends Module> ApiBuilder<M> with(@NonNull M module) {
71+
return new ApiBuilder<>(module);
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.strategyobject.substrateclient.api;
2+
3+
import com.google.inject.Guice;
4+
import com.google.inject.Module;
5+
import com.strategyobject.substrateclient.transport.ProviderInterface;
6+
import lombok.NonNull;
7+
import lombok.RequiredArgsConstructor;
8+
import lombok.val;
9+
10+
import java.util.concurrent.CompletableFuture;
11+
import java.util.function.Consumer;
12+
import java.util.function.Function;
13+
14+
@RequiredArgsConstructor
15+
public class ApiBuilder<M extends Module> {
16+
private final @NonNull M module;
17+
18+
public ApiBuilder<M> configure(@NonNull Consumer<M> configuration) {
19+
configuration.accept(module);
20+
return this;
21+
}
22+
23+
public <N extends Module> ApiBuilder<N> reconfigure(@NonNull Function<M, N> configuration) {
24+
return new ApiBuilder<>(configuration.apply(this.module));
25+
}
26+
27+
public CompletableFuture<Api> build() {
28+
val injector = Guice.createInjector(new RequireModule(), module);
29+
val provider = injector.getInstance(ProviderInterface.class);
30+
val result = provider.isConnected() ?
31+
CompletableFuture.<Void>completedFuture(null) :
32+
provider.connect();
33+
34+
return result.thenApply(ignored -> injector.getInstance(Api.class));
35+
}
36+
}

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

-34
This file was deleted.

0 commit comments

Comments
 (0)