Skip to content

Configuration

Yenwen Feng edited this page Jun 22, 2018 · 6 revisions

Configuration

Builder

Using EtherSpace.Builder to construct a new instance.

EtherSpaceetherSpace = newEtherSpace.Builder()
.provider("https://rinkeby.infura.io/") // Or your local node
.credentials(newCredentials(YOUR_PRIVATE_KEY_OR_WALLET))
.addCallAdapter(newCompletableFutureCallAdapter<>())
.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 / Async

    Return TypeCallAdapter
    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

Sync / Async

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.

CoroutineCallAdapter

// KotlininterfaceCoroutineGreeter {
@Throws(IOException::class)
@Call
fungreet(): 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
}

CompletableFutureCallAdapter

// JavainterfaceCompletableFutureCallAdapter {
@CallCompletableFuture<String> greet() throwsIOException;
}
EtherSpaceetherSpace = newEtherSpace.Builder()
.provider("https://rinkeby.infura.io/") // Or your local node
.addCallAdapter(newCompletableFutureCallAdapter<>())
.build();
Greetergreeter = etherSpace.create(SMART_CONTRACT_ADDRESS, CompletableFutureGreeter.class);
System.out.println(greeter.greet().join()); // Should be "Hello World"

RxJavaCallAdapter

// JavainterfaceRxJavaGreeter {
@CallObservable<String> greet();
}
EtherSpaceetherSpace = newEtherSpace.Builder()
.provider("https://rinkeby.infura.io/") // Or your local node
.addCallAdapter(newRxJavaCallAdapter<>())
.build();
Greetergreeter = etherSpace.create(SMART_CONTRACT_ADDRESS, RxJavaGreeter.class);
greeter.greet().subscribe(System.out::println); // Should be "Hello World"

Multi-threads

Because of nonce, transactions need to be submitted in order. After the transactions submitted, transaction receipts can be requested asynchronously.

CoroutineCallAdapter

// KotlininterfaceCoroutineGreeter {
@Throws(IOException::class)
@Send
funnewGreeting(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}") }
}

CompletableFutureCallAdapter

// JavainterfaceCompletableFutureCallAdapter {
@SendCompletableFuture<TransactionHash> newGreeting(Stringgreeting) throwsIOException;
}
EtherSpaceetherSpace = newEtherSpace.Builder()
.provider("https://rinkeby.infura.io/") // Or your local node
.addCallAdapter(newCompletableFutureCallAdapter<>())
.build();
Greetergreeter = etherSpace.create(SMART_CONTRACT_ADDRESS, CompletableFutureGreeter.class);
TransactionHashhash1 = greeter.newGreeting("call 1").join();
TransactionHashhash2 = 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()));