Configuration - Wraecca/etherspace-java GitHub Wiki
Using EtherSpace.Builder
to construct a new instance.
EtherSpace etherSpace = new EtherSpace.Builder()
.provider("https://rinkeby.infura.io/") // Or your local node
.credentials(new Credentials(YOUR_PRIVATE_KEY_OR_WALLET))
.addCallAdapter(new CompletableFutureCallAdapter<>())
.client(OkHttpClient.Builder().build())
.build();
-
provider*:
Ethereum node URL
-
credentials:
Private Key or your wallet file. (format: UTC JSON Keystore File) Can be
null
if you only want to make calls to constant functions. You Can also supply different credentials in Options. -
calladapters:
Etherspace supports different response types through
CallAdapter
. See Sync / AsyncReturn Type CallAdapter kotlinx.coroutines.experimental.Deferred<T>
cc.etherspace.calladapter.CoroutineCallAdapter
java.util.concurrent.CompletableFuture<T>
cc.etherspace.calladapter.CompletableFutureCallAdapter
rx.Observable<T>
cc.etherspace.calladapter.RxCallAdapter
-
client:
An
OkHttpClient
All method calls in Smart Contract interface are synchronized by default.
Etherspace supports Coroutine (Kotlin), CompletableFuture (Java), RxJava 1 (Java) for asynchronized calls.
Just make sure the corresponding CallAdapter
is added on EtherSpace builder.
// Kotlin
interface CoroutineGreeter {
@Throws(IOException::class)
@Call
fun greet(): Deferred<String>
}
val etherSpace = EtherSpace.build {
provider = "https://rinkeby.infura.io/"
callAdapters += CoroutineCallAdapter()
}
greeter = etherSpace.create(SMART_CONTRACT_ADDRESS, CoroutineGreeter::class.java)
runBlocking {
println(greeter.greet().await()) // Should be Hello World
}
// Java
interface CompletableFutureCallAdapter {
@Call
CompletableFuture<String> greet() throws IOException;
}
EtherSpace etherSpace = new EtherSpace.Builder()
.provider("https://rinkeby.infura.io/") // Or your local node
.addCallAdapter(new CompletableFutureCallAdapter<>())
.build();
Greeter greeter = etherSpace.create(SMART_CONTRACT_ADDRESS, CompletableFutureGreeter.class);
System.out.println(greeter.greet().join()); // Should be "Hello World"
// Java
interface RxJavaGreeter {
@Call
Observable<String> greet();
}
EtherSpace etherSpace = new EtherSpace.Builder()
.provider("https://rinkeby.infura.io/") // Or your local node
.addCallAdapter(new RxJavaCallAdapter<>())
.build();
Greeter greeter = etherSpace.create(SMART_CONTRACT_ADDRESS, RxJavaGreeter.class);
greeter.greet().subscribe(System.out::println); // Should be "Hello World"
Because of nonce, transactions need to be submitted in order. After the transactions submitted, transaction receipts can be requested asynchronously.
// Kotlin
interface CoroutineGreeter {
@Throws(IOException::class)
@Send
fun newGreeting(greeting: String): Deferred<TransactionHash>
}
val etherSpace = EtherSpace.build {
provider = "https://rinkeby.infura.io/"
callAdapters += CoroutineCallAdapter()
}
greeter = etherSpace.create(SMART_CONTRACT_ADDRESS, CoroutineGreeter::class.java)
runBlocking {
var hash1 = greeter.newGreeting("call 1").await()
var hash2 = greeter.newGreeting("call 2").await()
listOf(hash1, hash2)
.map { it.requestTransactionReceipt<Deferred<TransactionReceipt>>() }
.map { it.await() }
.forEach { println("Transaction returned with hash: ${it.transactionHash}") }
}
// Java
interface CompletableFutureCallAdapter {
@Send
CompletableFuture<TransactionHash> newGreeting(String greeting) throws IOException;
}
EtherSpace etherSpace = new EtherSpace.Builder()
.provider("https://rinkeby.infura.io/") // Or your local node
.addCallAdapter(new CompletableFutureCallAdapter<>())
.build();
Greeter greeter = etherSpace.create(SMART_CONTRACT_ADDRESS, CompletableFutureGreeter.class);
TransactionHash hash1 = greeter.newGreeting("call 1").join();
TransactionHash hash2 = greeter.newGreeting("call 2").join();
List<CompletableFuture<TransactionReceipt>> futures = Stream.of(hash1, hash2)
.map(TransactionHash::<CompletableFuture<TransactionReceipt>>requestTransactionReceipt)
.collect(Collectors.toList());
futures.stream()
.map(CompletableFuture::join)
.forEach(receipt -> System.out.println("Transaction returned with hash: " + receipt.getTransactionHash()));